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