cccamp-flower/__init__.py

60 lines
1.8 KiB
Python

from st3m.application import Application, ApplicationContext
from st3m.goose import Dict, Any
from st3m.reactor import Responder
from st3m.ui.view import BaseView, ViewManager
from st3m.input import InputState
from ctx import Context
import st3m.run
import leds
import json
import math
class AlternativebitNick(Application):
def __init__(self, app_ctx: ApplicationContext) -> None:
super().__init__(app_ctx)
self._led = 0.0
self._filename = "/flash/nick.json"
self._scale = 1.0
self._phase = 0.0
self._brightness = 0
self._brightness_time = 0.0
def draw(self, ctx: Context) -> None:
# Paint the background black
ctx.save()
ctx.scale(self._scale, 1)
ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill()
ctx.rgb(1,1,1).round_rectangle(10,0,70,70,2).fill()
ctx.rgb(1,1,1).round_rectangle(-80,0,70,70,2).fill()
ctx.rgb(1,1,1).round_rectangle(10,-90,70,70,2).fill()
ctx.restore()
leds.set_hsv(int(self._led), abs(self._scale) * 360, 1, 0.2)
leds.set_brightness(self._brightness)
leds.update()
def think(self, ins: InputState, delta_ms: int) -> None:
super().think(ins, delta_ms) # Let Application do its thing
self._led += delta_ms / 45
self._phase += delta_ms / 1000
# LED
self._brightness_time += delta_ms
if self._brightness_time < 50:
self._brightness = 0
else:
self._brightness = 69
if self._brightness_time > 1000:
self.brightness_time = 0
# Screen
self._scale = math.sin(self._phase)
if self._led >= 40:
self._led = 0
if __name__ == '__main__':
# Continue to make runnable via mpremote run.
st3m.run.run_view(AlternativebitNick(ApplicationContext()))