moves Hardware to seperate static module

This commit is contained in:
Nils Schulte 2020-03-20 00:38:31 +01:00
parent ebd49339a9
commit 533bb1bd0d
6 changed files with 113 additions and 103 deletions

39
Hardware.py Normal file
View File

@ -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)

View File

@ -20,6 +20,7 @@ class LEDs():
def fill(self,color): def fill(self,color):
self.neop.fill(color) self.neop.fill(color)
self.neop[0] = (0,0,0)
self.neop.write() self.neop.write()
def clear(self): def clear(self):

View File

@ -2,30 +2,38 @@ import StepperClock
import uasyncio as asyncio import uasyncio as asyncio
import Buzzer import Buzzer
import Settings import Settings
import Hardware as HW
class ClockScreen(): 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): def __init__(self):
self.clock = StepperClock.StepperClock(stepperhour,stepperminu) self.clock = StepperClock.StepperClock()
self.running = False self.running = False
self.buttons = buttons self.light = False
self.leds = leds
def stopIfAllPressed(self,button): def stopIfAllPressed(self,button):
self.leds[button].on() HW.leds[button].on()
for b in self.buttons: for b in HW.buttons:
if not b.isPushed(): if not b.isPushed():
break break
else: else:
self.running = False self.running = False
self onClicked(button): def onClicked(self,button):
if button == 0: 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): def __await__(self):
for i,b in enumerate(self.buttons): HW.housingLEDs.fill([int(c/255*ClockScreen.BRIGHTNESS[Settings.selectedBrightness]) for c in ClockScreen.COLORS[Settings.selectedColor]] if self.light else (0,0,0))
b.setCallbacks(onPushDown=lambda i=i:self.stopIfAllPressed(i),onPushUp=lambda i=i:self.leds[i].off(),onClick=lambda i=i:self.onClicked(i)) 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.running = True
self.clock.start() self.clock.start()
while self.running: while self.running:
@ -35,95 +43,88 @@ class ClockScreen():
class SettingsScreen(): class SettingsScreen():
def __init__(self,stepperhour,stepperminu,buttons,leds,housingLEDs,buzzer): def __init__(self):
self.stepperminu = stepperminu
self.stepperhour = stepperhour
self.running = False self.running = False
self.buttons = buttons
self.leds = leds
self.housingLEDs = housingLEDs
self.buzzer = buzzer
# TODO load these form file
def onButtonPressed(self,button): def onButtonPressed(self,button):
self.leds[button].on(overwriteFlashing=False) HW.leds[button].on(overwriteFlashing=False)
for b in self.buttons: for b in HW.buttons:
if not b.isPushed(): if not b.isPushed():
break break
else: else:
self.running = False self.running = False
return return
if not self.buttons[1].isPushed(): if not HW.buttons[1].isPushed():
if button == 0 : if button == 0 :
if self.mode == 0: if self.mode == 0:
self.stepperminu.rotateTo(direction=1) HW.stepperminu.rotateTo(direction=1)
if self.mode == 1: if self.mode == 1:
self.stepperhour.rotateTo(direction=1) HW.stepperhour.rotateTo(direction=1)
elif button == 2: elif button == 2:
if self.mode == 0: if self.mode == 0:
self.stepperminu.rotateTo(direction=-1) HW.stepperminu.rotateTo(direction=-1)
if self.mode == 1: if self.mode == 1:
self.stepperhour.rotateTo(direction=-1) HW.stepperhour.rotateTo(direction=-1)
def onButtonReleased(self,button): def onButtonReleased(self,button):
if not self.running: if not self.running:
return return
self.leds[button].off(overwriteFlashing=False) HW.leds[button].off(overwriteFlashing=False)
if button == 0:#left if button == 0:#left
if self.mode == 0 or self.mode == 1: if self.mode == 0 or self.mode == 1:
self.stepperminu.stop() HW.stepperminu.stop()
self.stepperhour.stop() HW.stepperhour.stop()
if self.mode == 3: if self.mode == 3:
Settings.selectedSound =(Settings.selectedSound -1) % len(Buzzer.SOUNDS) 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 elif button == 2:#right
if self.mode == 0 or self.mode == 1: if self.mode == 0 or self.mode == 1:
self.stepperminu.stop() HW.stepperminu.stop()
self.stepperhour.stop() HW.stepperhour.stop()
if self.mode == 3: if self.mode == 3:
Settings.selectedSound =(Settings.selectedSound +1) % len(Buzzer.SOUNDS) 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 elif button == 1: #middle
self.mode=(self.mode+1)%4 self.mode=(self.mode+1)%4
if self.mode == 3: if self.mode == 3:
self.buzzer.playSound(Buzzer.SOUNDS[Settings.selectedSound ]) HW.buzzer.playSound(Buzzer.SOUNDS[Settings.selectedSound ])
else: else:
self.buzzer.stop() HW.buzzer.stop()
if self.mode == 2: if self.mode == 2:
self.leds[0].flash(0.7) HW.leds[0].flash(0.7)
self.leds[1].flash(0.7) HW.leds[1].flash(0.7)
self.leds[2].flash(0.7) HW.leds[2].flash(0.7)
else: else:
self.leds[0].off() HW.leds[0].off()
self.leds[1].off() HW.leds[1].off()
self.leds[2].off() HW.leds[2].off()
if self.mode == 0 or self.mode == 1: if self.mode == 0 or self.mode == 1:
self.housingLEDs.upper((50,50,50)) HW.housingLEDs.upper((50,50,50))
else: else:
self.housingLEDs.clear() HW.housingLEDs.clear()
def __await__(self): def __await__(self):
self.mode = 0 self.mode = 0
self.stepperminu.rotateTo(0) HW.stepperminu.rotateTo(0)
self.stepperhour.rotateTo(0) HW.stepperhour.rotateTo(0)
while not self.stepperhour.isAtTarget() or not self.stepperminu.isAtTarget(): HW.housingLEDs.clear()
while not HW.stepperhour.isAtTarget() or not HW.stepperminu.isAtTarget():
await asyncio.sleep_ms(200) await asyncio.sleep_ms(200)
self.housingLEDs.upper((50,50,50)) HW.housingLEDs.upper((50,50,50))
for led in self.leds: for led in HW.leds:
led.off() 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)) b.setCallbacks(onPushDown=lambda i=i:self.onButtonPressed(i),onPushUp=lambda i=i:self.onButtonReleased(i))
self.running = True self.running = True
while self.running: while self.running:
await asyncio.sleep_ms(200) await asyncio.sleep_ms(200)
for led in self.leds: for led in HW.leds:
led.off() led.off()
self.housingLEDs.clear() HW.housingLEDs.clear()
self.stepperhour.reset() HW.stepperhour.reset()
self.stepperminu.reset() HW.stepperminu.reset()
self.buzzer.stop() HW.buzzer.stop()
Settings.save() Settings.save()
__iter__ = __await__ # https://github.com/micropython/micropython/issues/2678 __iter__ = __await__ # https://github.com/micropython/micropython/issues/2678

