LowPower
LowPowerATTiny.h
1 #pragma once
2 
3 #include <avr/interrupt.h>
4 #include <avr/power.h> // Power management
5 #include <avr/sleep.h> // Sleep Modes
6 #include <avr/wdt.h>
7 
8 #include "LowPowerCommon.h"
9 
10 //#include <SoftwareSerial.h>
11 //SoftwareSerial Serial(2, 3); // RX and TX
12 
13 namespace low_power {
14 
15 class ArduinoLowPowerATTiny;
16 ArduinoLowPowerATTiny *selfArduinoLowPowerATTiny = nullptr;
17 
28  public:
29  ArduinoLowPowerATTiny() { selfArduinoLowPowerATTiny = this; }
30 
32  bool isProcessingOnSleep(sleep_mode_enum_t sleep_mode) { return false; }
33 
35  bool sleep(void) override {
36  if (sleep_mode == sleep_mode_enum_t::deepSleep) {
37  open_watchdog_cycle = 0;
38  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
39  if (sleep_time_us > 0) setupWatchdog();
40  doDeepSleep();
41  // for the case where we need to sleep for multiple cycles
42  while (open_watchdog_cycle > 0) {
43  doDeepSleep();
44  }
45  wdt_disable();
46  set_sleep_mode(SLEEP_MODE_IDLE);
47  } else {
48  // disable all except the timer 1
49  set_sleep_mode(SLEEP_MODE_IDLE);
50  power_all_disable();
51  power_timer0_enable();
52  delay(sleep_time_us / 1000);
53  power_all_enable();
54  }
55  return true;
56  }
57 
58  bool setSleepTime(uint32_t time, time_unit_t time_unit_type) override {
59  sleep_time_us = toUs(time, time_unit);
60  return true;
61  }
62 
63  bool addWakeupPin(int pin, pin_change_t change_type) override {
64  // ADCSRA = 0; // ADC disabled
65  attachInterrupt(pin, pinWakupCB,
66  change_type == pin_change_t::on_high
67  ? RISING
68  : FALLING); // interrupt on falling edge of pin2
69  return true;
70  }
71 
72  bool isModeSupported(sleep_mode_enum_t sleep_mode) override { return true; }
73 
74  void clear() {
76  sleep_time_us = 0;
77  open_watchdog_cycle = 0;
78  pin_mask = 0;
79  set_sleep_mode(SLEEP_MODE_IDLE);
80  }
81 
83  static void processWatchdogCycle() {
84  if (selfArduinoLowPowerATTiny != nullptr &&
85  selfArduinoLowPowerATTiny->open_watchdog_cycle > 0) {
86  selfArduinoLowPowerATTiny->open_watchdog_cycle--;
87  }
88  }
89 
90  protected:
91  int open_watchdog_cycle = 0;
92  uint32_t sleep_time_us = 0;
93  uint32_t pin_mask = 0;
94  uint16_t timings_ms[8] = {15, 30, 60, 120, 250, 500, 1000, 2000};
95 
96 
97  void setupWatchdog() {
98  int sleep_time_ms = sleep_time_us / 1000;
99  int idx = getTimeIdx(sleep_time_ms);
100  open_watchdog_cycle = sleep_time_ms / timings_ms[idx];
101  wdt_enable(idx); // Set WDog
102  }
103 
104  // get the closest smaller time
105  int getTimeIdx(int ms) {
106  for (int j = 0; j < 5; j++) {
107  if (timings_ms[j + 1] > ms) return j;
108  }
109  // 1sec
110  return 6;
111  }
112 
113  static void pinWakupCB() {}
114 
115  // set processor into deep sleep
116  void doDeepSleep() {
117  cli();
118  sleep_enable();
119  sleep_bod_disable();
120  sei();
121  sleep_cpu();
122  if (open_watchdog_cycle <= 0) {
123  // after waking up
124  sleep_disable();
125  }
126  sei();
127  }
128 };
129 
130 static ArduinoLowPowerATTiny LowPower;
131 
132 } // namespace low_power
133 
134 
135 // watchdog interrupt
136 ISR(WDT_vect) {
137  wdt_reset();
138  if (low_power::selfArduinoLowPowerATTiny != nullptr)
139  low_power::selfArduinoLowPowerATTiny->processWatchdogCycle();
140 
141 } // end of WDT_vect
Low Power Management for ATTiny:
Definition: LowPowerATTiny.h:27
bool setSleepTime(uint32_t time, time_unit_t time_unit_type) override
Defines the sleep time.
Definition: LowPowerATTiny.h:58
static void processWatchdogCycle()
Called by watchdog interrupt.
Definition: LowPowerATTiny.h:83
bool isModeSupported(sleep_mode_enum_t sleep_mode) override
Provides information if the indicated mode is supported.
Definition: LowPowerATTiny.h:72
bool addWakeupPin(int pin, pin_change_t change_type) override
Defines the wakup pin.
Definition: LowPowerATTiny.h:63
bool sleep(void) override
sets processor into sleep mode
Definition: LowPowerATTiny.h:35
void clear()
reset the processing
Definition: LowPowerATTiny.h:74
bool isProcessingOnSleep(sleep_mode_enum_t sleep_mode)
you cant do any processing which we sleep
Definition: LowPowerATTiny.h:32
Common API for power saving modes for different processor architectures.
Definition: LowPowerCommon.h:33
virtual void clear()
reset the processing
Definition: LowPowerCommon.h:117