You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
774 B
32 lines
774 B
1 year ago
|
# Like soft_pwm_asyncio.py, but fading 2 LEDs with different phase.
|
||
8 years ago
|
# Also see original soft_pwm.py.
|
||
1 year ago
|
import asyncio
|
||
8 years ago
|
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)
|
||
1 year ago
|
await asyncio.sleep_ms(duty)
|
||
8 years ago
|
if duty_off:
|
||
|
led.value(0)
|
||
1 year ago
|
await asyncio.sleep_ms(duty_off)
|
||
8 years ago
|
|
||
|
|
||
|
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)
|
||
|
|
||
|
|
||
1 year ago
|
loop = asyncio.get_event_loop()
|
||
8 years ago
|
loop.create_task(fade_in_out(LED))
|
||
8 years ago
|
loop.call_later_ms(800, fade_in_out(LED2))
|
||
8 years ago
|
loop.run_forever()
|