3 #include <avr/interrupt.h>
8 #include "LowPowerCommon.h"
15 class ArduinoLowPowerATTiny;
16 ArduinoLowPowerATTiny *selfArduinoLowPowerATTiny =
nullptr;
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();
42 while (open_watchdog_cycle > 0) {
46 set_sleep_mode(SLEEP_MODE_IDLE);
49 set_sleep_mode(SLEEP_MODE_IDLE);
51 power_timer0_enable();
52 delay(sleep_time_us / 1000);
58 bool setSleepTime(uint32_t time, time_unit_t time_unit_type)
override {
59 sleep_time_us = toUs(time, time_unit);
65 attachInterrupt(pin, pinWakupCB,
66 change_type == pin_change_t::on_high
77 open_watchdog_cycle = 0;
79 set_sleep_mode(SLEEP_MODE_IDLE);
84 if (selfArduinoLowPowerATTiny !=
nullptr &&
85 selfArduinoLowPowerATTiny->open_watchdog_cycle > 0) {
86 selfArduinoLowPowerATTiny->open_watchdog_cycle--;
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};
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];
105 int getTimeIdx(
int ms) {
106 for (
int j = 0; j < 5; j++) {
107 if (timings_ms[j + 1] > ms)
return j;
113 static void pinWakupCB() {}
122 if (open_watchdog_cycle <= 0) {
130 static ArduinoLowPowerATTiny LowPower;
138 if (low_power::selfArduinoLowPowerATTiny !=
nullptr)
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