From d9243b0928849531fa8e7b2b5b9befe69ffa42c7 Mon Sep 17 00:00:00 2001 From: Tomas Krejci Date: Tue, 21 May 2024 17:45:07 +0200 Subject: [PATCH] up rgb --- lib/btn.py | 97 +++++++++++++++++++++++++++++++++++ lib/rgb.py | 145 ++++++++++++++++++++++++++++++++++------------------- 2 files changed, 190 insertions(+), 52 deletions(-) create mode 100644 lib/btn.py diff --git a/lib/btn.py b/lib/btn.py new file mode 100644 index 0000000..edee4c2 --- /dev/null +++ b/lib/btn.py @@ -0,0 +1,97 @@ +# btn.py + +import machine +from sys import platform +ESP32 = platform == 'esp32' +RP2 = platform == 'rp2' +PYBOARD = platform == 'pyboard' +if ESP32 or RP2: + from machine import Pin +else: + from pyb import Pin, LED +import uasyncio as asyncio +#from primitives.switch import Switch +from primitives.delay_ms import Delay_ms +import primitives.queue as queue +from primitives import Switch, Pushbutton +import rgb.py as rgb + + +# Settings +#led = machine.Pin(25, machine.Pin.OUT) +led = machine.Pin("LED", machine.Pin.OUT) # Pi Pico onboard LED +btn = machine.Pin(18, machine.Pin.IN, machine.Pin.PULL_UP) + + +# Pulse an LED (coroutine) +async def pulse(led, ms): + led.on() + await asyncio.sleep_ms(ms) + led.off() + + +# Pulse an LED when an event triggered +async def evt_pulse(event, led): + while True: + event.clear() + await event.wait() + led.on() + await asyncio.sleep_ms(500) + led.off() + + +# Toggle an LED (callback) +def toggle(led): + led.toggle() + + +# Quit test by connecting X2 to ground +async def killer(obj): + pin = btn + while pin.value(): + await asyncio.sleep_ms(50) + obj.deinit() + await asyncio.sleep_ms(0) + + + +# Test for the Pushbutton class (events) +async def do_btn_event(): + led.off() + pb = Pushbutton(btn) + pb.press_func(None) + pb.release_func(None) + pb.double_func(None) + pb.long_func(None) + #tasks = [] + #for event, led in ((pb.press, 1), (pb.release, 2), (pb.double, 3), (pb.long, 4)): + #tasks.append(asyncio.create_task(evt_pulse(pb.press, led))) + #tasks.append(asyncio.create_task(evt_pulse(pb.double, led))) + + asyncio.create_task(evt_pulse(pb.double, led)) + + #await killer(pb) + #for task in tasks: + # task.cancel() + + while True: + await asyncio.sleep_ms(100) + + + + +print("Test of pushbutton triggering events.") +print(""" +press pulse red +release pulses green +double click pulses yellow +long press pulses blue +""") +try: + asyncio.run(do_btn_event()) +except KeyboardInterrupt: + print("Interrupted") +finally: + asyncio.new_event_loop() + print('tests complete') + diff --git a/lib/rgb.py b/lib/rgb.py index 29af8b0..913e7e1 100644 --- a/lib/rgb.py +++ b/lib/rgb.py @@ -7,67 +7,108 @@ import machine import uasyncio as asyncio from machine import Pin, PWM -led_r = PWM(Pin(22), 5000) -led_g = PWM(Pin(26), 5000) -led_b = PWM(Pin(27), 5000) +class RGB: + #rgb_pwr = 1.0 + def __init__(self): + global rgb_pwr + rgb_pwr = 0.2 # 0.0 - 1.0 -rgb_pwr = 0.1 # 0.0 - 1.0 + self.led_r = PWM(Pin(22), 100) + self.led_g = PWM(Pin(26), 100) + self.led_b = PWM(Pin(27), 100) -async def init(): - led_r.duty_u16(0) - led_g.duty_u16(0) - led_b.duty_u16(0) + async def set(self, r, g, b): + global rgb_pwr + self.led_r.duty_u16(int(65535 * r * rgb_pwr)) + self.led_g.duty_u16(int(65535 * g * rgb_pwr)) + self.led_b.duty_u16(int(65535 * b * rgb_pwr)) + await uasyncio.sleep(0.01) -async def set_pwr(pwr): - global rgb_pwr - rgb_pwr = pwr + async def on(self): + await self.set(1, 1, 1) + async def off(self): + await self.set(0, 0, 0) -async def set(r, g, b): - led_r.duty_u16(int(65535 * r * rgb_pwr)) - led_g.duty_u16(int(65535 * g * rgb_pwr)) - led_b.duty_u16(int(65535 * b * rgb_pwr)) - await uasyncio.sleep(0.01) + async def set_pwr(self, pwr): + global rgb_pwr + rgb_pwr = pwr + async def get_pwr(self): + global rgb_pwr + return rgb_pwr - -async def on(): - led_r.duty_u16(65535 * rgb_pwr) # 65535 = on - led_b.duty_u16(65535 * rgb_pwr) # 65535 = on - led_g.duty_u16(65535 * rgb_pwr) # 65535 = on - -async def off(): - led_r.duty_u16(0) # 0 = off - led_g.duty_u16(0) # 0 = off - led_b.duty_u16(0) # 0 = off - -async def test(): - print("RGB demo") - print("Press Ctrl-C to exit") - await uasyncio.sleep(1) - print("RGB power:", rgb_pwr) - print("RGB initializing...") - await init() - await uasyncio.sleep(1) - while True: - print("RGB off") - await set(0, 0, 0) - await uasyncio.sleep(1) - print("Red on") - await set(1, 0, 0) - await uasyncio.sleep(1) - print("Green on") - await set(0, 1, 0) - await uasyncio.sleep(1) - print("Blue on") - await set(0, 0, 1) - await uasyncio.sleep(1) - print("RGB on") - await set(1, 1, 1) - await uasyncio.sleep(1) -asyncio.run(test()) + async def set_pwr_up(self): + await self.set_pwr(await self.get_pwr() * 2) + + async def set_pwr_down(self): + await self.set_pwr((await self.get_pwr()) * 0.5) + + async def set_red(self): + await self.set(1, 0, 0) + + async def set_green(self): + await self.set(0, 1, 0) + + async def set_blue(self): + await self.set(0, 0, 1) + + async def set_yellow(self): + await self.set(1, 1, 0) + + async def set_magenta(self): + await self.set(1, 0, 1) + + async def set_cyan(self): + await self.set(0, 1, 1) + + async def set_white(self): + await self.set(1, 1, 1) + + async def set_orange(self): + await self.set(1, 0.5, 0) + + + + + async def test(self): + sec = 3 + print("RGB demo") + print("Press Ctrl-C to exit") + await uasyncio.sleep(sec) + print("RGB power:", self.get_pwr()) + print("RGB initializing...") + + await uasyncio.sleep(sec) + while True: + print("RGB off") + await self.off() + await uasyncio.sleep(sec) + print("Red on") + await self.set(1, 0, 0) + await uasyncio.sleep(sec) + print("Green on") + await self.set_green() + await uasyncio.sleep(sec) + print("Blue on") + await self.set_blue() + await uasyncio.sleep(sec) + print("RGB on") + await self.on() + await uasyncio.sleep(sec) + print("RGB power down") + await self.set_pwr_down() + print("RGB power:", self.get_pwr()) + await uasyncio.sleep(sec) + print("RGB power up") + await self.set_pwr_up() + print("RGB power:", self.get_pwr()) + await uasyncio.sleep(sec) + +rgb = RGB() +asyncio.run(rgb.test())