Changed LEDs to use PWM
This commit is contained in:
parent
c2ac9c2f19
commit
4d0bd17839
53
LED.py
53
LED.py
@ -3,40 +3,51 @@ import machine
|
|||||||
|
|
||||||
class LED():
|
class LED():
|
||||||
def __init__(self, pin):
|
def __init__(self, pin):
|
||||||
self.pin = machine.Pin(pin,machine.Pin.OUT)
|
self.pin = machine.PWM(machine.Pin(pin,machine.Pin.OUT),freq=2000,duty=0)
|
||||||
self.rate = 0
|
self.pwmDutyOff = 0
|
||||||
|
self.pwmDutyOn = 1023
|
||||||
|
self.pwmDuty = 0
|
||||||
|
self.blinkRateOff = None
|
||||||
|
self.blinkRateOn = None
|
||||||
|
self.blinkRate = None
|
||||||
|
self.onState = False
|
||||||
|
self.blinkOn = False
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.create_task(self._update_async())
|
loop.create_task(self._update_async())
|
||||||
self.state = 0
|
|
||||||
self.on = False
|
|
||||||
|
|
||||||
async def _update_async(self):
|
async def _update_async(self):
|
||||||
while True:
|
while True:
|
||||||
if self.rate <= 0:
|
if self.blinkRate == None or self.blinkRate <= 0:
|
||||||
await asyncio.sleep_ms(200)
|
await asyncio.sleep_ms(200)
|
||||||
else:
|
else:
|
||||||
self.toggle()
|
self.pin.duty(self.pwmDuty if self.blinkOn else 0)
|
||||||
await asyncio.sleep_ms(int(500 / self.rate))
|
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))
|
||||||
|
|
||||||
def toggle(self):
|
def toggle(self):
|
||||||
self.pin.value(not self.pin.value())
|
self.onState = not self.onState
|
||||||
|
self.value(self.onState)
|
||||||
|
|
||||||
def flash(self, rate):
|
def setOnState(self, blinkRate = None, pwmDuty = 1023, ):
|
||||||
"""rate:\t flashing speed in Herz"""
|
self.blinkRateOn = blinkRate
|
||||||
self.rate = rate
|
self.pwmDutyOn = pwmDuty
|
||||||
|
|
||||||
def on(self,overwriteFlashing=True):
|
def setOffState(self, blinkRate = None, pwmDuty = 0):
|
||||||
self.pin.on()
|
self.blinkRateOff = blinkRate
|
||||||
if overwriteFlashing:
|
self.pwmDutyOff = pwmDuty
|
||||||
self.rate = 0
|
|
||||||
|
|
||||||
def off(self,overwriteFlashing=True):
|
def on(self):
|
||||||
self.pin.off()
|
self.value(True)
|
||||||
if overwriteFlashing:
|
|
||||||
self.rate = 0
|
def off(self):
|
||||||
|
self.value(False)
|
||||||
|
|
||||||
def value(self,value=None):
|
def value(self,value=None):
|
||||||
if value!=None:
|
if value!=None:
|
||||||
self.pin.value(value)
|
self.onState = value
|
||||||
return self.pin.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)
|
||||||
|
return self.onState
|
||||||
|
|
24
Screens.py
24
Screens.py
@ -46,17 +46,20 @@ class ClockScreen():
|
|||||||
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
|
||||||
|
HW.leds[2].setOffState(pwmDuty=200 if Settings.alarmOn else 0)
|
||||||
Settings.save()
|
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))
|
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):
|
def init(self):
|
||||||
|
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))
|
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):
|
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))
|
b.setCallbacks(onPushDown=lambda i=i:self.onButtonPressed(i),onPushUp=lambda pushDownTime,button=i:self.onButtonReleased(button,pushDownTime))
|
||||||
self.clock.start()
|
self.clock.start()
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
|
HW.leds[2].setOffState()
|
||||||
self.clock.stop()
|
self.clock.stop()
|
||||||
|
|
||||||
class InitHandsScreen():
|
class InitHandsScreen():
|
||||||
@ -218,7 +221,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].flash(1)
|
HW.leds[1].setOnState(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),\
|
||||||
@ -232,6 +235,7 @@ class Get2PositionsScreen():
|
|||||||
await asyncio.sleep_ms(200)
|
await asyncio.sleep_ms(200)
|
||||||
if self.onPicked:
|
if self.onPicked:
|
||||||
self.onPicked(self.pickedTime)
|
self.onPicked(self.pickedTime)
|
||||||
|
HW.leds[1].setOnState() # Reset to default
|
||||||
|
|
||||||
__iter__ = __await__ # https://github.com/micropython/micropython/issues/2678
|
__iter__ = __await__ # https://github.com/micropython/micropython/issues/2678
|
||||||
def finish(self):
|
def finish(self):
|
||||||
@ -242,17 +246,15 @@ class Get2PositionsScreen():
|
|||||||
l.off()
|
l.off()
|
||||||
|
|
||||||
def onButtonPressed(self,button):
|
def onButtonPressed(self,button):
|
||||||
if button == 1:
|
HW.leds[button].on()
|
||||||
HW.leds[button].on(overwriteFlashing=False)
|
if button != 1:
|
||||||
else:
|
|
||||||
HW.leds[button].on()
|
|
||||||
if self.mode == 0:
|
if self.mode == 0:
|
||||||
HW.stepperminu.rotateTo(direction=(1 if button == 0 else -1))
|
HW.stepperminu.rotateTo(direction=(1 if button == 0 else -1))
|
||||||
elif self.mode == 1:
|
elif self.mode == 1:
|
||||||
HW.stepperhour.rotateTo(direction=(1 if button == 0 else -1))
|
HW.stepperhour.rotateTo(direction=(1 if button == 0 else -1))
|
||||||
|
|
||||||
def onButtonReleased(self,button,time):
|
def onButtonReleased(self,button,time):
|
||||||
HW.leds[button].off(overwriteFlashing=(button!=1))
|
HW.leds[button].off()
|
||||||
if button == 1:
|
if button == 1:
|
||||||
self.mode=(1+self.mode )% 2
|
self.mode=(1+self.mode )% 2
|
||||||
if time > 700:
|
if time > 700:
|
||||||
@ -282,7 +284,7 @@ class GetTimeScreen():
|
|||||||
return (int(self.pickedTime/60),self.pickedTime%60)
|
return (int(self.pickedTime/60),self.pickedTime%60)
|
||||||
def __await__(self):
|
def __await__(self):
|
||||||
self.running = True
|
self.running = True
|
||||||
HW.leds[1].flash(1)
|
HW.leds[1].setOnState(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,14 +317,12 @@ class GetTimeScreen():
|
|||||||
l.off()
|
l.off()
|
||||||
|
|
||||||
def onButtonPressed(self,button):
|
def onButtonPressed(self,button):
|
||||||
if button == 1:
|
HW.leds[button].on()
|
||||||
HW.leds[button].on(overwriteFlashing=False)
|
if button != 1:
|
||||||
else:
|
|
||||||
HW.leds[button].on()
|
|
||||||
self.direction = (-1 if button == 0 else 1)
|
self.direction = (-1 if button == 0 else 1)
|
||||||
|
|
||||||
def onButtonReleased(self,button,time):
|
def onButtonReleased(self,button,time):
|
||||||
HW.leds[button].off(overwriteFlashing=(button!=1))
|
HW.leds[button].off()
|
||||||
if button == 1:
|
if button == 1:
|
||||||
if time > 700:
|
if time > 700:
|
||||||
self.finish()
|
self.finish()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user