View File

@ -1,4 +1,6 @@
selectedSound=0 selectedSound=0
selectedBrightness=0
selectedColor=0
alarmTime=((1,2,3,4),(8,),(0,)) alarmTime=((1,2,3,4),(8,),(0,))
@ -19,6 +21,10 @@ def save():
wf.write("selectedSound="+str(selectedSound)+"\n") wf.write("selectedSound="+str(selectedSound)+"\n")
elif l.startswith("alarmTime"): elif l.startswith("alarmTime"):
wf.write("alarmTime="+str(alarmTime)+"\n") 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: else:
wf.write(l) wf.write(l)
#printState() #printState()

View File

@ -1,11 +1,10 @@
import StepperL298M import StepperL298M
import time import time
import Hardware as HW
import uasyncio as asyncio import uasyncio as asyncio
class StepperClock: class StepperClock:
def __init__(self,stepperHour,stepperMin): def __init__(self):
self._stepperMinute = stepperMin
self._stepperHour = stepperHour
self.started_async = False self.started_async = False
self._async_running = False self._async_running = False
@ -28,13 +27,13 @@ class StepperClock:
def update(self): def update(self):
hour = time.localtime()[3] hour = time.localtime()[3]
minute = time.localtime()[4] minute = time.localtime()[4]
self._stepperHour.rotateTo(1-((hour%12)/12+minute/(12*60))) HW.stepperhour.rotateTo(1-((hour%12)/12+minute/(12*60)))
self._stepperMinute.rotateTo(1-minute/60) HW.stepperminu.rotateTo(1-minute/60)
def disablePower(self): def disablePower(self):
self._stepperHour.disablePower() HW.stepperhour.disablePower()
self._stepperMinute.disablePower() HW.stepperminu.disablePower()
#self._stepperHour.disablePower() #HW.stepperhour.disablePower()
def isRunning(self): def isRunning(self):
return self._async_running return self._async_running

46
main.py
View File

@ -1,56 +1,20 @@
import time import time
import machine
import uasyncio as asyncio import uasyncio as asyncio
import DS3231 import Hardware as HW
import Button
import LED
import HousingLEDs
import Buzzer
import StepperL298M
import Screens import Screens
#Async #Async
loop = asyncio.get_event_loop() 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 modus = 0
screens = (Screens.ClockScreen(stepperhour,stepperminu,buttons,leds),\ screens = (Screens.ClockScreen(),\
Screens.SettingsScreen(stepperhour,stepperminu,buttons,leds,housingLEDs,buzzer)) Screens.SettingsScreen())
async def run_screens(): async def run_screens():
global modus,screens global modus,screens
while True: while True:
await screens[modus%len(screens)] await screens[modus%len(screens)]
for i,b in enumerate(HW.buttons):
b.setCallbacks()#Clear Callbacks
modus += 1 modus += 1
loop.run_until_complete(run_screens()) loop.run_until_complete(run_screens())