diff options
| author | Dan Halbert <halbert@halwitz.org> | 2017-10-13 14:51:09 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-10-13 14:51:09 -0400 |
| commit | 6e8fa8b3c9e99f461b3b8124c4c6939cc8305840 (patch) | |
| tree | 8fab7b8087481b89e45ea02a921e431da28103cf /shared-bindings | |
| parent | b7591cfd19d308091dc67ae698c112cdb52a044f (diff) | |
| parent | 3c54e2c03c71522a81c24d349cb410fc8c1c1767 (diff) | |
Merge pull request #320 from dhalbert/master
merge 2.x changes into master; touch up Makefile CFLAGS logic
Diffstat (limited to 'shared-bindings')
38 files changed, 518 insertions, 27 deletions
diff --git a/shared-bindings/analogio/AnalogIn.c b/shared-bindings/analogio/AnalogIn.c index dc625e855..14b788295 100644 --- a/shared-bindings/analogio/AnalogIn.c +++ b/shared-bindings/analogio/AnalogIn.c @@ -34,6 +34,7 @@ #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/analogio/AnalogIn.h" +#include "shared-bindings/util.h" //| .. currentmodule:: analogio //| @@ -114,8 +115,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogin___exit___obj, 4, 4, //| :rtype: int //| STATIC mp_obj_t analogio_analogin_obj_get_value(mp_obj_t self_in) { - analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_analogio_analogin_get_value(self)); + analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_analogio_analogin_deinited(self)); + return MP_OBJ_NEW_SMALL_INT(common_hal_analogio_analogin_get_value(self)); } MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_value_obj, analogio_analogin_obj_get_value); @@ -134,8 +136,9 @@ const mp_obj_property_t analogio_analogin_value_obj = { //| :rtype: float //| STATIC mp_obj_t analogio_analogin_obj_get_reference_voltage(mp_obj_t self_in) { - analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_float(common_hal_analogio_analogin_get_reference_voltage(self)); + analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_analogio_analogin_deinited(self)); + return mp_obj_new_float(common_hal_analogio_analogin_get_reference_voltage(self)); } MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_reference_voltage_obj, analogio_analogin_obj_get_reference_voltage); diff --git a/shared-bindings/analogio/AnalogIn.h b/shared-bindings/analogio/AnalogIn.h index 9c13526ac..4aa7fca23 100644 --- a/shared-bindings/analogio/AnalogIn.h +++ b/shared-bindings/analogio/AnalogIn.h @@ -34,6 +34,7 @@ extern const mp_obj_type_t analogio_analogin_type; void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, const mcu_pin_obj_t *pin); void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t* self); +bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t* self); uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t* self); float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t* self); diff --git a/shared-bindings/analogio/AnalogOut.c b/shared-bindings/analogio/AnalogOut.c index 6b65878b4..0c7a59db8 100644 --- a/shared-bindings/analogio/AnalogOut.c +++ b/shared-bindings/analogio/AnalogOut.c @@ -30,8 +30,10 @@ #include "lib/utils/context_manager_helpers.h" #include "py/objproperty.h" #include "py/runtime.h" + #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/analogio/AnalogOut.h" +#include "shared-bindings/util.h" //| .. currentmodule:: analogio //| @@ -112,6 +114,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogout___exit___obj, 4, 4 //| STATIC mp_obj_t analogio_analogout_obj_set_value(mp_obj_t self_in, mp_obj_t value) { analogio_analogout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_analogio_analogout_deinited(self)); uint32_t v = mp_obj_get_int(value); if (v >= (1 << 16)) { mp_raise_ValueError("AnalogOut is only 16 bits. Value must be less than 65536."); diff --git a/shared-bindings/analogio/AnalogOut.h b/shared-bindings/analogio/AnalogOut.h index 4276ec4ae..6fe5d9b19 100644 --- a/shared-bindings/analogio/AnalogOut.h +++ b/shared-bindings/analogio/AnalogOut.h @@ -34,6 +34,7 @@ extern const mp_obj_type_t analogio_analogout_type; void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, const mcu_pin_obj_t *pin); void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self); +bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self); void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, uint16_t value); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGOUT_H diff --git a/shared-bindings/audiobusio/PDMIn.c b/shared-bindings/audiobusio/PDMIn.c index eea1e76ac..e13e09d64 100644 --- a/shared-bindings/audiobusio/PDMIn.c +++ b/shared-bindings/audiobusio/PDMIn.c @@ -32,6 +32,7 @@ #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/audiobusio/PDMIn.h" +#include "shared-bindings/util.h" //| .. currentmodule:: audiobusio //| @@ -161,7 +162,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audiobusio_pdmin___exit___obj, 4, 4, //| STATIC mp_obj_t audiobusio_pdmin_obj_record(mp_obj_t self_obj, mp_obj_t destination, mp_obj_t destination_length) { audiobusio_pdmin_obj_t *self = MP_OBJ_TO_PTR(self_obj); - + raise_error_if_deinited(common_hal_audiobusio_pdmin_deinited(self)); if (!MP_OBJ_IS_SMALL_INT(destination_length)) { mp_raise_TypeError("destination_length must be int"); } @@ -198,6 +199,7 @@ MP_DEFINE_CONST_FUN_OBJ_3(audiobusio_pdmin_record_obj, audiobusio_pdmin_obj_reco //| STATIC mp_obj_t audiobusio_pdmin_obj_get_frequency(mp_obj_t self_in) { audiobusio_pdmin_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_audiobusio_pdmin_deinited(self)); return MP_OBJ_NEW_SMALL_INT(common_hal_audiobusio_pdmin_get_frequency(self)); } MP_DEFINE_CONST_FUN_OBJ_1(audiobusio_pdmin_get_frequency_obj, audiobusio_pdmin_obj_get_frequency); diff --git a/shared-bindings/audiobusio/PDMIn.h b/shared-bindings/audiobusio/PDMIn.h index a12b0a1f1..a0702c978 100644 --- a/shared-bindings/audiobusio/PDMIn.h +++ b/shared-bindings/audiobusio/PDMIn.h @@ -37,6 +37,7 @@ void common_hal_audiobusio_pdmin_construct(audiobusio_pdmin_obj_t* self, const mcu_pin_obj_t* clock_pin, const mcu_pin_obj_t* data_pin, uint32_t frequency, uint8_t bit_depth, bool mono, uint8_t oversample); void common_hal_audiobusio_pdmin_deinit(audiobusio_pdmin_obj_t* self); +bool common_hal_audiobusio_pdmin_deinited(audiobusio_pdmin_obj_t* self); uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* self, uint16_t* buffer, uint32_t length); uint8_t common_hal_audiobusio_pdmin_get_bit_depth(audiobusio_pdmin_obj_t* self); diff --git a/shared-bindings/audioio/AudioOut.c b/shared-bindings/audioio/AudioOut.c index 56c307a4f..4e76565e9 100644 --- a/shared-bindings/audioio/AudioOut.c +++ b/shared-bindings/audioio/AudioOut.c @@ -32,6 +32,7 @@ #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/audioio/AudioOut.h" +#include "shared-bindings/util.h" //| .. currentmodule:: audioio //| @@ -162,6 +163,7 @@ STATIC mp_obj_t audioio_audioout_obj_play(size_t n_args, const mp_obj_t *pos_arg { MP_QSTR_loop, MP_ARG_BOOL, {.u_bool = false} }, }; audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + raise_error_if_deinited(common_hal_audioio_audioout_deinited(self)); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -177,6 +179,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(audioio_audioout_play_obj, 1, audioio_audioout_obj_pl //| STATIC mp_obj_t audioio_audioout_obj_stop(mp_obj_t self_in) { audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_audioio_audioout_deinited(self)); common_hal_audioio_audioout_stop(self); return mp_const_none; } @@ -188,6 +191,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_stop_obj, audioio_audioout_obj_stop); //| STATIC mp_obj_t audioio_audioout_obj_get_playing(mp_obj_t self_in) { audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_audioio_audioout_deinited(self)); return mp_obj_new_bool(common_hal_audioio_audioout_get_playing(self)); } MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_get_playing_obj, audioio_audioout_obj_get_playing); @@ -207,12 +211,14 @@ const mp_obj_property_t audioio_audioout_playing_obj = { //| STATIC mp_obj_t audioio_audioout_obj_get_frequency(mp_obj_t self_in) { audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_audioio_audioout_deinited(self)); return MP_OBJ_NEW_SMALL_INT(common_hal_audioio_audioout_get_frequency(self)); } MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_get_frequency_obj, audioio_audioout_obj_get_frequency); STATIC mp_obj_t audioio_audioout_obj_set_frequency(mp_obj_t self_in, mp_obj_t frequency) { audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_audioio_audioout_deinited(self)); common_hal_audioio_audioout_set_frequency(self, mp_obj_get_int(frequency)); return mp_const_none; } diff --git a/shared-bindings/audioio/AudioOut.h b/shared-bindings/audioio/AudioOut.h index 2293486ad..befd4d35e 100644 --- a/shared-bindings/audioio/AudioOut.h +++ b/shared-bindings/audioio/AudioOut.h @@ -39,6 +39,7 @@ void common_hal_audioio_audioout_construct_from_file(audioio_audioout_obj_t* sel const mcu_pin_obj_t* pin, pyb_file_obj_t* file); void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t* self); +bool common_hal_audioio_audioout_deinited(audioio_audioout_obj_t* self); void common_hal_audioio_audioout_play(audioio_audioout_obj_t* self, bool loop); void common_hal_audioio_audioout_stop(audioio_audioout_obj_t* self); bool common_hal_audioio_audioout_get_playing(audioio_audioout_obj_t* self); diff --git a/shared-bindings/bitbangio/I2C.c b/shared-bindings/bitbangio/I2C.c index 147a8d4f1..80718090a 100644 --- a/shared-bindings/bitbangio/I2C.c +++ b/shared-bindings/bitbangio/I2C.c @@ -29,6 +29,7 @@ #include "shared-bindings/bitbangio/I2C.h" #include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/util.h" #include "lib/utils/buffer_helper.h" #include "lib/utils/context_manager_helpers.h" @@ -52,6 +53,7 @@ STATIC mp_obj_t bitbangio_i2c_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_i2c_obj_t *self = m_new_obj(bitbangio_i2c_obj_t); + raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self)); self->base.type = &bitbangio_i2c_type; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); @@ -114,6 +116,7 @@ static void check_lock(bitbangio_i2c_obj_t *self) { //| STATIC mp_obj_t bitbangio_i2c_scan(mp_obj_t self_in) { bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self)); check_lock(self); mp_obj_t list = mp_obj_new_list(0, NULL); // 7-bit addresses 0b0000xxx and 0b1111xxx are reserved @@ -132,7 +135,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_scan_obj, bitbangio_i2c_scan); //| Attempts to grab the I2C lock. Returns True on success. //| STATIC mp_obj_t bitbangio_i2c_obj_try_lock(mp_obj_t self_in) { - return mp_obj_new_bool(shared_module_bitbangio_i2c_try_lock(MP_OBJ_TO_PTR(self_in))); + bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self)); + return mp_obj_new_bool(shared_module_bitbangio_i2c_try_lock(self)); } MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_try_lock_obj, bitbangio_i2c_obj_try_lock); @@ -141,7 +146,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_try_lock_obj, bitbangio_i2c_obj_try_lock //| Releases the I2C lock. //| STATIC mp_obj_t bitbangio_i2c_obj_unlock(mp_obj_t self_in) { - shared_module_bitbangio_i2c_unlock(MP_OBJ_TO_PTR(self_in)); + bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self)); + shared_module_bitbangio_i2c_unlock(self); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_unlock_obj, bitbangio_i2c_obj_unlock); @@ -169,6 +176,7 @@ STATIC mp_obj_t bitbangio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_a { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, }; bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self)); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); check_lock(self); @@ -215,6 +223,7 @@ STATIC mp_obj_t bitbangio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, m { MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, }; bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self)); check_lock(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); diff --git a/shared-bindings/bitbangio/I2C.h b/shared-bindings/bitbangio/I2C.h index d8acbecd2..9d393ee12 100644 --- a/shared-bindings/bitbangio/I2C.h +++ b/shared-bindings/bitbangio/I2C.h @@ -42,6 +42,7 @@ extern void shared_module_bitbangio_i2c_construct(bitbangio_i2c_obj_t *self, uint32_t frequency); extern void shared_module_bitbangio_i2c_deinit(bitbangio_i2c_obj_t *self); +extern bool shared_module_bitbangio_i2c_deinited(bitbangio_i2c_obj_t *self); extern bool shared_module_bitbangio_i2c_try_lock(bitbangio_i2c_obj_t *self); extern bool shared_module_bitbangio_i2c_has_lock(bitbangio_i2c_obj_t *self); diff --git a/shared-bindings/bitbangio/OneWire.c b/shared-bindings/bitbangio/OneWire.c index bbdf7c8f5..608c98100 100644 --- a/shared-bindings/bitbangio/OneWire.c +++ b/shared-bindings/bitbangio/OneWire.c @@ -32,6 +32,7 @@ #include "py/runtime0.h" #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/bitbangio/OneWire.h" +#include "shared-bindings/util.h" //| .. currentmodule:: bitbangio //| @@ -117,6 +118,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_onewire___exit___obj, 4, 4, //| STATIC mp_obj_t bitbangio_onewire_obj_reset(mp_obj_t self_in) { bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(shared_module_bitbangio_onewire_deinited(self)); return mp_obj_new_bool(shared_module_bitbangio_onewire_reset(self)); } @@ -131,6 +133,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_onewire_reset_obj, bitbangio_onewire_obj_res //| STATIC mp_obj_t bitbangio_onewire_obj_read_bit(mp_obj_t self_in) { bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(shared_module_bitbangio_onewire_deinited(self)); return mp_obj_new_bool(shared_module_bitbangio_onewire_read_bit(self)); } @@ -142,6 +145,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_onewire_read_bit_obj, bitbangio_onewire_obj_ //| STATIC mp_obj_t bitbangio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) { bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(shared_module_bitbangio_onewire_deinited(self)); shared_module_bitbangio_onewire_write_bit(self, mp_obj_is_true(bool_obj)); return mp_const_none; diff --git a/shared-bindings/bitbangio/OneWire.h b/shared-bindings/bitbangio/OneWire.h index 894c6e368..ef50db737 100644 --- a/shared-bindings/bitbangio/OneWire.h +++ b/shared-bindings/bitbangio/OneWire.h @@ -35,6 +35,7 @@ extern const mp_obj_type_t bitbangio_onewire_type; extern void shared_module_bitbangio_onewire_construct(bitbangio_onewire_obj_t* self, const mcu_pin_obj_t* pin); extern void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self); +extern bool shared_module_bitbangio_onewire_deinited(bitbangio_onewire_obj_t* self); extern bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self); extern bool shared_module_bitbangio_onewire_read_bit(bitbangio_onewire_obj_t* self); extern void shared_module_bitbangio_onewire_write_bit(bitbangio_onewire_obj_t* self, bool bit); diff --git a/shared-bindings/bitbangio/SPI.c b/shared-bindings/bitbangio/SPI.c index 8589025d9..6c7bac3e5 100644 --- a/shared-bindings/bitbangio/SPI.c +++ b/shared-bindings/bitbangio/SPI.c @@ -31,6 +31,7 @@ #include "shared-bindings/bitbangio/SPI.h" #include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/util.h" #include "lib/utils/context_manager_helpers.h" #include "py/mperrno.h" @@ -138,6 +139,7 @@ STATIC mp_obj_t bitbangio_spi_configure(size_t n_args, const mp_obj_t *pos_args, { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, }; bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + raise_error_if_deinited(shared_module_bitbangio_spi_deinited(self)); check_lock(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -168,7 +170,9 @@ MP_DEFINE_CONST_FUN_OBJ_KW(bitbangio_spi_configure_obj, 1, bitbangio_spi_configu //| :rtype: bool //| STATIC mp_obj_t bitbangio_spi_obj_try_lock(mp_obj_t self_in) { - return mp_obj_new_bool(shared_module_bitbangio_spi_try_lock(MP_OBJ_TO_PTR(self_in))); + bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(shared_module_bitbangio_spi_deinited(self)); + return mp_obj_new_bool(shared_module_bitbangio_spi_try_lock(self)); } MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_try_lock_obj, bitbangio_spi_obj_try_lock); @@ -177,7 +181,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_try_lock_obj, bitbangio_spi_obj_try_lock //| Releases the SPI lock. //| STATIC mp_obj_t bitbangio_spi_obj_unlock(mp_obj_t self_in) { - shared_module_bitbangio_spi_unlock(MP_OBJ_TO_PTR(self_in)); + bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(shared_module_bitbangio_spi_deinited(self)); + shared_module_bitbangio_spi_unlock(self); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_unlock_obj, bitbangio_spi_obj_unlock); @@ -188,9 +194,10 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_unlock_obj, bitbangio_spi_obj_unlock); //| // TODO(tannewt): Add support for start and end kwargs. STATIC mp_obj_t bitbangio_spi_write(mp_obj_t self_in, mp_obj_t wr_buf) { + bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(shared_module_bitbangio_spi_deinited(self)); mp_buffer_info_t src; mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ); - bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); check_lock(self); bool ok = shared_module_bitbangio_spi_write(self, src.buf, src.len); if (!ok) { @@ -207,10 +214,12 @@ MP_DEFINE_CONST_FUN_OBJ_2(bitbangio_spi_write_obj, bitbangio_spi_write); //| // TODO(tannewt): Add support for start and end kwargs. STATIC mp_obj_t bitbangio_spi_readinto(size_t n_args, const mp_obj_t *args) { + bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(args[0]); + raise_error_if_deinited(shared_module_bitbangio_spi_deinited(self)); mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE); check_lock(args[0]); - bool ok = shared_module_bitbangio_spi_read(args[0], bufinfo.buf, bufinfo.len); + bool ok = shared_module_bitbangio_spi_read(self, bufinfo.buf, bufinfo.len); if (!ok) { mp_raise_OSError(MP_EIO); } diff --git a/shared-bindings/bitbangio/SPI.h b/shared-bindings/bitbangio/SPI.h index 93089a8aa..0ce329798 100644 --- a/shared-bindings/bitbangio/SPI.h +++ b/shared-bindings/bitbangio/SPI.h @@ -41,6 +41,7 @@ extern void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self, const mcu_pin_obj_t * miso); extern void shared_module_bitbangio_spi_deinit(bitbangio_spi_obj_t *self); +extern bool shared_module_bitbangio_spi_deinited(bitbangio_spi_obj_t *self); extern void shared_module_bitbangio_spi_configure(bitbangio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits); diff --git a/shared-bindings/busio/I2C.c b/shared-bindings/busio/I2C.c index 0431add78..cdc960f80 100644 --- a/shared-bindings/busio/I2C.c +++ b/shared-bindings/busio/I2C.c @@ -29,6 +29,7 @@ #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/busio/I2C.h" +#include "shared-bindings/util.h" #include "lib/utils/buffer_helper.h" #include "lib/utils/context_manager_helpers.h" @@ -126,6 +127,7 @@ static void check_lock(busio_i2c_obj_t *self) { //| STATIC mp_obj_t busio_i2c_scan(mp_obj_t self_in) { busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_busio_i2c_deinited(self)); check_lock(self); mp_obj_t list = mp_obj_new_list(0, NULL); // 7-bit addresses 0b0000xxx and 0b1111xxx are reserved @@ -147,7 +149,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_scan_obj, busio_i2c_scan); //| :rtype: bool //| STATIC mp_obj_t busio_i2c_obj_try_lock(mp_obj_t self_in) { - return mp_obj_new_bool(common_hal_busio_i2c_try_lock(MP_OBJ_TO_PTR(self_in))); + busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_busio_i2c_deinited(self)); + return mp_obj_new_bool(common_hal_busio_i2c_try_lock(self)); } MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_try_lock_obj, busio_i2c_obj_try_lock); @@ -156,7 +160,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_try_lock_obj, busio_i2c_obj_try_lock); //| Releases the I2C lock. //| STATIC mp_obj_t busio_i2c_obj_unlock(mp_obj_t self_in) { - common_hal_busio_i2c_unlock(MP_OBJ_TO_PTR(self_in)); + busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_busio_i2c_deinited(self)); + common_hal_busio_i2c_unlock(self); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_unlock_obj, busio_i2c_obj_unlock); @@ -184,6 +190,7 @@ STATIC mp_obj_t busio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, }; busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + raise_error_if_deinited(common_hal_busio_i2c_deinited(self)); check_lock(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -229,6 +236,7 @@ STATIC mp_obj_t busio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_ma { MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, }; busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + raise_error_if_deinited(common_hal_busio_i2c_deinited(self)); check_lock(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); diff --git a/shared-bindings/busio/I2C.h b/shared-bindings/busio/I2C.h index 00a55726b..7185d6075 100644 --- a/shared-bindings/busio/I2C.h +++ b/shared-bindings/busio/I2C.h @@ -49,6 +49,7 @@ extern void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, uint32_t frequency); extern void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self); +extern bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self); extern bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self); extern bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self); diff --git a/shared-bindings/busio/OneWire.c b/shared-bindings/busio/OneWire.c index f270d9d92..95fce68f3 100644 --- a/shared-bindings/busio/OneWire.c +++ b/shared-bindings/busio/OneWire.c @@ -32,6 +32,7 @@ #include "py/runtime0.h" #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/busio/OneWire.h" +#include "shared-bindings/util.h" //| .. currentmodule:: busio //| @@ -120,6 +121,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_onewire___exit___obj, 4, 4, bus //| STATIC mp_obj_t busio_onewire_obj_reset(mp_obj_t self_in) { busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_busio_onewire_deinited(self)); return mp_obj_new_bool(common_hal_busio_onewire_reset(self)); } @@ -134,6 +136,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_reset_obj, busio_onewire_obj_reset); //| STATIC mp_obj_t busio_onewire_obj_read_bit(mp_obj_t self_in) { busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_busio_onewire_deinited(self)); return mp_obj_new_bool(common_hal_busio_onewire_read_bit(self)); } @@ -145,6 +148,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_read_bit_obj, busio_onewire_obj_read_bit //| STATIC mp_obj_t busio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) { busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_busio_onewire_deinited(self)); common_hal_busio_onewire_write_bit(self, mp_obj_is_true(bool_obj)); return mp_const_none; diff --git a/shared-bindings/busio/OneWire.h b/shared-bindings/busio/OneWire.h index 893ee9919..9ee5f639b 100644 --- a/shared-bindings/busio/OneWire.h +++ b/shared-bindings/busio/OneWire.h @@ -35,6 +35,7 @@ extern const mp_obj_type_t busio_onewire_type; extern void common_hal_busio_onewire_construct(busio_onewire_obj_t* self, const mcu_pin_obj_t* pin); extern void common_hal_busio_onewire_deinit(busio_onewire_obj_t* self); +extern bool common_hal_busio_onewire_deinited(busio_onewire_obj_t* self); extern bool common_hal_busio_onewire_reset(busio_onewire_obj_t* self); extern bool common_hal_busio_onewire_read_bit(busio_onewire_obj_t* self); extern void common_hal_busio_onewire_write_bit(busio_onewire_obj_t* self, bool bit); diff --git a/shared-bindings/busio/SPI.c b/shared-bindings/busio/SPI.c index 6e2ea5a6a..12f888f23 100644 --- a/shared-bindings/busio/SPI.c +++ b/shared-bindings/busio/SPI.c @@ -31,6 +31,7 @@ #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/busio/SPI.h" +#include "shared-bindings/util.h" #include "lib/utils/buffer_helper.h" #include "lib/utils/context_manager_helpers.h" @@ -151,6 +152,7 @@ STATIC mp_obj_t busio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_ { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, }; busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + raise_error_if_deinited(common_hal_busio_spi_deinited(self)); check_lock(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -184,7 +186,9 @@ MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_configure_obj, 1, busio_spi_configure); //| :rtype: bool //| STATIC mp_obj_t busio_spi_obj_try_lock(mp_obj_t self_in) { - return mp_obj_new_bool(common_hal_busio_spi_try_lock(MP_OBJ_TO_PTR(self_in))); + busio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_busio_spi_deinited(self)); + return mp_obj_new_bool(common_hal_busio_spi_try_lock(self)); } MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_try_lock_obj, busio_spi_obj_try_lock); @@ -193,7 +197,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_try_lock_obj, busio_spi_obj_try_lock); //| Releases the SPI lock. //| STATIC mp_obj_t busio_spi_obj_unlock(mp_obj_t self_in) { - common_hal_busio_spi_unlock(MP_OBJ_TO_PTR(self_in)); + busio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_busio_spi_deinited(self)); + common_hal_busio_spi_unlock(self); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_unlock_obj, busio_spi_obj_unlock); @@ -214,6 +220,7 @@ STATIC mp_obj_t busio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_map_ { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, }; busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + raise_error_if_deinited(common_hal_busio_spi_deinited(self)); check_lock(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -251,6 +258,7 @@ STATIC mp_obj_t busio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_m { MP_QSTR_write_value,MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, }; busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + raise_error_if_deinited(common_hal_busio_spi_deinited(self)); check_lock(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); diff --git a/shared-bindings/busio/SPI.h b/shared-bindings/busio/SPI.h index 966444f25..ac31d5060 100644 --- a/shared-bindings/busio/SPI.h +++ b/shared-bindings/busio/SPI.h @@ -41,6 +41,7 @@ extern void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t * miso); extern void common_hal_busio_spi_deinit(busio_spi_obj_t *self); +extern bool common_hal_busio_spi_deinited(busio_spi_obj_t *self); extern bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits); diff --git a/shared-bindings/busio/UART.c b/shared-bindings/busio/UART.c index ee23ff055..3a3cb4246 100644 --- a/shared-bindings/busio/UART.c +++ b/shared-bindings/busio/UART.c @@ -27,6 +27,8 @@ #include <stdint.h> #include "shared-bindings/busio/UART.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/util.h" #include "lib/utils/context_manager_helpers.h" @@ -34,7 +36,6 @@ #include "py/runtime.h" #include "py/stream.h" -#include "shared-bindings/microcontroller/Pin.h" //| .. currentmodule:: busio //| @@ -179,7 +180,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_ // These three methods are used by the shared stream methods. STATIC mp_uint_t busio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { - busio_uart_obj_t *self = self_in; + busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_busio_uart_deinited(self)); byte *buf = buf_in; // make sure we want at least 1 char @@ -191,14 +193,16 @@ STATIC mp_uint_t busio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, } STATIC mp_uint_t busio_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { - busio_uart_obj_t *self = self_in; + busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_busio_uart_deinited(self)); const byte *buf = buf_in; return common_hal_busio_uart_write(self, buf, size, errcode); } STATIC mp_uint_t busio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { - busio_uart_obj_t *self = self_in; + busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_busio_uart_deinited(self)); mp_uint_t ret; if (request == MP_IOCTL_POLL) { mp_uint_t flags = arg; diff --git a/shared-bindings/busio/UART.h b/shared-bindings/busio/UART.h index 4432d5996..28196931a 100644 --- a/shared-bindings/busio/UART.h +++ b/shared-bindings/busio/UART.h @@ -45,6 +45,7 @@ extern void common_hal_busio_uart_construct(busio_uart_obj_t *self, uint8_t receiver_buffer_size); extern void common_hal_busio_uart_deinit(busio_uart_obj_t *self); +extern bool common_hal_busio_uart_deinited(busio_uart_obj_t *self); // Read characters. len is in characters NOT bytes! extern size_t common_hal_busio_uart_read(busio_uart_obj_t *self, diff --git a/shared-bindings/digitalio/DigitalInOut.c b/shared-bindings/digitalio/DigitalInOut.c index 7fd823a60..ea2ba0b89 100644 --- a/shared-bindings/digitalio/DigitalInOut.c +++ b/shared-bindings/digitalio/DigitalInOut.c @@ -40,6 +40,7 @@ #include "shared-bindings/digitalio/Direction.h" #include "shared-bindings/digitalio/DriveMode.h" #include "shared-bindings/digitalio/Pull.h" +#include "shared-bindings/util.h" //| .. currentmodule:: digitalio //| @@ -119,6 +120,7 @@ STATIC mp_obj_t digitalio_digitalinout_switch_to_output(size_t n_args, const mp_ { MP_QSTR_drive_mode, MP_ARG_OBJ, {.u_rom_obj = &digitalio_drive_mode_push_pull_obj} }, }; digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self)); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -155,6 +157,7 @@ STATIC mp_obj_t digitalio_digitalinout_switch_to_input(size_t n_args, const mp_o { MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} }, }; digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self)); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -187,6 +190,7 @@ extern const digitalio_digitalio_direction_obj_t digitalio_digitalio_direction_o STATIC mp_obj_t digitalio_digitalinout_obj_get_direction(mp_obj_t self_in) { digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self)); enum digitalio_direction_t direction = common_hal_digitalio_digitalinout_get_direction(self); if (direction == DIRECTION_INPUT) { return (mp_obj_t)&digitalio_direction_input_obj; @@ -197,6 +201,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_direction_obj, digitalio_di STATIC mp_obj_t digitalio_digitalinout_obj_set_direction(mp_obj_t self_in, mp_obj_t value) { digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self)); if (value == &digitalio_direction_input_obj) { common_hal_digitalio_digitalinout_switch_to_input(self, PULL_NONE); } else if (value == &digitalio_direction_output_obj) { @@ -221,6 +226,7 @@ const mp_obj_property_t digitalio_digitalio_direction_obj = { //| STATIC mp_obj_t digitalio_digitalinout_obj_get_value(mp_obj_t self_in) { digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self)); bool value = common_hal_digitalio_digitalinout_get_value(self); return mp_obj_new_bool(value); } @@ -228,6 +234,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_value_obj, digitalio_digita STATIC mp_obj_t digitalio_digitalinout_obj_set_value(mp_obj_t self_in, mp_obj_t value) { digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self)); if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) { mp_raise_AttributeError("Cannot set value when direction is input."); return mp_const_none; @@ -250,6 +257,7 @@ const mp_obj_property_t digitalio_digitalinout_value_obj = { //| STATIC mp_obj_t digitalio_digitalinout_obj_get_drive_mode(mp_obj_t self_in) { digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self)); if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) { mp_raise_AttributeError("Drive mode not used when direction is input."); return mp_const_none; @@ -264,6 +272,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_drive_mode_obj, digitalio_d STATIC mp_obj_t digitalio_digitalinout_obj_set_drive_mode(mp_obj_t self_in, mp_obj_t drive_mode) { digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self)); if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) { mp_raise_AttributeError("Drive mode not used when direction is input."); return mp_const_none; @@ -293,6 +302,7 @@ const mp_obj_property_t digitalio_digitalio_drive_mode_obj = { //| STATIC mp_obj_t digitalio_digitalinout_obj_get_pull(mp_obj_t self_in) { digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self)); if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUTPUT) { mp_raise_AttributeError("Pull not used when direction is output."); return mp_const_none; @@ -309,6 +319,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_pull_obj, digitalio_digital STATIC mp_obj_t digitalio_digitalinout_obj_set_pull(mp_obj_t self_in, mp_obj_t pull_obj) { digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self)); if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUTPUT) { mp_raise_AttributeError("Pull not used when direction is output."); return mp_const_none; diff --git a/shared-bindings/digitalio/DigitalInOut.h b/shared-bindings/digitalio/DigitalInOut.h index da0856edc..74970881b 100644 --- a/shared-bindings/digitalio/DigitalInOut.h +++ b/shared-bindings/digitalio/DigitalInOut.h @@ -42,6 +42,7 @@ typedef enum { digitalinout_result_t common_hal_digitalio_digitalinout_construct(digitalio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin); void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self); +bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t* self); void common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t* self, enum digitalio_pull_t pull); void common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t* self, bool value, enum digitalio_drive_mode_t drive_mode); enum digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(digitalio_digitalinout_obj_t* self); diff --git a/shared-bindings/gamepad/GamePad.c b/shared-bindings/gamepad/GamePad.c new file mode 100644 index 000000000..bdc3359fb --- /dev/null +++ b/shared-bindings/gamepad/GamePad.c @@ -0,0 +1,146 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski 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 "py/obj.h" +#include "py/runtime.h" +#include "py/mphal.h" +#include "shared-module/gamepad/GamePad.h" +#include "GamePad.h" + + +gamepad_obj_t* gamepad_singleton = NULL; + +//| .. currentmodule:: gamepad +//| +//| :class:`GamePad` -- Scan buttons for presses +//| ============================================ +//| +//| Usage:: +//| +//| import board +//| import digitalio +//| import gamepad +//| import time +//| +//| B_UP = 1 << 0 +//| B_DOWN = 1 << 1 +//| +//| +//| pad = gamepad.GamePad( +//| digitalio.DigitalInOut(board.D0), +//| digitalio.DigitalInOut(board.D1), +//| ) +//| +//| y = 0 +//| while True: +//| buttons = pad.get_pressed() +//| if buttons & B_UP: +//| y -= 1 +//| print(y) +//| elif buttons & B_DOWN: +//| y += 1 +//| print(y) +//| time.sleep(0.1) +//| while pad.get_pressed(): +//| # Wait for all buttons to be released. +//| time.sleep(0.1) +//| + +//| .. class:: GamePad([b1[, b2[, b3[, b4[, b5[, b6[, b7[, b8]]]]]]]]) +//| +//| Initializes button scanning routines. +//| +//| The ``b1``-``b8`` parameters are ``DigitalInOut`` objects, which +//| immediately get switched to input with a pull-up, and then scanned +//| regularly for button presses. The order is the same as the order of +//| bits returned by the ``get_pressed`` function. You can re-initialize +//| it with different keys, then the new object will replace the previous +//| one. +//| +//| The basic feature required here is the ability to poll the keys at +//| regular intervals (so that de-bouncing is consistent) and fast enough +//| (so that we don't miss short button presses) while at the same time +//| letting the user code run normally, call blocking functions and wait +//| on delays. +//| +//| They button presses are accumulated, until the ``get_pressed`` method +//| is called, at which point the button state is cleared, and the new +//| button presses start to be recorded. +//| +STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, + size_t n_kw, const mp_obj_t *args) { + if (!gamepad_singleton) { + gamepad_singleton = m_new_obj(gamepad_obj_t); + gamepad_singleton->base.type = &gamepad_type; + } + gamepad_init(n_args, args); + return MP_OBJ_FROM_PTR(gamepad_singleton); +} + + +//| .. method:: get_pressed() +//| +//| Get the status of buttons pressed since the last call and clear it. +//| +//| Returns an 8-bit number, with bits that correspond to buttons, +//| which have been pressed (or held down) since the last call to this +//| function set to 1, and the remaining bits set to 0. Then it clears +//| the button state, so that new button presses (or buttons that are +//| held down) can be recorded for the next call. +//| +STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) { + gamepad_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(self->pressed); + self->pressed = 0; + return gamepad; +} +MP_DEFINE_CONST_FUN_OBJ_1(gamepad_get_pressed_obj, gamepad_get_pressed); + + +//| .. method:: deinit() +//| +//| Disable button scanning. +//| +STATIC mp_obj_t gamepad_deinit(mp_obj_t self_in) { + gamepad_singleton = NULL; + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(gamepad_deinit_obj, gamepad_deinit); + + +STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, + size_t n_kw, const mp_obj_t *args); +STATIC const mp_rom_map_elem_t gamepad_locals_dict_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepad_deinit_obj)}, +}; +STATIC MP_DEFINE_CONST_DICT(gamepad_locals_dict, gamepad_locals_dict_table); +const mp_obj_type_t gamepad_type = { + { &mp_type_type }, + .name = MP_QSTR_GamePad, + .make_new = gamepad_make_new, + .locals_dict = (mp_obj_dict_t*)&gamepad_locals_dict, +}; + diff --git a/shared-bindings/gamepad/GamePad.h b/shared-bindings/gamepad/GamePad.h new file mode 100644 index 000000000..172c95ace --- /dev/null +++ b/shared-bindings/gamepad/GamePad.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski 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_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H + +extern const mp_obj_type_t gamepad_type; + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H diff --git a/shared-bindings/gamepad/__init__.c b/shared-bindings/gamepad/__init__.c new file mode 100644 index 000000000..0c99d0d52 --- /dev/null +++ b/shared-bindings/gamepad/__init__.c @@ -0,0 +1,54 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski 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 "py/obj.h" +#include "py/runtime.h" +#include "py/mphal.h" +#include "GamePad.h" + + +//| :mod:`gamepad` --- Button handling +//| ================================== +//| +//| .. module:: gamepad +//| :synopsis: Button handling +//| :platform: SAMD21 +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| GamePad +//| +STATIC const mp_rom_map_elem_t gamepad_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gamepad) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_GamePad), MP_ROM_PTR(&gamepad_type)}, +}; +STATIC MP_DEFINE_CONST_DICT(gamepad_module_globals, + gamepad_module_globals_table); + +const mp_obj_module_t gamepad_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&gamepad_module_globals, +}; diff --git a/shared-bindings/index.rst b/shared-bindings/index.rst index 49b6ce5bc..556626268 100644 --- a/shared-bindings/index.rst +++ b/shared-bindings/index.rst @@ -21,9 +21,10 @@ Module / Port SAMD21 SAMD21 Express ESP8266 `board` **Yes** **Yes** **Yes** `busio` **Yes** **Yes** **Yes** `digitalio` **Yes** **Yes** **Yes** -`microcontroller` **Yes** **Yes** **Yes** +`gamepad` No **Yes** No +`microcontroller` **Yes** **Yes** **Yes** `multiterminal` No No **Yes** -`neopixel_write` **Yes** **Yes** **Yes** +`neopixel_write` **Yes** **Yes** **Yes** `nvm` No **Yes** No `os` **Yes** **Yes** **Yes** `pulseio` No **Yes** No diff --git a/shared-bindings/pulseio/PWMOut.c b/shared-bindings/pulseio/PWMOut.c index 80036ae72..8f6c76680 100644 --- a/shared-bindings/pulseio/PWMOut.c +++ b/shared-bindings/pulseio/PWMOut.c @@ -29,8 +29,10 @@ #include "lib/utils/context_manager_helpers.h" #include "py/objproperty.h" #include "py/runtime.h" + #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/pulseio/PWMOut.h" +#include "shared-bindings/util.h" //| .. currentmodule:: pulseio //| @@ -147,13 +149,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pwmout___exit___obj, 4, 4, pu //| (0). 0xffff will always be high, 0 will always be low and 0x7fff will //| be half high and then half low. STATIC mp_obj_t pulseio_pwmout_obj_get_duty_cycle(mp_obj_t self_in) { - pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_duty_cycle(self)); + pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_pulseio_pwmout_deinited(self)); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_duty_cycle(self)); } MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_get_duty_cycle_obj, pulseio_pwmout_obj_get_duty_cycle); STATIC mp_obj_t pulseio_pwmout_obj_set_duty_cycle(mp_obj_t self_in, mp_obj_t duty_cycle) { pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_pulseio_pwmout_deinited(self)); mp_int_t duty = mp_obj_get_int(duty_cycle); if (duty < 0 || duty > 0xffff) { mp_raise_ValueError("PWM duty_cycle must be between 0 and 65535 inclusive (16 bit resolution)"); @@ -176,13 +180,15 @@ const mp_obj_property_t pulseio_pwmout_duty_cycle_obj = { //| second). Only writeable when constructed with ``variable_frequency=True``. //| STATIC mp_obj_t pulseio_pwmout_obj_get_frequency(mp_obj_t self_in) { - pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_frequency(self)); + pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_pulseio_pwmout_deinited(self)); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_frequency(self)); } MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_get_frequency_obj, pulseio_pwmout_obj_get_frequency); STATIC mp_obj_t pulseio_pwmout_obj_set_frequency(mp_obj_t self_in, mp_obj_t frequency) { pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_pulseio_pwmout_deinited(self)); if (!common_hal_pulseio_pwmout_get_variable_frequency(self)) { mp_raise_AttributeError( "PWM frequency not writeable when variable_frequency is False on " diff --git a/shared-bindings/pulseio/PWMOut.h b/shared-bindings/pulseio/PWMOut.h index 98858654d..0b630816b 100644 --- a/shared-bindings/pulseio/PWMOut.h +++ b/shared-bindings/pulseio/PWMOut.h @@ -36,6 +36,7 @@ extern void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency, bool variable_frequency); extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self); +extern bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t* self); extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty); extern uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self); extern void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency); diff --git a/shared-bindings/pulseio/PulseIn.c b/shared-bindings/pulseio/PulseIn.c index 0abc06680..da9e6ac2c 100644 --- a/shared-bindings/pulseio/PulseIn.c +++ b/shared-bindings/pulseio/PulseIn.c @@ -32,6 +32,7 @@ #include "py/runtime0.h" #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/pulseio/PulseIn.h" +#include "shared-bindings/util.h" //| .. currentmodule:: pulseio //| @@ -139,6 +140,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pulsein___exit___obj, 4, 4, p //| STATIC mp_obj_t pulseio_pulsein_obj_pause(mp_obj_t self_in) { pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self)); common_hal_pulseio_pulsein_pause(self); return mp_const_none; @@ -162,6 +164,8 @@ STATIC mp_obj_t pulseio_pulsein_obj_resume(size_t n_args, const mp_obj_t *pos_ar { MP_QSTR_trigger_duration, MP_ARG_INT, {.u_int = 0} }, }; pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self)); + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -176,6 +180,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(pulseio_pulsein_resume_obj, 1, pulseio_pulsein_obj_re //| STATIC mp_obj_t pulseio_pulsein_obj_clear(mp_obj_t self_in) { pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self)); common_hal_pulseio_pulsein_clear(self); return mp_const_none; @@ -188,6 +193,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_clear_obj, pulseio_pulsein_obj_clear); //| STATIC mp_obj_t pulseio_pulsein_obj_popleft(mp_obj_t self_in) { pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self)); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_popleft(self)); } MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_popleft_obj, pulseio_pulsein_obj_popleft); @@ -199,6 +206,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_popleft_obj, pulseio_pulsein_obj_pople //| STATIC mp_obj_t pulseio_pulsein_obj_get_maxlen(mp_obj_t self_in) { pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self)); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_get_maxlen(self)); } MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_get_maxlen_obj, pulseio_pulsein_obj_get_maxlen); @@ -221,6 +230,7 @@ const mp_obj_property_t pulseio_pulsein_maxlen_obj = { //| STATIC mp_obj_t pulsein_unary_op(mp_uint_t op, mp_obj_t self_in) { pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self)); uint16_t len = common_hal_pulseio_pulsein_get_len(self); switch (op) { case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0); @@ -244,6 +254,8 @@ STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t va mp_raise_AttributeError("Cannot delete values"); } else { pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self)); + if (MP_OBJ_IS_TYPE(index_obj, &mp_type_slice)) { mp_raise_NotImplementedError("Slices not supported"); } else { diff --git a/shared-bindings/pulseio/PulseIn.h b/shared-bindings/pulseio/PulseIn.h index 1b96c93c9..88b6f356e 100644 --- a/shared-bindings/pulseio/PulseIn.h +++ b/shared-bindings/pulseio/PulseIn.h @@ -35,6 +35,7 @@ extern const mp_obj_type_t pulseio_pulsein_type; extern void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state); extern void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self); +extern bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self); extern void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self); extern void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration); extern void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self); diff --git a/shared-bindings/pulseio/PulseOut.c b/shared-bindings/pulseio/PulseOut.c index 0b1391f74..a44e28d5d 100644 --- a/shared-bindings/pulseio/PulseOut.c +++ b/shared-bindings/pulseio/PulseOut.c @@ -29,9 +29,11 @@ #include "lib/utils/context_manager_helpers.h" #include "py/objproperty.h" #include "py/runtime.h" + #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/pulseio/PulseOut.h" #include "shared-bindings/pulseio/PWMOut.h" +#include "shared-bindings/util.h" //| .. currentmodule:: pulseio //| @@ -123,6 +125,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pulseout___exit___obj, 4, 4, //| STATIC mp_obj_t pulseio_pulseout_obj_send(mp_obj_t self_in, mp_obj_t pulses) { pulseio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_pulseio_pulseout_deinited(self)); mp_buffer_info_t bufinfo; mp_get_buffer_raise(pulses, &bufinfo, MP_BUFFER_READ); diff --git a/shared-bindings/pulseio/PulseOut.h b/shared-bindings/pulseio/PulseOut.h index d6279ed6f..2cf78d3f2 100644 --- a/shared-bindings/pulseio/PulseOut.h +++ b/shared-bindings/pulseio/PulseOut.h @@ -36,6 +36,7 @@ extern const mp_obj_type_t pulseio_pulseout_type; extern void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, const pulseio_pwmout_obj_t* carrier); extern void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self); +extern bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self); extern void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t len); diff --git a/shared-bindings/touchio/TouchIn.c b/shared-bindings/touchio/TouchIn.c index 894781423..1d9123312 100644 --- a/shared-bindings/touchio/TouchIn.c +++ b/shared-bindings/touchio/TouchIn.c @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#include <stdint.h> #include <string.h> #include "lib/utils/context_manager_helpers.h" @@ -34,6 +35,7 @@ #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/touchio/TouchIn.h" +#include "shared-bindings/util.h" //| .. currentmodule:: touchio //| @@ -106,13 +108,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(touchio_touchin___exit___obj, 4, 4, t //| .. attribute:: value //| //| Whether the touch pad is being touched or not. +//| True if `raw_value` > `threshold`. //| //| :return: True when touched, False otherwise. //| :rtype: bool //| STATIC mp_obj_t touchio_touchin_obj_get_value(mp_obj_t self_in) { - touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(common_hal_touchio_touchin_get_value(self)); + touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_touchio_touchin_deinited(self)); + return mp_obj_new_bool(common_hal_touchio_touchin_get_value(self)); } MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_value_obj, touchio_touchin_obj_get_value); @@ -123,12 +127,79 @@ const mp_obj_property_t touchio_touchin_value_obj = { (mp_obj_t)&mp_const_none_obj}, }; + +//| .. attribute:: raw_value +//| +//| The raw touch measurement. Not settable. +//| +//| :return: an integer >= 0 +//| :rtype: int +//| +STATIC mp_obj_t touchio_touchin_obj_get_raw_value(mp_obj_t self_in) { + touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_touchio_touchin_deinited(self)); + return MP_OBJ_NEW_SMALL_INT(common_hal_touchio_touchin_get_raw_value(self)); +} + +MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_raw_value_obj, touchio_touchin_obj_get_raw_value); + +const mp_obj_property_t touchio_touchin_raw_value_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&touchio_touchin_get_raw_value_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, + }; + + +//| .. attribute:: threshold +//| +//| `value` will return True if `raw_value` is greater than than this threshold. +//| When the **TouchIn** object is created, an initial `raw_value` is read from the pin, +//| and then `threshold` is set to be 100 + that value. +//| +//| You can set the threshold to a different value to make the pin more or less sensitive. +//| +//| :return: an integer >= 0 +//| :rtype: int +//| +STATIC mp_obj_t touchio_touchin_obj_get_threshold(mp_obj_t self_in) { + touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_touchio_touchin_deinited(self)); + return MP_OBJ_NEW_SMALL_INT(common_hal_touchio_touchin_get_threshold(self)); +} + +MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_threshold_obj, touchio_touchin_obj_get_threshold); + +STATIC mp_obj_t touchio_touchin_obj_set_threshold(mp_obj_t self_in, mp_obj_t threshold_obj) { + touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); + raise_error_if_deinited(common_hal_touchio_touchin_deinited(self)); + uint32_t new_threshold = mp_obj_get_int(threshold_obj); + if (new_threshold < 0 || new_threshold > UINT16_MAX) { + // I would use MP_STRINGIFY(UINT16_MAX), but that prints "0xffff" instead of 65536. + mp_raise_ValueError("threshold must be in the range 0-65536"); + } + common_hal_touchio_touchin_set_threshold(self, new_threshold); + return mp_const_none; +} + +MP_DEFINE_CONST_FUN_OBJ_2(touchio_touchin_set_threshold_obj, touchio_touchin_obj_set_threshold); + +const mp_obj_property_t touchio_touchin_threshold_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&touchio_touchin_get_threshold_obj, + (mp_obj_t)&touchio_touchin_set_threshold_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + + STATIC const mp_rom_map_elem_t touchio_touchin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&touchio_touchin___exit___obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&touchio_touchin_deinit_obj) }, { MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&touchio_touchin_value_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_raw_value), MP_ROM_PTR(&touchio_touchin_raw_value_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_threshold), MP_ROM_PTR(&touchio_touchin_threshold_obj)}, }; STATIC MP_DEFINE_CONST_DICT(touchio_touchin_locals_dict, touchio_touchin_locals_dict_table); diff --git a/shared-bindings/touchio/TouchIn.h b/shared-bindings/touchio/TouchIn.h index 6ee668f38..2e5c51607 100644 --- a/shared-bindings/touchio/TouchIn.h +++ b/shared-bindings/touchio/TouchIn.h @@ -34,6 +34,10 @@ extern const mp_obj_type_t touchio_touchin_type; void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, const mcu_pin_obj_t *pin); void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self); +bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t* self); bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self); +uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self); +uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self); +void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, uint16_t new_threshold); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO_TOUCHIN_H diff --git a/shared-bindings/util.c b/shared-bindings/util.c new file mode 100644 index 000000000..3dadc4c18 --- /dev/null +++ b/shared-bindings/util.c @@ -0,0 +1,42 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Dan Halbert 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_SHARED_BINDINGS_UTIL_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_UTIL_H + +#include "py/runtime.h" + +#include "shared-bindings/util.h" + +// Check if pin is None. If so, deinit() has already been called on the object, so complain. +void raise_error_if_deinited(bool deinited) { + if (deinited) { + mp_raise_ValueError("Object has been deinitialized and can no longer be used. Create a new object."); + } +} + + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_UTIL_H diff --git a/shared-bindings/util.h b/shared-bindings/util.h new file mode 100644 index 000000000..b26ed7e93 --- /dev/null +++ b/shared-bindings/util.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Dan Halbert 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_ATMEL_SAMD_COMMON_HAL_UTIL_H +#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_UTIL_H + +void raise_error_if_deinited(bool deinited); + + +#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_UTIL_H |
