Advertisement
Guest User

Untitled

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