stepper-clock/Screens.py
2020-03-21 21:25:23 +01:00

158 lines
5.8 KiB
Python

import StepperClock
import uasyncio as asyncio
import Buzzer
import Settings
import Hardware as HW
class InitHandsScreen():
def __await__(self):
self.running = True
self.mode=0 # 0: Minutes, 1: Hours
HW.housingLEDs.fill((0,0,0))
HW.stepperminu.rotateTo(0)
HW.stepperhour.rotateTo(0)
HW.leds[1].flash(0.7)
for number,button in enumerate(HW.buttons):
button.setCallbacks(\
onPushDown=lambda i=number:self.onButtonPressed(i),\
onPushUp=lambda pushDownTime,button=number:self.onButtonReleased(button,pushDownTime))
# Wait for the Hands to move up
while not HW.stepperhour.isAtTarget() or not HW.stepperminu.isAtTarget():
await asyncio.sleep_ms(200)
HW.housingLEDs.upper((50,50,50))
while self.running:
await asyncio.sleep_ms(200)
HW.housingLEDs.fill((0,0,0))
self.cleanup()
__iter__ = __await__ # https://github.com/micropython/micropython/issues/2678
def finish(self):
self.running = False
for b in HW.buttons:
b.setCallbacks()#Clear Callbacks
for l in HW.leds:
l.off()
def __init__(self):
self.running = False
def onButtonPressed(self,button):
if button == 1:
HW.leds[button].on(overwriteFlashing=False)
else:
HW.leds[button].on()
if self.mode == 0:
HW.stepperminu.rotateTo(direction=(1 if button == 0 else -1))
elif self.mode == 1:
HW.stepperhour.rotateTo(direction=(1 if button == 0 else -1))
def onButtonReleased(self,button,time):
HW.leds[button].off(overwriteFlashing=(button!=1))
if button == 1:
self.mode+=1
if self.mode == 2:
self.finish()
else:
if self.mode == 0:
HW.stepperminu.stop()
else:
HW.stepperhour.stop()
def cleanup(self):
HW.stepperhour.reset() # Set the new Zero
HW.stepperminu.reset()
class ClockScreen():
def __await__(self):
self.running = True
self.init()
while self.running:
await asyncio.sleep_ms(200)
HW.housingLEDs.fill((0,0,0))
self.cleanup()
__iter__ = __await__ # https://github.com/micropython/micropython/issues/2678
def finish(self):
self.running = False
for b in HW.buttons:
b.setCallbacks()#Clear Callbacks
def __init__(self):
self.running = False
self.light = False
self.clock = StepperClock.StepperClock()
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 onButtonPressed(self,button):
HW.leds[button].on()
def onButtonReleased(self,button,pushDownTime):
print("Clock press",pushDownTime)
HW.leds[button].off()
if button == 0:
Settings.selectedBrightness=(Settings.selectedBrightness+1)%len(ClockScreen.BRIGHTNESS)
elif button == 1:
if pushDownTime > 1100:
self.finish()
return
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 init(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.onButtonPressed(i),onPushUp=lambda pushDownTime,button=i:self.onButtonReleased(button,pushDownTime))
self.clock.start()
def cleanup(self):
self.clock.stop()
class RingtoneSettingsScreen():
def __await__(self):
self.running = True
for i,b in enumerate(HW.buttons):
b.setCallbacks(onPushDown=lambda i=i:self.onButtonPressed(i),onPushUp=lambda pushDownTime,button=i:self.onButtonReleased(button,pushDownTime))
# Turn LEDs Red and play the selected Sound and turn the minute-hand to the selected sound
HW.housingLEDs.fill((200,0,0))
HW.buzzer.playSound(HW.Buzzer.ALARMTONES[Settings.selectedSound])
HW.stepperminu.rotateTo(-Settings.selectedSound/12)
HW.stepperhour.rotateTo(0)
while not HW.stepperhour.isAtTarget() or not HW.stepperminu.isAtTarget():
await asyncio.sleep_ms(200)
while self.running:
await asyncio.sleep_ms(200)
HW.housingLEDs.fill((0,0,0))
self.cleanup()
__iter__ = __await__ # https://github.com/micropython/micropython/issues/2678
def __init__(self):
self.running = False
def finish(self):
self.running = False
for b in HW.buttons:
b.setCallbacks()#Clear Callbacks
def onButtonPressed(self,button):
HW.leds[button].on()
def onButtonReleased(self,button,pushDownTime):
print("Ring Select push down",pushDownTime)
HW.leds[button].off()
if button == 1:
if pushDownTime > 1100:
Settings.save()
self.finish()
return
else:
if pushDownTime < 500:
Settings.selectedSound=(Settings.selectedSound+ (1 if button == 2 else -1))%len(Buzzer.ALARMTONES)
HW.stepperminu.rotateTo(-Settings.selectedSound/12)
HW.buzzer.playSound(HW.Buzzer.ALARMTONES[Settings.selectedSound])
def cleanup(self):
HW.housingLEDs.fill((0,0,0))
HW.buzzer.stop()