-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
198 lines (168 loc) · 6.24 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
import time
import app
import settings
from app_components import TextDialog, clear_background
from events.input import BUTTON_TYPES, Buttons
from system.eventbus import eventbus
from perf_timer import PerfTimer
from system.patterndisplay.events import *
from tildagonos import tildagonos
from power import BatteryLevel
class GraymeBadge(app.App):
name = None
# colors used
black = (0, 0, 0)
white = (255, 255, 255)
orange = (255,50,0)
red = (255,0,0)
rgb_min = 0
rgb_max = 255
# colors used for the 'hello my name is' part
header_fg_color = (0, 0, 0)
def __init__(self):
super().__init__()
self.button_states = Buttons(self)
self.state = "name"
self.states = {
"battery": {
"heading": "battery",
"subheading": "percentage is",
"text": "Unknown",
"colour": self.white,
},
"name": {
"heading": "Hello",
"subheading": "my name is",
"text": settings.get("name"),
"colour": self.white,
},
"context": {
"heading": "You",
"subheading": "killed my",
"text": "Father",
"led_colours": self.orange,
"colour": self.orange,
},
"threat": {
"heading": "prepare",
"subheading": "to",
"text": "DIE!",
"led_colours": self.red,
"colour": self.red,
},
}
self.update_battery()
self.name_state()
self.update_state()
async def run(self, render_update):
last_time = time.ticks_ms()
while True:
cur_time = time.ticks_ms()
delta_ticks = time.ticks_diff(cur_time, last_time)
with PerfTimer(f"Updating {self}"):
self.update(delta_ticks)
await render_update()
last_time = cur_time
if self.text is None:
dialog = TextDialog("What is your name?", self)
self.overlays = [dialog]
if await dialog.run(render_update):
self.text = dialog.text
settings.set("name", dialog.text)
try:
settings.save()
except Exception as ex:
print("failed to save settings", ex)
else:
self.minimise()
self.overlays = []
def update(self, delta):
# quit the app
if self.button_states.get(BUTTON_TYPES["CANCEL"]):
for i in range(0,12):
tildagonos.leds[i+1] = self.black
eventbus.emit(PatternEnable())
self.minimise()
self.button_states.clear()
elif self.button_states.get(BUTTON_TYPES["DOWN"]):
# Convenient battery meter
if self.state == "battery":
self.name_state()
# Inigo Montoya Escalation
elif self.state == "name":
self.context_state()
elif self.state == "context":
self.threat_state()
self.update_state()
elif self.button_states.get(BUTTON_TYPES["UP"]):
# Inigo Montoya De-escalation
if self.state == "threat":
self.context_state()
elif self.state == "context":
self.name_state()
# Convenient battery meter
elif self.state == "name":
self.battery_state()
self.update_state()
def draw(self, ctx):
clear_background(ctx)
ctx.text_align = ctx.CENTER
# draw backgrounds
ctx.rgb(*self.black).rectangle(-120, -120, 240, 240).fill()
ctx.rgb(*self.colour).rectangle(-120, -120, 240, 100).fill()
ctx.font_size = 56
ctx.font = "Arimo Bold"
ctx.rgb(*self.black).move_to(0, -60).text(self.heading)
if self.text is not None:
ctx.rgb(*self.colour).move_to(0, 60).text(self.text)
ctx.font_size = 28
ctx.font = "Arimo Bold"
ctx.rgb(*self.black).move_to(0, -30).text(self.subheading)
if self.text is None:
ctx.font = "Arimo Italic"
ctx.rgb(*self.colour).move_to(0, 20).text(
"Set your name in\nthe settings app!"
)
self.draw_overlays(ctx)
def update_state(self):
self.heading = self.states[self.state]["heading"]
self.subheading = self.states[self.state]["subheading"]
self.text = self.states[self.state]["text"]
self.colour = self.states[self.state]["colour"]
if "led_colours" in self.states[self.state]:
for i in range(0,12):
tildagonos.leds[i+1] = self.states[self.state]["led_colours"]
# if the badge was off, then turn off before we hand back to the pattern
# since the pattern doesn't rewrite the LEDs to off
elif settings.get("pattern") == "off":
for i in range(0,12):
tildagonos.leds[i+1] = self.black
self.button_states.clear()
def battery_state(self):
self.state = "battery"
self.update_battery()
def name_state(self):
self.state = "name"
eventbus.emit(PatternEnable())
def context_state(self):
self.state = "context"
eventbus.emit(PatternDisable())
def threat_state(self):
self.state = "threat"
eventbus.emit(PatternDisable())
def update_battery(self):
eventbus.emit(PatternDisable())
self.states["battery"]["text"] = f"{self.battery_level():.2f}%"
# Battery level is a float 0..100
# We set LED colour to reflect how low your battery is
self.states["battery"]["led_colours"] = (
self.clamp(255 - int((self.battery_level() / 100) * 255)),
self.clamp(int((self.battery_level() / 100) * 255)),
self.rgb_min,
)
def battery_level(self):
return self.clamp(BatteryLevel(), 0, 100)
# Used for ensuring valid RGB values for LEDs
def clamp(self, n, lower = rgb_min, upper = rgb_max):
return sorted([lower, n, upper])[1]
__app_export__ = GraymeBadge