Dependencies: nRF51822
Diff: Source/main.cpp
- Revision:
- 58:9b94a7caadba
- Parent:
- 57:6c0608782fc6
- Child:
- 59:1dd53bfb012d
diff -r 6c0608782fc6 -r 9b94a7caadba Source/main.cpp --- a/Source/main.cpp Fri Dec 11 21:34:00 2020 +0000 +++ b/Source/main.cpp Fri Dec 18 19:17:26 2020 +0000 @@ -209,6 +209,8 @@ state_t last_state = OTHER; int last_cap; +int back_to_sleep_delay = 0; +int ambient_debounce = 0; /// Main state machine /// Called whenever a sensor changes @@ -277,6 +279,8 @@ set_radio(false); // already done when NV_TESTING_REQUIRED was cleared. led = 0; state = SHIP_MODE_WAIT_DARK; + ambient_debounce = 0; + log_add(SMWD, is_cap_off, is_package_open, package_open_detected); } break; @@ -284,7 +288,9 @@ //flash_led(1,0.1); if(!is_package_open) { // its dark + test_cap(); state = SHIP_MODE_WAIT_LIGHT; + log_add(SMWL, is_cap_off, is_package_open, package_open_detected); } break; @@ -313,6 +319,9 @@ is_package_open.rise(&light_interrupt); is_package_open.enable_irq(); state = SHIP_MODE_CHECK_CAP; + back_to_sleep_delay = 0; + ambient_debounce = 0; + log_add(SMCC, is_cap_off, is_package_open, package_open_detected); break; @@ -328,15 +337,23 @@ // otherwise stay in this state if(!is_package_open) { // if ambient is dark - state = SHIP_MODE_WAIT_LIGHT; // prepare to go back to sleep + back_to_sleep_delay++; + ambient_debounce++; + if(back_to_sleep_delay > GO_BACK_TO_SLEEP_DELAY && ambient_debounce > AMBIENT_DEBOUNCE) + { // only go back to sleep if its dark for a GO_BACK_TO_SLEEP_DELAY and the ambient sensor is debounced signalling dark. + state = SHIP_MODE_WAIT_LIGHT; + log_add(SMWL, is_cap_off, is_package_open, package_open_detected); + } } else { // if ambient is light + ambient_debounce = 0; + //test_cap(); if(!is_cap_off) { // cap is on package_open_sense_enable = 0; set_rtc(0); - log_add(EVENT_WAKE_FROM_SHIP, 0, 0, 0); + log_add(EVENT_WAKE_FROM_SHIP, is_cap_off, is_package_open, package_open_detected); // (cap is on) before going to in-use, adapt the cap_on threshold (iff the cap is on) {