Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.73 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <stdint.h>
  3. #include <BLEPeripheral.h>
  4. #include <Adafruit_GFX.h>
  5. #include <nrf_nvic.h>//interrupt controller stuff
  6. #include <nrf_sdm.h>
  7. #include <nrf_soc.h>
  8. #include <WInterrupts.h>
  9. #include <Adafruit_SSD1306.h>
  10. #include <TimeLib.h>
  11. #include <nrf.h>
  12.  
  13. #define wdt_reset() NRF_WDT->RR[0] = WDT_RR_RR_Reload
  14. #define wdt_enable(timeout) \
  15. NRF_WDT->CONFIG = NRF_WDT->CONFIG = (WDT_CONFIG_HALT_Pause << WDT_CONFIG_HALT_Pos) | ( WDT_CONFIG_SLEEP_Pause << WDT_CONFIG_SLEEP_Pos); \
  16. NRF_WDT->CRV = (32768*timeout)/1000; \
  17. NRF_WDT->RREN |= WDT_RREN_RR0_Msk; \
  18. NRF_WDT->TASKS_START = 1
  19.  
  20. Adafruit_SSD1306 display(128, 32, &SPI, 28, 4, 29);
  21.  
  22. #define BUTTON_PIN 30
  23. #define refreshRate 100
  24.  
  25. int menu;
  26. volatile bool buttonPressed = false;
  27. long startbutton;
  28. unsigned long displayRefreshTime;
  29. int timezone;
  30. int steps;
  31. int steps1;
  32. String serialNr = "235246472";
  33. String versionNr = "110.200.051";
  34. String btversionNr = "100.016.051";
  35. String msgText;
  36.  
  37. String bleSymbol = " ";
  38. String intCmdTest = "";
  39.  
  40. BLEPeripheral blePeripheral = BLEPeripheral();
  41. BLEService batteryLevelService = BLEService("190A");
  42. BLECharacteristic TXchar = BLECharacteristic("0002", BLENotify, 20);
  43. BLECharacteristic RXchar = BLECharacteristic("0001", BLEWriteWithoutResponse, 20);
  44.  
  45. BLEService batteryLevelService1 = BLEService("190B");
  46. BLECharacteristic TXchar1 = BLECharacteristic("0004", BLENotify, 20);
  47. BLECharacteristic RXchar1 = BLECharacteristic("0003", BLEWriteWithoutResponse, 20);
  48.  
  49. void buttonHandler() {
  50. buttonPressed = true;
  51. }
  52.  
  53. void blePeripheralConnectHandler(BLECentral& central) {
  54. Serial.println("BLEconnected");
  55. bleSymbol = "B";
  56. }
  57.  
  58. void blePeripheralDisconnectHandler(BLECentral& central) {
  59. Serial.println("BLEdisconnected");
  60. bleSymbol = " ";
  61. }
  62.  
  63. String answer = "";
  64. String tempCmd = "";
  65. int tempLen = 0, tempLen1;
  66. boolean syn;
  67.  
  68. void characteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
  69. char remoteCharArray[21];
  70. tempLen1 = RXchar.valueLength();
  71. tempLen = tempLen + tempLen1;
  72. memset(remoteCharArray, 0, sizeof(remoteCharArray));
  73. memcpy(remoteCharArray, RXchar.value(), tempLen1);
  74. tempCmd = tempCmd + remoteCharArray;
  75. if (tempCmd[tempLen - 2] == '\r' && tempCmd[tempLen - 1] == '\n') {
  76. answer = tempCmd.substring(0, tempLen - 2);
  77. tempCmd = "";
  78. tempLen = 0;
  79. Serial.print("RxBle: ");
  80. Serial.println(answer);
  81. filterCmd(answer);
  82. }
  83. }
  84.  
  85. void filterCmd(String Command) {
  86. if (Command == "AT+BOND") {
  87. sendBLEcmd("AT+BOND:OK");
  88. } else if (Command == "AT+ACT") {
  89. sendBLEcmd("AT+ACT:0");
  90. } else if (Command.substring(0, 7) == "AT+RUN=") {
  91. sendBLEcmd("AT+RUN:" + Command.substring(7));
  92. } else if (Command.substring(0, 8) == "AT+USER=") {
  93. sendBLEcmd("AT+USER:" + Command.substring(8));
  94. } else if (Command.substring(0, 7) == "AT+REC=") {
  95. sendBLEcmd("AT+REC:" + Command.substring(7));
  96. } else if (Command.substring(0, 8) == "AT+PUSH=") {
  97. sendBLEcmd("AT+PUSH:OK");
  98. handlePush(Command.substring(8));
  99. } else if (Command.substring(0, 9) == "AT+MOTOR=") {
  100. sendBLEcmd("AT+MOTOR:" + Command.substring(9));
  101. } else if (Command.substring(0, 8) == "AT+DEST=") {
  102. sendBLEcmd("AT+DEST:" + Command.substring(8));
  103. } else if (Command.substring(0, 9) == "AT+ALARM=") {
  104. sendBLEcmd("AT+ALARM:" + Command.substring(9));
  105. } else if (Command.substring(0, 13) == "AT+HRMONITOR=") {
  106. sendBLEcmd("AT+HRMONITOR:" + Command.substring(13));
  107. } else if (Command.substring(0, 13) == "AT+FINDPHONE=") {
  108. sendBLEcmd("AT+FINDPHONE:" + Command.substring(13));
  109. } else if (Command.substring(0, 13) == "AT+ANTI_LOST=") {
  110. sendBLEcmd("AT+ANTI_LOST:" + Command.substring(13));
  111. } else if (Command.substring(0, 9) == "AT+UNITS=") {
  112. sendBLEcmd("AT+UNITS:" + Command.substring(9));
  113. } else if (Command.substring(0, 11) == "AT+HANDSUP=") {
  114. sendBLEcmd("AT+HANDSUP:" + Command.substring(11));
  115. } else if (Command.substring(0, 7) == "AT+SIT=") {
  116. sendBLEcmd("AT+SIT:" + Command.substring(7));
  117. } else if (Command.substring(0, 7) == "AT+LAN=") {
  118. sendBLEcmd("AT+LAN:ERR");
  119. } else if (Command.substring(0, 14) == "AT+TIMEFORMAT=") {
  120. sendBLEcmd("AT+TIMEFORMAT:" + Command.substring(14));
  121. } else if (Command == "AT+BATT") {
  122. sendBLEcmd("AT+BATT:" + String(getBatteryLevel()));
  123. } else if (Command == "BT+VER") {
  124. sendBLEcmd("BT+VER:" + btversionNr);
  125. } else if (Command == "AT+VER") {
  126. sendBLEcmd("AT+VER:" + versionNr);
  127. } else if (Command == "AT+SN") {
  128. sendBLEcmd("AT+SN:" + serialNr);
  129. } else if (Command.substring(0, 10) == "AT+DISMOD=") {
  130. sendBLEcmd("AT+DISMOD:" + Command.substring(10));
  131. } else if (Command.substring(0, 7) == "AT+LAN=") {
  132. sendBLEcmd("AT+LAN:ERR");
  133. } else if (Command.substring(0, 10) == "AT+MOTOR=1") {
  134. sendBLEcmd("AT+MOTOR:1" + Command.substring(10));
  135. digitalWrite(25, HIGH);
  136. delay(300);
  137. digitalWrite(25, LOW);
  138. } else if (Command.substring(0, 12) == "AT+CONTRAST=") {
  139. intCmdTest = Command.substring(12);
  140. } else if (Command.substring(0, 6) == "AT+DT=") {
  141. SetDateTimeString(Command.substring(6));
  142. sendBLEcmd("AT+DT:" + GetDateTimeString());
  143. } else if (Command.substring(0, 5) == "AT+DT") {
  144. sendBLEcmd("AT+DT:" + GetDateTimeString());
  145. } else if (Command.substring(0, 12) == "AT+TIMEZONE=") {
  146. timezone = Command.substring(12).toInt();
  147. sendBLEcmd("AT+TIMEZONE:" + String(timezone));
  148. } else if (Command.substring(0, 11) == "AT+TIMEZONE") {
  149. sendBLEcmd("AT+TIMEZONE:" + String(timezone));
  150. } else if (Command == "AT+STEPSTORE") {
  151. sendBLEcmd("AT+STEPSTORE:OK");
  152. } else if (Command == "AT+TOPACE=1") {
  153. sendBLEcmd("AT+TOPACE:OK");
  154. sendBLEcmd("NT+TOPACE:" + String(steps));
  155. } else if (Command == "AT+TOPACE=0") {
  156. sendBLEcmd("AT+TOPACE:" + String(steps));
  157. } else if (Command == "AT+DATA=0") {
  158. sendBLEcmd("AT+DATA:0,0,0,0");
  159. } else if (Command.substring(0, 8) == "AT+PACE=") {
  160. steps1 = Command.substring(8).toInt();
  161. sendBLEcmd("AT+PACE:" + String(steps1));
  162. } else if (Command == "AT+PACE") {
  163. sendBLEcmd("AT+PACE:" + String(steps1));
  164. } else if (Command == "AT+DATA=1") {
  165. sendBLEcmd("AT+DATA:0,0,0,0");
  166. } else if (Command.substring(0, 7) == "AT+SYN=") {
  167. if (Command.substring(7) == "1") {
  168. sendBLEcmd("AT+SYN:1");
  169. syn = true;
  170. } else {
  171. sendBLEcmd("AT+SYN:0");
  172. syn = false;
  173. }
  174. }
  175. }
  176.  
  177. void sendBLEcmd(String Command) {
  178. Serial.print("TxBle: ");
  179. Serial.println(Command);
  180. Command = Command + "\r\n";
  181. while (Command.length() > 0) {
  182. const char* TempSendCmd;
  183. String TempCommand = Command.substring(0, 20);
  184. TempSendCmd = &TempCommand[0];
  185. TXchar.setValue(TempSendCmd);
  186. TXchar1.setValue(TempSendCmd);
  187. Command = Command.substring(20);
  188. }
  189. }
  190.  
  191. String GetDateTimeString() {
  192. String datetime = String(year());
  193. if (month() < 10) datetime += "0";
  194. datetime += String(month());
  195. if (day() < 10) datetime += "0";
  196. datetime += String(day());
  197. if (hour() < 10) datetime += "0";
  198. datetime += String(hour());
  199. if (minute() < 10) datetime += "0";
  200. datetime += String(minute());
  201. return datetime;
  202. }
  203.  
  204. void SetDateTimeString(String datetime) {
  205. int year = datetime.substring(0, 4).toInt();
  206. int month = datetime.substring(4, 6).toInt();
  207. int day = datetime.substring(6, 8).toInt();
  208. int hr = datetime.substring(8, 10).toInt();
  209. int min = datetime.substring(10, 12).toInt();
  210. int sec = datetime.substring(12, 14).toInt();
  211. setTime( hr, min, sec, day, month, year);
  212. }
  213.  
  214. void handlePush(String pushMSG) {
  215. int commaIndex = pushMSG.indexOf(',');
  216. int secondCommaIndex = pushMSG.indexOf(',', commaIndex + 1);
  217. int lastCommaIndex = pushMSG.indexOf(',', secondCommaIndex + 1);
  218. String MsgText = pushMSG.substring(commaIndex + 1, secondCommaIndex);
  219. int timeShown = pushMSG.substring(secondCommaIndex + 1, lastCommaIndex).toInt();
  220. int SymbolNr = pushMSG.substring(lastCommaIndex + 1).toInt();
  221. msgText = MsgText;
  222. Serial.println("MSGtext: " + msgText);
  223. Serial.println("symbol: " + String(SymbolNr));
  224. }
  225.  
  226. int getBatteryLevel() {
  227. return map((6.61207596594 * analogRead(3)), 3700, 4200, 0, 100);
  228. }
  229.  
  230. void setup() {
  231. pinMode(BUTTON_PIN, INPUT);
  232. pinMode(3, INPUT);
  233. if (digitalRead(BUTTON_PIN) == LOW) {
  234. NRF_POWER->GPREGRET = 0x01;
  235. sd_nvic_SystemReset();
  236. }
  237. Serial.begin(115200);
  238. wdt_enable(5000);
  239. blePeripheral.setLocalName("DS-D6");
  240. blePeripheral.setAdvertisingInterval(555);
  241. blePeripheral.setAppearance(0x0000);
  242. blePeripheral.setConnectable(true);
  243. blePeripheral.setDeviceName("ATCDSD6");
  244. blePeripheral.setAdvertisedServiceUuid(batteryLevelService.uuid());
  245. blePeripheral.addAttribute(batteryLevelService);
  246. blePeripheral.addAttribute(TXchar);
  247. blePeripheral.addAttribute(RXchar);
  248. RXchar.setEventHandler(BLEWritten, characteristicWritten);
  249. blePeripheral.setAdvertisedServiceUuid(batteryLevelService1.uuid());
  250. blePeripheral.addAttribute(batteryLevelService1);
  251. blePeripheral.addAttribute(TXchar1);
  252. blePeripheral.addAttribute(RXchar1);
  253. RXchar1.setEventHandler(BLEWritten, characteristicWritten);
  254. blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
  255. blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
  256. blePeripheral.begin();
  257. attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonHandler, FALLING);
  258. pinMode(25, OUTPUT);
  259. digitalWrite(25, HIGH);
  260. pinMode(4, OUTPUT);
  261. digitalWrite(4, LOW);
  262. display.begin(SSD1306_SWITCHCAPVCC);
  263. delay(100);
  264. display.clearDisplay();
  265. display.display();
  266. display.setTextSize(1);
  267. display.setTextColor(WHITE);
  268. display.setCursor(10, 0);
  269. display.println("D6 Emulator");
  270. display.display();
  271. digitalWrite(25, LOW);
  272. }
  273.  
  274. void loop() {
  275. blePeripheral.poll();
  276. wdt_reset();
  277. if (millis() - displayRefreshTime > refreshRate) {
  278. displayRefreshTime = millis();
  279. switch (menu) {
  280. case 0:
  281. displayMenu0();
  282. break;
  283. case 1:
  284. displayMenu1();
  285. break;
  286. case 2:
  287. displayMenu2();
  288. break;
  289. case 3:
  290. displayMenu3();
  291. break;
  292. case 4:
  293. displayMenu4();
  294. break;
  295. }
  296. }
  297. if (buttonPressed) {
  298. buttonPressed = false;
  299. switch (menu) {
  300. case 0:
  301. menu = 1;
  302. break;
  303. case 1:
  304. menu = 2;
  305. break;
  306. case 2:
  307. menu = 3;
  308. break;
  309. case 3:
  310. menu = 4;
  311. break;
  312. case 4:
  313. startbutton = millis();
  314. while (!digitalRead(BUTTON_PIN)) {}
  315. if (millis() - startbutton > 1000) {
  316. delay(100);
  317. int err_code = sd_power_gpregret_set(0x01);
  318. sd_nvic_SystemReset();
  319. while (1) {};
  320. break;
  321. } else {
  322. menu = 0;
  323. }
  324. }
  325. }
  326. }
  327.  
  328.  
  329. void displayMenu0() {
  330. display.clearDisplay();
  331. display.setCursor(0, 0);
  332. display.print(bleSymbol);
  333. display.print(" Length: ");
  334. display.println(answer.length());
  335. display.println(answer);
  336. display.display();
  337. }
  338.  
  339. void displayMenu1() {
  340. display.clearDisplay();
  341. display.setCursor(0, 0);
  342. display.println("Menue 1");
  343. char tmp[16];
  344. sprintf(tmp, "%04X", NRF_FICR->DEVICEADDR[1] & 0xffff);
  345. String MyID = tmp;
  346. sprintf(tmp, "%08X", NRF_FICR->DEVICEADDR[0]);
  347. MyID += tmp;
  348. display.println(MyID);
  349. display.print(getBatteryLevel());
  350. display.println("%");
  351. display.display();
  352. }
  353.  
  354. void displayMenu2() {
  355. display.clearDisplay();
  356. display.setCursor(0, 0);
  357. display.println("Menue 2 int cmd test");
  358. display.println(intCmdTest);
  359. display.println(msgText);
  360. display.display();
  361. }
  362.  
  363. void displayMenu3() {
  364. display.clearDisplay();
  365. display.setCursor(0, 0);
  366. display.println("Menue 4");
  367. display.println(GetDateTimeString() + String(second()));
  368. display.display();
  369. }
  370. void displayMenu4() {
  371. display.clearDisplay();
  372. display.setCursor(0, 0);
  373. display.println("Hello From Arduino");
  374. display.println(" :)");
  375. display.println("Hold for Bootloader");
  376. display.display();
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement