summaryrefslogtreecommitdiffstatshomepage
path: root/examples/hwapi/soft_pwm2_uasyncio.py
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-11-14 01:30:51 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-11-14 01:37:27 +0300
commitbf318801d2fafa2786e74693d19a3c2fdb50ddda (patch)
tree5c47220f15b13c47c1963e4381552d9bc61a0f15 /examples/hwapi/soft_pwm2_uasyncio.py
parent8212773adb69309639833395bae8df487bd15c66 (diff)
downloadmicropython-bf318801d2fafa2786e74693d19a3c2fdb50ddda.tar.gz
micropython-bf318801d2fafa2786e74693d19a3c2fdb50ddda.zip
examples/hwapi: Add uasyncio example of fading 2 LEDs in parallel.
Diffstat (limited to 'examples/hwapi/soft_pwm2_uasyncio.py')
-rw-r--r--examples/hwapi/soft_pwm2_uasyncio.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/examples/hwapi/soft_pwm2_uasyncio.py b/examples/hwapi/soft_pwm2_uasyncio.py
new file mode 100644
index 0000000000..abeb4b1bfc
--- /dev/null
+++ b/examples/hwapi/soft_pwm2_uasyncio.py
@@ -0,0 +1,31 @@
+# Like soft_pwm_uasyncio.py, but fading 2 LEDs with different phase.
+# Also see original soft_pwm.py.
+import uasyncio
+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)
+ 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.create_task(fade_in_out(LED))
+loop.call_later_ms_(800, fade_in_out(LED2))
+loop.run_forever()