-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerManager.cpp
More file actions
63 lines (49 loc) · 1.22 KB
/
PowerManager.cpp
File metadata and controls
63 lines (49 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @file PowerManager.cpp
* @author M. Rakeh Saleem
* @brief Power management utilities implementation for ATtiny85
* @date Created: 2020
* @version 1.0
*/
#include "PowerManager.h"
bool PowerManager::sleepEnabled = false;
unsigned long PowerManager::wakeTime = 0;
void PowerManager::init() {
// Configure all pins as inputs to minimize power consumption
for (uint8_t i = 0; i < 5; i++) {
pinMode(i, INPUT);
}
// Enable pin change interrupts
GIMSK |= _BV(PCIE);
sei();
}
void PowerManager::enterSleep() {
sleepEnabled = true;
wakeTime = millis();
// Disable ADC
disableADC();
// Set sleep mode to power down
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
// Enable interrupts and sleep
sei();
sleep_cpu();
// Wake up
sleep_disable();
enableADC();
}
void PowerManager::wakeUp() {
sleepEnabled = false;
}
bool PowerManager::shouldStayAwake(unsigned long duration) {
return (millis() - wakeTime) < duration;
}
void PowerManager::setupWakeInterrupt(uint8_t pin) {
PCMSK |= _BV(pin);
}
void PowerManager::disableADC() {
ADCSRA &= ~_BV(ADEN);
}
void PowerManager::enableADC() {
ADCSRA |= _BV(ADEN);
}