misc fixes
This commit is contained in:
parent
4d0bd17839
commit
8a75738d6d
@ -48,7 +48,6 @@ class Buzzer():
|
|||||||
else:
|
else:
|
||||||
self.newSound = False
|
self.newSound = False
|
||||||
timesPlayed = 0
|
timesPlayed = 0
|
||||||
#print("Play sound!")
|
|
||||||
i = 0
|
i = 0
|
||||||
while i < len(self.sound):
|
while i < len(self.sound):
|
||||||
s = self.sound[i]
|
s = self.sound[i]
|
||||||
@ -69,9 +68,9 @@ class Buzzer():
|
|||||||
if self.newSound or self.sound == None:
|
if self.newSound or self.sound == None:
|
||||||
break
|
break
|
||||||
i+=1
|
i+=1
|
||||||
if (not self.newSound):
|
if not self.newSound:
|
||||||
self.sound = None
|
|
||||||
self.newSound = False
|
self.newSound = False
|
||||||
|
self.sound = None
|
||||||
|
|
||||||
def playSound(self,sound):
|
def playSound(self,sound):
|
||||||
self.newSound = True
|
self.newSound = True
|
||||||
@ -82,4 +81,4 @@ class Buzzer():
|
|||||||
self._pwm.duty(0)
|
self._pwm.duty(0)
|
||||||
|
|
||||||
def isPlaying(self):
|
def isPlaying(self):
|
||||||
return self.sound == None
|
return self.sound != None
|
@ -19,8 +19,8 @@ except Exception as e:
|
|||||||
buzzer = Buzzer.Buzzer(pin=2)
|
buzzer = Buzzer.Buzzer(pin=2)
|
||||||
|
|
||||||
#Initalise the Motors
|
#Initalise the Motors
|
||||||
stepperminu = StepperL298M.Stepper([19,21,22,23],inverted=False)
|
stepperminu = StepperL298M.Stepper((19,21,22,23),inverted=False)
|
||||||
stepperhour = StepperL298M.Stepper([12,27,26,25],inverted=True)
|
stepperhour = StepperL298M.Stepper((12,27,26,25),inverted=True)
|
||||||
|
|
||||||
#Init LEDs
|
#Init LEDs
|
||||||
housingLEDs = HousingLEDs.LEDs(pin=16)
|
housingLEDs = HousingLEDs.LEDs(pin=16)
|
||||||
|
33
LED.py
33
LED.py
@ -3,7 +3,7 @@ import machine
|
|||||||
|
|
||||||
class LED():
|
class LED():
|
||||||
def __init__(self, pin):
|
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.pwmDutyOff = 0
|
||||||
self.pwmDutyOn = 1023
|
self.pwmDutyOn = 1023
|
||||||
self.pwmDuty = 0
|
self.pwmDuty = 0
|
||||||
@ -12,30 +12,47 @@ class LED():
|
|||||||
self.blinkRate = None
|
self.blinkRate = None
|
||||||
self.onState = False
|
self.onState = False
|
||||||
self.blinkOn = False
|
self.blinkOn = False
|
||||||
|
self.alarmWentOff = False
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.create_task(self._update_async())
|
loop.create_task(self._update_async())
|
||||||
|
|
||||||
async def _update_async(self):
|
async def _update_async(self):
|
||||||
|
self.pin.duty(0)
|
||||||
while True:
|
while True:
|
||||||
if self.blinkRate == None or self.blinkRate <= 0:
|
if self.blinkRate == None:
|
||||||
await asyncio.sleep_ms(200)
|
await asyncio.sleep_ms(200)
|
||||||
else:
|
else:
|
||||||
self.pin.duty(self.pwmDuty if self.blinkOn else 0)
|
self.pin.duty(self.pwmDuty if self.blinkOn else 0)
|
||||||
self.blinkOn = not self.blinkOn
|
self.blinkOn = not self.blinkOn
|
||||||
# TODO split long waits into multible ones so we catch changes more often
|
waited=0
|
||||||
await asyncio.sleep_ms(int(500 / self.blinkRate))
|
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):
|
def toggle(self):
|
||||||
self.onState = not self.onState
|
self.onState = not self.onState
|
||||||
self.value(self.onState)
|
self.value(self.onState)
|
||||||
|
|
||||||
def setOnState(self, blinkRate = None, pwmDuty = 1023, ):
|
def setOnState(self, blinkRate = None, pwmDuty = 1023):
|
||||||
self.blinkRateOn = blinkRate
|
self.blinkRateOn = blinkRate if blinkRate != None and blinkRate >= 0 else None
|
||||||
self.pwmDutyOn = pwmDuty
|
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):
|
def setOffState(self, blinkRate = None, pwmDuty = 0):
|
||||||
self.blinkRateOff = blinkRate
|
self.blinkRateOff = blinkRate if blinkRate != None and blinkRate >= 0 else None
|
||||||
self.pwmDutyOff = pwmDuty
|
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):
|
def on(self):
|
||||||
self.value(True)
|
self.value(True)
|
||||||
@ -48,6 +65,6 @@ class LED():
|
|||||||
self.onState = value
|
self.onState = value
|
||||||
self.blinkRate = self.blinkRateOn if self.onState else self.blinkRateOff
|
self.blinkRate = self.blinkRateOn if self.onState else self.blinkRateOff
|
||||||
self.pwmDuty = (self.pwmDutyOn if self.onState else self.pwmDutyOff)
|
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
|
return self.onState
|
||||||
|
|
44
Screens.py
44
Screens.py
@ -5,12 +5,27 @@ import Settings
|
|||||||
import Hardware as HW
|
import Hardware as HW
|
||||||
import math
|
import math
|
||||||
import network
|
import network
|
||||||
|
import time
|
||||||
|
|
||||||
class ClockScreen():
|
class ClockScreen():
|
||||||
def __await__(self):
|
def __await__(self):
|
||||||
self.running = True
|
self.running = True
|
||||||
self.init()
|
self.init()
|
||||||
while self.running:
|
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)
|
await asyncio.sleep_ms(200)
|
||||||
HW.housingLEDs.fill((0,0,0))
|
HW.housingLEDs.fill((0,0,0))
|
||||||
self.cleanup()
|
self.cleanup()
|
||||||
@ -24,6 +39,8 @@ class ClockScreen():
|
|||||||
self.running = False
|
self.running = False
|
||||||
self.light = False
|
self.light = False
|
||||||
self.clock = StepperClock.StepperClock()
|
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))
|
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)
|
BRIGHTNESS = (5,10,50,100,160,255)
|
||||||
@ -32,8 +49,17 @@ class ClockScreen():
|
|||||||
HW.leds[button].on()
|
HW.leds[button].on()
|
||||||
|
|
||||||
def onButtonReleased(self,button,pushDownTime):
|
def onButtonReleased(self,button,pushDownTime):
|
||||||
print("Clock press",pushDownTime)
|
|
||||||
HW.leds[button].off()
|
HW.leds[button].off()
|
||||||
|
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:
|
if button == 0:
|
||||||
Settings.selectedBrightness=(Settings.selectedBrightness+1)%len(ClockScreen.BRIGHTNESS)
|
Settings.selectedBrightness=(Settings.selectedBrightness+1)%len(ClockScreen.BRIGHTNESS)
|
||||||
elif button == 1:
|
elif button == 1:
|
||||||
@ -42,7 +68,7 @@ class ClockScreen():
|
|||||||
return
|
return
|
||||||
self.light = not self.light
|
self.light = not self.light
|
||||||
elif button == 2:
|
elif button == 2:
|
||||||
if pushDownTime > 700:
|
if pushDownTime < 700:
|
||||||
Settings.selectedColor=(Settings.selectedColor+1)%len(ClockScreen.COLORS)
|
Settings.selectedColor=(Settings.selectedColor+1)%len(ClockScreen.COLORS)
|
||||||
else:
|
else:
|
||||||
Settings.alarmOn = not Settings.alarmOn
|
Settings.alarmOn = not Settings.alarmOn
|
||||||
@ -80,13 +106,14 @@ class InitHandsScreen():
|
|||||||
class SetAlarmTimeScreen():
|
class SetAlarmTimeScreen():
|
||||||
def setAlarmTime(self,selectedTime):
|
def setAlarmTime(self,selectedTime):
|
||||||
Settings.alarmTime = selectedTime
|
Settings.alarmTime = selectedTime
|
||||||
|
Settings.save()
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
HW.housingLEDs.fill((255,0,0))
|
HW.housingLEDs.fill((255,0,0))
|
||||||
timeScreen = GetTimeScreen(\
|
timeScreen = GetTimeScreen(\
|
||||||
startTime=Settings.alarmTime,\
|
startTime=Settings.alarmTime,\
|
||||||
onStart=lambda _: HW.buzzer.playSound(HW.Buzzer.BEEP),\
|
onStart=lambda _: HW.buzzer.playSound(HW.Buzzer.BEEP),\
|
||||||
onSelectedTimeChange=self.setAlarmTime,\
|
onSelectedTimeChange=None,\
|
||||||
onPicked=lambda _: Settings.save())
|
onPicked=self.setAlarmTime)
|
||||||
await timeScreen
|
await timeScreen
|
||||||
HW.housingLEDs.fill((0,0,0))
|
HW.housingLEDs.fill((0,0,0))
|
||||||
|
|
||||||
@ -189,7 +216,6 @@ class GetNumberScreen():
|
|||||||
HW.leds[button].on()
|
HW.leds[button].on()
|
||||||
|
|
||||||
def onButtonReleased(self,button,pushDownTime):
|
def onButtonReleased(self,button,pushDownTime):
|
||||||
print("NumberSel push down",pushDownTime)
|
|
||||||
HW.leds[button].off()
|
HW.leds[button].off()
|
||||||
if button == 1:
|
if button == 1:
|
||||||
if pushDownTime > 700:
|
if pushDownTime > 700:
|
||||||
@ -221,7 +247,7 @@ class Get2PositionsScreen():
|
|||||||
self.mode=0 # 0: Minutes, 1: Hours
|
self.mode=0 # 0: Minutes, 1: Hours
|
||||||
HW.stepperminu.rotateTo(-(self.pickedTime[1])/12)
|
HW.stepperminu.rotateTo(-(self.pickedTime[1])/12)
|
||||||
HW.stepperhour.rotateTo(-(self.pickedTime[0])/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):
|
for number,button in enumerate(HW.buttons):
|
||||||
button.setCallbacks(\
|
button.setCallbacks(\
|
||||||
onPushDown=lambda i=number:self.onButtonPressed(i),\
|
onPushDown=lambda i=number:self.onButtonPressed(i),\
|
||||||
@ -244,6 +270,7 @@ class Get2PositionsScreen():
|
|||||||
b.setCallbacks()#Clear Callbacks
|
b.setCallbacks()#Clear Callbacks
|
||||||
for l in HW.leds:
|
for l in HW.leds:
|
||||||
l.off()
|
l.off()
|
||||||
|
l.setOffState()
|
||||||
|
|
||||||
def onButtonPressed(self,button):
|
def onButtonPressed(self,button):
|
||||||
HW.leds[button].on()
|
HW.leds[button].on()
|
||||||
@ -281,10 +308,10 @@ class GetTimeScreen():
|
|||||||
self.AM = (self.pickedTime < 12*60)
|
self.AM = (self.pickedTime < 12*60)
|
||||||
self.direction = 0
|
self.direction = 0
|
||||||
def calcTupleTime(self):
|
def calcTupleTime(self):
|
||||||
return (int(self.pickedTime/60),self.pickedTime%60)
|
return (int(self.pickedTime/60)%24,self.pickedTime%60)
|
||||||
def __await__(self):
|
def __await__(self):
|
||||||
self.running = True
|
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)
|
HW.housingLEDs.upper(HW.HousingLEDs.BLUE if self.AM else HW.HousingLEDs.YELLOW)
|
||||||
for number,button in enumerate(HW.buttons):
|
for number,button in enumerate(HW.buttons):
|
||||||
button.setCallbacks(\
|
button.setCallbacks(\
|
||||||
@ -315,6 +342,7 @@ class GetTimeScreen():
|
|||||||
b.setCallbacks()#Clear Callbacks
|
b.setCallbacks()#Clear Callbacks
|
||||||
for l in HW.leds:
|
for l in HW.leds:
|
||||||
l.off()
|
l.off()
|
||||||
|
l.setOffState()
|
||||||
|
|
||||||
def onButtonPressed(self,button):
|
def onButtonPressed(self,button):
|
||||||
HW.leds[button].on()
|
HW.leds[button].on()
|
||||||
|
11
Settings.py
11
Settings.py
@ -1,12 +1,15 @@
|
|||||||
selectedSound=0
|
selectedSound=0
|
||||||
selectedBrightness=0
|
selectedBrightness=0
|
||||||
selectedColor=0
|
selectedColor=0
|
||||||
alarmTime=(8,0)
|
alarmTime=(9,0)
|
||||||
alarmOn=False
|
alarmOn=False
|
||||||
|
|
||||||
def printState():
|
def printState():
|
||||||
print("selectedSound=",selectedSound)
|
print("alarmOn=",alarmOn)
|
||||||
print("alarmTime=",alarmTime)
|
print("alarmTime=",alarmTime)
|
||||||
|
print("selectedSound=",selectedSound)
|
||||||
|
print("selectedColor=",selectedColor)
|
||||||
|
print("selectedBrightness=",selectedBrightness)
|
||||||
|
|
||||||
def save():
|
def save():
|
||||||
global selectedSound
|
global selectedSound
|
||||||
@ -22,12 +25,10 @@ def save():
|
|||||||
elif l.startswith("alarmTime"):
|
elif l.startswith("alarmTime"):
|
||||||
wf.write("alarmTime="+str(alarmTime)+"\n")
|
wf.write("alarmTime="+str(alarmTime)+"\n")
|
||||||
elif l.startswith("alarmOn"):
|
elif l.startswith("alarmOn"):
|
||||||
wf.write("alarmTime="+str(alarmOn)+"\n")
|
wf.write("alarmOn="+str(alarmOn)+"\n")
|
||||||
elif l.startswith("selectedColor"):
|
elif l.startswith("selectedColor"):
|
||||||
wf.write("selectedColor="+str(selectedColor)+"\n")
|
wf.write("selectedColor="+str(selectedColor)+"\n")
|
||||||
elif l.startswith("selectedBrightness"):
|
elif l.startswith("selectedBrightness"):
|
||||||
wf.write("selectedBrightness="+str(selectedBrightness)+"\n")
|
wf.write("selectedBrightness="+str(selectedBrightness)+"\n")
|
||||||
else:
|
else:
|
||||||
wf.write(l)
|
wf.write(l)
|
||||||
#printState()
|
|
||||||
#printState()
|
|
@ -60,7 +60,6 @@ class Stepper:
|
|||||||
self._rotTarget = self.stepnum
|
self._rotTarget = self.stepnum
|
||||||
|
|
||||||
async def _update_async(self):
|
async def _update_async(self):
|
||||||
print("update")
|
|
||||||
while(True):
|
while(True):
|
||||||
await asyncio.sleep_ms(self.stepDurationMs)
|
await asyncio.sleep_ms(self.stepDurationMs)
|
||||||
if not self.isAtTarget():
|
if not self.isAtTarget():
|
||||||
|
1
boot.py
1
boot.py
@ -4,6 +4,7 @@ import webrepl
|
|||||||
webrepl.start()
|
webrepl.start()
|
||||||
sta_if = network.WLAN(network.STA_IF)
|
sta_if = network.WLAN(network.STA_IF)
|
||||||
sta_if.active(True)
|
sta_if.active(True)
|
||||||
|
sta_if.config(dhcp_hostname="stepper-clock")
|
||||||
|
|
||||||
if sta_if.status() != network.STAT_GOT_IP:
|
if sta_if.status() != network.STAT_GOT_IP:
|
||||||
availableNetworkSSIDs = [s[0].decode() for s in sta_if.scan()]
|
availableNetworkSSIDs = [s[0].decode() for s in sta_if.scan()]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user