stepper-clock/Screens.py

130 lines
4.8 KiB
Python

import StepperClock
import uasyncio as asyncio
import Buzzer
import Settings
import Hardware as HW
class ClockScreen():
COLORS = ((255,0,0),(255,0,127),(127,0,255),(0,0,255),(0,127,255),(0,255,127),(0,255,0),(127,255,0),(255,127,0))
BRIGHTNESS = (5,10,50,100,160,255)
def __init__(self):
self.clock = StepperClock.StepperClock()
self.running = False
self.light = False
def stopIfAllPressed(self,button):
HW.leds[button].on()
for b in HW.buttons:
if not b.isPushed():
break
else:
self.running = False
def onClicked(self,button):
if button == 0:
Settings.selectedBrightness=(Settings.selectedBrightness+1)%len(ClockScreen.BRIGHTNESS)
elif button == 1:
self.light = not self.light
elif button == 2:
Settings.selectedColor=(Settings.selectedColor+1)%len(ClockScreen.COLORS)
HW.housingLEDs.fill([int(c/255*ClockScreen.BRIGHTNESS[Settings.selectedBrightness]) for c in ClockScreen.COLORS[Settings.selectedColor]] if self.light else (0,0,0))
def __await__(self):
HW.housingLEDs.fill([int(c/255*ClockScreen.BRIGHTNESS[Settings.selectedBrightness]) for c in ClockScreen.COLORS[Settings.selectedColor]] if self.light else (0,0,0))
for i,b in enumerate(HW.buttons):
b.setCallbacks(onPushDown=lambda i=i:self.stopIfAllPressed(i),onPushUp=lambda i=i:HW.leds[i].off(),onClick=lambda i=i:self.onClicked(i))
self.running = True
self.clock.start()
while self.running:
await asyncio.sleep_ms(200)
self.clock.stop()
__iter__ = __await__ # https://github.com/micropython/micropython/issues/2678
class SettingsScreen():
def __init__(self):
self.running = False
def onButtonPressed(self,button):
HW.leds[button].on(overwriteFlashing=False)
for b in HW.buttons:
if not b.isPushed():
break
else:
self.running = False
return
if not HW.buttons[1].isPushed():
if button == 0 :
if self.mode == 0:
HW.stepperminu.rotateTo(direction=1)
if self.mode == 1:
HW.stepperhour.rotateTo(direction=1)
elif button == 2:
if self.mode == 0:
HW.stepperminu.rotateTo(direction=-1)
if self.mode == 1:
HW.stepperhour.rotateTo(direction=-1)
def onButtonReleased(self,button):
if not self.running:
return
HW.leds[button].off(overwriteFlashing=False)
if button == 0:#left
if self.mode == 0 or self.mode == 1:
HW.stepperminu.stop()
HW.stepperhour.stop()
if self.mode == 3:
Settings.selectedSound =(Settings.selectedSound -1) % len(Buzzer.SOUNDS)
HW.buzzer.playSound(Buzzer.SOUNDS[Settings.selectedSound ])
elif button == 2:#right
if self.mode == 0 or self.mode == 1:
HW.stepperminu.stop()
HW.stepperhour.stop()
if self.mode == 3:
Settings.selectedSound =(Settings.selectedSound +1) % len(Buzzer.SOUNDS)
HW.buzzer.playSound(Buzzer.SOUNDS[Settings.selectedSound ])
elif button == 1: #middle
self.mode=(self.mode+1)%4
if self.mode == 3:
HW.buzzer.playSound(Buzzer.SOUNDS[Settings.selectedSound ])
else:
HW.buzzer.stop()
if self.mode == 2:
HW.leds[0].flash(0.7)
HW.leds[1].flash(0.7)
HW.leds[2].flash(0.7)
else:
HW.leds[0].off()
HW.leds[1].off()
HW.leds[2].off()
if self.mode == 0 or self.mode == 1:
HW.housingLEDs.upper((50,50,50))
else:
HW.housingLEDs.clear()
def __await__(self):
self.mode = 0
HW.stepperminu.rotateTo(0)
HW.stepperhour.rotateTo(0)
HW.housingLEDs.clear()
while not HW.stepperhour.isAtTarget() or not HW.stepperminu.isAtTarget():
await asyncio.sleep_ms(200)
HW.housingLEDs.upper((50,50,50))
for led in HW.leds:
led.off()
for i,b in enumerate(HW.buttons):
b.setCallbacks(onPushDown=lambda i=i:self.onButtonPressed(i),onPushUp=lambda i=i:self.onButtonReleased(i))
self.running = True
while self.running:
await asyncio.sleep_ms(200)
for led in HW.leds:
led.off()
HW.housingLEDs.clear()
HW.stepperhour.reset()
HW.stepperminu.reset()
HW.buzzer.stop()
Settings.save()
__iter__ = __await__ # https://github.com/micropython/micropython/issues/2678