diff --git a/Buzzer.py b/Buzzer.py index c124f22..7247a87 100644 --- a/Buzzer.py +++ b/Buzzer.py @@ -48,7 +48,6 @@ class Buzzer(): else: self.newSound = False timesPlayed = 0 - #print("Play sound!") i = 0 while i < len(self.sound): s = self.sound[i] @@ -69,9 +68,9 @@ class Buzzer(): if self.newSound or self.sound == None: break i+=1 - if (not self.newSound): - self.sound = None + if not self.newSound: self.newSound = False + self.sound = None def playSound(self,sound): self.newSound = True @@ -82,4 +81,4 @@ class Buzzer(): self._pwm.duty(0) def isPlaying(self): - return self.sound == None \ No newline at end of file + return self.sound != None \ No newline at end of file diff --git a/Hardware.py b/Hardware.py index ef31256..9d47119 100644 --- a/Hardware.py +++ b/Hardware.py @@ -19,8 +19,8 @@ except Exception as e: buzzer = Buzzer.Buzzer(pin=2) #Initalise the Motors -stepperminu = StepperL298M.Stepper([19,21,22,23],inverted=False) -stepperhour = StepperL298M.Stepper([12,27,26,25],inverted=True) +stepperminu = StepperL298M.Stepper((19,21,22,23),inverted=False) +stepperhour = StepperL298M.Stepper((12,27,26,25),inverted=True) #Init LEDs housingLEDs = HousingLEDs.LEDs(pin=16) diff --git a/LED.py b/LED.py index 8dc382e..4dac0dd 100644 --- a/LED.py +++ b/LED.py @@ -3,7 +3,7 @@ import machine class LED(): def __init__(self, pin): - self.pin = machine.PWM(machine.Pin(pin,machine.Pin.OUT),freq=2000,duty=0) + self.pin = machine.PWM(machine.Pin(pin),freq=1000,duty=512)#,machine.Pin.OUT self.pwmDutyOff = 0 self.pwmDutyOn = 1023 self.pwmDuty = 0 @@ -12,30 +12,47 @@ class LED(): self.blinkRate = None self.onState = False self.blinkOn = False + self.alarmWentOff = False loop = asyncio.get_event_loop() loop.create_task(self._update_async()) async def _update_async(self): + self.pin.duty(0) while True: - if self.blinkRate == None or self.blinkRate <= 0: + if self.blinkRate == None: await asyncio.sleep_ms(200) else: self.pin.duty(self.pwmDuty if self.blinkOn else 0) self.blinkOn = not self.blinkOn - # TODO split long waits into multible ones so we catch changes more often - await asyncio.sleep_ms(int(500 / self.blinkRate)) + waited=0 + blinkWaitMs = int(500 / self.blinkRate) + while waited < blinkWaitMs: + toWait = min(50,blinkWaitMs) + await asyncio.sleep_ms(toWait) + waited += toWait + blinkWaitMs = int(500 / self.blinkRate) def toggle(self): self.onState = not self.onState self.value(self.onState) - def setOnState(self, blinkRate = None, pwmDuty = 1023, ): - self.blinkRateOn = blinkRate + def setOnState(self, blinkRate = None, pwmDuty = 1023): + self.blinkRateOn = blinkRate if blinkRate != None and blinkRate >= 0 else None self.pwmDutyOn = pwmDuty + if self.onState: + self.blinkRate = self.blinkRateOn if self.onState else self.blinkRateOff + self.pwmDuty = (self.pwmDutyOn if self.onState else self.pwmDutyOff) + self.pin.duty(self.pwmDuty if self.blinkRate == None else 0) + def setOffState(self, blinkRate = None, pwmDuty = 0): - self.blinkRateOff = blinkRate + self.blinkRateOff = blinkRate if blinkRate != None and blinkRate >= 0 else None self.pwmDutyOff = pwmDuty + if not self.onState: + self.blinkRate = self.blinkRateOn if self.onState else self.blinkRateOff + self.pwmDuty = (self.pwmDutyOn if self.onState else self.pwmDutyOff) + self.pin.duty(self.pwmDuty if self.blinkRate == None else 0) + def on(self): self.value(True) @@ -48,6 +65,6 @@ class LED(): self.onState = value self.blinkRate = self.blinkRateOn if self.onState else self.blinkRateOff self.pwmDuty = (self.pwmDutyOn if self.onState else self.pwmDutyOff) - self.pin.duty(self.pwmDuty if self.blinkOn else 0) + self.pin.duty(self.pwmDuty if self.blinkRate == None else 0) return self.onState \ No newline at end of file diff --git a/Screens.py b/Screens.py index 9fc52ab..1f7bef5 100644 --- a/Screens.py +++ b/Screens.py @@ -5,12 +5,27 @@ import Settings import Hardware as HW import math import network +import time class ClockScreen(): def __await__(self): self.running = True self.init() while self.running: + if Settings.alarmOn: + minutesNow = (time.localtime()[3]*60+time.localtime()[4])%(24*60) + minutesAlarm = (Settings.alarmTime[0]*60+Settings.alarmTime[1])%(24*60) + if not self.alarmTrunedOff: + self.alarmPlaying = self.alarmPlaying or (minutesNow >= minutesAlarm and ((minutesNow-minutesAlarm)%(24*60) <=5 )) + if self.alarmPlaying and not HW.buzzer.isPlaying(): + HW.housingLEDs.fill((255,0,0)) + for l in HW.leds: + l.setOffState(blinkRate=4,pwmDuty=1023) + HW.buzzer.playSound(HW.Buzzer.ALARMTONES[Settings.selectedSound]) + else: + pass + elif minutesNow < minutesAlarm: + self.alarmTrunedOff = False await asyncio.sleep_ms(200) HW.housingLEDs.fill((0,0,0)) self.cleanup() @@ -24,6 +39,8 @@ class ClockScreen(): self.running = False self.light = False self.clock = StepperClock.StepperClock() + self.alarmTrunedOff = False + self.alarmPlaying = False COLORS = ((255,0,0),(255,0,127),(127,0,255),(0,0,255),(0,127,255),(140,70,255),(0,255,127),(0,255,0),(127,255,0),(255,127,0)) BRIGHTNESS = (5,10,50,100,160,255) @@ -32,23 +49,32 @@ class ClockScreen(): 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 > 700: - self.finish() - return - self.light = not self.light - elif button == 2: - if pushDownTime > 700: - Settings.selectedColor=(Settings.selectedColor+1)%len(ClockScreen.COLORS) - else: - Settings.alarmOn = not Settings.alarmOn - HW.leds[2].setOffState(pwmDuty=200 if Settings.alarmOn else 0) - Settings.save() - HW.housingLEDs.fill([int(c/255*ClockScreen.BRIGHTNESS[Settings.selectedBrightness]) for c in ClockScreen.COLORS[Settings.selectedColor]] if self.light else (0,0,0)) + if self.alarmPlaying: + HW.buzzer.stop() + self.alarmTrunedOff = True + self.alarmPlaying = False + HW.housingLEDs.fill((0,0,0)) + HW.leds[0].setOffState() + HW.leds[1].setOffState() + HW.leds[2].setOffState(pwmDuty=200 if Settings.alarmOn else 0) + HW.housingLEDs.fill([int(c/255*ClockScreen.BRIGHTNESS[Settings.selectedBrightness]) for c in ClockScreen.COLORS[Settings.selectedColor]] if self.light else (0,0,0)) + else: + if button == 0: + Settings.selectedBrightness=(Settings.selectedBrightness+1)%len(ClockScreen.BRIGHTNESS) + elif button == 1: + if pushDownTime > 700: + self.finish() + return + self.light = not self.light + elif button == 2: + if pushDownTime < 700: + Settings.selectedColor=(Settings.selectedColor+1)%len(ClockScreen.COLORS) + else: + Settings.alarmOn = not Settings.alarmOn + HW.leds[2].setOffState(pwmDuty=200 if Settings.alarmOn else 0) + Settings.save() + 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): @@ -80,13 +106,14 @@ class InitHandsScreen(): class SetAlarmTimeScreen(): def setAlarmTime(self,selectedTime): Settings.alarmTime = selectedTime + Settings.save() def __iter__(self): HW.housingLEDs.fill((255,0,0)) timeScreen = GetTimeScreen(\ startTime=Settings.alarmTime,\ onStart=lambda _: HW.buzzer.playSound(HW.Buzzer.BEEP),\ - onSelectedTimeChange=self.setAlarmTime,\ - onPicked=lambda _: Settings.save()) + onSelectedTimeChange=None,\ + onPicked=self.setAlarmTime) await timeScreen HW.housingLEDs.fill((0,0,0)) @@ -189,7 +216,6 @@ class GetNumberScreen(): HW.leds[button].on() def onButtonReleased(self,button,pushDownTime): - print("NumberSel push down",pushDownTime) HW.leds[button].off() if button == 1: if pushDownTime > 700: @@ -221,7 +247,7 @@ class Get2PositionsScreen(): self.mode=0 # 0: Minutes, 1: Hours HW.stepperminu.rotateTo(-(self.pickedTime[1])/12) HW.stepperhour.rotateTo(-(self.pickedTime[0])/12) - HW.leds[1].setOnState(blinkRate=1.2) + HW.leds[1].setOffState(blinkRate=1.2) for number,button in enumerate(HW.buttons): button.setCallbacks(\ onPushDown=lambda i=number:self.onButtonPressed(i),\ @@ -244,6 +270,7 @@ class Get2PositionsScreen(): b.setCallbacks()#Clear Callbacks for l in HW.leds: l.off() + l.setOffState() def onButtonPressed(self,button): HW.leds[button].on() @@ -281,10 +308,10 @@ class GetTimeScreen(): self.AM = (self.pickedTime < 12*60) self.direction = 0 def calcTupleTime(self): - return (int(self.pickedTime/60),self.pickedTime%60) + return (int(self.pickedTime/60)%24,self.pickedTime%60) def __await__(self): self.running = True - HW.leds[1].setOnState(blinkRate=1.2) + HW.leds[1].setOffState(blinkRate=1.2) HW.housingLEDs.upper(HW.HousingLEDs.BLUE if self.AM else HW.HousingLEDs.YELLOW) for number,button in enumerate(HW.buttons): button.setCallbacks(\ @@ -315,6 +342,7 @@ class GetTimeScreen(): b.setCallbacks()#Clear Callbacks for l in HW.leds: l.off() + l.setOffState() def onButtonPressed(self,button): HW.leds[button].on() diff --git a/Settings.py b/Settings.py index 94b0765..b95ccbf 100644 --- a/Settings.py +++ b/Settings.py @@ -1,12 +1,15 @@ selectedSound=0 selectedBrightness=0 selectedColor=0 -alarmTime=(8,0) +alarmTime=(9,0) alarmOn=False def printState(): - print("selectedSound=",selectedSound) + print("alarmOn=",alarmOn) print("alarmTime=",alarmTime) + print("selectedSound=",selectedSound) + print("selectedColor=",selectedColor) + print("selectedBrightness=",selectedBrightness) def save(): global selectedSound @@ -22,12 +25,10 @@ def save(): elif l.startswith("alarmTime"): wf.write("alarmTime="+str(alarmTime)+"\n") elif l.startswith("alarmOn"): - wf.write("alarmTime="+str(alarmOn)+"\n") + wf.write("alarmOn="+str(alarmOn)+"\n") elif l.startswith("selectedColor"): wf.write("selectedColor="+str(selectedColor)+"\n") elif l.startswith("selectedBrightness"): wf.write("selectedBrightness="+str(selectedBrightness)+"\n") else: - wf.write(l) - #printState() -#printState() \ No newline at end of file + wf.write(l) \ No newline at end of file diff --git a/StepperL298M.py b/StepperL298M.py index 059ea4a..fa4b0df 100644 --- a/StepperL298M.py +++ b/StepperL298M.py @@ -60,7 +60,6 @@ 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 45c3c80..a6974b0 100644 --- a/boot.py +++ b/boot.py @@ -4,6 +4,7 @@ import webrepl webrepl.start() sta_if = network.WLAN(network.STA_IF) sta_if.active(True) +sta_if.config(dhcp_hostname="stepper-clock") if sta_if.status() != network.STAT_GOT_IP: availableNetworkSSIDs = [s[0].decode() for s in sta_if.scan()]