From 533bb1bd0deeee52782b5b0684c0e935353610e0 Mon Sep 17 00:00:00 2001 From: Nils Schulte Date: Fri, 20 Mar 2020 00:38:31 +0100 Subject: [PATCH] moves Hardware to seperate static module --- Hardware.py | 39 +++++++++++++++++ HousingLEDs.py | 1 + Screens.py | 109 ++++++++++++++++++++++++------------------------ Settings.py | 6 +++ StepperClock.py | 15 ++++--- main.py | 46 +++----------------- 6 files changed, 113 insertions(+), 103 deletions(-) create mode 100644 Hardware.py diff --git a/Hardware.py b/Hardware.py new file mode 100644 index 0000000..387b3ce --- /dev/null +++ b/Hardware.py @@ -0,0 +1,39 @@ +import machine +import DS3231 +import Buzzer +import StepperL298M +import HousingLEDs +import LED +import Button + + +#Initialise the time module with the RTC +try: + dsRtc = DS3231.DS3231(machine.I2C(sda = machine.Pin(17), scl=machine.Pin(18))) + rtc = machine.RTC() + rtc.init(dsRtc.DateTime()+[0]) +except Exception as e: + print("Error on DS3231-Inititalisation\n",e) + +#Init Buzzer +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) + +#Init LEDs +housingLEDs = HousingLEDs.LEDs(pin=16) + +leftButtonLED = LED.LED(13) +middleButtonLED = LED.LED(33) +rightButtonLED = LED.LED(32) + +#Init Buttons +leftButton = Button.Button(pin=34,inverted=True) +middleButton = Button.Button(pin=39,inverted=True) +rightButton = Button.Button(pin=36,inverted=True) + + +buttons = (leftButton,middleButton,rightButton) +leds = (leftButtonLED,middleButtonLED,rightButtonLED) \ No newline at end of file diff --git a/HousingLEDs.py b/HousingLEDs.py index 27da5af..d3238ae 100644 --- a/HousingLEDs.py +++ b/HousingLEDs.py @@ -20,6 +20,7 @@ class LEDs(): def fill(self,color): self.neop.fill(color) + self.neop[0] = (0,0,0) self.neop.write() def clear(self): diff --git a/Screens.py b/Screens.py index 453637a..7d011ab 100644 --- a/Screens.py +++ b/Screens.py @@ -2,30 +2,38 @@ 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,stepperhour,stepperminu,buttons,leds): - self.clock = StepperClock.StepperClock(stepperhour,stepperminu) + def __init__(self): + self.clock = StepperClock.StepperClock() self.running = False - self.buttons = buttons - self.leds = leds + self.light = False def stopIfAllPressed(self,button): - self.leds[button].on() - for b in self.buttons: + HW.leds[button].on() + for b in HW.buttons: if not b.isPushed(): break else: self.running = False - self onClicked(button): + def onClicked(self,button): if button == 0: - pass + 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): - for i,b in enumerate(self.buttons): - b.setCallbacks(onPushDown=lambda i=i:self.stopIfAllPressed(i),onPushUp=lambda i=i:self.leds[i].off(),onClick=lambda i=i:self.onClicked(i)) + 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: @@ -35,95 +43,88 @@ class ClockScreen(): class SettingsScreen(): - def __init__(self,stepperhour,stepperminu,buttons,leds,housingLEDs,buzzer): - self.stepperminu = stepperminu - self.stepperhour = stepperhour + def __init__(self): 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: + HW.leds[button].on(overwriteFlashing=False) + for b in HW.buttons: if not b.isPushed(): break else: self.running = False return - if not self.buttons[1].isPushed(): + if not HW.buttons[1].isPushed(): if button == 0 : if self.mode == 0: - self.stepperminu.rotateTo(direction=1) + HW.stepperminu.rotateTo(direction=1) if self.mode == 1: - self.stepperhour.rotateTo(direction=1) + HW.stepperhour.rotateTo(direction=1) elif button == 2: if self.mode == 0: - self.stepperminu.rotateTo(direction=-1) + HW.stepperminu.rotateTo(direction=-1) if self.mode == 1: - self.stepperhour.rotateTo(direction=-1) + HW.stepperhour.rotateTo(direction=-1) def onButtonReleased(self,button): if not self.running: return - self.leds[button].off(overwriteFlashing=False) + HW.leds[button].off(overwriteFlashing=False) if button == 0:#left if self.mode == 0 or self.mode == 1: - self.stepperminu.stop() - self.stepperhour.stop() + HW.stepperminu.stop() + HW.stepperhour.stop() if self.mode == 3: Settings.selectedSound =(Settings.selectedSound -1) % len(Buzzer.SOUNDS) - self.buzzer.playSound(Buzzer.SOUNDS[Settings.selectedSound ]) + HW.buzzer.playSound(Buzzer.SOUNDS[Settings.selectedSound ]) elif button == 2:#right if self.mode == 0 or self.mode == 1: - self.stepperminu.stop() - self.stepperhour.stop() + HW.stepperminu.stop() + HW.stepperhour.stop() if self.mode == 3: Settings.selectedSound =(Settings.selectedSound +1) % len(Buzzer.SOUNDS) - self.buzzer.playSound(Buzzer.SOUNDS[Settings.selectedSound ]) + HW.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 ]) + HW.buzzer.playSound(Buzzer.SOUNDS[Settings.selectedSound ]) else: - self.buzzer.stop() + HW.buzzer.stop() if self.mode == 2: - self.leds[0].flash(0.7) - self.leds[1].flash(0.7) - self.leds[2].flash(0.7) + HW.leds[0].flash(0.7) + HW.leds[1].flash(0.7) + HW.leds[2].flash(0.7) else: - self.leds[0].off() - self.leds[1].off() - self.leds[2].off() + HW.leds[0].off() + HW.leds[1].off() + HW.leds[2].off() if self.mode == 0 or self.mode == 1: - self.housingLEDs.upper((50,50,50)) + HW.housingLEDs.upper((50,50,50)) else: - self.housingLEDs.clear() + HW.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(): + 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) - self.housingLEDs.upper((50,50,50)) - for led in self.leds: + HW.housingLEDs.upper((50,50,50)) + for led in HW.leds: led.off() - for i,b in enumerate(self.buttons): + 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 self.leds: + for led in HW.leds: led.off() - self.housingLEDs.clear() - self.stepperhour.reset() - self.stepperminu.reset() - self.buzzer.stop() + 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 diff --git a/Settings.py b/Settings.py index 49ead57..06f1ab9 100644 --- a/Settings.py +++ b/Settings.py @@ -1,4 +1,6 @@ selectedSound=0 +selectedBrightness=0 +selectedColor=0 alarmTime=((1,2,3,4),(8,),(0,)) @@ -19,6 +21,10 @@ def save(): wf.write("selectedSound="+str(selectedSound)+"\n") elif l.startswith("alarmTime"): wf.write("alarmTime="+str(alarmTime)+"\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() diff --git a/StepperClock.py b/StepperClock.py index 491c48b..34fed45 100644 --- a/StepperClock.py +++ b/StepperClock.py @@ -1,11 +1,10 @@ import StepperL298M import time +import Hardware as HW import uasyncio as asyncio class StepperClock: - def __init__(self,stepperHour,stepperMin): - self._stepperMinute = stepperMin - self._stepperHour = stepperHour + def __init__(self): self.started_async = False self._async_running = False @@ -28,13 +27,13 @@ class StepperClock: def update(self): hour = time.localtime()[3] minute = time.localtime()[4] - self._stepperHour.rotateTo(1-((hour%12)/12+minute/(12*60))) - self._stepperMinute.rotateTo(1-minute/60) + HW.stepperhour.rotateTo(1-((hour%12)/12+minute/(12*60))) + HW.stepperminu.rotateTo(1-minute/60) def disablePower(self): - self._stepperHour.disablePower() - self._stepperMinute.disablePower() - #self._stepperHour.disablePower() + HW.stepperhour.disablePower() + HW.stepperminu.disablePower() + #HW.stepperhour.disablePower() def isRunning(self): return self._async_running diff --git a/main.py b/main.py index 9057c80..2c27700 100644 --- a/main.py +++ b/main.py @@ -1,56 +1,20 @@ import time -import machine import uasyncio as asyncio -import DS3231 -import Button -import LED -import HousingLEDs -import Buzzer -import StepperL298M - +import Hardware as HW import Screens #Async loop = asyncio.get_event_loop() -#Initialise the time module with the RTC -try: - dsRtc = DS3231.DS3231(machine.I2C(sda = machine.Pin(17), scl=machine.Pin(18))) - rtc = machine.RTC() - rtc.init(dsRtc.DateTime()+[0]) -except: - print("Error on DS3231-Inititalisation") - -#Init Buzzer -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) - -#Init LEDs -housingLEDs = HousingLEDs.LEDs(pin=16) - -leftButtonLED = LED.LED(13) -middleButtonLED = LED.LED(33) -rightButtonLED = LED.LED(32) - -#Init Buttons -leftButton = Button.Button(pin=34,inverted=True) -middleButton = Button.Button(pin=39,inverted=True) -rightButton = Button.Button(pin=36,inverted=True) - - -buttons = (leftButton,middleButton,rightButton) -leds = (leftButtonLED,middleButtonLED,rightButtonLED) - modus = 0 -screens = (Screens.ClockScreen(stepperhour,stepperminu,buttons,leds),\ - Screens.SettingsScreen(stepperhour,stepperminu,buttons,leds,housingLEDs,buzzer)) +screens = (Screens.ClockScreen(),\ + Screens.SettingsScreen()) async def run_screens(): global modus,screens while True: await screens[modus%len(screens)] + for i,b in enumerate(HW.buttons): + b.setCallbacks()#Clear Callbacks modus += 1 loop.run_until_complete(run_screens()) \ No newline at end of file