Switch enum-like attributes to all caps and add print support for them. Make room for this functionality by adding a shared __enter__ function object. #76

This commit is contained in:
Scott Shawcroft 2017-02-19 16:11:33 +01:00
parent c4ee6d5716
commit e9659e61f8
14 changed files with 161 additions and 75 deletions

View File

@ -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 \

View File

@ -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 \

View File

@ -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__);

View File

@ -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__

View File

@ -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) },

View File

@ -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) },

View File

@ -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)},

View File

@ -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

View File

@ -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) },

View File

@ -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) },

View File

@ -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

View File

@ -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) },

View File

@ -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)},

View File

@ -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.