summaryrefslogtreecommitdiff
path: root/examples/hwapi/soft_pwm_asyncio.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/hwapi/soft_pwm_asyncio.py')
-rw-r--r--examples/hwapi/soft_pwm_asyncio.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/examples/hwapi/soft_pwm_asyncio.py b/examples/hwapi/soft_pwm_asyncio.py
new file mode 100644
index 000000000..ae6c7340f
--- /dev/null
+++ b/examples/hwapi/soft_pwm_asyncio.py
@@ -0,0 +1,28 @@
+# See original soft_pwm.py for detailed comments.
+import asyncio
+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 asyncio.sleep_ms(duty)
+ if duty_off:
+ led.value(0)
+ await asyncio.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 = asyncio.get_event_loop()
+loop.run_until_complete(fade_in_out(LED))