LowPower
LowPowerCommon.h
1 #pragma once
2 
3 #include <Arduino.h>
4 
5 #include "LowPowerCommon.h"
6 
7 namespace low_power {
8 
9 enum class sleep_mode_enum_t {
10  noSleep,
11  lightSleep,
12  deepSleep,
13  modemSleep,
14 };
15 
16 enum class time_unit_t {
17  sec,
18  ms,
19  us,
20 };
21 
22 enum class pin_change_t {
23  on_high,
24  on_low,
25 };
26 
34  public:
36  virtual bool sleep(void) = 0;
37 
39  virtual bool setSleepTime(uint32_t time, time_unit_t time_unit_type) = 0;
40 
42  virtual bool addWakeupPin(int pin, pin_change_t change_type) = 0;
43 
45  virtual bool sleepFor(uint32_t time, time_unit_t time_unit_type) {
46  setSleepTime(time, time_unit_type);
47  sleep();
48  return true;
49  }
50 
52  virtual void setActive(bool flag) { is_active = false; }
53 
55  virtual void setActiveTime(uint32_t time, time_unit_t time_unit_type) {
56  uint32_t timeout_us = toUs(time, time_unit_type);
57  timeout_end = millis() + timeout_us / 1000;
58  ;
59  }
60 
62  virtual bool isActive() {
63  if (timeout_end > 0 && timeout_end > millis()) return false;
64  if (!is_active) return false;
65  return true;
66  }
67 
69  virtual operator bool() { return isActive(); }
70 
72  virtual bool setSleepMode(sleep_mode_enum_t mode) {
73  sleep_mode = mode;
74  bool result = isModeSupported(mode);
75  if (result && mode == sleep_mode_enum_t::modemSleep) sleep();
76  return result;
77  }
78 
81  virtual void process() {
82  if (isActive()) return;
83  sleep();
84  // recalculate next timeout
85  setActiveTime(timeout_us, time_unit_t::us);
86  }
87 
89  virtual bool isProcessingOnSleep(sleep_mode_enum_t sleep_mode) = 0;
90 
92  virtual bool isProcessingOnSleep() {
93  return isProcessingOnSleep(sleep_mode);
94  };
95 
97  virtual bool isModeSupported(sleep_mode_enum_t sleep_mode) {
98  bool result = true;
99  switch (sleep_mode) {
100  case sleep_mode_enum_t::noSleep:
101  result = false;
102  break;
103  case sleep_mode_enum_t::lightSleep:
104  result = true;
105  break;
106  case sleep_mode_enum_t::deepSleep:
107  result = true;
108  break;
109  case sleep_mode_enum_t::modemSleep:
110  result = true;
111  break;
112  }
113  return result;
114  }
115 
117  virtual void clear() {
118  sleep_mode = sleep_mode_enum_t::deepSleep;
119  time_unit = time_unit_t::ms;
120  timeout_us = 0;
121  timeout_end = 0;
122  is_active = true;
123  }
124 
125  protected:
126  bool is_active = true;
127  uint32_t timeout_end = 0;
128  uint32_t timeout_us = 0;
129  time_unit_t time_unit = time_unit_t::ms;
130  sleep_mode_enum_t sleep_mode = sleep_mode_enum_t::deepSleep;
131 
132  uint64_t toUs(uint64_t time, time_unit_t time_unit) {
133  switch (time_unit) {
134  case time_unit_t::sec:
135  return time * 1000000;
136  case time_unit_t::ms:
137  return time * 1000;
138  case time_unit_t::us:
139  return time;
140  }
141  return 0;
142  }
143 };
144 
145 } // namespace low_power
Common API for power saving modes for different processor architectures.
Definition: LowPowerCommon.h:33
virtual void setActiveTime(uint32_t time, time_unit_t time_unit_type)
Defiles the active time.
Definition: LowPowerCommon.h:55
virtual bool sleep(void)=0
Sets processor into sleep mode.
virtual bool isActive()
Checks if we are active (not sleeping)
Definition: LowPowerCommon.h:62
virtual void process()
Triggers the processing to be active or sleeping based on the set definitions.
Definition: LowPowerCommon.h:81
virtual bool sleepFor(uint32_t time, time_unit_t time_unit_type)
sets mc into sleep mode to sleep for indicated millis
Definition: LowPowerCommon.h:45
virtual bool setSleepTime(uint32_t time, time_unit_t time_unit_type)=0
Defines the sleep time.
virtual void setActive(bool flag)
sets the flag to be active
Definition: LowPowerCommon.h:52
virtual bool isProcessingOnSleep(sleep_mode_enum_t sleep_mode)=0
Returns true if processing is possible in the current sleep mode.
virtual void clear()
reset the processing
Definition: LowPowerCommon.h:117
virtual bool addWakeupPin(int pin, pin_change_t change_type)=0
Defines the wakup pin.
virtual bool setSleepMode(sleep_mode_enum_t mode)
Defines the sleep mode.
Definition: LowPowerCommon.h:72
virtual bool isModeSupported(sleep_mode_enum_t sleep_mode)
Provides information if the indicated mode is supported.
Definition: LowPowerCommon.h:97
virtual bool isProcessingOnSleep()
Returns true if processing is possible in the current sleep mode.
Definition: LowPowerCommon.h:92