diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/embedding/Makefile.upylib | 3 | ||||
-rw-r--r-- | examples/embedding/README.md (renamed from examples/embedding/README) | 33 | ||||
-rw-r--r-- | examples/embedding/hello-embed.c | 6 | ||||
-rw-r--r-- | examples/embedding/mpconfigport_minimal.h | 2 | ||||
-rw-r--r-- | examples/hwapi/button_reaction.py | 8 | ||||
-rw-r--r-- | examples/hwapi/hwconfig_dragonboard410c.py | 6 | ||||
-rw-r--r-- | examples/hwapi/hwconfig_esp8266_esp12.py | 6 | ||||
-rw-r--r-- | examples/hwapi/hwconfig_pyboard.py | 13 | ||||
-rw-r--r-- | examples/hwapi/hwconfig_z_96b_carbon.py | 9 | ||||
-rw-r--r-- | examples/hwapi/hwconfig_z_frdm_k64f.py | 4 | ||||
-rw-r--r-- | examples/hwapi/soft_pwm.py | 4 | ||||
-rw-r--r-- | examples/hwapi/soft_pwm2_uasyncio.py | 2 |
12 files changed, 58 insertions, 38 deletions
diff --git a/examples/embedding/Makefile.upylib b/examples/embedding/Makefile.upylib index d8dbade3f..873c0fd34 100644 --- a/examples/embedding/Makefile.upylib +++ b/examples/embedding/Makefile.upylib @@ -14,13 +14,12 @@ INC += -I. INC += -I.. INC += -I$(MPTOP) INC += -I$(MPTOP)/unix -#INC += -I../lib/timeutils INC += -I$(BUILD) # compiler settings CWARN = -Wall -Werror CWARN += -Wpointer-arith -Wuninitialized -CFLAGS = $(INC) $(CWARN) -ansi -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) +CFLAGS = $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) # Debugging/Optimization ifdef DEBUG diff --git a/examples/embedding/README b/examples/embedding/README.md index 0475e8739..989ce1fc8 100644 --- a/examples/embedding/README +++ b/examples/embedding/README.md @@ -11,7 +11,7 @@ Python statement which prints to the standard output. Building the example -------------------- -Build the example is as simple as running: +Building the example is as simple as running: make @@ -20,37 +20,38 @@ It's worth to trace what's happening behind the scenes though: 1. As a first step, a MicroPython library is built. This is handled by a seperate makefile, Makefile.upylib. It is more or less complex, but the good news is that you won't need to change anything in it, just use it -as is, the main Makefile shows how. What may need editing though is -MicroPython configuration file. MicroPython is highly configurable, so +as is, the main Makefile shows how. What may require editing though is +a MicroPython configuration file. MicroPython is highly configurable, so you would need to build a library suiting your application well, while not bloating its size. Check the options in the file "mpconfigport.h". -Included is a copy of "minimal" Unix port, which should be good start -for minimal embedding. For list of all available options, see py/mpconfig.h. +Included is a copy of the "minimal" Unix port, which should be a good start +for minimal embedding. For the list of all available options, see +py/mpconfig.h. -2. Once the library is built, your application is compiled and linked with -the MicroPython library produced in the previous step. The main Makefile -is very simple and shows that changes you would need to do to your -application's Makefile (or other build configuration) are also simple: +2. Once the MicroPython library is built, your application is compiled +and linked it. The main Makefile is very simple and shows that the changes +you would need to do to your application's Makefile (or other build +configuration) are also simple: -a) You would need to use C99 standard (you're using 15+ years old standard -already, not a 25+ years old one, right?). +a) You would need to use C99 standard (you're using this 15+ years old +standard already, not a 25+ years old one, right?). -b) You need to provide path to MicroPython's top-level dir, for includes. +b) You need to provide a path to MicroPython's top-level dir, for includes. c) You need to include -DNO_QSTR compile-time flag. -d) Otherwise, just link with micropython library produced in step 1. +d) Otherwise, just link with the MicroPython library produced in step 1. Out of tree build ----------------- -This example set up to work out of the box, being part of the MicroPython +This example is set up to work out of the box, being part of the MicroPython tree. Your application of course will be outside of its tree, but the only thing you need to do is to pass MPTOP variable pointing to MicroPython directory to both Makefiles (in this example, the main Makefile -automatically pass it to Makefile.upylib; in your own Makefile, don't forget -to use suitable value). +automatically passes it to Makefile.upylib; in your own Makefile, don't forget +to use a suitable value). A practical way to embed MicroPython in your application is to include it as a git submodule. Suppose you included it as libs/micropython. Then in diff --git a/examples/embedding/hello-embed.c b/examples/embedding/hello-embed.c index 194930518..e3a484783 100644 --- a/examples/embedding/hello-embed.c +++ b/examples/embedding/hello-embed.c @@ -35,9 +35,10 @@ static char heap[16384]; -mp_obj_t execute_from_lexer(mp_lexer_t *lex) { +mp_obj_t execute_from_str(const char *str) { nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { + mp_lexer_t *lex = mp_lexer_new_from_str_len(0/*MP_QSTR_*/, str, strlen(str), false); mp_parse_tree_t pt = mp_parse(lex, MP_PARSE_FILE_INPUT); mp_obj_t module_fun = mp_compile(&pt, lex->source_name, MP_EMIT_OPT_NONE, false); mp_call_function_0(module_fun); @@ -58,8 +59,7 @@ int main() { mp_init(); const char str[] = "print('Hello world of easy embedding!')"; - mp_lexer_t *lex = mp_lexer_new_from_str_len(0/*MP_QSTR_*/, str, strlen(str), false); - if (execute_from_lexer(lex)) { + if (execute_from_str(str)) { printf("Error\n"); } } diff --git a/examples/embedding/mpconfigport_minimal.h b/examples/embedding/mpconfigport_minimal.h index 48d200858..d16827fa3 100644 --- a/examples/embedding/mpconfigport_minimal.h +++ b/examples/embedding/mpconfigport_minimal.h @@ -119,8 +119,6 @@ typedef int mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size #endif -#define BYTES_PER_WORD sizeof(mp_int_t) - // Cannot include <sys/types.h>, as it may lead to symbol name clashes #if _FILE_OFFSET_BITS == 64 && !defined(__LP64__) typedef long long mp_off_t; diff --git a/examples/hwapi/button_reaction.py b/examples/hwapi/button_reaction.py index 5e1890d5a..b72e813e4 100644 --- a/examples/hwapi/button_reaction.py +++ b/examples/hwapi/button_reaction.py @@ -11,9 +11,9 @@ 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: + delay = machine.time_pulse_us(BUTTON, 1, 10*1000*1000) + if delay < 0: print("Well, you're *really* slow") + else: + print("You are as slow as %d microseconds!" % delay) utime.sleep_ms(10) diff --git a/examples/hwapi/hwconfig_dragonboard410c.py b/examples/hwapi/hwconfig_dragonboard410c.py index 8061d7b74..eec358203 100644 --- a/examples/hwapi/hwconfig_dragonboard410c.py +++ b/examples/hwapi/hwconfig_dragonboard410c.py @@ -1,4 +1,4 @@ -from machine import Pin +from machine import Pin, Signal # 96Boards/Qualcomm DragonBoard 410c # @@ -13,10 +13,10 @@ from machine import Pin # echo -n "gpio_keys" >/sys/class/input/input1/device/driver/unbind # User LED 1 on gpio21 -LED = Pin(21, Pin.OUT) +LED = Signal(Pin(21, Pin.OUT)) # User LED 2 on gpio120 -LED2 = Pin(120, Pin.OUT) +LED2 = Signal(Pin(120, Pin.OUT)) # Button S3 on gpio107 BUTTON = Pin(107, Pin.IN) diff --git a/examples/hwapi/hwconfig_esp8266_esp12.py b/examples/hwapi/hwconfig_esp8266_esp12.py index e8cf2d12e..2e855ee3d 100644 --- a/examples/hwapi/hwconfig_esp8266_esp12.py +++ b/examples/hwapi/hwconfig_esp8266_esp12.py @@ -1,5 +1,5 @@ -from machine import Pin +from machine import Pin, Signal # ESP12 module as used by many boards -# Blue LED on pin 2 -LED = Pin(2, Pin.OUT) +# Blue LED on pin 2, active low (inverted) +LED = Signal(2, Pin.OUT, invert=True) diff --git a/examples/hwapi/hwconfig_pyboard.py b/examples/hwapi/hwconfig_pyboard.py new file mode 100644 index 000000000..fb260033e --- /dev/null +++ b/examples/hwapi/hwconfig_pyboard.py @@ -0,0 +1,13 @@ +from machine import Pin, Signal + +# Red LED on pin LED_RED also kown as A13 +LED = Signal('LED_RED', Pin.OUT) + +# Green LED on pin LED_GREEN also known as A14 +LED2 = Signal('LED_GREEN', Pin.OUT) + +# Yellow LED on pin LED_YELLOW also known as A15 +LED3 = Signal('LED_YELLOW', Pin.OUT) + +# Blue LED on pin LED_BLUE also known as B4 +LED4 = Signal('LED_BLUE', Pin.OUT) diff --git a/examples/hwapi/hwconfig_z_96b_carbon.py b/examples/hwapi/hwconfig_z_96b_carbon.py new file mode 100644 index 000000000..97fd57a07 --- /dev/null +++ b/examples/hwapi/hwconfig_z_96b_carbon.py @@ -0,0 +1,9 @@ +from machine import Signal + +# 96Boards Carbon board +# USR1 - User controlled led, connected to PD2 +# USR2 - User controlled led, connected to PA15 +# BT - Bluetooth indicator, connected to PB5. +# Note - 96b_carbon uses (at the time of writing) non-standard +# for Zephyr port device naming convention. +LED = Signal(("GPIOA", 15), Pin.OUT) diff --git a/examples/hwapi/hwconfig_z_frdm_k64f.py b/examples/hwapi/hwconfig_z_frdm_k64f.py index c45e3e756..377c63878 100644 --- a/examples/hwapi/hwconfig_z_frdm_k64f.py +++ b/examples/hwapi/hwconfig_z_frdm_k64f.py @@ -1,5 +1,5 @@ -from machine import Pin +from machine import Pin, Signal # Freescale/NXP FRDM-K64F board # Blue LED on port B, pin 21 -LED = Pin(("GPIO_1", 21), Pin.OUT) +LED = Signal(("GPIO_1", 21), Pin.OUT) diff --git a/examples/hwapi/soft_pwm.py b/examples/hwapi/soft_pwm.py index a9a556171..72291b0ec 100644 --- a/examples/hwapi/soft_pwm.py +++ b/examples/hwapi/soft_pwm.py @@ -14,10 +14,10 @@ def pwm_cycle(led, duty, cycles): duty_off = 20 - duty for i in range(cycles): if duty: - led.value(1) + led.on() utime.sleep_ms(duty) if duty_off: - led.value(0) + led.off() utime.sleep_ms(duty_off) diff --git a/examples/hwapi/soft_pwm2_uasyncio.py b/examples/hwapi/soft_pwm2_uasyncio.py index abeb4b1bf..908ef2d8a 100644 --- a/examples/hwapi/soft_pwm2_uasyncio.py +++ b/examples/hwapi/soft_pwm2_uasyncio.py @@ -27,5 +27,5 @@ async def fade_in_out(LED): loop = uasyncio.get_event_loop() loop.create_task(fade_in_out(LED)) -loop.call_later_ms_(800, fade_in_out(LED2)) +loop.call_later_ms(800, fade_in_out(LED2)) loop.run_forever() |