-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
217 lines (185 loc) · 6.5 KB
/
app.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import app
from app_components import Menu, Notification, clear_background
import power
main_menu_items = [
"Power",
]
power_menu_items = ["On", "Off"]
class BatteryMeter(app.App):
def __init__(self):
# Menu setup
self.menu = None
self.current_menu = None
self.set_menu("main")
# Notification setup
self.notification = None
# App setup
self.chargingCounter = 0
def back_handler(self):
# If in the topmost menu, minimize, otherwise move one menu up.
if self.current_menu == "main":
self.minimise()
else:
self.set_menu("main")
def select_handler(self, item, idx):
# If Power or Preset item selected enter that menu
if item in main_menu_items:
self.set_menu(item)
else:
if self.current_menu == "Power":
if item == "On":
self.notification = Notification("Power On")
self.power = True
self.eye.value(0)
self.set_menu("main")
elif item == "Off":
self.notification = Notification("Power Off")
self.power = False
self.dot1.value(1)
self.dot2.value(1)
self.dot3.value(1)
self.eye.value(1)
self.set_menu("main")
else:
self.notification = Notification(self.current_menu + "." + item + '"!')
def set_menu(
self,
menu_name: Literal[
"main",
"Power",
],
):
if self.menu:
self.menu._cleanup()
if self.current_menu:
previous_menu = self.current_menu
else:
previous_menu = "Power"
self.current_menu = menu_name
if menu_name == "main":
self.menu = Menu(
self,
main_menu_items,
select_handler=self.select_handler,
back_handler=self.back_handler,
position=(main_menu_items).index(previous_menu),
)
elif menu_name == "Power":
self.menu = Menu(
self,
power_menu_items,
select_handler=self.select_handler,
back_handler=self.back_handler,
position=0 if self.power else 1,
)
def draw(self, ctx):
BatteryChargeState = power.BatteryChargeState()
BatteryLevel = power.BatteryLevel()
Vbat = power.Vbat()
Fault = power.Fault()
SupplyCapabilities = power.SupplyCapabilities()[0]
Icharge = power.Icharge()
Vsys = power.Vsys()
Vin = power.Vin()
if BatteryLevel > 100:
BatteryLevel = 100
elif BatteryLevel < 0:
BatteryLevel = 0
if BatteryChargeState == "Terminated":
BatteryChargeState = "Finished Charging"
clear_background(ctx)
self.menu.draw(ctx)
if self.notification:
self.notification.draw(ctx)
batteryH = 120
batteryW = 60
batteryX = (batteryW / 2) * -1
batteryY = 20
clear_background(ctx)
ctx.rgb(0, 0, 0)
ctx.rectangle(-120, -120, 240, 240).fill()
ctx.rgb(255, 255, 255)
ctx.rectangle(-11, batteryY - batteryH - 11, 22, 12).fill()
ctx.rectangle(
batteryX - 1, batteryY + 1, batteryW + 2, (batteryH + 2) * -1
).fill()
ctx.rgb(0, 0, 0)
ctx.rectangle(-10, batteryY - batteryH - 10, 20, 10).fill()
ctx.rectangle(batteryX, batteryY, batteryW, -batteryH).fill()
r, g, b = get_color(BatteryLevel)
ctx.rgb(r, g, b).rectangle(
batteryX + 3,
batteryY - 3,
batteryW - 6,
-(BatteryLevel / 100 * (batteryH - 6)),
).fill()
ctx.rgb(r, g, b).move_to(0, 35).text("{:.1f}".format(BatteryLevel) + "%")
# Charging animation
if BatteryChargeState == "Fast Charging":
self.chargingCounter = self.chargingCounter + 2.5
ChargingBatteryLevel = BatteryLevel + self.chargingCounter
if ChargingBatteryLevel > 100:
ChargingBatteryLevel = 100
r2, g2, b2 = get_color(ChargingBatteryLevel)
ctx.rgb(r2, g2, b2).rectangle(
batteryX + 3,
batteryY - 3 - (BatteryLevel / 100 * (batteryH - 6)) - 1,
batteryW - 6,
-(ChargingBatteryLevel / 100 * (batteryH - 6))
+ (BatteryLevel / 100 * (batteryH - 6))
+ 2,
).fill()
if ChargingBatteryLevel >= 100:
self.chargingCounter = 0
else:
self.chargingCounter = 0
ctx.rgb(255, 255, 255)
ctx.move_to(1, 63).text(BatteryChargeState)
ctx.move_to(0, 62).text(BatteryChargeState)
if BatteryChargeState != "Not Charging":
ctx.move_to(0, 85).text("{:.0f}".format(Icharge * 1000) + "ma")
ctx.move_to(0, 105).text("{:.2f}".format(Vin) + "V")
# ctx.rgb(255, 0, 0).move_to(0, -50).text(
# SupplyCapabilities[0]
# + " "
# + "{:.2f}".format(SupplyCapabilities[1])
# + " "
# + "{:.2f}".format(SupplyCapabilities[2])
# )
ctx.rgb(255, 255, 255).move_to(-69, -24).text("Bat")
ctx.rgb(255, 255, 255).move_to(-70, -25).text("Bat")
ctx.move_to(-70, 0).text("{:.2f}".format(Vbat) + "V")
ctx.rgb(255, 255, 255).move_to(71, -24).text("Sys")
ctx.rgb(255, 255, 255).move_to(70, -25).text("Sys")
ctx.move_to(70, 0).text("{:.2f}".format(Vsys) + "V")
def update(self, delta):
self.menu.update(delta)
if self.notification:
self.notification.update(delta)
def get_color(battery_percentage):
hue = 120 * (battery_percentage / 100)
r, g, b = hsv_to_rgb(hue / 360, 1, 1)
return (r / 255), (g / 255), (b / 255)
def hsv_to_rgb(h, s, v):
if s == 0.0:
return (v, v, v)
i = int(h * 6.0)
f = (h * 6.0) - i
p = int(v * (1.0 - s) * 255)
q = int(v * (1.0 - s * f) * 255)
t = int(v * (1.0 - s * (1.0 - f)) * 255)
v = int(v * 255)
i %= 6
if i == 0:
return (v, t, p)
if i == 1:
return (q, v, p)
if i == 2:
return (p, v, t)
if i == 3:
return (p, q, v)
if i == 4:
return (t, p, v)
if i == 5:
return (v, p, q)
__app_export__ = BatteryMeter