10 #include <AlarmClock.h>
12 #if defined(ARDUINO_ARCH_AVR) && ! ( defined(ARDUINO_AVR_ATmega32) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega128__))
18 #if defined(ARDUINO_ARCH_AVR) && ! (defined(ARDUINO_AVR_ATmega32) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega128__))
21 template <
bool ENABLETIMER2=false,
bool ENABLEADC=false>
25 static void waitSerial () {
28 while (!(UCSR0A & (1 << UDRE0))) {
31 while (!(UCSR0A & (1 << TXC0)));
35 static void powerSave (__attribute__((unused)) Hal& hal) {
36 #if defined __AVR_ATmega644P__ || defined (__AVR_ATmega1284P__)
37 LowPower.idle(SLEEP_FOREVER,ENABLEADC==
true?ADC_ON:ADC_OFF,ENABLETIMER2==
false?TIMER2_OFF:TIMER2_ON,TIMER1_ON,TIMER0_OFF,SPI_ON,USART1_OFF,USART0_ON,TWI_OFF);
38 #elif defined __AVR_ATmega2560__
40 LowPower.idle(SLEEP_FOREVER,ENABLEADC==
true?ADC_ON:ADC_OFF, TIMER5_OFF, TIMER4_OFF, TIMER3_OFF,ENABLETIMER2==
false?TIMER2_OFF:TIMER2_ON, TIMER1_ON, TIMER0_OFF, SPI_ON, USART3_OFF,USART2_OFF, USART1_OFF, USART0_ON, TWI_OFF);
42 LowPower.idle(SLEEP_FOREVER,ENABLEADC==
true?ADC_ON:ADC_OFF,ENABLETIMER2==
false?TIMER2_OFF:TIMER2_ON,TIMER1_ON,TIMER0_OFF,SPI_ON,USART0_ON,TWI_OFF);
48 template <
bool ENABLETIMER2=false>
51 static uint32_t doSleep (uint32_t ticks) {
53 period_t sleeptime = SLEEP_FOREVER;
55 if( ticks > seconds2ticks(8) ) { offset = seconds2ticks(8); sleeptime = SLEEP_8S; }
58 else if( ticks > seconds2ticks(1) ) { offset = seconds2ticks(1); sleeptime = SLEEP_1S; }
61 else if( ticks > millis2ticks(120) ) { offset = millis2ticks(120); sleeptime = SLEEP_120MS; }
64 else if( ticks > millis2ticks(15) ) { offset = millis2ticks(15); sleeptime = SLEEP_15MS; }
66 if( ENABLETIMER2 ==
false ) {
67 LowPower.powerDown(sleeptime,ADC_OFF,BOD_OFF);
70 LowPower.powerExtStandby(sleeptime,ADC_OFF,BOD_OFF,TIMER2_ON);
76 static void powerSave (Hal& hal) {
78 uint32_t ticks = sysclock.next();
79 if( sysclock.isready() ==
false ) {
80 if( ticks == 0 || ticks > millis2ticks(15) ) {
82 uint32_t offset = doSleep(ticks);
84 sysclock.correct(offset);
101 static uint32_t doSleep (uint32_t ticks) {
103 period_t sleeptime = SLEEP_FOREVER;
105 if( ticks > seconds2ticks(1) ) { sleeptime = SLEEP_FOREVER; }
106 else if( ticks > millis2ticks(500) ) { sleeptime = SLEEP_500MS; }
107 else if( ticks > millis2ticks(250) ) { sleeptime = SLEEP_250MS; }
108 else if( ticks > millis2ticks(120) ) { sleeptime = SLEEP_120MS; }
109 else if( ticks > millis2ticks(60) ) { sleeptime = SLEEP_60MS; }
110 else if( ticks > millis2ticks(30) ) { sleeptime = SLEEP_30MS; }
111 else if( ticks > millis2ticks(15) ) { sleeptime = SLEEP_15MS; }
113 uint32_t c1 = rtc.getCounter(
true);
114 LowPower.powerSave(sleeptime,ADC_OFF,BOD_OFF,TIMER2_ON);
115 uint32_t c2 = rtc.getCounter(
false);
116 offset = (c2 - c1) * seconds2ticks(1) / 256;
119 return min(ticks,offset);
123 static void powerSave (Hal& hal) {
125 uint32_t ticks = sysclock.next();
126 if( sysclock.isready() ==
false ) {
127 if( ticks == 0 || ticks > millis2ticks(15) ) {
129 uint32_t offset = doSleep(ticks);
131 sysclock.correct(offset);
160 virtual void trigger (__attribute__((unused))
AlarmClock& clock) {
165 void stayAwake (uint32_t time) {
166 uint32_t old = sysclock.get(*
this);
169 sysclock.cancel(*
this);
175 bool stayAwake ()
const {
179 template <
class Saver,
class Hal>
180 void savePower (Hal& hal) {
181 if( awake ==
false ) {
185 Saver::powerSave(hal);
194 void sleepForever (Hal& hal) {
197 #if defined(ARDUINO_ARCH_AVR) && ! (defined(ARDUINO_AVR_ATmega32) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega128__))
198 LowPower.powerDown(SLEEP_FOREVER,ADC_OFF,BOD_OFF);
205 template <
class HalType>
213 uint32_t next = millis2ticks(250);
214 if( hal.activity.stayAwake() ==
false ) {
215 bool detect = hal.radio.detectBurst();
216 if( detect ==
true ) {
217 if( burst ==
false ) {
219 next = millis2ticks(30);
224 hal.activity.stayAwake(millis2ticks(500));