AskSin++
ResetOnBoot.h
1 //- -----------------------------------------------------------------------------------------------------------------------
2 // AskSin++
3 // 2019-09-30 jp112sdl Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
4 // 2019-09-30 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
5 //- -----------------------------------------------------------------------------------------------------------------------
6 
7 #ifndef __RESETONBOOT_H__
8 #define __RESETONBOOT_H__
9 
10 #define BOOT_STATE_NORMAL 0x00
11 #define BOOT_STATE_PRE_RESET 0x01
12 #define BOOT_STATE_RESET 0x02
13 
14 #include <AskSinPP.h>
15 
16 namespace as {
17 
18 template <class DEVTYPE>
19 class ResetOnBoot : public Alarm {
20  DEVTYPE& dev;
21 private:
22  uint8_t cnt;
23  uint8_t ms;
24 public:
25  ResetOnBoot (DEVTYPE& d) : Alarm(0), dev(d), cnt(0), ms(200) { async(true); }
26  virtual ~ResetOnBoot() {}
27 
28  void setBootState(uint8_t state) {
29  StorageConfig sc = dev.getConfigArea();
30  sc.setByte(CONFIG_BOOTSTATE, state);
31  //DPRINT(F("SET NEXT BOOT STATE : "));DDECLN(state);
32  sc.validate();
33  }
34 
35  uint8_t getBootState() {
36  StorageConfig sc = dev.getConfigArea();
37  //DPRINT(F("GET CURRENT BOOT STATE : "));DDECLN(sc.getByte(CONFIG_BOOTSTATE));
38  return sc.getByte(CONFIG_BOOTSTATE);
39  }
40 
41  void finish() {
42  led(false);
43  setBootState(BOOT_STATE_NORMAL);
44  }
45 
46  virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) {
47  if (cnt < (4000 / ms)) {
48  cnt++;
49  cnt % 2 == 0 ? led(true) : led(false);
50  tick = millis2ticks(ms);
51  clock.add(*this);
52  } else {
53  finish();
54  }
55  }
56 
57  virtual void led(bool on) {
58  on == true ? dev.led().ledOn() : dev.led().ledOff();
59  }
60 
61  void init() {
62  if (getBootState() == BOOT_STATE_RESET) {
63  DPRINT(F("Activated RESET ON BOOT"));
64  finish();
65  dev.reset();
66  } else if (getBootState() == BOOT_STATE_PRE_RESET) {
67  setBootState(BOOT_STATE_RESET);
68  ms = 100;
69  } else if (getBootState() == BOOT_STATE_NORMAL) {
70  setBootState(BOOT_STATE_PRE_RESET);
71  }
72  set(millis2ticks(ms));
73  sysclock.add(*this);
74  }
75 };
76 
77 }
78 #endif
as::ResetOnBoot
Definition: ResetOnBoot.h:19
as::Alarm
Definition: Alarm.h:15
as::StorageConfig
Definition: Storage.h:464
as::AlarmClock
Definition: AlarmClock.h:32