/* long_lp_timer starts a timer for 2hr 35min 15:025sec can time up to 24,500 years in hrs,mm,ss,ms (c)2018 Forward Computing and Control Pty. Ltd. This example code is in the public domain. */ // comment out the next define to remove Serial support // and save the supply current Serial.prints use. #define DEBUG #include #include int led = 14; // Use P0.14 has the 'led' pin bool ledOn = false; lp_timer ledTimer; const unsigned long DELAY_TIME = 500000; // ms == 10Hz lp_BLESerial bleSerial; // create a BLE serial connection void startBLE() { // set advertised name bleSerial.setName("long_lp_timer"); // <<<<<<<< set your device name here // begin initialization bleSerial.begin(); } lp_timer mins6_timer; // counts 6min lp_timer mins_secs_ms_delay; // times the last mins, sec, ms int hrsCounter; uint32_t mins6Counter; uint32_t minsSecms_timeout; void startHrMinSecTimer(unsigned int hrs, unsigned int mins, unsigned int secs, unsigned int ms) { // do checks here for valid input const uint32_t ms_6min = 1000 * 60 * 6; const uint32_t us_6min = 1000 * 60 * 6 * 1000; if (ms >= 1000) { secs += ms / 1000; ms = ms % 1000; } if (secs >= 60) { mins += secs / 60; secs = secs % 60; } if (mins >= 60) { hrs += mins / 60; mins = mins % 60; } #ifdef DEBUG Serial.println(); Serial.println(" -- startHrMinSecTimer NORMALIZED TO -- "); Serial.print(" hrs:"); Serial.print( hrs); Serial.print(" mins:"); Serial.print(mins); Serial.print(" secs:"); Serial.print( secs); Serial.print(" ms:"); Serial.print(ms); Serial.println(); #endif uint64_t ms_timeout = ms + 1000 * (secs + 60 * mins); // move excess hrs to hrsCounter uint64_t excess6min = 0; if (ms_timeout > ms_6min) { excess6min = ms_timeout / ms_6min; ms_timeout = ms_timeout % ms_6min; } minsSecms_timeout = (uint32_t) ms_timeout; mins6Counter = hrs * 10 + excess6min; #ifdef DEBUG Serial.print("mins6Counter:"); Serial.print( mins6Counter); Serial.print(" minsSecms_timeout:"); Serial.print(minsSecms_timeout); Serial.println(); Serial.print(millis()); Serial.print("ms -- "); Serial.println("start long_lp_timer"); #endif if (mins6Counter > 0) { int err_code = mins6_timer.startTimer_us(us_6min, min6_handler); // start counting 6mins if (err_code) { #ifdef DEBUG Serial.print("mins_secs_ms_delay start failed with code :"); Serial.println(err_code); #endif } else { #ifdef DEBUG Serial.print("start mins6_timer 6mins x "); Serial.println(mins6Counter); #endif } } else { // just do mins sec int err_code = mins_secs_ms_delay.startDelay_us( minsSecms_timeout * 1000, ms_handler); if (err_code) { #ifdef DEBUG Serial.print("mins_secs_ms_delay start failed with code :"); Serial.println(err_code); #endif } else { #ifdef DEBUG Serial.print("start mins_secs_ms_delay:"); Serial.println(minsSecms_timeout); #endif } } } void min6_handler() { mins6Counter -= 1; #ifdef DEBUG Serial.print("min6_handler remaining 6mins x "); Serial.println(mins6Counter); #endif if (mins6Counter == 0) { mins6_timer.stop(); int err_code = mins_secs_ms_delay.startDelay_us( minsSecms_timeout * 1000, ms_handler); if (err_code) { #ifdef DEBUG Serial.print("mins_secs_ms_delay start failed with code :"); Serial.println(err_code); #endif } else { #ifdef DEBUG Serial.print("start mins_secs_ms_delay:"); Serial.println(minsSecms_timeout); #endif } } // else } void ms_handler() { #ifdef DEBUG Serial.println(); Serial.println("long_lp_timer timed out"); #endif } void handleLedTimer() { ledOn = !ledOn; // toggle state if (ledOn) { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) Serial.println('H'); } else { digitalWrite(led, LOW); // turn the LED off by making the voltage LOW Serial.println('L'); } } void setup() { #ifdef DEBUG // default Serial pins are Rx P0.30 and Tx P0.29 for Generic nRF52832 board settings Serial.setPins(7, 8); // remap Rx to P0.7 and Tx to P0.8 for test board //Serial.setPins(11,12); // remap Rx to P0.11 and Tx to P0.12 for GT832E_01 Serial.begin(115200); for (int i = 10; i > 0; i--) { Serial.print(i); Serial.print(' '); delay(500); } Serial.println(); #endif pinMode(led, OUTPUT); startBLE(); // startHrMinSecTimer(2, 35, 15, 25); startHrMinSecTimer(0, 35, 65, 1025); // start flashing at 0.5sec ledTimer.startTimer_us(DELAY_TIME*1000, handleLedTimer); #ifdef DEBUG Serial.print(millis()); Serial.print("ms -- "); Serial.println("startup() finished"); #endif } void loop() { sleep(); #ifdef DEBUG Serial.print(millis()); Serial.print("ms -- "); Serial.println("loop() triggered"); #endif }