examples/hwapi: Add soft_pwm example converted to uasyncio.

This commit is contained in:
Paul Sokolovsky 2016-11-13 17:00:24 +03:00
parent 30cfdc29ed
commit 99e5badeb1
1 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,28 @@
# See original soft_pwm.py for detailed comments.
import uasyncio
from hwconfig import LED
async def pwm_cycle(led, duty, cycles):
duty_off = 20 - duty
for i in range(cycles):
if duty:
led.value(1)
await uasyncio.sleep_ms(duty)
if duty_off:
led.value(0)
await uasyncio.sleep_ms(duty_off)
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)
loop = uasyncio.get_event_loop()
loop.run_until_complete(fade_in_out(LED))