summaryrefslogtreecommitdiffstatshomepage
path: root/examples/hwapi
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-11-13 17:00:24 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-11-13 17:00:24 +0300
commit99e5badeb1e2e6aeffd3b3d56902d4257e168326 (patch)
tree479c2b90d34ad3feecf5a600b79386127af9f9df /examples/hwapi
parent30cfdc29edc7415f7d63be108f3312389c2df202 (diff)
downloadmicropython-99e5badeb1e2e6aeffd3b3d56902d4257e168326.tar.gz
micropython-99e5badeb1e2e6aeffd3b3d56902d4257e168326.zip
examples/hwapi: Add soft_pwm example converted to uasyncio.
Diffstat (limited to 'examples/hwapi')
-rw-r--r--examples/hwapi/soft_pwm_uasyncio.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/examples/hwapi/soft_pwm_uasyncio.py b/examples/hwapi/soft_pwm_uasyncio.py
new file mode 100644
index 0000000000..8d7ad8c9e0
--- /dev/null
+++ b/examples/hwapi/soft_pwm_uasyncio.py
@@ -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))