diff --git a/ToyKeeper/spaghetti-monster/anduril/anduril.c b/ToyKeeper/spaghetti-monster/anduril/anduril.c index 12a4d541..30515344 100644 --- a/ToyKeeper/spaghetti-monster/anduril/anduril.c +++ b/ToyKeeper/spaghetti-monster/anduril/anduril.c @@ -192,6 +192,9 @@ typedef enum { rgb_led_off_mode_e, rgb_led_lockout_mode_e, #endif + #if defined(USE_AUTOLOCK) && defined(TICK_DURING_STANDBY) + autolock_e, + #endif eeprom_indexes_e_END } eeprom_indexes_e; #define EEPROM_BYTES eeprom_indexes_e_END @@ -216,6 +219,10 @@ typedef enum { #define USE_REBOOT #endif +#if defined(USE_AUTOLOCK) && !defined(TICK_DURING_STANDBY) + #warning "USE_AUTOLOCK requires TICK_DURING_STANDBY." +#endif + #include "spaghetti-monster.h" @@ -294,6 +301,11 @@ uint8_t momentary_active = 0; // boolean, true if active *right now* uint8_t muggle_state(Event event, uint16_t arg); uint8_t muggle_mode_active = 0; #endif +#if defined(USE_AUTOLOCK) && defined(TICK_DURING_STANDBY) +uint8_t autolock_state(Event event, uint16_t arg); +uint8_t autolock_config_state(Event event, uint16_t arg); +uint8_t autolock = 0; +#endif // general helper function for config modes uint8_t number_entry_state(Event event, uint16_t arg); @@ -510,7 +522,7 @@ uint8_t off_state(Event event, uint16_t arg) { } return MISCHIEF_MANAGED; } - #if defined(TICK_DURING_STANDBY) && (defined(USE_INDICATOR_LED) || defined(USE_AUX_RGB_LEDS)) + #if defined(TICK_DURING_STANDBY) && (defined(USE_INDICATOR_LED) || defined(USE_AUX_RGB_LEDS) || defined(USE_AUTOLOCK)) // blink the indicator LED, maybe else if (event == EV_sleep_tick) { #ifdef USE_INDICATOR_LED @@ -520,6 +532,13 @@ uint8_t off_state(Event event, uint16_t arg) { #elif defined(USE_AUX_RGB_LEDS) rgb_led_update(rgb_led_off_mode, arg); #endif + #ifdef USE_AUTOLOCK + // TODO Handle rollover + uint16_t locktime = autolock * (uint16_t)(SLEEP_TICKS_PER_SECOND * 60); + if (autolock && arg > locktime) { + set_state(lockout_state, 0); + } + #endif return MISCHIEF_MANAGED; } #endif @@ -1746,6 +1765,13 @@ uint8_t lockout_state(Event event, uint16_t arg) { set_state(off_state, 0); return MISCHIEF_MANAGED; } + #if defined(USE_AUTOLOCK) && defined(TICK_DURING_STANDBY) + // 5 clicks: enable autolock + else if (event == EV_5clicks) { + push_state(autolock_config_state, 0); + return MISCHIEF_MANAGED; + } + #endif return EVENT_NOT_HANDLED; } @@ -2078,6 +2104,18 @@ static inline void beacon_mode_iter() { #endif // #ifdef USE_BEACON_MODE +#if defined(USE_AUTOLOCK) && defined(TICK_DURING_STANDBY) +void autolock_config_save() { + autolock = config_state_values[0]; +} + +uint8_t autolock_config_state(Event event, uint16_t arg) { + return config_state_base(event, arg, + 1, autolock_config_save); +} +#endif // #if defined(USE_AUTOLOCK) && defined(TICK_DURING_STANDBY) + + uint8_t number_entry_state(Event event, uint16_t arg) { static uint8_t value; static uint8_t blinks_left; @@ -2412,6 +2450,9 @@ void load_config() { rgb_led_off_mode = eeprom[rgb_led_off_mode_e]; rgb_led_lockout_mode = eeprom[rgb_led_lockout_mode_e]; #endif + #if defined(USE_AUTOLOCK) && defined(TICK_DURING_STANDBY) + autolock = eeprom[autolock_e]; + #endif } #ifdef START_AT_MEMORIZED_LEVEL if (load_eeprom_wl()) { @@ -2460,6 +2501,9 @@ void save_config() { eeprom[rgb_led_off_mode_e] = rgb_led_off_mode; eeprom[rgb_led_lockout_mode_e] = rgb_led_lockout_mode; #endif + #if defined(USE_AUTOLOCK) && defined(TICK_DURING_STANDBY) + eeprom[autolock_e] = autolock; + #endif save_eeprom(); } diff --git a/ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4s.h b/ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4s.h index 230ac7cc..2afb83f2 100644 --- a/ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4s.h +++ b/ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4s.h @@ -10,6 +10,7 @@ // enable blinking indicator LED while off #define TICK_DURING_STANDBY #define STANDBY_TICK_SPEED 3 // every 0.128 s +#define SLEEP_TICKS_PER_SECOND 7.8125 #define USE_FANCIER_BLINKING_INDICATOR #ifdef RAMP_LENGTH @@ -46,3 +47,5 @@ #define THERMAL_WARNING_SECONDS 3 #define THERMAL_UPDATE_SPEED 2 #define THERM_PREDICTION_STRENGTH 4 + +#define USE_AUTOLOCK