diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/accellog.py | 2 | ||||
| -rw-r--r-- | examples/embedding/mpconfigport_minimal.h | 1 | ||||
| -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 | ||||
| -rw-r--r-- | examples/network/http_client.py | 2 |
9 files changed, 113 insertions, 2 deletions
diff --git a/examples/accellog.py b/examples/accellog.py index bb711b05f..b1f289f8a 100644 --- a/examples/accellog.py +++ b/examples/accellog.py @@ -5,7 +5,7 @@ import pyb accel = pyb.Accel() # create object of accelerometer blue = pyb.LED(4) # create object of blue LED -log = open('1:/log.csv', 'w') # open file to write data - 1:/ ist the SD-card, 0:/ the internal memory +log = open('/sd/log.csv', 'w') # open file to write data - /sd/ is the SD-card, /flash/ the internal memory blue.on() # turn on blue LED for i in range(100): # do 100 times (if the board is connected via USB, you can't write longer because the PC tries to open the filesystem which messes up your file.) diff --git a/examples/embedding/mpconfigport_minimal.h b/examples/embedding/mpconfigport_minimal.h index 90011b3f8..48d200858 100644 --- a/examples/embedding/mpconfigport_minimal.h +++ b/examples/embedding/mpconfigport_minimal.h @@ -33,6 +33,7 @@ #define MICROPY_COMP_CONST (0) #define MICROPY_MEM_STATS (0) #define MICROPY_DEBUG_PRINTERS (0) +#define MICROPY_READER_POSIX (1) #define MICROPY_HELPER_REPL (1) #define MICROPY_HELPER_LEXER_UNIX (1) #define MICROPY_ENABLE_SOURCE_LINE (0) diff --git a/examples/hwapi/button_led.py b/examples/hwapi/button_led.py new file mode 100644 index 000000000..bd6fe0172 --- /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 000000000..5e1890d5a --- /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 000000000..4b0b0d79b --- /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 00f21a658..8061d7b74 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 000000000..abeb4b1bf --- /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 000000000..8d7ad8c9e --- /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)) diff --git a/examples/network/http_client.py b/examples/network/http_client.py index b245a721a..0791c8066 100644 --- a/examples/network/http_client.py +++ b/examples/network/http_client.py @@ -19,7 +19,7 @@ def main(use_stream=False): # directly, but the line below is needed for CPython. s = s.makefile("rwb", 0) s.write(b"GET / HTTP/1.0\r\n\r\n") - print(s.readall()) + print(s.read()) else: s.send(b"GET / HTTP/1.0\r\n\r\n") print(s.recv(4096)) |
