diff options
Diffstat (limited to 'examples/hwapi')
-rw-r--r-- | examples/hwapi/button_led.py | 9 | ||||
-rw-r--r-- | examples/hwapi/button_reaction.py | 19 | ||||
-rw-r--r-- | examples/hwapi/hwconfig_console.py | 13 | ||||
-rw-r--r-- | examples/hwapi/hwconfig_dragonboard410c.py | 10 | ||||
-rw-r--r-- | examples/hwapi/soft_pwm2_uasyncio.py | 31 | ||||
-rw-r--r-- | examples/hwapi/soft_pwm_uasyncio.py | 28 |
6 files changed, 110 insertions, 0 deletions
diff --git a/examples/hwapi/button_led.py b/examples/hwapi/button_led.py new file mode 100644 index 0000000000..bd6fe01729 --- /dev/null +++ b/examples/hwapi/button_led.py @@ -0,0 +1,9 @@ +import utime +from hwconfig import LED, BUTTON + +# Light LED when (and while) a BUTTON is pressed + +while 1: + LED.value(BUTTON.value()) + # Don't burn CPU + utime.sleep_ms(10) diff --git a/examples/hwapi/button_reaction.py b/examples/hwapi/button_reaction.py new file mode 100644 index 0000000000..5e1890d5aa --- /dev/null +++ b/examples/hwapi/button_reaction.py @@ -0,0 +1,19 @@ +import utime +import machine +from hwconfig import LED, BUTTON + +# machine.time_pulse_us() function demo + +print("""\ +Let's play an interesting game: +You click button as fast as you can, and I tell you how slow you are. +Ready? Cliiiiick! +""") + +while 1: + try: + delay = machine.time_pulse_us(BUTTON, 1, 10*1000*1000) + print("You are as slow as %d microseconds!" % delay) + except OSError: + print("Well, you're *really* slow") + utime.sleep_ms(10) diff --git a/examples/hwapi/hwconfig_console.py b/examples/hwapi/hwconfig_console.py new file mode 100644 index 0000000000..4b0b0d79bd --- /dev/null +++ b/examples/hwapi/hwconfig_console.py @@ -0,0 +1,13 @@ +# This is hwconfig for "emulation" for cases when there's no real hardware. +# It just prints information to console. +class LEDClass: + + def __init__(self, id): + self.id = "LED(%d):" % id + + def value(self, v): + print(self.id, v) + + +LED = LEDClass(1) +LED2 = LEDClass(12) diff --git a/examples/hwapi/hwconfig_dragonboard410c.py b/examples/hwapi/hwconfig_dragonboard410c.py index 00f21a658f..8061d7b743 100644 --- a/examples/hwapi/hwconfig_dragonboard410c.py +++ b/examples/hwapi/hwconfig_dragonboard410c.py @@ -1,12 +1,22 @@ from machine import Pin # 96Boards/Qualcomm DragonBoard 410c +# # By default, on-board LEDs are controlled by kernel LED driver. # To make corresponding pins be available as normal GPIO, # corresponding driver needs to be unbound first (as root): # echo -n "soc:leds" >/sys/class/leds/apq8016-sbc:green:user1/device/driver/unbind # Note that application also either should be run as root, or # /sys/class/gpio ownership needs to be changed. +# Likewise, onboard buttons are controlled by gpio_keys driver. +# To release corresponding GPIOs: +# echo -n "gpio_keys" >/sys/class/input/input1/device/driver/unbind # User LED 1 on gpio21 LED = Pin(21, Pin.OUT) + +# User LED 2 on gpio120 +LED2 = Pin(120, Pin.OUT) + +# Button S3 on gpio107 +BUTTON = Pin(107, Pin.IN) 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() 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)) |