-
Notifications
You must be signed in to change notification settings - Fork 3
/
picopower.py
70 lines (51 loc) · 2.1 KB
/
picopower.py
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
64
65
66
67
68
69
70
from machine import ADC
import time
import machine
class PicoPower:
def __init__(self,
adc_vin,
conversion_factor,
full_battery_voltage,
empty_battery_voltage,
log_file = None
):
self._adc_vin = adc_vin
self._conversion_factor = conversion_factor
self._full_battery = full_battery_voltage
self._empty_battery = empty_battery_voltage
self._log_file = log_file
self._just_started = True
def voltage(self):
''' reads the voltage in adc_vin and converts it
to get the actual battery voltage '''
return self._adc_vin.read_u16() * self._conversion_factor
def percentage(self, voltage):
''' converts battery voltage to percentage, according
to the reference full/empty battery voltages '''
percentage = 100 * ((voltage - self._empty_battery) / (self._full_battery - self._empty_battery))
return 100.00 if percentage > 100 else percentage
def log_power_reading(self):
# performs a power reading, prints, logs, and returns data
tm = time.localtime()
voltage = self.voltage()
percentage = self.percentage(voltage)
if self._just_started:
if machine.reset_cause == 3:
cause = "deepsleep"
else:
cause = "reboot"
restart_message = f"=== System restarted (waking up from {cause}) ===\n"
print(restart_message, end="")
message = '%04d-%02d-%02d %02d:%02d:%02d Lipo voltage: %.2f (%03d%%)\n' % (
tm[0], tm[1], tm[2],
tm[3], tm[4], tm[5],
voltage, percentage)
print(message, end="")
# log if enabled
if self._log_file is not None:
with open(self._log_file, "a") as f:
if self._just_started:
f.write(restart_message)
f.write(message)
self._just_started = False
return(tm, voltage, percentage)