diff options
| -rw-r--r-- | atmel-samd/Makefile | 1 | ||||
| -rw-r--r-- | esp8266/Makefile | 1 | ||||
| -rw-r--r-- | lib/utils/context_manager_helpers.c | 34 | ||||
| -rw-r--r-- | lib/utils/context_manager_helpers.h | 34 | ||||
| -rw-r--r-- | shared-bindings/bitbangio/I2C.c | 8 | ||||
| -rw-r--r-- | shared-bindings/bitbangio/SPI.c | 9 | ||||
| -rw-r--r-- | shared-bindings/nativeio/AnalogIn.c | 8 | ||||
| -rw-r--r-- | shared-bindings/nativeio/AnalogOut.c | 8 | ||||
| -rw-r--r-- | shared-bindings/nativeio/DigitalInOut.c | 68 | ||||
| -rw-r--r-- | shared-bindings/nativeio/I2C.c | 8 | ||||
| -rw-r--r-- | shared-bindings/nativeio/PWMOut.c | 8 | ||||
| -rw-r--r-- | shared-bindings/nativeio/SPI.c | 8 | ||||
| -rw-r--r-- | shared-bindings/nativeio/TouchIn.c | 9 | ||||
| -rw-r--r-- | shared-bindings/nativeio/UART.c | 32 |
14 files changed, 161 insertions, 75 deletions
diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile index 675a2c23f..cbfe4fc0a 100644 --- a/atmel-samd/Makefile +++ b/atmel-samd/Makefile @@ -197,6 +197,7 @@ SRC_C = \ lib/fatfs/ff.c \ lib/fatfs/option/ccsbcs.c \ lib/timeutils/timeutils.c \ + lib/utils/context_manager_helpers.c \ lib/utils/interrupt_char.c \ lib/utils/pyexec.c \ lib/utils/pyhelp.c \ diff --git a/esp8266/Makefile b/esp8266/Makefile index f87955397..26c3bffa5 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -165,6 +165,7 @@ LIB_SRC_C = $(addprefix lib/,\ mp-readline/readline.c \ netutils/netutils.c \ timeutils/timeutils.c \ + utils/context_manager_helpers.c \ utils/pyexec.c \ utils/pyhelp.c \ utils/interrupt_char.c \ diff --git a/lib/utils/context_manager_helpers.c b/lib/utils/context_manager_helpers.c new file mode 100644 index 000000000..901c27030 --- /dev/null +++ b/lib/utils/context_manager_helpers.c @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "lib/utils/context_manager_helpers.h" + +#include "py/obj.h" + +STATIC mp_obj_t default___enter__(mp_obj_t self_in) { + return self_in; +} +MP_DEFINE_CONST_FUN_OBJ_1(default___enter___obj, default___enter__); diff --git a/lib/utils/context_manager_helpers.h b/lib/utils/context_manager_helpers.h new file mode 100644 index 000000000..7164951f1 --- /dev/null +++ b/lib/utils/context_manager_helpers.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __MICROPY_INCLUDED_LIB_UTILS_CONTEXT_MANAGER_HELPERS_H__ +#define __MICROPY_INCLUDED_LIB_UTILS_CONTEXT_MANAGER_HELPERS_H__ + +#include "py/obj.h" + +MP_DECLARE_CONST_FUN_OBJ_1(default___enter___obj); + +#endif // __MICROPY_INCLUDED_LIB_UTILS_CONTEXT_MANAGER_HELPERS_H__ diff --git a/shared-bindings/bitbangio/I2C.c b/shared-bindings/bitbangio/I2C.c index cc77dc00e..2b547d65c 100644 --- a/shared-bindings/bitbangio/I2C.c +++ b/shared-bindings/bitbangio/I2C.c @@ -30,6 +30,7 @@ #include "shared-bindings/bitbangio/I2C.h" #include "shared-bindings/microcontroller/Pin.h" +#include "lib/utils/context_manager_helpers.h" #include "py/runtime.h" //| .. currentmodule:: bitbangio //| @@ -83,10 +84,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_deinit_obj, bitbangio_i2c_obj_deinit); //| //| No-op used in Context Managers. //| -STATIC mp_obj_t bitbangio_i2c_obj___enter__(mp_obj_t self_in) { - return self_in; -} -MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c___enter___obj, bitbangio_i2c_obj___enter__); +// Provided by context manager helper. //| .. method:: I2C.__exit__() //| @@ -248,7 +246,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bitbangio_i2c_writeto_obj, 1, bitbangio_i2c_wr STATIC const mp_rom_map_elem_t bitbangio_i2c_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bitbangio_i2c_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&bitbangio_i2c___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&bitbangio_i2c_obj___exit___obj) }, { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&bitbangio_i2c_scan_obj) }, diff --git a/shared-bindings/bitbangio/SPI.c b/shared-bindings/bitbangio/SPI.c index bec6fcdaa..a5d3f358f 100644 --- a/shared-bindings/bitbangio/SPI.c +++ b/shared-bindings/bitbangio/SPI.c @@ -32,6 +32,7 @@ #include "shared-bindings/bitbangio/SPI.h" #include "shared-bindings/microcontroller/Pin.h" +#include "lib/utils/context_manager_helpers.h" #include "py/runtime.h" //| .. currentmodule:: bitbangio @@ -57,7 +58,6 @@ //| // TODO(tannewt): Support LSB SPI. -// TODO(tannewt): Support phase, polarity and bit order. STATIC mp_obj_t bitbangio_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true); bitbangio_spi_obj_t *self = m_new_obj(bitbangio_spi_obj_t); @@ -97,10 +97,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_deinit_obj, bitbangio_spi_obj_deinit); //| //| No-op used by Context Managers. //| -STATIC mp_obj_t bitbangio_spi_obj___enter__(mp_obj_t self_in) { - return self_in; -} -MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi___enter___obj, bitbangio_spi_obj___enter__); +// Provided by context manager helper. //| .. method:: SPI.__exit__() //| @@ -211,7 +208,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_spi_readinto_obj, 2, 2, bitbangio_ STATIC const mp_rom_map_elem_t bitbangio_spi_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bitbangio_spi_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&bitbangio_spi___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&bitbangio_spi_obj___exit___obj) }, { MP_ROM_QSTR(MP_QSTR_configure), MP_ROM_PTR(&bitbangio_spi_configure_obj) }, diff --git a/shared-bindings/nativeio/AnalogIn.c b/shared-bindings/nativeio/AnalogIn.c index d41ba523f..e7f46a9d5 100644 --- a/shared-bindings/nativeio/AnalogIn.c +++ b/shared-bindings/nativeio/AnalogIn.c @@ -26,6 +26,7 @@ #include <string.h> +#include "lib/utils/context_manager_helpers.h" #include "py/binary.h" #include "py/mphal.h" #include "py/nlr.h" @@ -88,10 +89,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_deinit_obj, nativeio_analogin_deinit //| //| No-op used by Context Managers. //| -STATIC mp_obj_t nativeio_analogin___enter__(mp_obj_t self_in) { - return self_in; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin___enter___obj, nativeio_analogin___enter__); +// Provided by context manager helper. //| .. method:: __exit__() //| @@ -150,7 +148,7 @@ mp_obj_property_t nativeio_analogin_reference_voltage_obj = { STATIC const mp_rom_map_elem_t nativeio_analogin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_analogin_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&nativeio_analogin___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_analogin___exit___obj) }, { MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_analogin_value_obj)}, { MP_OBJ_NEW_QSTR(MP_QSTR_reference_voltage), MP_ROM_PTR(&nativeio_analogin_reference_voltage_obj)}, diff --git a/shared-bindings/nativeio/AnalogOut.c b/shared-bindings/nativeio/AnalogOut.c index 32bd93274..950faabb5 100644 --- a/shared-bindings/nativeio/AnalogOut.c +++ b/shared-bindings/nativeio/AnalogOut.c @@ -27,6 +27,7 @@ #include <stdint.h> #include <string.h> +#include "lib/utils/context_manager_helpers.h" #include "py/objproperty.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" @@ -86,10 +87,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogout_deinit_obj, nativeio_analogo //| //| No-op used by Context Managers. //| -STATIC mp_obj_t nativeio_analogout___enter__(mp_obj_t self_in) { - return self_in; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogout___enter___obj, nativeio_analogout___enter__); +// Provided by context manager helper. //| .. method:: __exit__() //| @@ -133,7 +131,7 @@ mp_obj_property_t nativeio_analogout_value_obj = { STATIC const mp_rom_map_elem_t nativeio_analogout_locals_dict_table[] = { // instance methods { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_analogout_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&nativeio_analogout___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_analogout___exit___obj) }, // Properties diff --git a/shared-bindings/nativeio/DigitalInOut.c b/shared-bindings/nativeio/DigitalInOut.c index d06c9b5f5..b45086be1 100644 --- a/shared-bindings/nativeio/DigitalInOut.c +++ b/shared-bindings/nativeio/DigitalInOut.c @@ -27,6 +27,8 @@ #include <stdint.h> #include <string.h> +#include "lib/utils/context_manager_helpers.h" + #include "py/nlr.h" #include "py/objtype.h" #include "py/objproperty.h" @@ -84,10 +86,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_deinit_obj, nativeio_digitalinou //| //| No-op used by Context Managers. //| -STATIC mp_obj_t nativeio_digitalinout_obj___enter__(mp_obj_t self_in) { - return self_in; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout___enter___obj, nativeio_digitalinout_obj___enter__); +// Provided by context manager helper. //| .. method:: __exit__() //| @@ -101,12 +100,12 @@ STATIC mp_obj_t nativeio_digitalinout_obj___exit__(size_t n_args, const mp_obj_t STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_digitalinout_obj___exit___obj, 4, 4, nativeio_digitalinout_obj___exit__); //| -//| .. method:: switch_to_output(value=False, drive_mode=DriveMode.push_pull) +//| .. method:: switch_to_output(value=False, drive_mode=DriveMode.PUSH_PULL) //| //| Switch to writing out digital values. //| //| :param bool value: default value to set upon switching -//| :param DriveMode push_pull: drive mode for the output +//| :param DriveMode drive_mode: drive mode for the output //| typedef struct { mp_obj_base_t base; @@ -146,9 +145,9 @@ MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_digitalinout_switch_to_output_obj, 1, native //| import board //| //| with nativeio.DigitalInOut(board.SLIDE_SWITCH) as switch: -//| switch.switch_to_input(pull=nativeio.DigitalInOut.Pull.up) +//| switch.switch_to_input(pull=nativeio.DigitalInOut.Pull.UP) //| # Or, after switch_to_input -//| switch.pull = nativeio.DigitalInOut.Pull.up +//| switch.pull = nativeio.DigitalInOut.Pull.UP //| print(switch.value) //| typedef struct { @@ -332,11 +331,11 @@ mp_obj_property_t nativeio_digitalinout_pull_obj = { //| Enum-like class to define which direction the digital values are //| going. //| -//| .. data:: in +//| .. data:: IN //| //| Read digital data in //| -//| .. data:: out +//| .. data:: OUT //| //| Write digital data out //| @@ -351,14 +350,23 @@ const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_out_ }; STATIC const mp_rom_map_elem_t nativeio_digitalinout_direction_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_in), MP_ROM_PTR(&nativeio_digitalinout_direction_in_obj) }, - { MP_ROM_QSTR(MP_QSTR_out), MP_ROM_PTR(&nativeio_digitalinout_direction_out_obj) }, + { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_PTR(&nativeio_digitalinout_direction_in_obj) }, + { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_PTR(&nativeio_digitalinout_direction_out_obj) }, }; STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_direction_locals_dict, nativeio_digitalinout_direction_locals_dict_table); +STATIC void nativeio_digitalinout_direction_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + qstr direction = MP_QSTR_IN; + if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_digitalinout_direction_out_obj)) { + direction = MP_QSTR_OUT; + } + mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_DigitalInOut, MP_QSTR_Direction, direction); +} + const mp_obj_type_t nativeio_digitalinout_direction_type = { { &mp_type_type }, .name = MP_QSTR_Direction, + .print = nativeio_digitalinout_direction_print, .locals_dict = (mp_obj_t)&nativeio_digitalinout_direction_locals_dict, }; @@ -367,11 +375,11 @@ const mp_obj_type_t nativeio_digitalinout_direction_type = { //| Enum-like class to define the drive mode used when outputting //| digital values. //| -//| .. data:: push_pull +//| .. data:: PUSH_PULL //| //| Output both high and low digital values //| -//| .. data:: open_drain +//| .. data:: OPEN_DRAIN //| //| Output low digital values but go into high z for digital high. This is //| useful for i2c and other protocols that share a digital line. @@ -387,14 +395,23 @@ const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_op }; STATIC const mp_rom_map_elem_t nativeio_digitalinout_drive_mode_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_push_pull), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_push_pull_obj) }, - { MP_ROM_QSTR(MP_QSTR_open_drain), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_open_drain_obj) }, + { MP_ROM_QSTR(MP_QSTR_PUSH_PULL), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_push_pull_obj) }, + { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_open_drain_obj) }, }; STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_drive_mode_locals_dict, nativeio_digitalinout_drive_mode_locals_dict_table); +STATIC void nativeio_digitalinout_drive_mode_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + qstr drive_mode = MP_QSTR_PUSH_PULL; + if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_digitalinout_drive_mode_open_drain_obj)) { + drive_mode = MP_QSTR_OPEN_DRAIN; + } + mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_DigitalInOut, MP_QSTR_DriveMode, drive_mode); +} + const mp_obj_type_t nativeio_digitalinout_drive_mode_type = { { &mp_type_type }, .name = MP_QSTR_DriveMode, + .print = nativeio_digitalinout_drive_mode_print, .locals_dict = (mp_obj_t)&nativeio_digitalinout_drive_mode_locals_dict, }; @@ -403,12 +420,12 @@ const mp_obj_type_t nativeio_digitalinout_drive_mode_type = { //| Enum-like class to define the pull value, if any, used while reading //| digital values in. //| -//| .. data:: up +//| .. data:: UP //| //| When the input line isn't being driven the pull up can pull the state //| of the line high so it reads as true. //| -//| .. data:: down +//| .. data:: DOWN //| //| When the input line isn't being driven the pull down can pull the //| state of the line low so it reads as false. @@ -424,21 +441,30 @@ const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_down_obj = { }; STATIC const mp_rom_map_elem_t nativeio_digitalinout_pull_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_up), MP_ROM_PTR(&nativeio_digitalinout_pull_up_obj) }, - { MP_ROM_QSTR(MP_QSTR_down), MP_ROM_PTR(&nativeio_digitalinout_pull_down_obj) }, + { MP_ROM_QSTR(MP_QSTR_UP), MP_ROM_PTR(&nativeio_digitalinout_pull_up_obj) }, + { MP_ROM_QSTR(MP_QSTR_DOWN), MP_ROM_PTR(&nativeio_digitalinout_pull_down_obj) }, }; STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_pull_locals_dict, nativeio_digitalinout_pull_locals_dict_table); +STATIC void nativeio_digitalinout_pull_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + qstr pull = MP_QSTR_UP; + if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_digitalinout_pull_down_obj)) { + pull = MP_QSTR_DOWN; + } + mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_DigitalInOut, MP_QSTR_Pull, pull); +} + const mp_obj_type_t nativeio_digitalinout_pull_type = { { &mp_type_type }, .name = MP_QSTR_Pull, + .print = nativeio_digitalinout_pull_print, .locals_dict = (mp_obj_t)&nativeio_digitalinout_pull_locals_dict, }; STATIC const mp_rom_map_elem_t nativeio_digitalinout_locals_dict_table[] = { // instance methods { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_digitalinout_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&nativeio_digitalinout___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_digitalinout_obj___exit___obj) }, { MP_ROM_QSTR(MP_QSTR_switch_to_output), MP_ROM_PTR(&nativeio_digitalinout_switch_to_output_obj) }, { MP_ROM_QSTR(MP_QSTR_switch_to_input), MP_ROM_PTR(&nativeio_digitalinout_switch_to_input_obj) }, diff --git a/shared-bindings/nativeio/I2C.c b/shared-bindings/nativeio/I2C.c index 3e608dc53..bb827a95d 100644 --- a/shared-bindings/nativeio/I2C.c +++ b/shared-bindings/nativeio/I2C.c @@ -30,6 +30,7 @@ #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/nativeio/I2C.h" +#include "lib/utils/context_manager_helpers.h" #include "py/runtime.h" //| .. currentmodule:: nativeio //| @@ -93,10 +94,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_deinit_obj, nativeio_i2c_obj_deinit); //| //| No-op used in Context Managers. //| -STATIC mp_obj_t nativeio_i2c_obj___enter__(mp_obj_t self_in) { - return self_in; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c___enter___obj, nativeio_i2c_obj___enter__); +// Provided by context manager helper. //| .. method:: I2C.__exit__() //| @@ -261,7 +259,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_i2c_writeto_obj, 1, nativeio_i2c_writ STATIC const mp_rom_map_elem_t nativeio_i2c_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_i2c_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&nativeio_i2c___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_i2c___exit___obj) }, { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&nativeio_i2c_scan_obj) }, diff --git a/shared-bindings/nativeio/PWMOut.c b/shared-bindings/nativeio/PWMOut.c index 59f6e7158..330ffb187 100644 --- a/shared-bindings/nativeio/PWMOut.c +++ b/shared-bindings/nativeio/PWMOut.c @@ -26,6 +26,7 @@ #include <stdint.h> +#include "lib/utils/context_manager_helpers.h" #include "py/objproperty.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" @@ -126,10 +127,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_deinit_obj, nativeio_pwmout_dei //| //| No-op used by Context Managers. //| -STATIC mp_obj_t nativeio_pwmout_obj___enter__(mp_obj_t self_in) { - return self_in; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout___enter___obj, nativeio_pwmout_obj___enter__); +// Provided by context manager helper. //| .. method:: __exit__() //| @@ -206,7 +204,7 @@ mp_obj_property_t nativeio_pwmout_frequency_obj = { STATIC const mp_rom_map_elem_t nativeio_pwmout_locals_dict_table[] = { // Methods { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pwmout_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&nativeio_pwmout___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_pwmout___exit___obj) }, // Properties diff --git a/shared-bindings/nativeio/SPI.c b/shared-bindings/nativeio/SPI.c index e5eddb189..465334353 100644 --- a/shared-bindings/nativeio/SPI.c +++ b/shared-bindings/nativeio/SPI.c @@ -32,6 +32,7 @@ #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/nativeio/SPI.h" +#include "lib/utils/context_manager_helpers.h" #include "py/nlr.h" #include "py/runtime.h" @@ -108,10 +109,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_deinit_obj, nativeio_spi_obj_deinit); //| //| No-op used by Context Managers. //| -STATIC mp_obj_t nativeio_spi_obj___enter__(mp_obj_t self_in) { - return self_in; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi___enter___obj, nativeio_spi_obj___enter__); +// Provided by context manager helper. //| .. method:: SPI.__exit__() //| @@ -277,7 +275,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_readinto_obj, 2, nativeio_spi_readinto); STATIC const mp_rom_map_elem_t nativeio_spi_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_spi_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&nativeio_spi___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_spi_obj___exit___obj) }, { MP_ROM_QSTR(MP_QSTR_configure), MP_ROM_PTR(&nativeio_spi_configure_obj) }, diff --git a/shared-bindings/nativeio/TouchIn.c b/shared-bindings/nativeio/TouchIn.c index dd7261514..90b86f42f 100644 --- a/shared-bindings/nativeio/TouchIn.c +++ b/shared-bindings/nativeio/TouchIn.c @@ -26,6 +26,7 @@ #include <string.h> +#include "lib/utils/context_manager_helpers.h" #include "py/binary.h" #include "py/mphal.h" #include "py/nlr.h" @@ -87,10 +88,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_touchin_deinit_obj, nativeio_touchin_d //| //| No-op used by Context Managers. //| -STATIC mp_obj_t nativeio_touchin_obj___enter__(mp_obj_t self_in) { - return self_in; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_touchin___enter___obj, nativeio_touchin_obj___enter__); +// Provided by context manager helper. //| .. method:: __exit__() //| @@ -124,9 +122,8 @@ mp_obj_property_t nativeio_touchin_value_obj = { }; STATIC const mp_rom_map_elem_t nativeio_touchin_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&nativeio_touchin___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_touchin___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&nativeio_touchin_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_touchin_deinit_obj) }, { MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_touchin_value_obj)}, diff --git a/shared-bindings/nativeio/UART.c b/shared-bindings/nativeio/UART.c index c2cab4033..f4921814b 100644 --- a/shared-bindings/nativeio/UART.c +++ b/shared-bindings/nativeio/UART.c @@ -28,6 +28,8 @@ #include "shared-bindings/nativeio/UART.h" +#include "lib/utils/context_manager_helpers.h" + #include "py/ioctl.h" #include "py/runtime.h" #include "py/stream.h" @@ -49,7 +51,7 @@ //| :param ~microcontroller.Pin rx: the pin to receive on //| :param int baudrate: the transmit and receive speed /// :param int bits: the number of bits per byte, 7, 8 or 9. -/// :param int parity: the parity used for error checking, `None`, 0 (even) or 1 (odd). +/// :param Parity parity: the parity used for error checking /// :param int stop: the number of stop bits, 1 or 2. /// :param int timeout: the timeout in milliseconds to wait for the first character and between subsequent characters. /// :param int receiver_buffer_size: the character length of the read buffer (0 to disable). (When a character is 9 bits the buffer will be 2 * receiver_buffer_size bytes.) @@ -95,9 +97,9 @@ STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args, } uart_parity_t parity = PARITY_NONE; - if (args[ARG_baudrate].u_obj == &nativeio_uart_parity_even_obj) { + if (args[ARG_parity].u_obj == &nativeio_uart_parity_even_obj) { parity = PARITY_EVEN; - } else if (args[ARG_baudrate].u_obj == &nativeio_uart_parity_odd_obj) { + } else if (args[ARG_parity].u_obj == &nativeio_uart_parity_odd_obj) { parity = PARITY_ODD; } @@ -128,10 +130,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_uart_deinit_obj, nativeio_uart_obj_dei //| //| No-op used by Context Managers. //| -STATIC mp_obj_t nativeio_uart_obj___enter__(mp_obj_t self_in) { - return self_in; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_uart___enter___obj, nativeio_uart_obj___enter__); +// Provided by context manager helper. //| .. method:: __exit__() //| @@ -220,11 +219,11 @@ STATIC mp_uint_t nativeio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uin //| //| Enum-like class to define the parity used to verify correct data transfer. //| -//| .. data:: odd +//| .. data:: ODD //| //| Total number of ones should be odd. //| -//| .. data:: even +//| .. data:: EVEN //| //| Total number of ones should be even. //| @@ -239,20 +238,29 @@ const nativeio_uart_parity_obj_t nativeio_uart_parity_even_obj = { }; STATIC const mp_rom_map_elem_t nativeio_uart_parity_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_odd), MP_ROM_PTR(&nativeio_uart_parity_odd_obj) }, - { MP_ROM_QSTR(MP_QSTR_even), MP_ROM_PTR(&nativeio_uart_parity_even_obj) }, + { MP_ROM_QSTR(MP_QSTR_ODD), MP_ROM_PTR(&nativeio_uart_parity_odd_obj) }, + { MP_ROM_QSTR(MP_QSTR_EVEN), MP_ROM_PTR(&nativeio_uart_parity_even_obj) }, }; STATIC MP_DEFINE_CONST_DICT(nativeio_uart_parity_locals_dict, nativeio_uart_parity_locals_dict_table); +STATIC void nativeio_uart_parity_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + qstr parity = MP_QSTR_ODD; + if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_uart_parity_even_obj)) { + parity = MP_QSTR_EVEN; + } + mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_UART, MP_QSTR_Parity, parity); +} + const mp_obj_type_t nativeio_uart_parity_type = { { &mp_type_type }, .name = MP_QSTR_Parity, + .print = nativeio_uart_parity_print, .locals_dict = (mp_obj_t)&nativeio_uart_parity_locals_dict, }; STATIC const mp_rom_map_elem_t nativeio_uart_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_uart_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&nativeio_uart___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_uart___exit___obj) }, // Standard stream methods. |
