|
@@ -16,6 +16,9 @@
|
16
|
16
|
# See <http://www.gnu.org/licenses/>.
|
17
|
17
|
# ----------------------------------------------------------------------------
|
18
|
18
|
|
|
19
|
+from lcd import LCD
|
|
20
|
+lcd = LCD()
|
|
21
|
+
|
19
|
22
|
import uasyncio as asyncio
|
20
|
23
|
import io
|
21
|
24
|
import sys
|
|
@@ -23,13 +26,16 @@ import machine
|
23
|
26
|
import os
|
24
|
27
|
import gc
|
25
|
28
|
import time
|
|
29
|
+from state_wait_temp import from_hsv, translate
|
26
|
30
|
|
27
|
31
|
# https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/examples/pico_lipo_shim/battery_pico.py
|
28
|
32
|
# https://github.com/pimoroni/enviro/pull/146
|
29
|
33
|
# TODO https://github.com/micropython/micropython/issues/11185
|
30
|
34
|
|
31
|
|
-full_battery = 4.2
|
|
35
|
+full_battery = 4.1
|
32
|
36
|
empty_battery = 3.2
|
|
37
|
+batt_warn_limit = 15
|
|
38
|
+batt_reread_limit = 2.7
|
33
|
39
|
|
34
|
40
|
charging = machine.Pin("WL_GPIO2", machine.Pin.IN)
|
35
|
41
|
conversion_factor = 3 * 3.3 / 65535
|
|
@@ -51,7 +57,7 @@ def batteryVoltageAverage():
|
51
|
57
|
old_pad = get_pad(29)
|
52
|
58
|
set_pad(29, 128) # no pulls, no output, no input
|
53
|
59
|
|
54
|
|
- sample_count = 10
|
|
60
|
+ sample_count = 3
|
55
|
61
|
voltage = 0
|
56
|
62
|
for i in range(0, sample_count):
|
57
|
63
|
voltage += batteryVoltageRead()
|
|
@@ -63,13 +69,17 @@ def batteryVoltageAverage():
|
63
|
69
|
def batteryVoltage():
|
64
|
70
|
global cachedVoltage, lastCaching
|
65
|
71
|
|
66
|
|
- if ((time.time() - lastCaching) >= 2) or (cachedVoltage == None):
|
|
72
|
+ if ((time.time() - lastCaching) > 0) or (cachedVoltage == None):
|
67
|
73
|
lastCaching = time.time()
|
68
|
74
|
cachedVoltage = batteryVoltageAverage()
|
|
75
|
+ if cachedVoltage <= batt_reread_limit:
|
|
76
|
+ cachedVoltage = batteryVoltageAverage()
|
69
|
77
|
|
70
|
78
|
percentage = 100.0 * ((cachedVoltage - empty_battery) / (full_battery - empty_battery))
|
71
|
|
- if percentage > 100.0:
|
72
|
|
- percentage = 100.0
|
|
79
|
+ if percentage >= 100.0:
|
|
80
|
+ percentage = 99.0
|
|
81
|
+ elif percentage < 0.0:
|
|
82
|
+ percentage = 0.0
|
73
|
83
|
|
74
|
84
|
return cachedVoltage, percentage
|
75
|
85
|
|
|
@@ -86,20 +96,33 @@ class States:
|
86
|
96
|
self.lcd.fill(self.lcd.black)
|
87
|
97
|
self.lcd.text("Volcano Remote Control App", 0, 0, self.lcd.green)
|
88
|
98
|
|
89
|
|
- r = await self.states[self.current].draw()
|
|
99
|
+ ret = await self.states[self.current].draw()
|
90
|
100
|
|
91
|
101
|
voltage, percentage = batteryVoltage()
|
92
|
|
- s = "Charging ({:.2f}V)".format(voltage)
|
93
|
|
- c = self.lcd.green
|
|
102
|
+ s = "Charging"
|
|
103
|
+ c = self.lcd.white
|
94
|
104
|
if charging.value() != 1:
|
95
|
105
|
s = "{:.0f}% ({:.2f}V)".format(percentage, voltage)
|
96
|
|
- c = self.lcd.white
|
97
|
|
- if percentage <= 20:
|
|
106
|
+ if percentage <= batt_warn_limit:
|
98
|
107
|
c = self.lcd.red
|
99
|
|
- self.lcd.text("Battery: {}".format(s), 0, self.lcd.height - 10, c)
|
|
108
|
+ else:
|
|
109
|
+ hue = translate(percentage, batt_warn_limit, 100, 0.0, 0.333)
|
|
110
|
+ r, g, b = from_hsv(hue, 1.0, 1.0)
|
|
111
|
+ c = self.lcd.color(r, g, b)
|
|
112
|
+ whole = "Batt: {}".format(s)
|
|
113
|
+ self.lcd.text(whole, 0, self.lcd.height - 10, c)
|
|
114
|
+
|
|
115
|
+ off = (len(whole) + 1) * 8
|
|
116
|
+ if percentage <= batt_warn_limit:
|
|
117
|
+ self.lcd.text("CHARGE NOW!", off, self.lcd.height - 10, self.lcd.red)
|
|
118
|
+ elif charging.value() != 1:
|
|
119
|
+ self.lcd.rect(off, self.lcd.height - 10, self.lcd.width - off, 8, c, False)
|
|
120
|
+ max_w = self.lcd.width - off - 2
|
|
121
|
+ w = int(percentage / 100.0 * max_w)
|
|
122
|
+ self.lcd.rect(off + 1, self.lcd.height - 9, w, 6, c, True)
|
100
|
123
|
|
101
|
124
|
self.lcd.show()
|
102
|
|
- return r
|
|
125
|
+ return ret
|
103
|
126
|
|
104
|
127
|
def run(self):
|
105
|
128
|
if self.current == None:
|
|
@@ -166,9 +189,6 @@ def state_machine(lcd):
|
166
|
189
|
while True:
|
167
|
190
|
states.run()
|
168
|
191
|
|
169
|
|
-from lcd import LCD
|
170
|
|
-lcd = LCD()
|
171
|
|
-
|
172
|
192
|
def main():
|
173
|
193
|
# splash screen
|
174
|
194
|
from state_wait_temp import from_hsv
|
|
@@ -194,7 +214,6 @@ def main():
|
194
|
214
|
lcd.textC(os.uname()[4][30 : 60], int(lcd.width / 2), lcd.height - 10, lcd.white, lcd.black)
|
195
|
215
|
|
196
|
216
|
lcd.show()
|
197
|
|
- lcd.brightness(1.0)
|
198
|
217
|
|
199
|
218
|
# bootloader access with face buttons
|
200
|
219
|
keys = lcd.buttons()
|