121 lines
4.3 KiB
Python
121 lines
4.3 KiB
Python
import StepperClock
|
|
import uasyncio as asyncio
|
|
import Buzzer
|
|
import Settings
|
|
|
|
class ClockScreen():
|
|
|
|
def __init__(self,stepperhour,stepperminu,buttons,leds):
|
|
self.clock = StepperClock.StepperClock(stepperhour,stepperminu)
|
|
self.running = False
|
|
self.buttons = buttons
|
|
self.leds = leds
|
|
|
|
def stopIfAllPressed(self,button):
|
|
self.leds[button].on()
|
|
for b in self.buttons:
|
|
if not b.isPushed():
|
|
break
|
|
else:
|
|
self.running = False
|
|
|
|
def __await__(self):
|
|
for i,b in enumerate(self.buttons):
|
|
b.setCallbacks(onPushDown=lambda i=i:self.stopIfAllPressed(i),onPushUp=lambda i=i:self.leds[i].off())
|
|
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,stepperhour,stepperminu,buttons,leds,housingLEDs,buzzer):
|
|
self.stepperminu = stepperminu
|
|
self.stepperhour = stepperhour
|
|
self.running = False
|
|
self.buttons = buttons
|
|
self.leds = leds
|
|
self.housingLEDs = housingLEDs
|
|
self.buzzer = buzzer
|
|
|
|
# TODO load these form file
|
|
|
|
def onButtonPressed(self,button):
|
|
self.leds[button].on(overwriteFlashing=False)
|
|
for b in self.buttons:
|
|
if not b.isPushed():
|
|
break
|
|
else:
|
|
self.running = False
|
|
return
|
|
if not self.buttons[1].isPushed():
|
|
if button == 0 :
|
|
if self.mode == 0:
|
|
self.stepperminu.rotateTo(direction=1)
|
|
if self.mode == 1:
|
|
self.stepperhour.rotateTo(direction=1)
|
|
elif button == 2:
|
|
if self.mode == 0:
|
|
self.stepperminu.rotateTo(direction=-1)
|
|
if self.mode == 1:
|
|
self.stepperhour.rotateTo(direction=-1)
|
|
|
|
def onButtonReleased(self,button):
|
|
self.leds[button].off(overwriteFlashing=False)
|
|
if button == 0:#left
|
|
if self.mode == 0 or self.mode == 1:
|
|
self.stepperminu.stop()
|
|
self.stepperhour.stop()
|
|
if self.mode == 3:
|
|
Settings.selectedSound =(Settings.selectedSound -1) % len(Buzzer.SOUNDS)
|
|
self.buzzer.playSound(Buzzer.SOUNDS[Settings.selectedSound ])
|
|
elif button == 2:#right
|
|
if self.mode == 0 or self.mode == 1:
|
|
self.stepperminu.stop()
|
|
self.stepperhour.stop()
|
|
if self.mode == 3:
|
|
Settings.selectedSound =(Settings.selectedSound +1) % len(Buzzer.SOUNDS)
|
|
self.buzzer.playSound(Buzzer.SOUNDS[Settings.selectedSound ])
|
|
elif button == 1: #middle
|
|
self.mode=(self.mode+1)%4
|
|
if self.mode == 3:
|
|
self.buzzer.playSound(Buzzer.SOUNDS[Settings.selectedSound ])
|
|
else:
|
|
self.buzzer.stop()
|
|
|
|
if self.mode == 2:
|
|
self.leds[0].flash(0.7)
|
|
self.leds[1].flash(0.7)
|
|
self.leds[2].flash(0.7)
|
|
else:
|
|
self.leds[0].off()
|
|
self.leds[1].off()
|
|
self.leds[2].off()
|
|
|
|
if self.mode == 0 or self.mode == 1:
|
|
self.housingLEDs.upper((50,50,50))
|
|
else:
|
|
self.housingLEDs.clear()
|
|
|
|
def __await__(self):
|
|
self.mode = 0
|
|
self.stepperminu.rotateTo(0)
|
|
self.stepperhour.rotateTo(0)
|
|
while not self.stepperhour.isAtTarget() or not self.stepperminu.isAtTarget():
|
|
await asyncio.sleep_ms(200)
|
|
self.housingLEDs.upper((50,50,50))
|
|
for led in self.leds:
|
|
led.off()
|
|
for i,b in enumerate(self.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 self.leds:
|
|
led.off()
|
|
self.housingLEDs.clear()
|
|
self.stepperhour.reset()
|
|
self.stepperminu.reset()
|
|
__iter__ = __await__ # https://github.com/micropython/micropython/issues/2678 |