Fix formatting, add some classes
This commit is contained in:
parent
6e936007c1
commit
b9557be16c
77
__init__.py
77
__init__.py
@ -9,12 +9,39 @@ import captouch
|
|||||||
import json
|
import json
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
# class Bluetooth():
|
||||||
|
# def __init__(self) -> None:
|
||||||
|
|
||||||
|
|
||||||
|
# class Social():
|
||||||
|
# def
|
||||||
|
class Movements:
|
||||||
|
def orientation(self) -> int:
|
||||||
|
# Get current orientation
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def update(self) -> None:
|
||||||
|
# Read from the gyroscope and accelerometers
|
||||||
|
print("Updating movements")
|
||||||
|
|
||||||
|
def last_movement(self) -> int:
|
||||||
|
# Time since last movement
|
||||||
|
return 1
|
||||||
|
|
||||||
|
def is_moving(self) -> bool:
|
||||||
|
# Is the badge currently in movement
|
||||||
|
return False
|
||||||
|
|
||||||
|
def is_shaking(self) -> bool:
|
||||||
|
# Active when the badge has been shaked
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
class Configuration:
|
class Configuration:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
# Add up to 5 items with custom header
|
# Add up to 5 items with custom header
|
||||||
self.hdrs: list = ['name', 'handel', 'lang', 'pronouns', 'empty']
|
self.hdrs: list = ["name", "handel", "lang", "pronouns", "phone"]
|
||||||
self.strs: list = ['flow3r', '', 'noLang', '', '']
|
self.strs: list = ["flow3r", "", "noLang", "", ""]
|
||||||
self.size: int = 75
|
self.size: int = 75
|
||||||
self.font: int = 5
|
self.font: int = 5
|
||||||
self.colour: int = (1.0, 1.0, 1.0)
|
self.colour: int = (1.0, 1.0, 1.0)
|
||||||
@ -23,7 +50,7 @@ class Configuration:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def load(cls, path: str) -> "Configuration":
|
def load(cls, path: str) -> "Configuration":
|
||||||
res = cls()
|
res = cls()
|
||||||
#return res # for debug
|
# return res # for debug
|
||||||
try:
|
try:
|
||||||
with open(path) as f:
|
with open(path) as f:
|
||||||
jsondata = f.read()
|
jsondata = f.read()
|
||||||
@ -43,16 +70,26 @@ class Configuration:
|
|||||||
res.size = data["size"]
|
res.size = data["size"]
|
||||||
if "font" in data and type(data["font"]) == int:
|
if "font" in data and type(data["font"]) == int:
|
||||||
res.font = data["font"]
|
res.font = data["font"]
|
||||||
if "color" in data and type(data["color"]) == str and len(data["color"]) == 7 and data["color"][0] == '#':
|
if (
|
||||||
|
"color" in data
|
||||||
|
and type(data["color"]) == str
|
||||||
|
and len(data["color"]) == 7
|
||||||
|
and data["color"][0] == "#"
|
||||||
|
):
|
||||||
res.colour = (
|
res.colour = (
|
||||||
int(data["color"][1:3], 16)/256.0,
|
int(data["color"][1:3], 16) / 256.0,
|
||||||
int(data["color"][3:5], 16)/256.0,
|
int(data["color"][3:5], 16) / 256.0,
|
||||||
int(data["color"][5:], 16)/256.0
|
int(data["color"][5:], 16) / 256.0,
|
||||||
)
|
)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def save(self, path: str) -> None:
|
def save(self, path: str) -> None:
|
||||||
colourstr: str = '#' + hex(int(255*self.colour[0]))[2:4] + hex(int(255*self.colour[1]))[2:4] + hex(int(255*self.colour[2]))[2:4]
|
colourstr: str = (
|
||||||
|
"#"
|
||||||
|
+ hex(int(255 * self.colour[0]))[2:4]
|
||||||
|
+ hex(int(255 * self.colour[1]))[2:4]
|
||||||
|
+ hex(int(255 * self.colour[2]))[2:4]
|
||||||
|
)
|
||||||
d = {
|
d = {
|
||||||
"size": self.size,
|
"size": self.size,
|
||||||
"font": self.font,
|
"font": self.font,
|
||||||
@ -67,7 +104,7 @@ class Configuration:
|
|||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
class SensibleNickApp(Application):
|
class BetterNickApp(Application):
|
||||||
state: int = 0
|
state: int = 0
|
||||||
since_state_change: int = 0
|
since_state_change: int = 0
|
||||||
|
|
||||||
@ -86,11 +123,16 @@ class SensibleNickApp(Application):
|
|||||||
self._colours: list = [GO_GREEN, PUSH_RED, self._config.colour]
|
self._colours: list = [GO_GREEN, PUSH_RED, self._config.colour]
|
||||||
self._en_colour = 2
|
self._en_colour = 2
|
||||||
self.varprint: list(str) = [str for str in self._config.strs if len(str) > 0]
|
self.varprint: list(str) = [str for str in self._config.strs if len(str) > 0]
|
||||||
|
self.movement_state = Movements()
|
||||||
|
|
||||||
def draw(self, ctx: Context) -> None:
|
def draw(self, ctx: Context) -> None:
|
||||||
|
if not self.movement_state.is_moving():
|
||||||
|
ctx.rgb(*BLACK).rectangle(-120, -120, 240, 240).fill()
|
||||||
|
return
|
||||||
|
|
||||||
ctx.text_align = ctx.CENTER
|
ctx.text_align = ctx.CENTER
|
||||||
ctx.text_baseline = ctx.MIDDLE
|
ctx.text_baseline = ctx.MIDDLE
|
||||||
#ctx.font_size = self._config.size
|
# ctx.font_size = self._config.size
|
||||||
ctx.font = ctx.get_font_name(self._config.font)
|
ctx.font = ctx.get_font_name(self._config.font)
|
||||||
|
|
||||||
ctx.rgb(*BLACK).rectangle(-120, -120, 240, 240).fill()
|
ctx.rgb(*BLACK).rectangle(-120, -120, 240, 240).fill()
|
||||||
@ -103,7 +145,7 @@ class SensibleNickApp(Application):
|
|||||||
ctx.scale(self._scale, 1)
|
ctx.scale(self._scale, 1)
|
||||||
ctx.font_size = 100
|
ctx.font_size = 100
|
||||||
pre = ctx.text_width(self.varprint[self.state])
|
pre = ctx.text_width(self.varprint[self.state])
|
||||||
ctx.font_size = 98*240/pre
|
ctx.font_size = 98 * 240 / pre
|
||||||
if ctx.font_size > 150:
|
if ctx.font_size > 150:
|
||||||
ctx.font_size = 150
|
ctx.font_size = 150
|
||||||
post = ctx.text_width(self.varprint[self.state])
|
post = ctx.text_width(self.varprint[self.state])
|
||||||
@ -114,7 +156,7 @@ class SensibleNickApp(Application):
|
|||||||
leds.set_hsv(int(self._led), abs(self._scale) * 360, 1, 1.0)
|
leds.set_hsv(int(self._led), abs(self._scale) * 360, 1, 1.0)
|
||||||
|
|
||||||
leds.update()
|
leds.update()
|
||||||
#ctx.fill()
|
# ctx.fill()
|
||||||
|
|
||||||
def on_exit(self) -> None:
|
def on_exit(self) -> None:
|
||||||
self._config.save(self._filename)
|
self._config.save(self._filename)
|
||||||
@ -122,6 +164,10 @@ class SensibleNickApp(Application):
|
|||||||
def think(self, ins: InputState, delta_ms: int) -> None:
|
def think(self, ins: InputState, delta_ms: int) -> None:
|
||||||
super().think(ins, delta_ms)
|
super().think(ins, delta_ms)
|
||||||
|
|
||||||
|
self.movement_state.update()
|
||||||
|
|
||||||
|
self.ins.imu
|
||||||
|
|
||||||
self._phase += delta_ms / 900
|
self._phase += delta_ms / 900
|
||||||
self._scale = math.sin(self._phase)
|
self._scale = math.sin(self._phase)
|
||||||
self._led += delta_ms / 100
|
self._led += delta_ms / 100
|
||||||
@ -137,12 +183,12 @@ class SensibleNickApp(Application):
|
|||||||
|
|
||||||
petals = captouch.read().petals
|
petals = captouch.read().petals
|
||||||
for i in range(5):
|
for i in range(5):
|
||||||
if petals[2*i].pressed and i < len(self.varprint):
|
if petals[2 * i].pressed and i < len(self.varprint):
|
||||||
self.state = i
|
self.state = i
|
||||||
self.since_state_change = 0
|
self.since_state_change = 0
|
||||||
|
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
if petals[2*i+3].pressed:
|
if petals[2 * i + 3].pressed:
|
||||||
self._en_colour = i
|
self._en_colour = i
|
||||||
|
|
||||||
if petals[1].pressed:
|
if petals[1].pressed:
|
||||||
@ -159,8 +205,9 @@ class SensibleNickApp(Application):
|
|||||||
self._en_iter_pressed = petals[1].pressed
|
self._en_iter_pressed = petals[1].pressed
|
||||||
self._en_scale_pressed = petals[9].pressed
|
self._en_scale_pressed = petals[9].pressed
|
||||||
|
|
||||||
|
|
||||||
# For running with `mpremote run`:
|
# For running with `mpremote run`:
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import st3m.run
|
import st3m.run
|
||||||
|
|
||||||
st3m.run.run_view(SensibleNickApp(ApplicationContext()))
|
st3m.run.run_view(BetterNickApp(ApplicationContext()))
|
||||||
|
Loading…
Reference in New Issue
Block a user