From ed30bf42d94ce1fe8537a759bee811186c7de234 Mon Sep 17 00:00:00 2001 From: Nils Schulte Date: Sat, 21 Mar 2020 21:25:23 +0100 Subject: [PATCH] Screens cleanup --- Button.py | 12 +-- Buzzer.py | 30 ++++--- Screens.py | 222 +++++++++++++++++++++++++++--------------------- StepperL298M.py | 1 + boot.py | 29 ++++--- main.py | 15 ++-- 6 files changed, 175 insertions(+), 134 deletions(-) diff --git a/Button.py b/Button.py index 8458470..ea9efe4 100644 --- a/Button.py +++ b/Button.py @@ -2,22 +2,20 @@ import machine import time class Button(): - def __init__(self,pin,onPushDown = None, onPushUp = None,onClick = None,onHold = None,clickTime=300,debounceTimeMs = 50,inputMode = machine.Pin.IN,inverted = False): + def __init__(self,pin,onPushDown = None, onPushUp = None,clickTime=300,debounceTimeMs = 50,inputMode = machine.Pin.IN,inverted = False): self.pin = machine.Pin(pin,inputMode) self.debounceTimestamp = -debounceTimeMs self.debounceTimeMs = int(debounceTimeMs) self.clickTime = clickTime self.pushDownTimeStamp = -clickTime - self.setCallbacks(onPushDown,onPushUp,onClick,onHold) + self.setCallbacks(onPushDown,onPushUp) self.pin.irq(trigger=machine.Pin.IRQ_RISING|machine.Pin.IRQ_FALLING , handler=self._IRQ) self.value = self.pin.value() self.inverted = inverted - def setCallbacks(self,onPushDown = None, onPushUp = None,onClick = None,onHold = None): + def setCallbacks(self,onPushDown = None, onPushUp = None): self.onPushDown = onPushDown self.onPushUp = onPushUp - self.onClick = onClick - self.onHold = onHold def _IRQ(self,p): new_value = self.pin.value() @@ -28,9 +26,7 @@ class Button(): self.debounceTimestamp = time_ms if (new_value ^ self.inverted): if (self.onPushUp != None): - self.onPushUp() - if (self.onClick != None and (self.pushDownTimeStamp + self.clickTime > time_ms)): - self.onClick() + self.onPushUp(time_ms-self.pushDownTimeStamp) else: if (self.onPushDown != None): self.onPushDown() diff --git a/Buzzer.py b/Buzzer.py index cfa0949..c7ece3f 100644 --- a/Buzzer.py +++ b/Buzzer.py @@ -11,20 +11,23 @@ a = 440 #Hz b = 493 #Hz C = 523 #Hz +# Sounds # (Duration,Freq) -# If Last Duration is None play endlessly -# 0: Silent -# 1: BeepBeep -# 2: Alle Meine Entchen -SOUNDS = [((0,None),),\ - ((700,1000),(100,None),(600,1000)),\ +# If Last Duration is None play last Freq times (negative = infinite) + +BEEP = ((100,1100),) +BEEPBEEP = ((700,1000),(100,None),(600,1000)) + +# 0: BEEEEP BEEEEP BEEEEEP +# 1: Alle Meine Entchen +ALARMTONES = (((300,1200),(100,None),(200,1200),(300,None),(None,10)), ((400,c),(100,None),(400,d),(100,None),(400,e),(100,None),(400,f),(100,None),\ (900,g),(100,None),(900,g),(100,None),(400,a),(100,None),(400,a),(100,None),\ (400,a),(100,None),(400,a),(100,None),(1900,g),(100,None),(400,a),(100,None),\ (400,a),(100,None),(400,a),(100,None),(400,a),(100,None),(1900,g),(100,None),\ (400,f),(100,None),(400,f),(100,None),(400,f),(100,None),(400,f),(100,None),\ (900,e),(100,None),(900,e),(100,None),(400,d),(100,None),(400,d),(100,None),\ - (400,d),(100,None),(400,d),(100,None),(2000,c))]#,(None,None) + (400,d),(100,None),(400,d),(100,None),(2000,c)),) class Buzzer(): def __init__(self,pin,duty = 100): @@ -42,13 +45,20 @@ class Buzzer(): self._pwm.duty(0) else: self.newSound = False + timesPlayed = 0 + print("Play sound!") for i in range(len(self.sound)): s = self.sound[i] if s[0] == None: - i = 0 + if s[1]-timesPlayed == 0: + break + else: + i = 0 + timesPlayed += 1 if (s[1] != None): - self._pwm.freq(s[1]) - self._pwm.duty(self.duty) + #self._pwm.freq(s[1]) + #self._pwm.duty(self.duty) + pass else: self._pwm.duty(0) await asyncio.sleep_ms(s[0]) diff --git a/Screens.py b/Screens.py index 7d011ab..fca4b45 100644 --- a/Screens.py +++ b/Screens.py @@ -4,127 +4,155 @@ 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(): - 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 __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.clock = StepperClock.StepperClock() self.running = False self.light = False + self.clock = StepperClock.StepperClock() - def stopIfAllPressed(self,button): + 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() - for b in HW.buttons: - if not b.isPushed(): - break - else: - self.running = False - def onClicked(self,button): + 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 __await__(self): + + 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.stopIfAllPressed(i),onPushUp=lambda i=i:HW.leds[i].off(),onClick=lambda i=i:self.onClicked(i)) - self.running = True + b.setCallbacks(onPushDown=lambda i=i:self.onButtonPressed(i),onPushUp=lambda pushDownTime,button=i:self.onButtonReleased(button,pushDownTime)) self.clock.start() - while self.running: - await asyncio.sleep_ms(200) + + def cleanup(self): 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() +class RingtoneSettingsScreen(): def __await__(self): - self.mode = 0 - HW.stepperminu.rotateTo(0) + 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) - 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 \ No newline at end of file + 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() \ No newline at end of file diff --git a/StepperL298M.py b/StepperL298M.py index b5f4bc4..3700b15 100644 --- a/StepperL298M.py +++ b/StepperL298M.py @@ -60,6 +60,7 @@ class Stepper: self._rotTarget = self.stepnum async def _update_async(self): + print("update") while(True): await asyncio.sleep_ms(self.stepDurationMs) if not self.isAtTarget(): diff --git a/boot.py b/boot.py index 00837e3..45c3c80 100644 --- a/boot.py +++ b/boot.py @@ -5,16 +5,19 @@ webrepl.start() sta_if = network.WLAN(network.STA_IF) sta_if.active(True) -availableNetworkSSIDs = [s[0].decode() for s in sta_if.scan()] -print(availableNetworkSSIDs) -#load wifi credentials from file -with open("wifi-credentials","r") as f: - while True: - ssid = f.readline() - password = f.readline()# - if not ssid or not password: - break - ssid = ssid.replace("\n","") - password = password.replace("\n","") - if ssid in availableNetworkSSIDs: - sta_if.connect(ssid,password) +if sta_if.status() != network.STAT_GOT_IP: + availableNetworkSSIDs = [s[0].decode() for s in sta_if.scan()] + print(availableNetworkSSIDs) + #load wifi credentials from file + with open("wifi-credentials","r") as f: + while sta_if.status() != network.STAT_GOT_IP: + ssid = f.readline() + password = f.readline()# + if not ssid or not password: + break + ssid = ssid.replace("\n","") + password = password.replace("\n","") + if ssid in availableNetworkSSIDs: + print("Connecting to",ssid) + sta_if.connect(ssid,password) + break \ No newline at end of file diff --git a/main.py b/main.py index f329c54..022eebd 100644 --- a/main.py +++ b/main.py @@ -6,15 +6,18 @@ import Screens #Async loop = asyncio.get_event_loop() -modus = 0 -screens = (Screens.ClockScreen(),\ - Screens.SettingsScreen()) async def run_screens(): - global modus,screens + InitScreen = Screens.InitHandsScreen() + await InitScreen + + modus = 0 + screens = (Screens.ClockScreen(),\ + Screens.RingtoneSettingsScreen()) while True: - await screens[modus%len(screens)] + HW.buzzer.playSound(HW.Buzzer.BEEP) + await screens[modus] for b in HW.buttons: b.setCallbacks()#Clear Callbacks - modus += 1 + modus = (modus+1)%len(screens) loop.run_until_complete(run_screens()) \ No newline at end of file