From 41fef25bf05bb8fa377c9bcbbc7c0b5858cd20a1 Mon Sep 17 00:00:00 2001 From: Selene ToyKeeper Date: Thu, 27 Jun 2019 02:40:38 -0600 Subject: added factory reset function to Anduril, and reboot function for FSM --- spaghetti-monster/anduril/anduril.c | 80 ++++++++++++++++++++++++++++++++++-- spaghetti-monster/anduril/cfg-fw3a.h | 3 ++ spaghetti-monster/fsm-main.c | 5 +++ spaghetti-monster/fsm-misc.c | 17 ++++++++ spaghetti-monster/fsm-misc.h | 4 ++ 5 files changed, 106 insertions(+), 3 deletions(-) diff --git a/spaghetti-monster/anduril/anduril.c b/spaghetti-monster/anduril/anduril.c index 913a857..c2a4b77 100644 --- a/spaghetti-monster/anduril/anduril.c +++ b/spaghetti-monster/anduril/anduril.c @@ -28,6 +28,14 @@ #define USE_THERMAL_REGULATION #define DEFAULT_THERM_CEIL 45 // try not to get hotter than this +#define USE_FACTORY_RESET +//#define USE_SOFT_FACTORY_RESET // only needed on models which can't use hold-button-at-boot + +// dual-switch support (second switch is a tail clicky) +// (currently incompatible with factory reset) +//#define START_AT_MEMORIZED_LEVEL + + // short blip when crossing from "click" to "hold" from off // (helps the user hit moon mode exactly, instead of holding too long // or too short) @@ -81,9 +89,6 @@ //#define USE_POLICE_STROBE_MODE //#define USE_SOS_MODE -// dual-switch support (second switch is a tail clicky) -//#define START_AT_MEMORIZED_LEVEL - /***** specific settings for known driver types *****/ #include "tk.h" #include incfile(CONFIGFILE) @@ -201,6 +206,10 @@ typedef enum { #endif #endif +#ifdef USE_SOFT_FACTORY_RESET +#define USE_REBOOT +#endif + #include "spaghetti-monster.h" @@ -291,6 +300,10 @@ void blip(); void indicator_blink(uint8_t arg); #endif +#ifdef USE_FACTORY_RESET +void factory_reset(); +#endif + // remember stuff even after battery was changed void load_config(); void save_config(); @@ -603,6 +616,13 @@ uint8_t off_state(Event event, uint16_t arg) { return MISCHIEF_MANAGED; } #endif + #if defined(USE_FACTORY_RESET) && defined(USE_SOFT_FACTORY_RESET) + // 13 clicks and hold the last click: invoke factory reset (reboot) + else if (event == EV_click13_hold) { + reboot(); + return MISCHIEF_MANAGED; + } + #endif return EVENT_NOT_HANDLED; } @@ -2136,6 +2156,55 @@ void indicator_blink(uint8_t arg) { #endif +#ifdef USE_FACTORY_RESET +void factory_reset() { + // display a warning for a few seconds before doing the actual reset, + // so the user has time to abort if they want + #define SPLODEY_TIME 3000 + #define SPLODEY_STEPS 64 + #define SPLODEY_TIME_PER_STEP (SPLODEY_TIME/SPLODEY_STEPS) + uint8_t bright; + uint8_t reset = 1; + // wind up to an explosion + for (bright=0; bright>1); + delay_4ms(SPLODEY_TIME_PER_STEP/2/4); + if (! button_is_pressed()) { + reset = 0; + break; + } + } + // explode, if button pressed long enough + if (reset) { + #ifdef USE_THERMAL_REGULATION + // auto-calibrate temperature... assume current temperature is 21 C + config_state_values[0] = 21; + config_state_values[1] = 0; + thermal_config_save(); + #endif + // save all settings to eeprom + // (assuming they're all at default because we haven't loaded them yet) + save_config(); + + bright = MAX_LEVEL; + for (; bright > 0; bright--) { + set_level(bright); + delay_4ms(SPLODEY_TIME_PER_STEP/6/4); + } + } + // explosion cancelled, fade away + else { + for (; bright > 0; bright--) { + set_level(bright); + delay_4ms(SPLODEY_TIME_PER_STEP/3/4); + } + } +} +#endif + + void load_config() { if (load_eeprom()) { ramp_style = eeprom[ramp_style_e]; @@ -2286,6 +2355,11 @@ void setup() { delay_4ms(3); set_level(0); + #ifdef USE_FACTORY_RESET + if (button_is_pressed()) + factory_reset(); + #endif + load_config(); #ifdef USE_TINT_RAMPING diff --git a/spaghetti-monster/anduril/cfg-fw3a.h b/spaghetti-monster/anduril/cfg-fw3a.h index a28d12a..489766c 100644 --- a/spaghetti-monster/anduril/cfg-fw3a.h +++ b/spaghetti-monster/anduril/cfg-fw3a.h @@ -17,3 +17,6 @@ #define THERM_FASTER_LEVEL MAX_Nx7135 #define USE_TENCLICK_THERMAL_CONFIG + +// can't reset the normal way because power is connected before the button +#define USE_SOFT_FACTORY_RESET diff --git a/spaghetti-monster/fsm-main.c b/spaghetti-monster/fsm-main.c index 6f74e9b..1c28f5f 100644 --- a/spaghetti-monster/fsm-main.c +++ b/spaghetti-monster/fsm-main.c @@ -40,6 +40,11 @@ int main() { // Don't allow interrupts while booting cli(); + #ifdef USE_REBOOT // prevent reboot loops + MCUSR &= ~(1<