arduino-spybug/spybug/sys.cpp

38 lines
892 B
C++
Raw Normal View History

2022-04-19 19:30:15 +02:00
// Copyright 2022 Darwin Schuppan <darwin@nobrain.org>
// SPDX license identifier: MIT
#include "sys.hh"
2022-04-19 19:45:13 +02:00
void (*full_reset)() = nullptr;
2022-04-19 19:30:15 +02:00
static volatile bool wdt_int_sleep_mode = false;
ISR (WDT_vect) {
if (wdt_int_sleep_mode)
wdt_disable();
else
full_reset();
}
/* Based on https://github.com/rocketscream/Low-Power. */
void low_power_sleep_minutes(unsigned long t) {
wdt_int_sleep_mode = true;
ADCSRA &= ~_BV(ADEN); /* Disable ADC. */
for (unsigned long i = 0; 8ul * i < 60ul * t; i++) {
// Power Down for 8s
wdt_enable(WDTO_8S); /* Start watchdog timer for 8s. */
WDTCSR |= (1 << WDIE); /* Enable watchdog interrupt. */
do {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
cli();
sleep_enable();
sleep_bod_disable();
sei();
sleep_cpu();
sleep_disable();
sei();
} while (0);
}
ADCSRA |= _BV(ADEN); /* Re-enable ADC. */
wdt_int_sleep_mode = false;
}