2023-06-08 07:11:49 +01:00
|
|
|
# Like soft_pwm_asyncio.py, but fading 2 LEDs with different phase.
|
2016-11-13 22:30:51 +00:00
|
|
|
# Also see original soft_pwm.py.
|
2023-06-08 07:11:49 +01:00
|
|
|
import asyncio
|
2016-11-13 22:30:51 +00:00
|
|
|
from hwconfig import LED, LED2
|
|
|
|
|
|
|
|
|
|
|
|
async def pwm_cycle(led, duty, cycles):
|
|
|
|
duty_off = 20 - duty
|
|
|
|
for i in range(cycles):
|
|
|
|
if duty:
|
|
|
|
led.value(1)
|
2023-06-08 07:11:49 +01:00
|
|
|
await asyncio.sleep_ms(duty)
|
2016-11-13 22:30:51 +00:00
|
|
|
if duty_off:
|
|
|
|
led.value(0)
|
2023-06-08 07:11:49 +01:00
|
|
|
await asyncio.sleep_ms(duty_off)
|
2016-11-13 22:30:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def fade_in_out(LED):
|
|
|
|
while True:
|
|
|
|
# Fade in
|
|
|
|
for i in range(1, 21):
|
|
|
|
await pwm_cycle(LED, i, 2)
|
|
|
|
# Fade out
|
|
|
|
for i in range(20, 0, -1):
|
|
|
|
await pwm_cycle(LED, i, 2)
|
|
|
|
|
|
|
|
|
2023-06-08 07:11:49 +01:00
|
|
|
loop = asyncio.get_event_loop()
|
2016-11-13 22:30:51 +00:00
|
|
|
loop.create_task(fade_in_out(LED))
|
2017-05-16 22:42:26 +01:00
|
|
|
loop.call_later_ms(800, fade_in_out(LED2))
|
2016-11-13 22:30:51 +00:00
|
|
|
loop.run_forever()
|