diff options
Diffstat (limited to 'shared-bindings')
| -rw-r--r-- | shared-bindings/analogio/AnalogIn.c (renamed from shared-bindings/nativeio/AnalogIn.c) | 74 | ||||
| -rw-r--r-- | shared-bindings/analogio/AnalogIn.h (renamed from shared-bindings/nativeio/AnalogIn.h) | 18 | ||||
| -rw-r--r-- | shared-bindings/analogio/AnalogOut.c (renamed from shared-bindings/nativeio/AnalogOut.c) | 58 | ||||
| -rw-r--r-- | shared-bindings/analogio/AnalogOut.h (renamed from shared-bindings/nativeio/AnalogOut.h) | 16 | ||||
| -rw-r--r-- | shared-bindings/analogio/__init__.c | 84 | ||||
| -rw-r--r-- | shared-bindings/analogio/__init__.h (renamed from shared-bindings/nativeio/__init__.h) | 6 | ||||
| -rw-r--r-- | shared-bindings/bitbangio/OneWire.h | 6 | ||||
| -rw-r--r-- | shared-bindings/bitbangio/__init__.c | 11 | ||||
| -rw-r--r-- | shared-bindings/busio/I2C.c (renamed from shared-bindings/nativeio/I2C.c) | 92 | ||||
| -rw-r--r-- | shared-bindings/busio/I2C.h (renamed from shared-bindings/nativeio/I2C.h) | 26 | ||||
| -rw-r--r-- | shared-bindings/busio/OneWire.c (renamed from shared-bindings/nativeio/OneWire.c) | 76 | ||||
| -rw-r--r-- | shared-bindings/busio/OneWire.h (renamed from shared-bindings/nativeio/OneWire.h) | 20 | ||||
| -rw-r--r-- | shared-bindings/busio/SPI.c (renamed from shared-bindings/nativeio/SPI.c) | 94 | ||||
| -rw-r--r-- | shared-bindings/busio/SPI.h (renamed from shared-bindings/nativeio/SPI.h) | 26 | ||||
| -rw-r--r-- | shared-bindings/busio/UART.c (renamed from shared-bindings/nativeio/UART.c) | 110 | ||||
| -rw-r--r-- | shared-bindings/busio/UART.h (renamed from shared-bindings/nativeio/UART.h) | 22 | ||||
| -rw-r--r-- | shared-bindings/busio/__init__.c | 98 | ||||
| -rw-r--r-- | shared-bindings/busio/__init__.h | 34 | ||||
| -rw-r--r-- | shared-bindings/digitalio/DigitalInOut.c | 486 | ||||
| -rw-r--r-- | shared-bindings/digitalio/DigitalInOut.h (renamed from shared-bindings/nativeio/DigitalInOut.h) | 32 | ||||
| -rw-r--r-- | shared-bindings/digitalio/__init__.c | 95 | ||||
| -rw-r--r-- | shared-bindings/digitalio/__init__.h | 34 | ||||
| -rw-r--r-- | shared-bindings/nativeio/DigitalInOut.c | 486 | ||||
| -rw-r--r-- | shared-bindings/nativeio/__init__.c | 140 | ||||
| -rw-r--r-- | shared-bindings/neopixel_write/__init__.c | 12 | ||||
| -rw-r--r-- | shared-bindings/neopixel_write/__init__.h | 4 | ||||
| -rw-r--r-- | shared-bindings/pulseio/PWMOut.c (renamed from shared-bindings/nativeio/PWMOut.c) | 102 | ||||
| -rw-r--r-- | shared-bindings/pulseio/PWMOut.h (renamed from shared-bindings/nativeio/PWMOut.h) | 24 | ||||
| -rw-r--r-- | shared-bindings/pulseio/PulseIn.c (renamed from shared-bindings/nativeio/PulseIn.c) | 110 | ||||
| -rw-r--r-- | shared-bindings/pulseio/PulseIn.h (renamed from shared-bindings/nativeio/PulseIn.h) | 28 | ||||
| -rw-r--r-- | shared-bindings/pulseio/PulseOut.c (renamed from shared-bindings/nativeio/PulseOut.c) | 64 | ||||
| -rw-r--r-- | shared-bindings/pulseio/PulseOut.h (renamed from shared-bindings/nativeio/PulseOut.h) | 19 | ||||
| -rw-r--r-- | shared-bindings/pulseio/__init__.c | 87 | ||||
| -rw-r--r-- | shared-bindings/pulseio/__init__.h | 34 | ||||
| -rw-r--r-- | shared-bindings/touchio/TouchIn.c (renamed from shared-bindings/nativeio/TouchIn.c) | 58 | ||||
| -rw-r--r-- | shared-bindings/touchio/TouchIn.h (renamed from shared-bindings/nativeio/TouchIn.h) | 16 | ||||
| -rw-r--r-- | shared-bindings/touchio/__init__.c | 82 | ||||
| -rw-r--r-- | shared-bindings/touchio/__init__.h | 34 | ||||
| -rw-r--r-- | shared-bindings/usb_hid/Device.h | 6 | ||||
| -rw-r--r-- | shared-bindings/usb_hid/__init__.h | 2 |
40 files changed, 1633 insertions, 1193 deletions
diff --git a/shared-bindings/nativeio/AnalogIn.c b/shared-bindings/analogio/AnalogIn.c index 4513cd8af..f07302250 100644 --- a/shared-bindings/nativeio/AnalogIn.c +++ b/shared-bindings/analogio/AnalogIn.c @@ -33,19 +33,19 @@ #include "py/objproperty.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/AnalogIn.h" +#include "shared-bindings/analogio/AnalogIn.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: analogio //| //| :class:`AnalogIn` -- read analog voltage //| ============================================ //| //| Usage:: //| -//| import nativeio +//| import analogio //| from board import * //| -//| with nativeio.AnalogIn(A1) as adc: +//| with analogio.AnalogIn(A1) as adc: //| val = adc.value //| @@ -56,7 +56,7 @@ //| //| :param ~microcontroller.Pin pin: the pin to read from //| -STATIC mp_obj_t nativeio_analogin_make_new(const mp_obj_type_t *type, +STATIC mp_obj_t analogio_analogin_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { // check number of arguments mp_arg_check_num(n_args, n_kw, 1, 1, false); @@ -65,11 +65,11 @@ STATIC mp_obj_t nativeio_analogin_make_new(const mp_obj_type_t *type, mp_obj_t pin_obj = args[0]; assert_pin(pin_obj, false); - nativeio_analogin_obj_t *self = m_new_obj(nativeio_analogin_obj_t); - self->base.type = &nativeio_analogin_type; + analogio_analogin_obj_t *self = m_new_obj(analogio_analogin_obj_t); + self->base.type = &analogio_analogin_type; const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj); assert_pin_free(pin); - common_hal_nativeio_analogin_construct(self, pin); + common_hal_analogio_analogin_construct(self, pin); return (mp_obj_t) self; } @@ -78,12 +78,12 @@ STATIC mp_obj_t nativeio_analogin_make_new(const mp_obj_type_t *type, //| //| Turn off the AnalogIn and release the pin for other use. //| -STATIC mp_obj_t nativeio_analogin_deinit(mp_obj_t self_in) { - nativeio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_analogin_deinit(self); +STATIC mp_obj_t analogio_analogin_deinit(mp_obj_t self_in) { + analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_analogio_analogin_deinit(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_deinit_obj, nativeio_analogin_deinit); +MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_deinit_obj, analogio_analogin_deinit); //| .. method:: __enter__() //| @@ -95,12 +95,12 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_deinit_obj, nativeio_analogin_deinit //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_analogin___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t analogio_analogin___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_analogin_deinit(args[0]); + common_hal_analogio_analogin_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogin___exit___obj, 4, 4, nativeio_analogin___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogin___exit___obj, 4, 4, analogio_analogin___exit__); //| .. attribute:: value //| @@ -112,15 +112,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogin___exit___obj, 4, 4, //| :return: the data read //| :rtype: int //| -STATIC mp_obj_t nativeio_analogin_obj_get_value(mp_obj_t self_in) { - nativeio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_analogin_get_value(self)); +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)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_get_value_obj, nativeio_analogin_obj_get_value); +MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_value_obj, analogio_analogin_obj_get_value); -const mp_obj_property_t nativeio_analogin_value_obj = { +const mp_obj_property_t analogio_analogin_value_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_analogin_get_value_obj, + .proxy = {(mp_obj_t)&analogio_analogin_get_value_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj}, }; @@ -132,33 +132,33 @@ const mp_obj_property_t nativeio_analogin_value_obj = { //| :return: the reference voltage //| :rtype: float //| -STATIC mp_obj_t nativeio_analogin_obj_get_reference_voltage(mp_obj_t self_in) { - nativeio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_float(common_hal_nativeio_analogin_get_reference_voltage(self)); +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)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_get_reference_voltage_obj, - nativeio_analogin_obj_get_reference_voltage); +MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_reference_voltage_obj, + analogio_analogin_obj_get_reference_voltage); -const mp_obj_property_t nativeio_analogin_reference_voltage_obj = { +const mp_obj_property_t analogio_analogin_reference_voltage_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_analogin_get_reference_voltage_obj, + .proxy = {(mp_obj_t)&analogio_analogin_get_reference_voltage_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_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) }, +STATIC const mp_rom_map_elem_t analogio_analogin_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogio_analogin_deinit_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)}, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogio_analogin___exit___obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&analogio_analogin_value_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_reference_voltage), MP_ROM_PTR(&analogio_analogin_reference_voltage_obj)}, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_analogin_locals_dict, nativeio_analogin_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(analogio_analogin_locals_dict, analogio_analogin_locals_dict_table); -const mp_obj_type_t nativeio_analogin_type = { +const mp_obj_type_t analogio_analogin_type = { { &mp_type_type }, .name = MP_QSTR_AnalogIn, - .make_new = nativeio_analogin_make_new, - .locals_dict = (mp_obj_t)&nativeio_analogin_locals_dict, + .make_new = analogio_analogin_make_new, + .locals_dict = (mp_obj_t)&analogio_analogin_locals_dict, }; diff --git a/shared-bindings/nativeio/AnalogIn.h b/shared-bindings/analogio/AnalogIn.h index ea5cfb84e..64ddaab0f 100644 --- a/shared-bindings/nativeio/AnalogIn.h +++ b/shared-bindings/analogio/AnalogIn.h @@ -24,17 +24,17 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGIN_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGIN_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/analogio/AnalogIn.h" -extern const mp_obj_type_t nativeio_analogin_type; +extern const mp_obj_type_t analogio_analogin_type; -void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self, const mcu_pin_obj_t *pin); -void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t* self); -uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self); -float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self); +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); +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); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGIN_H__ diff --git a/shared-bindings/nativeio/AnalogOut.c b/shared-bindings/analogio/AnalogOut.c index c34fa48f3..6b02f8c0a 100644 --- a/shared-bindings/nativeio/AnalogOut.c +++ b/shared-bindings/analogio/AnalogOut.c @@ -31,9 +31,9 @@ #include "py/objproperty.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/AnalogOut.h" +#include "shared-bindings/analogio/AnalogOut.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: analogio //| //| :class:`AnalogOut` -- output analog voltage //| ============================================ @@ -42,10 +42,10 @@ //| //| Example usage:: //| -//| import nativeio +//| import analogio //| from microcontroller import pin //| -//| with nativeio.AnalogOut(pin.PA02) as dac: # output on pin PA02 +//| with analogio.AnalogOut(pin.PA02) as dac: # output on pin PA02 //| dac.value = 32768 # makes PA02 1.65V //| @@ -55,17 +55,17 @@ //| //| :param ~microcontroller.Pin pin: the pin to output to //| -STATIC mp_obj_t nativeio_analogout_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t analogio_analogout_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { // check arguments mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); assert_pin(args[0], false); const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(args[0]); - nativeio_analogout_obj_t *self = m_new_obj(nativeio_analogout_obj_t); - self->base.type = &nativeio_analogout_type; + analogio_analogout_obj_t *self = m_new_obj(analogio_analogout_obj_t); + self->base.type = &analogio_analogout_type; assert_pin_free(pin); - common_hal_nativeio_analogout_construct(self, pin); + common_hal_analogio_analogout_construct(self, pin); return self; } @@ -74,14 +74,14 @@ STATIC mp_obj_t nativeio_analogout_make_new(const mp_obj_type_t *type, mp_uint_t //| //| Turn off the AnalogOut and release the pin for other use. //| -STATIC mp_obj_t nativeio_analogout_deinit(mp_obj_t self_in) { - nativeio_analogout_obj_t *self = self_in; +STATIC mp_obj_t analogio_analogout_deinit(mp_obj_t self_in) { + analogio_analogout_obj_t *self = self_in; - common_hal_nativeio_analogout_deinit(self); + common_hal_analogio_analogout_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogout_deinit_obj, nativeio_analogout_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogout_deinit_obj, analogio_analogout_deinit); //| .. method:: __enter__() //| @@ -93,12 +93,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogout_deinit_obj, nativeio_analogo //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_analogout___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t analogio_analogout___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_analogout_deinit(args[0]); + common_hal_analogio_analogout_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogout___exit___obj, 4, 4, nativeio_analogout___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogout___exit___obj, 4, 4, analogio_analogout___exit__); //| .. attribute:: value //| @@ -109,39 +109,39 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogout___exit___obj, 4, 4 //| :return: the last value written //| :rtype: int //| -STATIC mp_obj_t nativeio_analogout_obj_set_value(mp_obj_t self_in, mp_obj_t value) { - nativeio_analogout_obj_t *self = MP_OBJ_TO_PTR(self_in); +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); 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."); } - common_hal_nativeio_analogout_set_value(self, v); + common_hal_analogio_analogout_set_value(self, v); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_analogout_set_value_obj, nativeio_analogout_obj_set_value); +MP_DEFINE_CONST_FUN_OBJ_2(analogio_analogout_set_value_obj, analogio_analogout_obj_set_value); -const mp_obj_property_t nativeio_analogout_value_obj = { +const mp_obj_property_t analogio_analogout_value_obj = { .base.type = &mp_type_property, .proxy = {(mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&nativeio_analogout_set_value_obj, + (mp_obj_t)&analogio_analogout_set_value_obj, (mp_obj_t)&mp_const_none_obj}, }; -STATIC const mp_rom_map_elem_t nativeio_analogout_locals_dict_table[] = { +STATIC const mp_rom_map_elem_t analogio_analogout_locals_dict_table[] = { // instance methods - { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_analogout_deinit_obj) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogio_analogout_deinit_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) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogio_analogout___exit___obj) }, // Properties - { MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&nativeio_analogout_value_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&analogio_analogout_value_obj }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_analogout_locals_dict, nativeio_analogout_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(analogio_analogout_locals_dict, analogio_analogout_locals_dict_table); -const mp_obj_type_t nativeio_analogout_type = { +const mp_obj_type_t analogio_analogout_type = { { &mp_type_type }, .name = MP_QSTR_AnalogOut, - .make_new = nativeio_analogout_make_new, - .locals_dict = (mp_obj_t)&nativeio_analogout_locals_dict, + .make_new = analogio_analogout_make_new, + .locals_dict = (mp_obj_t)&analogio_analogout_locals_dict, }; diff --git a/shared-bindings/nativeio/AnalogOut.h b/shared-bindings/analogio/AnalogOut.h index 195e4c313..3aa19ac29 100644 --- a/shared-bindings/nativeio/AnalogOut.h +++ b/shared-bindings/analogio/AnalogOut.h @@ -24,16 +24,16 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGOUT_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGOUT_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGOUT_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGOUT_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/analogio/AnalogOut.h" -extern const mp_obj_type_t nativeio_analogout_type; +extern const mp_obj_type_t analogio_analogout_type; -void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self, const mcu_pin_obj_t *pin); -void common_hal_nativeio_analogout_deinit(nativeio_analogout_obj_t *self); -void common_hal_nativeio_analogout_set_value(nativeio_analogout_obj_t *self, uint16_t value); +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); +void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, uint16_t value); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGOUT_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGOUT_H__ diff --git a/shared-bindings/analogio/__init__.c b/shared-bindings/analogio/__init__.c new file mode 100644 index 000000000..39d6bdcda --- /dev/null +++ b/shared-bindings/analogio/__init__.c @@ -0,0 +1,84 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 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 <stdint.h> + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/analogio/__init__.h" +#include "shared-bindings/analogio/AnalogIn.h" +#include "shared-bindings/analogio/AnalogOut.h" + +//| :mod:`analogio` --- Analog hardware support +//| ================================================= +//| +//| .. module:: analogio +//| :synopsis: Analog hardware support +//| :platform: SAMD21, ESP8266 +//| +//| The `analogio` module contains classes to provide access to analog IO +//| typically implemented with digital-to-analog (DAC) and analog-to-digital +//| (ADC) converters. +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| AnalogIn +//| AnalogOut +//| +//| All libraries change hardware state and should be deinitialized when they +//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a +//| context manager. +//| +//| For example:: +//| +//| import analogio +//| from board import * +//| +//| with analogio.AnalogIn(A0) as pin: +//| print(pin.value) +//| +//| This example will initialize the the device, read +//| :py:data:`~analogio.AnalogIn.value` and then +//| :py:meth:`~analogio.AnalogIn.deinit` the hardware. +//| + +STATIC const mp_rom_map_elem_t analogio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_analogio) }, + { MP_ROM_QSTR(MP_QSTR_AnalogIn), MP_ROM_PTR(&analogio_analogin_type) }, + { MP_ROM_QSTR(MP_QSTR_AnalogOut), MP_ROM_PTR(&analogio_analogout_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(analogio_module_globals, analogio_module_globals_table); + +const mp_obj_module_t analogio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&analogio_module_globals, +}; diff --git a/shared-bindings/nativeio/__init__.h b/shared-bindings/analogio/__init__.h index 7f4c01e05..9a0c77cb8 100644 --- a/shared-bindings/nativeio/__init__.h +++ b/shared-bindings/analogio/__init__.h @@ -24,11 +24,11 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO___INIT___H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO___INIT___H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO___INIT___H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO___INIT___H__ #include "py/obj.h" // Nothing now. -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO___INIT___H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO___INIT___H__ diff --git a/shared-bindings/bitbangio/OneWire.h b/shared-bindings/bitbangio/OneWire.h index dcc0386d4..a888dae04 100644 --- a/shared-bindings/bitbangio/OneWire.h +++ b/shared-bindings/bitbangio/OneWire.h @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_ONEWIRE_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_ONEWIRE_H__ #include "common-hal/microcontroller/types.h" #include "shared-module/bitbangio/types.h" @@ -39,4 +39,4 @@ 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); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_ONEWIRE_H__ diff --git a/shared-bindings/bitbangio/__init__.c b/shared-bindings/bitbangio/__init__.c index 5fe469758..85acab0d3 100644 --- a/shared-bindings/bitbangio/__init__.c +++ b/shared-bindings/bitbangio/__init__.c @@ -25,7 +25,7 @@ */ // bitbangio implements some standard protocols in the processor. Its only -// dependency is nativeio.DigitalInOut. +// dependency is digitalio. #include <stdint.h> @@ -45,12 +45,13 @@ //| //| .. module:: bitbangio //| :synopsis: Digital protocols implemented by the CPU -//| :platform: SAMD21 +//| :platform: SAMD21, ESP8266 //| -//| The `bitbangio` module contains classes to provide digital protocol support -//| regardless of whether the underlying hardware exists to use the protocol. +//| The `bitbangio` module contains classes to provide digital bus protocol +//| support regardless of whether the underlying hardware exists to use the +//| protocol. //| -//| First try to use `nativeio` module instead which may utilize peripheral +//| First try to use `busio` module instead which may utilize peripheral //| hardware to implement the protocols. Native implementations will be faster //| than bitbanged versions and have more capabilities. //| diff --git a/shared-bindings/nativeio/I2C.c b/shared-bindings/busio/I2C.c index fe3e206b2..7383cb104 100644 --- a/shared-bindings/nativeio/I2C.c +++ b/shared-bindings/busio/I2C.c @@ -25,14 +25,14 @@ */ // This file contains all of the Python API definitions for the -// nativeio.I2C class. +// busio.I2C class. #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/I2C.h" +#include "shared-bindings/busio/I2C.h" #include "lib/utils/context_manager_helpers.h" #include "py/runtime.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: busio //| //| :class:`I2C` --- Two wire serial protocol //| ------------------------------------------ @@ -55,10 +55,10 @@ //| :param ~microcontroller.Pin sda: The data pin //| :param int frequency: The clock frequency in Hertz //| -STATIC mp_obj_t nativeio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { +STATIC mp_obj_t busio_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); - nativeio_i2c_obj_t *self = m_new_obj(nativeio_i2c_obj_t); - self->base.type = &nativeio_i2c_type; + busio_i2c_obj_t *self = m_new_obj(busio_i2c_obj_t); + self->base.type = &busio_i2c_type; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); enum { ARG_scl, ARG_sda, ARG_frequency }; @@ -75,7 +75,7 @@ STATIC mp_obj_t nativeio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, assert_pin_free(scl); const mcu_pin_obj_t* sda = MP_OBJ_TO_PTR(args[ARG_sda].u_obj); assert_pin_free(sda); - common_hal_nativeio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int); + common_hal_busio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int); return (mp_obj_t)self; } @@ -83,12 +83,12 @@ STATIC mp_obj_t nativeio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, //| //| Releases control of the underlying hardware so other classes can use it. //| -STATIC mp_obj_t nativeio_i2c_obj_deinit(mp_obj_t self_in) { - nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_i2c_deinit(self); +STATIC mp_obj_t busio_i2c_obj_deinit(mp_obj_t self_in) { + busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_busio_i2c_deinit(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_deinit_obj, nativeio_i2c_obj_deinit); +MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_deinit_obj, busio_i2c_obj_deinit); //| .. method:: I2C.__enter__() //| @@ -100,15 +100,15 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_deinit_obj, nativeio_i2c_obj_deinit); //| //| Automatically deinitializes the hardware on context exit. //| -STATIC mp_obj_t nativeio_i2c_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t busio_i2c_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_i2c_deinit(args[0]); + common_hal_busio_i2c_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_i2c___exit___obj, 4, 4, nativeio_i2c_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_i2c___exit___obj, 4, 4, busio_i2c_obj___exit__); -static void check_lock(nativeio_i2c_obj_t *self) { - if (!common_hal_nativeio_i2c_has_lock(self)) { +static void check_lock(busio_i2c_obj_t *self) { + if (!common_hal_busio_i2c_has_lock(self)) { mp_raise_RuntimeError("Function requires lock."); } } @@ -121,20 +121,20 @@ static void check_lock(nativeio_i2c_obj_t *self) { //| :return: List of device ids on the I2C bus //| :rtype: list //| -STATIC mp_obj_t nativeio_i2c_scan(mp_obj_t self_in) { - nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t busio_i2c_scan(mp_obj_t self_in) { + busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); check_lock(self); mp_obj_t list = mp_obj_new_list(0, NULL); // 7-bit addresses 0b0000xxx and 0b1111xxx are reserved for (int addr = 0x08; addr < 0x78; ++addr) { - bool success = common_hal_nativeio_i2c_probe(self, addr); + bool success = common_hal_busio_i2c_probe(self, addr); if (success) { mp_obj_list_append(list, MP_OBJ_NEW_SMALL_INT(addr)); } } return list; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_scan_obj, nativeio_i2c_scan); +MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_scan_obj, busio_i2c_scan); //| .. method:: I2C.try_lock() //| @@ -143,20 +143,20 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_scan_obj, nativeio_i2c_scan); //| :return: True when lock has been grabbed //| :rtype: bool //| -STATIC mp_obj_t nativeio_i2c_obj_try_lock(mp_obj_t self_in) { - return mp_obj_new_bool(common_hal_nativeio_i2c_try_lock(MP_OBJ_TO_PTR(self_in))); +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))); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_try_lock_obj, nativeio_i2c_obj_try_lock); +MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_try_lock_obj, busio_i2c_obj_try_lock); //| .. method:: I2C.unlock() //| //| Releases the I2C lock. //| -STATIC mp_obj_t nativeio_i2c_obj_unlock(mp_obj_t self_in) { - common_hal_nativeio_i2c_unlock(MP_OBJ_TO_PTR(self_in)); +STATIC mp_obj_t busio_i2c_obj_unlock(mp_obj_t self_in) { + common_hal_busio_i2c_unlock(MP_OBJ_TO_PTR(self_in)); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_unlock_obj, nativeio_i2c_obj_unlock); +MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_unlock_obj, busio_i2c_obj_unlock); //| .. method:: I2C.readfrom_into(address, buffer, \*, start=0, end=len(buffer)) //| @@ -172,7 +172,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_unlock_obj, nativeio_i2c_obj_unlock); //| :param int start: Index to start writing at //| :param int end: Index to write up to but not include //| -STATIC mp_obj_t nativeio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +STATIC mp_obj_t busio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_address, ARG_buffer, ARG_start, ARG_end }; static const mp_arg_t allowed_args[] = { { MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, @@ -180,7 +180,7 @@ STATIC mp_obj_t nativeio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_ar { MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, }; - nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); 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); @@ -198,14 +198,14 @@ STATIC mp_obj_t nativeio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_ar } else if (len > bufinfo.len) { len = bufinfo.len; } - uint8_t status = common_hal_nativeio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, len); + uint8_t status = common_hal_busio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, len); if (status != 0) { mp_raise_OSError(status); } return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_i2c_readfrom_into_obj, 3, nativeio_i2c_readfrom_into); +MP_DEFINE_CONST_FUN_OBJ_KW(busio_i2c_readfrom_into_obj, 3, busio_i2c_readfrom_into); //| .. method:: I2C.writeto(address, buffer, \*, start=0, end=len(buffer), stop=True) //| @@ -223,7 +223,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_i2c_readfrom_into_obj, 3, nativeio_i2c_readf //| :param bool stop: If true, output an I2C stop condition after the //| buffer is written //| -STATIC mp_obj_t nativeio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +STATIC mp_obj_t busio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_address, ARG_buffer, ARG_start, ARG_end, ARG_stop }; static const mp_arg_t allowed_args[] = { { MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} }, @@ -232,7 +232,7 @@ STATIC mp_obj_t nativeio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, { MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, }; - nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); 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); @@ -254,33 +254,33 @@ STATIC mp_obj_t nativeio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp } // do the transfer - uint8_t status = common_hal_nativeio_i2c_write(self, args[ARG_address].u_int, + uint8_t status = common_hal_busio_i2c_write(self, args[ARG_address].u_int, ((uint8_t*) bufinfo.buf) + start, len, args[ARG_stop].u_bool); if (status != 0) { mp_raise_OSError(status); } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_i2c_writeto_obj, 1, nativeio_i2c_writeto); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(busio_i2c_writeto_obj, 1, busio_i2c_writeto); -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) }, +STATIC const mp_rom_map_elem_t busio_i2c_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_i2c_deinit_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) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_i2c___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&busio_i2c_scan_obj) }, - { MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&nativeio_i2c_try_lock_obj) }, - { MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&nativeio_i2c_unlock_obj) }, + { MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&busio_i2c_try_lock_obj) }, + { MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&busio_i2c_unlock_obj) }, - { MP_ROM_QSTR(MP_QSTR_readfrom_into), MP_ROM_PTR(&nativeio_i2c_readfrom_into_obj) }, - { MP_ROM_QSTR(MP_QSTR_writeto), MP_ROM_PTR(&nativeio_i2c_writeto_obj) }, + { MP_ROM_QSTR(MP_QSTR_readfrom_into), MP_ROM_PTR(&busio_i2c_readfrom_into_obj) }, + { MP_ROM_QSTR(MP_QSTR_writeto), MP_ROM_PTR(&busio_i2c_writeto_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_i2c_locals_dict, nativeio_i2c_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(busio_i2c_locals_dict, busio_i2c_locals_dict_table); -const mp_obj_type_t nativeio_i2c_type = { +const mp_obj_type_t busio_i2c_type = { { &mp_type_type }, .name = MP_QSTR_I2C, - .make_new = nativeio_i2c_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_i2c_locals_dict, + .make_new = busio_i2c_make_new, + .locals_dict = (mp_obj_dict_t*)&busio_i2c_locals_dict, }; diff --git a/shared-bindings/nativeio/I2C.h b/shared-bindings/busio/I2C.h index 5f3c89a77..0d260a309 100644 --- a/shared-bindings/nativeio/I2C.h +++ b/shared-bindings/busio/I2C.h @@ -31,40 +31,40 @@ // This file includes externs for all functions a port should implement to // support the machine module. -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_I2C_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_I2C_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_I2C_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_I2C_H__ #include "py/obj.h" #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/busio/I2C.h" // Type object used in Python. Should be shared between ports. -extern const mp_obj_type_t nativeio_i2c_type; +extern const mp_obj_type_t busio_i2c_type; // Initializes the hardware peripheral. -extern void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self, +extern void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t * scl, const mcu_pin_obj_t * sda, uint32_t frequency); -extern void common_hal_nativeio_i2c_deinit(nativeio_i2c_obj_t *self); +extern void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self); -extern bool common_hal_nativeio_i2c_try_lock(nativeio_i2c_obj_t *self); -extern bool common_hal_nativeio_i2c_has_lock(nativeio_i2c_obj_t *self); -extern void common_hal_nativeio_i2c_unlock(nativeio_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); +extern void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self); // Probe the bus to see if a device acknowledges the given address. -extern bool common_hal_nativeio_i2c_probe(nativeio_i2c_obj_t *self, uint8_t addr); +extern bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr); // Write to the device and return 0 on success or an appropriate error code from mperrno.h -extern uint8_t common_hal_nativeio_i2c_write(nativeio_i2c_obj_t *self, uint16_t address, +extern uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t address, const uint8_t * data, size_t len, bool stop); // Reads memory of the i2c device picking up where it left off and return 0 on // success or an appropriate error code from mperrno.h -extern uint8_t common_hal_nativeio_i2c_read(nativeio_i2c_obj_t *self, uint16_t address, +extern uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t address, uint8_t * data, size_t len); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_I2C_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_I2C_H__ diff --git a/shared-bindings/nativeio/OneWire.c b/shared-bindings/busio/OneWire.c index f9fbbe63c..52aed2135 100644 --- a/shared-bindings/nativeio/OneWire.c +++ b/shared-bindings/busio/OneWire.c @@ -31,14 +31,14 @@ #include "py/runtime.h" #include "py/runtime0.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/OneWire.h" +#include "shared-bindings/busio/OneWire.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: busio //| //| :class:`OneWire` -- Lowest-level of the Maxim OneWire protocol //| ================================================================= //| -//| :class:`~nativeio.OneWire` implements the timing-sensitive foundation of the Maxim +//| :class:`~busio.OneWire` implements the timing-sensitive foundation of the Maxim //| (formerly Dallas Semi) OneWire protocol. //| //| Protocol definition is here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/126 @@ -52,16 +52,16 @@ //| //| Read a short series of pulses:: //| -//| import nativeio +//| import busio //| import board //| -//| with nativeio.OneWire(board.D7) as onewire: +//| with busio.OneWire(board.D7) as onewire: //| onewire.reset() //| onewire.write_bit(True) //| onewire.write_bit(False) //| print(onewire.read_bit()) //| -STATIC mp_obj_t nativeio_onewire_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { +STATIC mp_obj_t busio_onewire_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, 1, MP_OBJ_FUN_ARGS_MAX, true); mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); @@ -75,10 +75,10 @@ STATIC mp_obj_t nativeio_onewire_make_new(const mp_obj_type_t *type, size_t n_ar const mcu_pin_obj_t* pin = MP_OBJ_TO_PTR(args[ARG_pin].u_obj); assert_pin_free(pin); - nativeio_onewire_obj_t *self = m_new_obj(nativeio_onewire_obj_t); - self->base.type = &nativeio_onewire_type; + busio_onewire_obj_t *self = m_new_obj(busio_onewire_obj_t); + self->base.type = &busio_onewire_type; - common_hal_nativeio_onewire_construct(self, pin); + common_hal_busio_onewire_construct(self, pin); return MP_OBJ_FROM_PTR(self); } @@ -86,12 +86,12 @@ STATIC mp_obj_t nativeio_onewire_make_new(const mp_obj_type_t *type, size_t n_ar //| //| Deinitialize the OneWire bus and release any hardware resources for reuse. //| -STATIC mp_obj_t nativeio_onewire_deinit(mp_obj_t self_in) { - nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_onewire_deinit(self); +STATIC mp_obj_t busio_onewire_deinit(mp_obj_t self_in) { + busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_busio_onewire_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_deinit_obj, nativeio_onewire_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_deinit_obj, busio_onewire_deinit); //| .. method:: __enter__() //| @@ -103,12 +103,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_deinit_obj, nativeio_onewire_d //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_onewire_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t busio_onewire_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_onewire_deinit(args[0]); + common_hal_busio_onewire_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_onewire___exit___obj, 4, 4, nativeio_onewire_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_onewire___exit___obj, 4, 4, busio_onewire_obj___exit__); //| .. method:: reset() //| @@ -117,12 +117,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_onewire___exit___obj, 4, 4, //| :returns: False when at least one device is present //| :rtype: bool //| -STATIC mp_obj_t nativeio_onewire_obj_reset(mp_obj_t self_in) { - nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t busio_onewire_obj_reset(mp_obj_t self_in) { + busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(common_hal_nativeio_onewire_reset(self)); + return mp_obj_new_bool(common_hal_busio_onewire_reset(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_reset_obj, nativeio_onewire_obj_reset); +MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_reset_obj, busio_onewire_obj_reset); //| .. method:: read_bit() //| @@ -131,39 +131,39 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_reset_obj, nativeio_onewire_obj_reset //| :returns: bit state read //| :rtype: bool //| -STATIC mp_obj_t nativeio_onewire_obj_read_bit(mp_obj_t self_in) { - nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); +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); - return mp_obj_new_bool(common_hal_nativeio_onewire_read_bit(self)); + return mp_obj_new_bool(common_hal_busio_onewire_read_bit(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_read_bit_obj, nativeio_onewire_obj_read_bit); +MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_read_bit_obj, busio_onewire_obj_read_bit); //| .. method:: write_bit(value) //| //| Write out a bit based on value. //| -STATIC mp_obj_t nativeio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) { - nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); +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); - common_hal_nativeio_onewire_write_bit(self, mp_obj_is_true(bool_obj)); + common_hal_busio_onewire_write_bit(self, mp_obj_is_true(bool_obj)); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_onewire_write_bit_obj, nativeio_onewire_obj_write_bit); +MP_DEFINE_CONST_FUN_OBJ_2(busio_onewire_write_bit_obj, busio_onewire_obj_write_bit); -STATIC const mp_rom_map_elem_t nativeio_onewire_locals_dict_table[] = { +STATIC const mp_rom_map_elem_t busio_onewire_locals_dict_table[] = { // Methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_onewire_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_onewire_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_onewire___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&nativeio_onewire_reset_obj) }, - { MP_ROM_QSTR(MP_QSTR_read_bit), MP_ROM_PTR(&nativeio_onewire_read_bit_obj) }, - { MP_ROM_QSTR(MP_QSTR_write_bit), MP_ROM_PTR(&nativeio_onewire_write_bit_obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_onewire___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&busio_onewire_reset_obj) }, + { MP_ROM_QSTR(MP_QSTR_read_bit), MP_ROM_PTR(&busio_onewire_read_bit_obj) }, + { MP_ROM_QSTR(MP_QSTR_write_bit), MP_ROM_PTR(&busio_onewire_write_bit_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_onewire_locals_dict, nativeio_onewire_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(busio_onewire_locals_dict, busio_onewire_locals_dict_table); -const mp_obj_type_t nativeio_onewire_type = { +const mp_obj_type_t busio_onewire_type = { { &mp_type_type }, .name = MP_QSTR_OneWire, - .make_new = nativeio_onewire_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_onewire_locals_dict, + .make_new = busio_onewire_make_new, + .locals_dict = (mp_obj_dict_t*)&busio_onewire_locals_dict, }; diff --git a/shared-bindings/nativeio/OneWire.h b/shared-bindings/busio/OneWire.h index 9d5c2c932..df5cecb7c 100644 --- a/shared-bindings/nativeio/OneWire.h +++ b/shared-bindings/busio/OneWire.h @@ -24,19 +24,19 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_ONEWIRE_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_ONEWIRE_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/busio/OneWire.h" -extern const mp_obj_type_t nativeio_onewire_type; +extern const mp_obj_type_t busio_onewire_type; -extern void common_hal_nativeio_onewire_construct(nativeio_onewire_obj_t* self, +extern void common_hal_busio_onewire_construct(busio_onewire_obj_t* self, const mcu_pin_obj_t* pin); -extern void common_hal_nativeio_onewire_deinit(nativeio_onewire_obj_t* self); -extern bool common_hal_nativeio_onewire_reset(nativeio_onewire_obj_t* self); -extern bool common_hal_nativeio_onewire_read_bit(nativeio_onewire_obj_t* self); -extern void common_hal_nativeio_onewire_write_bit(nativeio_onewire_obj_t* self, bool bit); +extern void common_hal_busio_onewire_deinit(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); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_ONEWIRE_H__ diff --git a/shared-bindings/nativeio/SPI.c b/shared-bindings/busio/SPI.c index 8d545a127..d4d9ec345 100644 --- a/shared-bindings/nativeio/SPI.c +++ b/shared-bindings/busio/SPI.c @@ -25,25 +25,25 @@ */ // This file contains all of the Python API definitions for the -// nativeio.SPI class. +// busio.SPI class. #include <string.h> #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/SPI.h" +#include "shared-bindings/busio/SPI.h" #include "lib/utils/context_manager_helpers.h" #include "py/mperrno.h" #include "py/nlr.h" #include "py/runtime.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: busio //| //| :class:`SPI` -- a 3-4 wire serial protocol //| ----------------------------------------------- //| //| SPI is a serial protocol that has exclusive pins for data in and out of the -//| master. It is typically faster than :py:class:`~nativeio.I2C` because a +//| master. It is typically faster than :py:class:`~busio.I2C` because a //| separate pin is used to control the active slave rather than a transitted //| address. This class only manages three of the four SPI lines: `!clock`, //| `!MOSI`, `!MISO`. Its up to the client to manage the appropriate slave @@ -68,10 +68,10 @@ //| // TODO(tannewt): Support LSB SPI. -STATIC mp_obj_t nativeio_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { +STATIC mp_obj_t busio_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); - nativeio_spi_obj_t *self = m_new_obj(nativeio_spi_obj_t); - self->base.type = &nativeio_spi_type; + busio_spi_obj_t *self = m_new_obj(busio_spi_obj_t); + self->base.type = &busio_spi_type; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); enum { ARG_clock, ARG_MOSI, ARG_MISO }; @@ -91,7 +91,7 @@ STATIC mp_obj_t nativeio_spi_make_new(const mp_obj_type_t *type, size_t n_args, assert_pin_free(mosi); const mcu_pin_obj_t* miso = MP_OBJ_TO_PTR(args[ARG_MISO].u_obj); assert_pin_free(miso); - common_hal_nativeio_spi_construct(self, clock, mosi, miso); + common_hal_busio_spi_construct(self, clock, mosi, miso); return (mp_obj_t)self; } @@ -99,12 +99,12 @@ STATIC mp_obj_t nativeio_spi_make_new(const mp_obj_type_t *type, size_t n_args, //| //| Turn off the SPI bus. //| -STATIC mp_obj_t nativeio_spi_obj_deinit(mp_obj_t self_in) { - nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_spi_deinit(self); +STATIC mp_obj_t busio_spi_obj_deinit(mp_obj_t self_in) { + busio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_busio_spi_deinit(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_deinit_obj, nativeio_spi_obj_deinit); +MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_deinit_obj, busio_spi_obj_deinit); //| .. method:: SPI.__enter__() //| @@ -116,15 +116,15 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_deinit_obj, nativeio_spi_obj_deinit); //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_spi_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t busio_spi_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_spi_deinit(args[0]); + common_hal_busio_spi_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_spi_obj___exit___obj, 4, 4, nativeio_spi_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_spi_obj___exit___obj, 4, 4, busio_spi_obj___exit__); -static void check_lock(nativeio_spi_obj_t *self) { - if (!common_hal_nativeio_spi_has_lock(self)) { +static void check_lock(busio_spi_obj_t *self) { + if (!common_hal_busio_spi_has_lock(self)) { mp_raise_RuntimeError("Function requires lock"); } } @@ -139,7 +139,7 @@ static void check_lock(nativeio_spi_obj_t *self) { //| or second (1). Rising or falling depends on clock polarity. //| :param int bits: the number of bits per word //| -STATIC mp_obj_t nativeio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +STATIC mp_obj_t busio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits }; static const mp_arg_t allowed_args[] = { { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} }, @@ -147,7 +147,7 @@ STATIC mp_obj_t nativeio_spi_configure(size_t n_args, const mp_obj_t *pos_args, { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, }; - nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); 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); @@ -165,13 +165,13 @@ STATIC mp_obj_t nativeio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_raise_ValueError("Invalid number of bits"); } - if (!common_hal_nativeio_spi_configure(self, args[ARG_baudrate].u_int, + if (!common_hal_busio_spi_configure(self, args[ARG_baudrate].u_int, polarity, phase, bits)) { mp_raise_OSError(MP_EIO); } return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_configure_obj, 1, nativeio_spi_configure); +MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_configure_obj, 1, busio_spi_configure); //| .. method:: SPI.try_lock() //| @@ -180,20 +180,20 @@ MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_configure_obj, 1, nativeio_spi_configure //| :return: True when lock has been grabbed //| :rtype: bool //| -STATIC mp_obj_t nativeio_spi_obj_try_lock(mp_obj_t self_in) { - return mp_obj_new_bool(common_hal_nativeio_spi_try_lock(MP_OBJ_TO_PTR(self_in))); +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))); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_try_lock_obj, nativeio_spi_obj_try_lock); +MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_try_lock_obj, busio_spi_obj_try_lock); //| .. method:: SPI.unlock() //| //| Releases the SPI lock. //| -STATIC mp_obj_t nativeio_spi_obj_unlock(mp_obj_t self_in) { - common_hal_nativeio_spi_unlock(MP_OBJ_TO_PTR(self_in)); +STATIC mp_obj_t busio_spi_obj_unlock(mp_obj_t self_in) { + common_hal_busio_spi_unlock(MP_OBJ_TO_PTR(self_in)); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_unlock_obj, nativeio_spi_obj_unlock); +MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_unlock_obj, busio_spi_obj_unlock); //| .. method:: SPI.write(buffer, \*, start=0, end=len(buffer)) //| @@ -203,14 +203,14 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_unlock_obj, nativeio_spi_obj_unlock); //| :param int start: Index to start writing from //| :param int end: Index to read up to but not include //| -STATIC mp_obj_t nativeio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +STATIC mp_obj_t busio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_buffer, ARG_start, ARG_end }; static const mp_arg_t allowed_args[] = { { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, }; - nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); 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,13 +229,13 @@ STATIC mp_obj_t nativeio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_m len = bufinfo.len; } - bool ok = common_hal_nativeio_spi_write(self, ((uint8_t*)bufinfo.buf) + start, len); + bool ok = common_hal_busio_spi_write(self, ((uint8_t*)bufinfo.buf) + start, len); if (!ok) { mp_raise_OSError(MP_EIO); } return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_write_obj, 2, nativeio_spi_write); +MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_write_obj, 2, busio_spi_write); //| .. method:: SPI.readinto(buffer, \*, start=0, end=len(buffer), write_value=0) @@ -247,7 +247,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_write_obj, 2, nativeio_spi_write); //| :param int end: Index to write up to but not include //| :param int write_value: Value to write reading. (Usually ignored.) //| -STATIC mp_obj_t nativeio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +STATIC mp_obj_t busio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_buffer, ARG_start, ARG_end, ARG_write_value }; static const mp_arg_t allowed_args[] = { { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, @@ -255,7 +255,7 @@ STATIC mp_obj_t nativeio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, m { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, { MP_QSTR_write_value,MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, }; - nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); 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); @@ -274,31 +274,31 @@ STATIC mp_obj_t nativeio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, m len = bufinfo.len; } - bool ok = common_hal_nativeio_spi_read(self, ((uint8_t*)bufinfo.buf) + start, len, args[ARG_write_value].u_int); + bool ok = common_hal_busio_spi_read(self, ((uint8_t*)bufinfo.buf) + start, len, args[ARG_write_value].u_int); if (!ok) { mp_raise_OSError(MP_EIO); } return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_readinto_obj, 2, nativeio_spi_readinto); +MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_readinto_obj, 2, busio_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) }, +STATIC const mp_rom_map_elem_t busio_spi_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_spi_deinit_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___exit__), MP_ROM_PTR(&busio_spi_obj___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_configure), MP_ROM_PTR(&nativeio_spi_configure_obj) }, - { MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&nativeio_spi_try_lock_obj) }, - { MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&nativeio_spi_unlock_obj) }, + { MP_ROM_QSTR(MP_QSTR_configure), MP_ROM_PTR(&busio_spi_configure_obj) }, + { MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&busio_spi_try_lock_obj) }, + { MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&busio_spi_unlock_obj) }, - { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&nativeio_spi_readinto_obj) }, - { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&nativeio_spi_write_obj) }, + { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&busio_spi_readinto_obj) }, + { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&busio_spi_write_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_spi_locals_dict, nativeio_spi_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(busio_spi_locals_dict, busio_spi_locals_dict_table); -const mp_obj_type_t nativeio_spi_type = { +const mp_obj_type_t busio_spi_type = { { &mp_type_type }, .name = MP_QSTR_SPI, - .make_new = nativeio_spi_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_spi_locals_dict, + .make_new = busio_spi_make_new, + .locals_dict = (mp_obj_dict_t*)&busio_spi_locals_dict, }; diff --git a/shared-bindings/nativeio/SPI.h b/shared-bindings/busio/SPI.h index b997ddf67..e3deb0e72 100644 --- a/shared-bindings/nativeio/SPI.h +++ b/shared-bindings/busio/SPI.h @@ -24,34 +24,34 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_SPI_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_SPI_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_SPI_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_SPI_H__ #include "py/obj.h" #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/busio/SPI.h" // Type object used in Python. Should be shared between ports. -extern const mp_obj_type_t nativeio_spi_type; +extern const mp_obj_type_t busio_spi_type; // Construct an underlying SPI object. -extern void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self, +extern void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi, const mcu_pin_obj_t * miso); -extern void common_hal_nativeio_spi_deinit(nativeio_spi_obj_t *self); +extern void common_hal_busio_spi_deinit(busio_spi_obj_t *self); -extern bool common_hal_nativeio_spi_configure(nativeio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits); +extern bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits); -extern bool common_hal_nativeio_spi_try_lock(nativeio_spi_obj_t *self); -extern bool common_hal_nativeio_spi_has_lock(nativeio_spi_obj_t *self); -extern void common_hal_nativeio_spi_unlock(nativeio_spi_obj_t *self); +extern bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self); +extern bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self); +extern void common_hal_busio_spi_unlock(busio_spi_obj_t *self); // Writes out the given data. -extern bool common_hal_nativeio_spi_write(nativeio_spi_obj_t *self, const uint8_t *data, size_t len); +extern bool common_hal_busio_spi_write(busio_spi_obj_t *self, const uint8_t *data, size_t len); // Reads in len bytes while outputting zeroes. -extern bool common_hal_nativeio_spi_read(nativeio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value); +extern bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_SPI_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_SPI_H__ diff --git a/shared-bindings/nativeio/UART.c b/shared-bindings/busio/UART.c index c56130593..11ee91355 100644 --- a/shared-bindings/nativeio/UART.c +++ b/shared-bindings/busio/UART.c @@ -26,7 +26,7 @@ #include <stdint.h> -#include "shared-bindings/nativeio/UART.h" +#include "shared-bindings/busio/UART.h" #include "lib/utils/context_manager_helpers.h" @@ -36,7 +36,7 @@ #include "shared-bindings/microcontroller/Pin.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: busio //| //| :class:`UART` -- a bidirectional serial protocol //| ================================================= @@ -58,14 +58,14 @@ //| typedef struct { mp_obj_base_t base; -} nativeio_uart_parity_obj_t; -extern const nativeio_uart_parity_obj_t nativeio_uart_parity_even_obj; -extern const nativeio_uart_parity_obj_t nativeio_uart_parity_odd_obj; +} busio_uart_parity_obj_t; +extern const busio_uart_parity_obj_t busio_uart_parity_even_obj; +extern const busio_uart_parity_obj_t busio_uart_parity_odd_obj; -STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { +STATIC mp_obj_t busio_uart_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); - nativeio_uart_obj_t *self = m_new_obj(nativeio_uart_obj_t); - self->base.type = &nativeio_uart_type; + busio_uart_obj_t *self = m_new_obj(busio_uart_obj_t); + self->base.type = &busio_uart_type; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); enum { ARG_tx, ARG_rx, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_timeout, ARG_receiver_buffer_size}; @@ -96,9 +96,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_parity].u_obj == &nativeio_uart_parity_even_obj) { + if (args[ARG_parity].u_obj == &busio_uart_parity_even_obj) { parity = PARITY_EVEN; - } else if (args[ARG_parity].u_obj == &nativeio_uart_parity_odd_obj) { + } else if (args[ARG_parity].u_obj == &busio_uart_parity_odd_obj) { parity = PARITY_ODD; } @@ -107,7 +107,7 @@ STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args, mp_raise_ValueError("stop must be 1 or 2"); } - common_hal_nativeio_uart_construct(self, tx, rx, + common_hal_busio_uart_construct(self, tx, rx, args[ARG_baudrate].u_int, bits, parity, stop, args[ARG_timeout].u_int, args[ARG_receiver_buffer_size].u_int); return (mp_obj_t)self; @@ -117,12 +117,12 @@ STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args, //| //| Deinitialises the UART and releases any hardware resources for reuse. //| -STATIC mp_obj_t nativeio_uart_obj_deinit(mp_obj_t self_in) { - nativeio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_uart_deinit(self); +STATIC mp_obj_t busio_uart_obj_deinit(mp_obj_t self_in) { + busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_busio_uart_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_uart_deinit_obj, nativeio_uart_obj_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(busio_uart_deinit_obj, busio_uart_obj_deinit); //| .. method:: __enter__() //| @@ -134,12 +134,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_uart_deinit_obj, nativeio_uart_obj_dei //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_uart_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t busio_uart_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_uart_deinit(args[0]); + common_hal_busio_uart_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_uart___exit___obj, 4, 4, nativeio_uart_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_uart_obj___exit__); // These are standard stream methods. Code is in py/stream.c. // @@ -175,8 +175,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_uart___exit___obj, 4, 4, nat //| // These three methods are used by the shared stream methods. -STATIC mp_uint_t nativeio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { - nativeio_uart_obj_t *self = self_in; +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; byte *buf = buf_in; // make sure we want at least 1 char @@ -184,26 +184,26 @@ STATIC mp_uint_t nativeio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t si return 0; } - return common_hal_nativeio_uart_read(self, buf, size, errcode); + return common_hal_busio_uart_read(self, buf, size, errcode); } -STATIC mp_uint_t nativeio_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { - nativeio_uart_obj_t *self = self_in; +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; const byte *buf = buf_in; - return common_hal_nativeio_uart_write(self, buf, size, errcode); + return common_hal_busio_uart_write(self, buf, size, errcode); } -STATIC mp_uint_t nativeio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { - nativeio_uart_obj_t *self = self_in; +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; mp_uint_t ret; if (request == MP_IOCTL_POLL) { mp_uint_t flags = arg; ret = 0; - if ((flags & MP_IOCTL_POLL_RD) && common_hal_nativeio_uart_rx_characters_available(self) > 0) { + if ((flags & MP_IOCTL_POLL_RD) && common_hal_busio_uart_rx_characters_available(self) > 0) { ret |= MP_IOCTL_POLL_RD; } - if ((flags & MP_IOCTL_POLL_WR) && common_hal_nativeio_uart_ready_to_tx(self)) { + if ((flags & MP_IOCTL_POLL_WR) && common_hal_busio_uart_ready_to_tx(self)) { ret |= MP_IOCTL_POLL_WR; } } else { @@ -213,7 +213,7 @@ STATIC mp_uint_t nativeio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uin return ret; } -//| .. class:: nativeio.UART.Parity +//| .. class:: busio.UART.Parity //| //| Enum-like class to define the parity used to verify correct data transfer. //| @@ -225,41 +225,41 @@ STATIC mp_uint_t nativeio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uin //| //| Total number of ones should be even. //| -const mp_obj_type_t nativeio_uart_parity_type; +const mp_obj_type_t busio_uart_parity_type; -const nativeio_uart_parity_obj_t nativeio_uart_parity_odd_obj = { - { &nativeio_uart_parity_type }, +const busio_uart_parity_obj_t busio_uart_parity_odd_obj = { + { &busio_uart_parity_type }, }; -const nativeio_uart_parity_obj_t nativeio_uart_parity_even_obj = { - { &nativeio_uart_parity_type }, +const busio_uart_parity_obj_t busio_uart_parity_even_obj = { + { &busio_uart_parity_type }, }; -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) }, +STATIC const mp_rom_map_elem_t busio_uart_parity_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_ODD), MP_ROM_PTR(&busio_uart_parity_odd_obj) }, + { MP_ROM_QSTR(MP_QSTR_EVEN), MP_ROM_PTR(&busio_uart_parity_even_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_uart_parity_locals_dict, nativeio_uart_parity_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(busio_uart_parity_locals_dict, busio_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) { +STATIC void busio_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)) { + if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&busio_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); + mp_printf(print, "%q.%q.%q.%q", MP_QSTR_busio, MP_QSTR_UART, MP_QSTR_Parity, parity); } -const mp_obj_type_t nativeio_uart_parity_type = { +const mp_obj_type_t busio_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, + .print = busio_uart_parity_print, + .locals_dict = (mp_obj_t)&busio_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) }, +STATIC const mp_rom_map_elem_t busio_uart_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_uart_deinit_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) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_uart___exit___obj) }, // Standard stream methods. { MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, @@ -268,23 +268,23 @@ STATIC const mp_rom_map_elem_t nativeio_uart_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, // Nested Enum-like Classes. - { MP_ROM_QSTR(MP_QSTR_Parity), MP_ROM_PTR(&nativeio_uart_parity_type) }, + { MP_ROM_QSTR(MP_QSTR_Parity), MP_ROM_PTR(&busio_uart_parity_type) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_uart_locals_dict, nativeio_uart_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(busio_uart_locals_dict, busio_uart_locals_dict_table); STATIC const mp_stream_p_t uart_stream_p = { - .read = nativeio_uart_read, - .write = nativeio_uart_write, - .ioctl = nativeio_uart_ioctl, + .read = busio_uart_read, + .write = busio_uart_write, + .ioctl = busio_uart_ioctl, .is_text = false, }; -const mp_obj_type_t nativeio_uart_type = { +const mp_obj_type_t busio_uart_type = { { &mp_type_type }, .name = MP_QSTR_UART, - .make_new = nativeio_uart_make_new, + .make_new = busio_uart_make_new, .getiter = mp_identity, .iternext = mp_stream_unbuffered_iter, .protocol = &uart_stream_p, - .locals_dict = (mp_obj_dict_t*)&nativeio_uart_locals_dict, + .locals_dict = (mp_obj_dict_t*)&busio_uart_locals_dict, }; diff --git a/shared-bindings/nativeio/UART.h b/shared-bindings/busio/UART.h index bafc5b730..e0a8a0b09 100644 --- a/shared-bindings/nativeio/UART.h +++ b/shared-bindings/busio/UART.h @@ -24,13 +24,13 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_UART_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_UART_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_UART_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_UART_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/busio/UART.h" -extern const mp_obj_type_t nativeio_uart_type; +extern const mp_obj_type_t busio_uart_type; typedef enum { PARITY_NONE, @@ -39,22 +39,22 @@ typedef enum { } uart_parity_t; // Construct an underlying UART object. -extern void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self, +extern void common_hal_busio_uart_construct(busio_uart_obj_t *self, const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout, uint8_t receiver_buffer_size); -extern void common_hal_nativeio_uart_deinit(nativeio_uart_obj_t *self); +extern void common_hal_busio_uart_deinit(busio_uart_obj_t *self); // Read characters. len is in characters NOT bytes! -extern size_t common_hal_nativeio_uart_read(nativeio_uart_obj_t *self, +extern size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode); // Write characters. len is in characters NOT bytes! -extern size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self, +extern size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode); -extern uint32_t common_hal_nativeio_uart_rx_characters_available(nativeio_uart_obj_t *self); -extern bool common_hal_nativeio_uart_ready_to_tx(nativeio_uart_obj_t *self); +extern uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self); +extern bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_UART_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_UART_H__ diff --git a/shared-bindings/busio/__init__.c b/shared-bindings/busio/__init__.c new file mode 100644 index 000000000..9e075752d --- /dev/null +++ b/shared-bindings/busio/__init__.c @@ -0,0 +1,98 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 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 <stdint.h> + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/busio/__init__.h" +#include "shared-bindings/busio/I2C.h" +#include "shared-bindings/busio/OneWire.h" +#include "shared-bindings/busio/SPI.h" +#include "shared-bindings/busio/UART.h" +#include "shared-bindings/busio/__init__.h" + +#include "py/runtime.h" + +//| :mod:`busio` --- Hardware accelerated behavior +//| ================================================= +//| +//| .. module:: busio +//| :synopsis: Hardware accelerated behavior +//| :platform: SAMD21 +//| +//| The `busio` module contains classes to support a variety of serial +//| protocols. +//| +//| When the microcontroller does not support the behavior in a hardware +//| accelerated fashion it may internally use a bitbang routine. However, if +//| hardware support is available on a subset of pins but not those provided, +//| then a RuntimeError will be raised. Use the `bitbangio` module to explicitly +//| bitbang a serial protocol on any general purpose pins. +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| I2C +//| OneWire +//| SPI +//| UART +//| +//| All libraries change hardware state and should be deinitialized when they +//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a +//| context manager. +//| +//| For example:: +//| +//| import busio +//| from board import * +//| +//| with busio.I2C(SCL, SDA) as i2c: +//| i2c.scan() +//| +//| This example will initialize the the device, run +//| :py:meth:`~busio.I2C.scan` and then :py:meth:`~busio.I2C.deinit` the +//| hardware. +//| + +STATIC const mp_rom_map_elem_t busio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_busio) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&busio_i2c_type) }, + { MP_ROM_QSTR(MP_QSTR_OneWire), MP_ROM_PTR(&busio_onewire_type) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&busio_spi_type) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&busio_uart_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(busio_module_globals, busio_module_globals_table); + +const mp_obj_module_t busio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&busio_module_globals, +}; diff --git a/shared-bindings/busio/__init__.h b/shared-bindings/busio/__init__.h new file mode 100644 index 000000000..214c2b144 --- /dev/null +++ b/shared-bindings/busio/__init__.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_BUSIO___INIT___H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO___INIT___H__ + +#include "py/obj.h" + +// Nothing now. + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO___INIT___H__ diff --git a/shared-bindings/digitalio/DigitalInOut.c b/shared-bindings/digitalio/DigitalInOut.c new file mode 100644 index 000000000..65ace8357 --- /dev/null +++ b/shared-bindings/digitalio/DigitalInOut.c @@ -0,0 +1,486 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * 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 <stdint.h> +#include <string.h> + +#include "lib/utils/context_manager_helpers.h" + +#include "py/nlr.h" +#include "py/objtype.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "py/mphal.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/digitalio/DigitalInOut.h" + +//| .. currentmodule:: digitalio +//| +//| :class:`DigitalInOut` -- digital input and output +//| ========================================================= +//| +//| A DigitalInOut is used to digitally control I/O pins. For analog control of +//| a pin, see the :py:class:`~digitalio.AnalogIn` and +//| :py:class:`~digitalio.AnalogOut` classes. +//| + +//| .. class:: DigitalInOut(pin) +//| +//| Create a new DigitalInOut object associated with the pin. Defaults to input +//| with no pull. Use :py:meth:`switch_to_input` and +//| :py:meth:`switch_to_output` to change the direction. +//| +//| :param ~microcontroller.Pin pin: The pin to control +//| +STATIC mp_obj_t digitalio_digitalinout_make_new(const mp_obj_type_t *type, + mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); + + digitalio_digitalinout_obj_t *self = m_new_obj(digitalio_digitalinout_obj_t); + self->base.type = &digitalio_digitalinout_type; + + assert_pin(args[0], false); + mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(args[0]); + assert_pin_free(pin); + common_hal_digitalio_digitalinout_construct(self, pin); + + return (mp_obj_t)self; +} + +//| .. method:: deinit() +//| +//| Turn off the DigitalInOut and release the pin for other use. +//| +STATIC mp_obj_t digitalio_digitalinout_obj_deinit(mp_obj_t self_in) { + digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_digitalio_digitalinout_deinit(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_deinit_obj, digitalio_digitalinout_obj_deinit); + +//| .. method:: __enter__() +//| +//| No-op used by Context Managers. +//| +// Provided by context manager helper. + +//| .. method:: __exit__() +//| +//| Automatically deinitializes the hardware when exiting a context. +//| +STATIC mp_obj_t digitalio_digitalinout_obj___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + common_hal_digitalio_digitalinout_deinit(args[0]); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(digitalio_digitalinout_obj___exit___obj, 4, 4, digitalio_digitalinout_obj___exit__); + +//| +//| .. 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 drive_mode: drive mode for the output +//| +typedef struct { + mp_obj_base_t base; +} digitalio_digitalinout_drive_mode_obj_t; +extern const digitalio_digitalinout_drive_mode_obj_t digitalio_digitalinout_drive_mode_push_pull_obj; +extern const digitalio_digitalinout_drive_mode_obj_t digitalio_digitalinout_drive_mode_open_drain_obj; + +STATIC mp_obj_t digitalio_digitalinout_switch_to_output(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_value, ARG_drive_mode }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_value, MP_ARG_BOOL, {.u_bool = false} }, + { MP_QSTR_drive_mode, MP_ARG_OBJ, {.u_rom_obj = &digitalio_digitalinout_drive_mode_push_pull_obj} }, + }; + digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + 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); + + enum digitalinout_drive_mode_t drive_mode = DRIVE_MODE_PUSH_PULL; + if (args[ARG_drive_mode].u_rom_obj == &digitalio_digitalinout_drive_mode_open_drain_obj) { + drive_mode = DRIVE_MODE_OPEN_DRAIN; + } + // do the transfer + common_hal_digitalio_digitalinout_switch_to_output(self, args[ARG_value].u_bool, drive_mode); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(digitalio_digitalinout_switch_to_output_obj, 1, digitalio_digitalinout_switch_to_output); + +//| .. method:: switch_to_input(pull=None) +//| +//| Switch to read in digital values. +//| +//| :param Pull pull: pull configuration for the input +//| +//| Example usage:: +//| +//| import digitalio +//| import board +//| +//| with digitalio.DigitalInOut(board.SLIDE_SWITCH) as switch: +//| switch.switch_to_input(pull=digitalio.DigitalInOut.Pull.UP) +//| # Or, after switch_to_input +//| switch.pull = digitalio.DigitalInOut.Pull.UP +//| print(switch.value) +//| +typedef struct { + mp_obj_base_t base; +} digitalio_digitalinout_pull_obj_t; +extern const digitalio_digitalinout_pull_obj_t digitalio_digitalinout_pull_up_obj; +extern const digitalio_digitalinout_pull_obj_t digitalio_digitalinout_pull_down_obj; + +STATIC mp_obj_t digitalio_digitalinout_switch_to_input(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_pull }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} }, + }; + digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + 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); + + enum digitalinout_pull_t pull = PULL_NONE; + if (args[ARG_pull].u_rom_obj == &digitalio_digitalinout_pull_up_obj) { + pull = PULL_UP; + }else if (args[ARG_pull].u_rom_obj == &digitalio_digitalinout_pull_down_obj) { + pull = PULL_DOWN; + } + // do the transfer + common_hal_digitalio_digitalinout_switch_to_input(self, pull); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(digitalio_digitalinout_switch_to_input_obj, 1, digitalio_digitalinout_switch_to_input); + +//| .. attribute:: direction +//| +//| Get the direction of the pin. +//| +//| :raises AttributeError: when set. Use :py:meth:`switch_to_input` and :py:meth:`switch_to_output` to change the direction. +//| +typedef struct { + mp_obj_base_t base; +} digitalio_digitalinout_direction_obj_t; +extern const digitalio_digitalinout_direction_obj_t digitalio_digitalinout_direction_in_obj; +extern const digitalio_digitalinout_direction_obj_t digitalio_digitalinout_direction_out_obj; + +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); + enum digitalinout_direction_t direction = common_hal_digitalio_digitalinout_get_direction(self); + if (direction == DIRECTION_IN) { + return (mp_obj_t)&digitalio_digitalinout_direction_in_obj; + } + return (mp_obj_t)&digitalio_digitalinout_direction_out_obj; +} +MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_direction_obj, digitalio_digitalinout_obj_get_direction); + +const mp_obj_property_t digitalio_digitalinout_direction_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&digitalio_digitalinout_get_direction_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. attribute:: value +//| +//| The digital logic level of the pin. +//| +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); + bool value = common_hal_digitalio_digitalinout_get_value(self); + return mp_obj_new_bool(value); +} +MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_value_obj, digitalio_digitalinout_obj_get_value); + +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); + if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_IN) { + mp_raise_AttributeError("Cannot set value when direction is input."); + return mp_const_none; + } + common_hal_digitalio_digitalinout_set_value(self, mp_obj_is_true(value)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_value_obj, digitalio_digitalinout_obj_set_value); + +const mp_obj_property_t digitalio_digitalinout_value_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&digitalio_digitalinout_get_value_obj, + (mp_obj_t)&digitalio_digitalinout_set_value_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. attribute:: drive_mode +//| +//| Get or set the pin drive mode. +//| +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); + if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_IN) { + mp_raise_AttributeError("Drive mode not used when direction is input."); + return mp_const_none; + } + enum digitalinout_drive_mode_t drive_mode = common_hal_digitalio_digitalinout_get_drive_mode(self); + if (drive_mode == DRIVE_MODE_PUSH_PULL) { + return (mp_obj_t)&digitalio_digitalinout_drive_mode_push_pull_obj; + } + return (mp_obj_t)&digitalio_digitalinout_drive_mode_open_drain_obj; +} +MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_drive_mode_obj, digitalio_digitalinout_obj_get_drive_mode); + +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); + if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_IN) { + mp_raise_AttributeError("Drive mode not used when direction is input."); + return mp_const_none; + } + enum digitalinout_drive_mode_t c_drive_mode = DRIVE_MODE_PUSH_PULL; + if (drive_mode == &digitalio_digitalinout_drive_mode_open_drain_obj) { + c_drive_mode = DRIVE_MODE_OPEN_DRAIN; + } + common_hal_digitalio_digitalinout_set_drive_mode(self, c_drive_mode); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_drive_mode_obj, digitalio_digitalinout_obj_set_drive_mode); + +const mp_obj_property_t digitalio_digitalinout_drive_mode_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&digitalio_digitalinout_get_drive_mode_obj, + (mp_obj_t)&digitalio_digitalinout_set_drive_mode_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. attribute:: pull +//| +//| Get or set the pin pull. +//| +//| :raises AttributeError: if the direction is ~`Direction.OUT`. +//| +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); + if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUT) { + mp_raise_AttributeError("Pull not used when direction is output."); + return mp_const_none; + } + enum digitalinout_pull_t pull = common_hal_digitalio_digitalinout_get_pull(self); + if (pull == PULL_UP) { + return (mp_obj_t)&digitalio_digitalinout_pull_up_obj; + } else if (pull == PULL_DOWN) { + return (mp_obj_t)&digitalio_digitalinout_pull_down_obj; + } + return (mp_obj_t)&mp_const_none_obj; +} +MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_pull_obj, digitalio_digitalinout_obj_get_pull); + +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); + if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUT) { + mp_raise_AttributeError("Pull not used when direction is output."); + return mp_const_none; + } + enum digitalinout_pull_t pull = PULL_NONE; + if (pull_obj == &digitalio_digitalinout_pull_up_obj) { + pull = PULL_UP; + } else if (pull_obj == &digitalio_digitalinout_pull_down_obj) { + pull = PULL_DOWN; + } + common_hal_digitalio_digitalinout_set_pull(self, pull); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_pull_obj, digitalio_digitalinout_obj_set_pull); + +const mp_obj_property_t digitalio_digitalinout_pull_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&digitalio_digitalinout_get_pull_obj, + (mp_obj_t)&digitalio_digitalinout_set_pull_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. class:: digitalio.DigitalInOut.Direction +//| +//| Enum-like class to define which direction the digital values are +//| going. +//| +//| .. data:: IN +//| +//| Read digital data in +//| +//| .. data:: OUT +//| +//| Write digital data out +//| +const mp_obj_type_t digitalio_digitalinout_direction_type; + +const digitalio_digitalinout_direction_obj_t digitalio_digitalinout_direction_in_obj = { + { &digitalio_digitalinout_direction_type }, +}; + +const digitalio_digitalinout_direction_obj_t digitalio_digitalinout_direction_out_obj = { + { &digitalio_digitalinout_direction_type }, +}; + +STATIC const mp_rom_map_elem_t digitalio_digitalinout_direction_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_PTR(&digitalio_digitalinout_direction_in_obj) }, + { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_PTR(&digitalio_digitalinout_direction_out_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(digitalio_digitalinout_direction_locals_dict, digitalio_digitalinout_direction_locals_dict_table); + +STATIC void digitalio_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(&digitalio_digitalinout_direction_out_obj)) { + direction = MP_QSTR_OUT; + } + mp_printf(print, "%q.%q.%q.%q", MP_QSTR_digitalio, MP_QSTR_DigitalInOut, MP_QSTR_Direction, direction); +} + +const mp_obj_type_t digitalio_digitalinout_direction_type = { + { &mp_type_type }, + .name = MP_QSTR_Direction, + .print = digitalio_digitalinout_direction_print, + .locals_dict = (mp_obj_t)&digitalio_digitalinout_direction_locals_dict, +}; + +//| .. class:: digitalio.DigitalInOut.DriveMode +//| +//| Enum-like class to define the drive mode used when outputting +//| digital values. +//| +//| .. data:: PUSH_PULL +//| +//| Output both high and low digital values +//| +//| .. 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. +//| +const mp_obj_type_t digitalio_digitalinout_drive_mode_type; + +const digitalio_digitalinout_drive_mode_obj_t digitalio_digitalinout_drive_mode_push_pull_obj = { + { &digitalio_digitalinout_drive_mode_type }, +}; + +const digitalio_digitalinout_drive_mode_obj_t digitalio_digitalinout_drive_mode_open_drain_obj = { + { &digitalio_digitalinout_drive_mode_type }, +}; + +STATIC const mp_rom_map_elem_t digitalio_digitalinout_drive_mode_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_PUSH_PULL), MP_ROM_PTR(&digitalio_digitalinout_drive_mode_push_pull_obj) }, + { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_PTR(&digitalio_digitalinout_drive_mode_open_drain_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(digitalio_digitalinout_drive_mode_locals_dict, digitalio_digitalinout_drive_mode_locals_dict_table); + +STATIC void digitalio_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(&digitalio_digitalinout_drive_mode_open_drain_obj)) { + drive_mode = MP_QSTR_OPEN_DRAIN; + } + mp_printf(print, "%q.%q.%q.%q", MP_QSTR_digitalio, MP_QSTR_DigitalInOut, MP_QSTR_DriveMode, drive_mode); +} + +const mp_obj_type_t digitalio_digitalinout_drive_mode_type = { + { &mp_type_type }, + .name = MP_QSTR_DriveMode, + .print = digitalio_digitalinout_drive_mode_print, + .locals_dict = (mp_obj_t)&digitalio_digitalinout_drive_mode_locals_dict, +}; + +//| .. class:: digitalio.DigitalInOut.Pull +//| +//| Enum-like class to define the pull value, if any, used while reading +//| digital values in. +//| +//| .. 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 +//| +//| When the input line isn't being driven the pull down can pull the +//| state of the line low so it reads as false. +//| +const mp_obj_type_t digitalio_digitalinout_pull_type; + +const digitalio_digitalinout_pull_obj_t digitalio_digitalinout_pull_up_obj = { + { &digitalio_digitalinout_pull_type }, +}; + +const digitalio_digitalinout_pull_obj_t digitalio_digitalinout_pull_down_obj = { + { &digitalio_digitalinout_pull_type }, +}; + +STATIC const mp_rom_map_elem_t digitalio_digitalinout_pull_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_UP), MP_ROM_PTR(&digitalio_digitalinout_pull_up_obj) }, + { MP_ROM_QSTR(MP_QSTR_DOWN), MP_ROM_PTR(&digitalio_digitalinout_pull_down_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(digitalio_digitalinout_pull_locals_dict, digitalio_digitalinout_pull_locals_dict_table); + +STATIC void digitalio_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(&digitalio_digitalinout_pull_down_obj)) { + pull = MP_QSTR_DOWN; + } + mp_printf(print, "%q.%q.%q.%q", MP_QSTR_digitalio, MP_QSTR_DigitalInOut, MP_QSTR_Pull, pull); +} + +const mp_obj_type_t digitalio_digitalinout_pull_type = { + { &mp_type_type }, + .name = MP_QSTR_Pull, + .print = digitalio_digitalinout_pull_print, + .locals_dict = (mp_obj_t)&digitalio_digitalinout_pull_locals_dict, +}; + +STATIC const mp_rom_map_elem_t digitalio_digitalinout_locals_dict_table[] = { + // instance methods + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&digitalio_digitalinout_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&digitalio_digitalinout_obj___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_switch_to_output), MP_ROM_PTR(&digitalio_digitalinout_switch_to_output_obj) }, + { MP_ROM_QSTR(MP_QSTR_switch_to_input), MP_ROM_PTR(&digitalio_digitalinout_switch_to_input_obj) }, + + // Properties + { MP_ROM_QSTR(MP_QSTR_direction), MP_ROM_PTR(&digitalio_digitalinout_direction_obj) }, + { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&digitalio_digitalinout_value_obj) }, + { MP_ROM_QSTR(MP_QSTR_drive_mode), MP_ROM_PTR(&digitalio_digitalinout_drive_mode_obj) }, + { MP_ROM_QSTR(MP_QSTR_pull), MP_ROM_PTR(&digitalio_digitalinout_pull_obj) }, + + // Nested Enum-like Classes. + { MP_ROM_QSTR(MP_QSTR_Direction), MP_ROM_PTR(&digitalio_digitalinout_direction_type) }, + { MP_ROM_QSTR(MP_QSTR_DriveMode), MP_ROM_PTR(&digitalio_digitalinout_drive_mode_type) }, + { MP_ROM_QSTR(MP_QSTR_Pull), MP_ROM_PTR(&digitalio_digitalinout_pull_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(digitalio_digitalinout_locals_dict, digitalio_digitalinout_locals_dict_table); + +const mp_obj_type_t digitalio_digitalinout_type = { + { &mp_type_type }, + .name = MP_QSTR_DigitalInOut, + .make_new = digitalio_digitalinout_make_new, + .locals_dict = (mp_obj_t)&digitalio_digitalinout_locals_dict, +}; diff --git a/shared-bindings/nativeio/DigitalInOut.h b/shared-bindings/digitalio/DigitalInOut.h index b712a811c..7fd6810f1 100644 --- a/shared-bindings/nativeio/DigitalInOut.h +++ b/shared-bindings/digitalio/DigitalInOut.h @@ -24,13 +24,13 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_DIGITALINOUT_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_DIGITALINOUT_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO_DIGITALINOUT_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO_DIGITALINOUT_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/digitalio/DigitalInOut.h" -extern const mp_obj_type_t nativeio_digitalinout_type; +extern const mp_obj_type_t digitalio_digitalinout_type; enum digitalinout_direction_t { DIRECTION_IN, @@ -53,16 +53,16 @@ typedef enum { DIGITALINOUT_PIN_BUSY } digitalinout_result_t; -digitalinout_result_t common_hal_nativeio_digitalinout_construct(nativeio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin); -void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self); -void common_hal_nativeio_digitalinout_switch_to_input(nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull); -void common_hal_nativeio_digitalinout_switch_to_output(nativeio_digitalinout_obj_t* self, bool value, enum digitalinout_drive_mode_t drive_mode); -enum digitalinout_direction_t common_hal_nativeio_digitalinout_get_direction(nativeio_digitalinout_obj_t* self); -void common_hal_nativeio_digitalinout_set_value(nativeio_digitalinout_obj_t* self, bool value); -bool common_hal_nativeio_digitalinout_get_value(nativeio_digitalinout_obj_t* self); -void common_hal_nativeio_digitalinout_set_drive_mode(nativeio_digitalinout_obj_t* self, enum digitalinout_drive_mode_t drive_mode); -enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode(nativeio_digitalinout_obj_t* self); -void common_hal_nativeio_digitalinout_set_pull(nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull); -enum digitalinout_pull_t common_hal_nativeio_digitalinout_get_pull(nativeio_digitalinout_obj_t* self); +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); +void common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull); +void common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t* self, bool value, enum digitalinout_drive_mode_t drive_mode); +enum digitalinout_direction_t common_hal_digitalio_digitalinout_get_direction(digitalio_digitalinout_obj_t* self); +void common_hal_digitalio_digitalinout_set_value(digitalio_digitalinout_obj_t* self, bool value); +bool common_hal_digitalio_digitalinout_get_value(digitalio_digitalinout_obj_t* self); +void common_hal_digitalio_digitalinout_set_drive_mode(digitalio_digitalinout_obj_t* self, enum digitalinout_drive_mode_t drive_mode); +enum digitalinout_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(digitalio_digitalinout_obj_t* self); +void common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull); +enum digitalinout_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalinout_obj_t* self); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_DIGITALINOUT_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO_DIGITALINOUT_H__ diff --git a/shared-bindings/digitalio/__init__.c b/shared-bindings/digitalio/__init__.c new file mode 100644 index 000000000..687110671 --- /dev/null +++ b/shared-bindings/digitalio/__init__.c @@ -0,0 +1,95 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 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 <stdint.h> + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/digitalio/__init__.h" +#include "shared-bindings/digitalio/DigitalInOut.h" + +#include "py/runtime.h" + +//| :mod:`digitalio` --- Basic digital pin support +//| ================================================= +//| +//| .. module:: digitalio +//| :synopsis: Basic digital pin support +//| :platform: SAMD21, ESP8266 +//| +//| The `digitalio` module contains classes to provide access to basic digital IO. +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| DigitalInOut +//| +//| All classes change hardware state and should be deinitialized when they +//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a +//| context manager. +//| +//| For example:: +//| +//| import digitalio +//| from board import * +//| +//| with digitalio.DigitalInOut(D13) as pin: +//| print(pin.value) +//| +//| This example will initialize the the device, read +//| :py:data:`~digitalio.DigitalInOut.value` and then +//| :py:meth:`~digitalio.DigitalInOut.deinit` the hardware. +//| +//| Here is blinky:: +//| +//| import digitalio +//| from board import * +//| import time +//| +//| with digitalio.DigitalInOut(D13) as led: +//| led.switch_to_output() +//| while True: +//| led.value = True +//| time.sleep(0.1) +//| led.value = False +//| time.sleep(0.1) +//| + +STATIC const mp_rom_map_elem_t digitalio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_digitalio) }, + { MP_ROM_QSTR(MP_QSTR_DigitalInOut), MP_ROM_PTR(&digitalio_digitalinout_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(digitalio_module_globals, digitalio_module_globals_table); + +const mp_obj_module_t digitalio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&digitalio_module_globals, +}; diff --git a/shared-bindings/digitalio/__init__.h b/shared-bindings/digitalio/__init__.h new file mode 100644 index 000000000..ed7cb5181 --- /dev/null +++ b/shared-bindings/digitalio/__init__.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_DIGITALIO___INIT___H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO___INIT___H__ + +#include "py/obj.h" + +// Nothing now. + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO___INIT___H__ diff --git a/shared-bindings/nativeio/DigitalInOut.c b/shared-bindings/nativeio/DigitalInOut.c deleted file mode 100644 index 421e14277..000000000 --- a/shared-bindings/nativeio/DigitalInOut.c +++ /dev/null @@ -1,486 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 Damien P. George - * - * 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 <stdint.h> -#include <string.h> - -#include "lib/utils/context_manager_helpers.h" - -#include "py/nlr.h" -#include "py/objtype.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "py/mphal.h" - -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/DigitalInOut.h" - -//| .. currentmodule:: nativeio -//| -//| :class:`DigitalInOut` -- digital input and output -//| ========================================================= -//| -//| A DigitalInOut is used to digitally control I/O pins. For analog control of -//| a pin, see the :py:class:`~nativeio.AnalogIn` and -//| :py:class:`~nativeio.AnalogOut` classes. -//| - -//| .. class:: DigitalInOut(pin) -//| -//| Create a new DigitalInOut object associated with the pin. Defaults to input -//| with no pull. Use :py:meth:`switch_to_input` and -//| :py:meth:`switch_to_output` to change the direction. -//| -//| :param ~microcontroller.Pin pin: The pin to control -//| -STATIC mp_obj_t nativeio_digitalinout_make_new(const mp_obj_type_t *type, - mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { - mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - - nativeio_digitalinout_obj_t *self = m_new_obj(nativeio_digitalinout_obj_t); - self->base.type = &nativeio_digitalinout_type; - - assert_pin(args[0], false); - mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(args[0]); - assert_pin_free(pin); - common_hal_nativeio_digitalinout_construct(self, pin); - - return (mp_obj_t)self; -} - -//| .. method:: deinit() -//| -//| Turn off the DigitalInOut and release the pin for other use. -//| -STATIC mp_obj_t nativeio_digitalinout_obj_deinit(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_digitalinout_deinit(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_deinit_obj, nativeio_digitalinout_obj_deinit); - -//| .. method:: __enter__() -//| -//| No-op used by Context Managers. -//| -// Provided by context manager helper. - -//| .. method:: __exit__() -//| -//| Automatically deinitializes the hardware when exiting a context. -//| -STATIC mp_obj_t nativeio_digitalinout_obj___exit__(size_t n_args, const mp_obj_t *args) { - (void)n_args; - common_hal_nativeio_digitalinout_deinit(args[0]); - return mp_const_none; -} -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) -//| -//| Switch to writing out digital values. -//| -//| :param bool value: default value to set upon switching -//| :param DriveMode drive_mode: drive mode for the output -//| -typedef struct { - mp_obj_base_t base; -} nativeio_digitalinout_drive_mode_obj_t; -extern const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_push_pull_obj; -extern const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_open_drain_obj; - -STATIC mp_obj_t nativeio_digitalinout_switch_to_output(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_value, ARG_drive_mode }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_value, MP_ARG_BOOL, {.u_bool = false} }, - { MP_QSTR_drive_mode, MP_ARG_OBJ, {.u_rom_obj = &nativeio_digitalinout_drive_mode_push_pull_obj} }, - }; - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - 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); - - enum digitalinout_drive_mode_t drive_mode = DRIVE_MODE_PUSH_PULL; - if (args[ARG_drive_mode].u_rom_obj == &nativeio_digitalinout_drive_mode_open_drain_obj) { - drive_mode = DRIVE_MODE_OPEN_DRAIN; - } - // do the transfer - common_hal_nativeio_digitalinout_switch_to_output(self, args[ARG_value].u_bool, drive_mode); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_digitalinout_switch_to_output_obj, 1, nativeio_digitalinout_switch_to_output); - -//| .. method:: switch_to_input(pull=None) -//| -//| Switch to read in digital values. -//| -//| :param Pull pull: pull configuration for the input -//| -//| Example usage:: -//| -//| import nativeio -//| import board -//| -//| with nativeio.DigitalInOut(board.SLIDE_SWITCH) as switch: -//| switch.switch_to_input(pull=nativeio.DigitalInOut.Pull.UP) -//| # Or, after switch_to_input -//| switch.pull = nativeio.DigitalInOut.Pull.UP -//| print(switch.value) -//| -typedef struct { - mp_obj_base_t base; -} nativeio_digitalinout_pull_obj_t; -extern const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_up_obj; -extern const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_down_obj; - -STATIC mp_obj_t nativeio_digitalinout_switch_to_input(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_pull }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} }, - }; - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - 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); - - enum digitalinout_pull_t pull = PULL_NONE; - if (args[ARG_pull].u_rom_obj == &nativeio_digitalinout_pull_up_obj) { - pull = PULL_UP; - }else if (args[ARG_pull].u_rom_obj == &nativeio_digitalinout_pull_down_obj) { - pull = PULL_DOWN; - } - // do the transfer - common_hal_nativeio_digitalinout_switch_to_input(self, pull); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_digitalinout_switch_to_input_obj, 1, nativeio_digitalinout_switch_to_input); - -//| .. attribute:: direction -//| -//| Get the direction of the pin. -//| -//| :raises AttributeError: when set. Use :py:meth:`switch_to_input` and :py:meth:`switch_to_output` to change the direction. -//| -typedef struct { - mp_obj_base_t base; -} nativeio_digitalinout_direction_obj_t; -extern const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_in_obj; -extern const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_out_obj; - -STATIC mp_obj_t nativeio_digitalinout_obj_get_direction(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - enum digitalinout_direction_t direction = common_hal_nativeio_digitalinout_get_direction(self); - if (direction == DIRECTION_IN) { - return (mp_obj_t)&nativeio_digitalinout_direction_in_obj; - } - return (mp_obj_t)&nativeio_digitalinout_direction_out_obj; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_direction_obj, nativeio_digitalinout_obj_get_direction); - -const mp_obj_property_t nativeio_digitalinout_direction_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_digitalinout_get_direction_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. attribute:: value -//| -//| The digital logic level of the pin. -//| -STATIC mp_obj_t nativeio_digitalinout_obj_get_value(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - bool value = common_hal_nativeio_digitalinout_get_value(self); - return mp_obj_new_bool(value); -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_value_obj, nativeio_digitalinout_obj_get_value); - -STATIC mp_obj_t nativeio_digitalinout_obj_set_value(mp_obj_t self_in, mp_obj_t value) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) { - mp_raise_AttributeError("Cannot set value when direction is input."); - return mp_const_none; - } - common_hal_nativeio_digitalinout_set_value(self, mp_obj_is_true(value)); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_digitalinout_set_value_obj, nativeio_digitalinout_obj_set_value); - -const mp_obj_property_t nativeio_digitalinout_value_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_digitalinout_get_value_obj, - (mp_obj_t)&nativeio_digitalinout_set_value_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. attribute:: drive_mode -//| -//| Get or set the pin drive mode. -//| -STATIC mp_obj_t nativeio_digitalinout_obj_get_drive_mode(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) { - mp_raise_AttributeError("Drive mode not used when direction is input."); - return mp_const_none; - } - enum digitalinout_drive_mode_t drive_mode = common_hal_nativeio_digitalinout_get_drive_mode(self); - if (drive_mode == DRIVE_MODE_PUSH_PULL) { - return (mp_obj_t)&nativeio_digitalinout_drive_mode_push_pull_obj; - } - return (mp_obj_t)&nativeio_digitalinout_drive_mode_open_drain_obj; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_drive_mode_obj, nativeio_digitalinout_obj_get_drive_mode); - -STATIC mp_obj_t nativeio_digitalinout_obj_set_drive_mode(mp_obj_t self_in, mp_obj_t drive_mode) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) { - mp_raise_AttributeError("Drive mode not used when direction is input."); - return mp_const_none; - } - enum digitalinout_drive_mode_t c_drive_mode = DRIVE_MODE_PUSH_PULL; - if (drive_mode == &nativeio_digitalinout_drive_mode_open_drain_obj) { - c_drive_mode = DRIVE_MODE_OPEN_DRAIN; - } - common_hal_nativeio_digitalinout_set_drive_mode(self, c_drive_mode); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_digitalinout_set_drive_mode_obj, nativeio_digitalinout_obj_set_drive_mode); - -const mp_obj_property_t nativeio_digitalinout_drive_mode_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_digitalinout_get_drive_mode_obj, - (mp_obj_t)&nativeio_digitalinout_set_drive_mode_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. attribute:: pull -//| -//| Get or set the pin pull. -//| -//| :raises AttributeError: if the direction is ~`Direction.OUT`. -//| -STATIC mp_obj_t nativeio_digitalinout_obj_get_pull(mp_obj_t self_in) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_OUT) { - mp_raise_AttributeError("Pull not used when direction is output."); - return mp_const_none; - } - enum digitalinout_pull_t pull = common_hal_nativeio_digitalinout_get_pull(self); - if (pull == PULL_UP) { - return (mp_obj_t)&nativeio_digitalinout_pull_up_obj; - } else if (pull == PULL_DOWN) { - return (mp_obj_t)&nativeio_digitalinout_pull_down_obj; - } - return (mp_obj_t)&mp_const_none_obj; -} -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_pull_obj, nativeio_digitalinout_obj_get_pull); - -STATIC mp_obj_t nativeio_digitalinout_obj_set_pull(mp_obj_t self_in, mp_obj_t pull_obj) { - nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_OUT) { - mp_raise_AttributeError("Pull not used when direction is output."); - return mp_const_none; - } - enum digitalinout_pull_t pull = PULL_NONE; - if (pull_obj == &nativeio_digitalinout_pull_up_obj) { - pull = PULL_UP; - } else if (pull_obj == &nativeio_digitalinout_pull_down_obj) { - pull = PULL_DOWN; - } - common_hal_nativeio_digitalinout_set_pull(self, pull); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_digitalinout_set_pull_obj, nativeio_digitalinout_obj_set_pull); - -const mp_obj_property_t nativeio_digitalinout_pull_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_digitalinout_get_pull_obj, - (mp_obj_t)&nativeio_digitalinout_set_pull_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. class:: microcontroller.DigitalInOut.Direction -//| -//| Enum-like class to define which direction the digital values are -//| going. -//| -//| .. data:: IN -//| -//| Read digital data in -//| -//| .. data:: OUT -//| -//| Write digital data out -//| -const mp_obj_type_t nativeio_digitalinout_direction_type; - -const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_in_obj = { - { &nativeio_digitalinout_direction_type }, -}; - -const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_out_obj = { - { &nativeio_digitalinout_direction_type }, -}; - -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) }, -}; -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, -}; - -//| .. class:: nativeio.DigitalInOut.DriveMode -//| -//| Enum-like class to define the drive mode used when outputting -//| digital values. -//| -//| .. data:: PUSH_PULL -//| -//| Output both high and low digital values -//| -//| .. 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. -//| -const mp_obj_type_t nativeio_digitalinout_drive_mode_type; - -const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_push_pull_obj = { - { &nativeio_digitalinout_drive_mode_type }, -}; - -const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_open_drain_obj = { - { &nativeio_digitalinout_drive_mode_type }, -}; - -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) }, -}; -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, -}; - -//| .. class:: nativeio.DigitalInOut.Pull -//| -//| Enum-like class to define the pull value, if any, used while reading -//| digital values in. -//| -//| .. 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 -//| -//| When the input line isn't being driven the pull down can pull the -//| state of the line low so it reads as false. -//| -const mp_obj_type_t nativeio_digitalinout_pull_type; - -const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_up_obj = { - { &nativeio_digitalinout_pull_type }, -}; - -const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_down_obj = { - { &nativeio_digitalinout_pull_type }, -}; - -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) }, -}; -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(&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) }, - - // Properties - { MP_ROM_QSTR(MP_QSTR_direction), MP_ROM_PTR(&nativeio_digitalinout_direction_obj) }, - { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_digitalinout_value_obj) }, - { MP_ROM_QSTR(MP_QSTR_drive_mode), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_obj) }, - { MP_ROM_QSTR(MP_QSTR_pull), MP_ROM_PTR(&nativeio_digitalinout_pull_obj) }, - - // Nested Enum-like Classes. - { MP_ROM_QSTR(MP_QSTR_Direction), MP_ROM_PTR(&nativeio_digitalinout_direction_type) }, - { MP_ROM_QSTR(MP_QSTR_DriveMode), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_type) }, - { MP_ROM_QSTR(MP_QSTR_Pull), MP_ROM_PTR(&nativeio_digitalinout_pull_type) }, -}; - -STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_locals_dict, nativeio_digitalinout_locals_dict_table); - -const mp_obj_type_t nativeio_digitalinout_type = { - { &mp_type_type }, - .name = MP_QSTR_DigitalInOut, - .make_new = nativeio_digitalinout_make_new, - .locals_dict = (mp_obj_t)&nativeio_digitalinout_locals_dict, -}; diff --git a/shared-bindings/nativeio/__init__.c b/shared-bindings/nativeio/__init__.c deleted file mode 100644 index 3287a46cd..000000000 --- a/shared-bindings/nativeio/__init__.c +++ /dev/null @@ -1,140 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 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. - */ - -// nativeio is the HAL for low-level, hardware accelerated classes. It -// is not meant to simplify APIs, its only meant to unify them so that other -// libraries do not require port specific logic. - -#include <stdint.h> - -#include "py/obj.h" -#include "py/runtime.h" - -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/__init__.h" -#include "shared-bindings/nativeio/AnalogIn.h" -#include "shared-bindings/nativeio/AnalogOut.h" -#include "shared-bindings/nativeio/DigitalInOut.h" -#include "shared-bindings/nativeio/I2C.h" -#include "shared-bindings/nativeio/OneWire.h" -#include "shared-bindings/nativeio/PulseIn.h" -#include "shared-bindings/nativeio/PulseOut.h" -#include "shared-bindings/nativeio/PWMOut.h" -#include "shared-bindings/nativeio/SPI.h" -#include "shared-bindings/nativeio/TouchIn.h" -#include "shared-bindings/nativeio/UART.h" -#include "common-hal/nativeio/types.h" -#include "shared-bindings/nativeio/__init__.h" - -#include "py/runtime.h" - -//| :mod:`nativeio` --- Hardware accelerated behavior -//| ================================================= -//| -//| .. module:: nativeio -//| :synopsis: Hardware accelerated behavior -//| :platform: SAMD21 -//| -//| The `nativeio` module contains classes to provide access to IO typically -//| accelerated by hardware on the onboard microcontroller. The classes are -//| meant to align with commonly hardware accelerated IO and not necessarily -//| match up with microcontroller structure (because it varies). -//| -//| When the microcontroller does not support the behavior in a hardware -//| accelerated fashion it may internally use a bitbang routine. However, if -//| hardware support is available on a subset of pins but not those provided, -//| then a RuntimeError will be raised. Use the `bitbangio` module to explicitly -//| bitbang a protocol on any general purpose pins. -//| -//| Libraries -//| -//| .. toctree:: -//| :maxdepth: 3 -//| -//| AnalogIn -//| AnalogOut -//| DigitalInOut -//| I2C -//| OneWire -//| PulseIn -//| PulseOut -//| PWMOut -//| SPI -//| TouchIn -//| UART -//| -//| All libraries change hardware state and should be deinitialized when they -//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a -//| context manager. -//| -//| For example:: -//| -//| import nativeio -//| from board import * -//| -//| with nativeio.I2C(SCL, SDA) as i2c: -//| i2c.scan() -//| -//| This example will initialize the the device, run -//| :py:meth:`~nativeio.I2C.scan` and then :py:meth:`~nativeio.I2C.deinit` the -//| hardware. -//| -//| Here is blinky:: -//| -//| import nativeio -//| from board import * -//| import time -//| -//| with nativeio.DigitalInOut(D13) as led: -//| led.switch_to_output() -//| while True: -//| led.value = True -//| time.sleep(0.1) -//| led.value = False -//| time.sleep(0.1) -//| - -STATIC const mp_rom_map_elem_t nativeio_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_nativeio) }, - { MP_ROM_QSTR(MP_QSTR_AnalogIn), MP_ROM_PTR(&nativeio_analogin_type) }, - { MP_ROM_QSTR(MP_QSTR_AnalogOut), MP_ROM_PTR(&nativeio_analogout_type) }, - { MP_ROM_QSTR(MP_QSTR_DigitalInOut), MP_ROM_PTR(&nativeio_digitalinout_type) }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&nativeio_i2c_type) }, - { MP_ROM_QSTR(MP_QSTR_OneWire), MP_ROM_PTR(&nativeio_onewire_type) }, - { MP_ROM_QSTR(MP_QSTR_PulseIn), MP_ROM_PTR(&nativeio_pulsein_type) }, - { MP_ROM_QSTR(MP_QSTR_PulseOut), MP_ROM_PTR(&nativeio_pulseout_type) }, - { MP_ROM_QSTR(MP_QSTR_PWMOut), MP_ROM_PTR(&nativeio_pwmout_type) }, - { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&nativeio_spi_type) }, - { MP_ROM_QSTR(MP_QSTR_TouchIn), MP_ROM_PTR(&nativeio_touchin_type) }, - { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&nativeio_uart_type) }, -}; - -STATIC MP_DEFINE_CONST_DICT(nativeio_module_globals, nativeio_module_globals_table); - -const mp_obj_module_t nativeio_module = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&nativeio_module_globals, -}; diff --git a/shared-bindings/neopixel_write/__init__.c b/shared-bindings/neopixel_write/__init__.c index b0490e1ae..faf2433d0 100644 --- a/shared-bindings/neopixel_write/__init__.c +++ b/shared-bindings/neopixel_write/__init__.c @@ -28,9 +28,7 @@ #include "py/mphal.h" #include "py/runtime.h" -#include "common-hal/nativeio/types.h" - -#include "shared-bindings/nativeio/DigitalInOut.h" +#include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/neopixel_write/__init__.h" //| :mod:`neopixel_write` --- Low-level neopixel implementation @@ -47,15 +45,15 @@ //| //| Write buf out on the given DigitalInOut. //| -//| :param ~nativeio.DigitalInOut gpio: the DigitalInOut to output with +//| :param ~digitalio.DigitalInOut gpio: the DigitalInOut to output with //| :param bytearray buf: The bytes to clock out. No assumption is made about color order //| STATIC mp_obj_t neopixel_write_neopixel_write_(mp_obj_t digitalinout_obj, mp_obj_t buf) { - if (!MP_OBJ_IS_TYPE(digitalinout_obj, &nativeio_digitalinout_type)) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", nativeio_digitalinout_type.name)); + if (!MP_OBJ_IS_TYPE(digitalinout_obj, &digitalio_digitalinout_type)) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", digitalio_digitalinout_type.name)); } // Convert parameters into expected types. - const nativeio_digitalinout_obj_t *digitalinout = MP_OBJ_TO_PTR(digitalinout_obj); + const digitalio_digitalinout_obj_t *digitalinout = MP_OBJ_TO_PTR(digitalinout_obj); mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); // Call platform's neopixel write function with provided buffer and options. diff --git a/shared-bindings/neopixel_write/__init__.h b/shared-bindings/neopixel_write/__init__.h index 87f85ee01..39b565ee2 100644 --- a/shared-bindings/neopixel_write/__init__.h +++ b/shared-bindings/neopixel_write/__init__.h @@ -30,8 +30,8 @@ #include <stdint.h> #include <stdbool.h> -#include "common-hal/nativeio/types.h" +#include "common-hal/digitalio/DigitalInOut.h" -extern void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* gpio, uint8_t *pixels, uint32_t numBytes); +extern void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* gpio, uint8_t *pixels, uint32_t numBytes); #endif diff --git a/shared-bindings/nativeio/PWMOut.c b/shared-bindings/pulseio/PWMOut.c index 6d5710f8e..73f3bfd3e 100644 --- a/shared-bindings/nativeio/PWMOut.c +++ b/shared-bindings/pulseio/PWMOut.c @@ -30,9 +30,9 @@ #include "py/objproperty.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/PWMOut.h" +#include "shared-bindings/pulseio/PWMOut.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: pulseio //| //| :class:`PWMOut` -- Output a Pulse Width Modulated signal //| ======================================================== @@ -57,32 +57,32 @@ //| //| Simple LED fade:: //| -//| import nativeio +//| import pulseio //| import board //| -//| with nativeio.PWMOut(board.D13) as pwm: # output on D13 +//| with pulseio.PWMOut(board.D13) as pwm: # output on D13 //| pwm.duty_cycle = 2 ** 15 # Cycles the pin with 50% duty cycle (half of 2 ** 16) at the default 500hz //| //| PWM at specific frequency (servos and motors):: //| -//| import nativeio +//| import pulseio //| import board //| -//| with nativeio.PWMOut(board.D13, frequency=50) as pwm: +//| with pulseio.PWMOut(board.D13, frequency=50) as pwm: //| pwm.duty_cycle = 2 ** 15 # Cycles the pin with 50% duty cycle (half of 2 ** 16) at 50hz //| //| Variable frequency (usually tones):: //| -//| import nativeio +//| import pulseio //| import board //| import time //| -//| with nativeio.PWMOut(board.D13, duty_cycle=2 ** 15, frequency=440, variable_frequency=True) as pwm: +//| with pulseio.PWMOut(board.D13, duty_cycle=2 ** 15, frequency=440, variable_frequency=True) as pwm: //| time.sleep(0.2) //| pwm.frequency = 880 //| time.sleep(0.1) //| -STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t pulseio_pwmout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); mp_obj_t pin_obj = args[0]; assert_pin(pin_obj, false); @@ -90,8 +90,8 @@ STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_arg assert_pin_free(pin); // create PWM object from the given pin - nativeio_pwmout_obj_t *self = m_new_obj(nativeio_pwmout_obj_t); - self->base.type = &nativeio_pwmout_type; + pulseio_pwmout_obj_t *self = m_new_obj(pulseio_pwmout_obj_t); + self->base.type = &pulseio_pwmout_type; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); @@ -107,7 +107,7 @@ STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_arg uint32_t frequency = parsed_args[ARG_frequency].u_int; bool variable_frequency = parsed_args[ARG_variable_frequency].u_int; - common_hal_nativeio_pwmout_construct(self, pin, duty_cycle, frequency, variable_frequency); + common_hal_pulseio_pwmout_construct(self, pin, duty_cycle, frequency, variable_frequency); return MP_OBJ_FROM_PTR(self); } @@ -116,12 +116,12 @@ STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_arg //| //| Deinitialises the PWMOut and releases any hardware resources for reuse. //| -STATIC mp_obj_t nativeio_pwmout_deinit(mp_obj_t self_in) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_pwmout_deinit(self); +STATIC mp_obj_t pulseio_pwmout_deinit(mp_obj_t self_in) { + pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_pulseio_pwmout_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_deinit_obj, nativeio_pwmout_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_deinit_obj, pulseio_pwmout_deinit); //| .. method:: __enter__() //| @@ -133,39 +133,39 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_deinit_obj, nativeio_pwmout_dei //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_pwmout_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t pulseio_pwmout_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_pwmout_deinit(args[0]); + common_hal_pulseio_pwmout_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pwmout___exit___obj, 4, 4, nativeio_pwmout_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pwmout___exit___obj, 4, 4, pulseio_pwmout_obj___exit__); //| .. attribute:: duty_cycle //| //| 16 bit value that dictates how much of one cycle is high (1) versus low //| (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 nativeio_pwmout_obj_get_duty_cycle(mp_obj_t self_in) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pwmout_get_duty_cycle(self)); +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)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_get_duty_cycle_obj, nativeio_pwmout_obj_get_duty_cycle); +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_get_duty_cycle_obj, pulseio_pwmout_obj_get_duty_cycle); -STATIC mp_obj_t nativeio_pwmout_obj_set_duty_cycle(mp_obj_t self_in, mp_obj_t duty_cycle) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); +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); mp_int_t duty = mp_obj_get_int(duty_cycle); if (duty < 0 || duty > 0xffff) { mp_raise_ValueError("PWM duty must be between 0 and 65536 (16 bit resolution)"); } - common_hal_nativeio_pwmout_set_duty_cycle(self, duty); + common_hal_pulseio_pwmout_set_duty_cycle(self, duty); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pwmout_set_duty_cycle_obj, nativeio_pwmout_obj_set_duty_cycle); +MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pwmout_set_duty_cycle_obj, pulseio_pwmout_obj_set_duty_cycle); -const mp_obj_property_t nativeio_pwmout_duty_cycle_obj = { +const mp_obj_property_t pulseio_pwmout_duty_cycle_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_pwmout_get_duty_cycle_obj, - (mp_obj_t)&nativeio_pwmout_set_duty_cycle_obj, + .proxy = {(mp_obj_t)&pulseio_pwmout_get_duty_cycle_obj, + (mp_obj_t)&pulseio_pwmout_set_duty_cycle_obj, (mp_obj_t)&mp_const_none_obj}, }; @@ -174,48 +174,48 @@ const mp_obj_property_t nativeio_pwmout_duty_cycle_obj = { //| 32 bit value that dictates the PWM frequency in Hertz (cycles per //| second). Only writeable when constructed with ``variable_frequency=True``. //| -STATIC mp_obj_t nativeio_pwmout_obj_get_frequency(mp_obj_t self_in) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pwmout_get_frequency(self)); +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)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_get_frequency_obj, nativeio_pwmout_obj_get_frequency); +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_get_frequency_obj, pulseio_pwmout_obj_get_frequency); -STATIC mp_obj_t nativeio_pwmout_obj_set_frequency(mp_obj_t self_in, mp_obj_t frequency) { - nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (!common_hal_nativeio_pwmout_get_variable_frequency(self)) { +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); + if (!common_hal_pulseio_pwmout_get_variable_frequency(self)) { mp_raise_AttributeError( "PWM frequency not writeable when variable_frequency is False on " "construction."); } - common_hal_nativeio_pwmout_set_frequency(self, mp_obj_get_int(frequency)); + common_hal_pulseio_pwmout_set_frequency(self, mp_obj_get_int(frequency)); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pwmout_set_frequency_obj, nativeio_pwmout_obj_set_frequency); +MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pwmout_set_frequency_obj, pulseio_pwmout_obj_set_frequency); -const mp_obj_property_t nativeio_pwmout_frequency_obj = { +const mp_obj_property_t pulseio_pwmout_frequency_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_pwmout_get_frequency_obj, - (mp_obj_t)&nativeio_pwmout_set_frequency_obj, + .proxy = {(mp_obj_t)&pulseio_pwmout_get_frequency_obj, + (mp_obj_t)&pulseio_pwmout_set_frequency_obj, (mp_obj_t)&mp_const_none_obj}, }; -STATIC const mp_rom_map_elem_t nativeio_pwmout_locals_dict_table[] = { +STATIC const mp_rom_map_elem_t pulseio_pwmout_locals_dict_table[] = { // Methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pwmout_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pulseio_pwmout_deinit_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) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&pulseio_pwmout___exit___obj) }, // Properties - { MP_ROM_QSTR(MP_QSTR_duty_cycle), MP_ROM_PTR(&nativeio_pwmout_duty_cycle_obj) }, - { MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&nativeio_pwmout_frequency_obj) }, + { MP_ROM_QSTR(MP_QSTR_duty_cycle), MP_ROM_PTR(&pulseio_pwmout_duty_cycle_obj) }, + { MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&pulseio_pwmout_frequency_obj) }, // TODO(tannewt): Add enabled to determine whether the signal is output // without giving up the resources. Useful for IR output. }; -STATIC MP_DEFINE_CONST_DICT(nativeio_pwmout_locals_dict, nativeio_pwmout_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(pulseio_pwmout_locals_dict, pulseio_pwmout_locals_dict_table); -const mp_obj_type_t nativeio_pwmout_type = { +const mp_obj_type_t pulseio_pwmout_type = { { &mp_type_type }, .name = MP_QSTR_PWMOut, - .make_new = nativeio_pwmout_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_pwmout_locals_dict, + .make_new = pulseio_pwmout_make_new, + .locals_dict = (mp_obj_dict_t*)&pulseio_pwmout_locals_dict, }; diff --git a/shared-bindings/nativeio/PWMOut.h b/shared-bindings/pulseio/PWMOut.h index 588992ceb..f7a0bff22 100644 --- a/shared-bindings/nativeio/PWMOut.h +++ b/shared-bindings/pulseio/PWMOut.h @@ -24,22 +24,22 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PWMOUT_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PWMOUT_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PWMOUT_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PWMOUT_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/pulseio/PWMOut.h" -extern const mp_obj_type_t nativeio_pwmout_type; +extern const mp_obj_type_t pulseio_pwmout_type; -extern void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self, +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_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self); -extern void common_hal_nativeio_pwmout_set_duty_cycle(nativeio_pwmout_obj_t* self, uint16_t duty); -extern uint16_t common_hal_nativeio_pwmout_get_duty_cycle(nativeio_pwmout_obj_t* self); -extern void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self, uint32_t frequency); -extern uint32_t common_hal_nativeio_pwmout_get_frequency(nativeio_pwmout_obj_t* self); -extern bool common_hal_nativeio_pwmout_get_variable_frequency(nativeio_pwmout_obj_t* self); +extern void common_hal_pulseio_pwmout_deinit(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); +extern uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self); +extern bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PWMOUT_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PWMOUT_H__ diff --git a/shared-bindings/nativeio/PulseIn.c b/shared-bindings/pulseio/PulseIn.c index 78e0c84e4..e87020cd9 100644 --- a/shared-bindings/nativeio/PulseIn.c +++ b/shared-bindings/pulseio/PulseIn.c @@ -31,9 +31,9 @@ #include "py/runtime.h" #include "py/runtime0.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/PulseIn.h" +#include "shared-bindings/pulseio/PulseIn.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: pulseio //| //| :class:`PulseIn` -- Read a series of pulse durations //| ======================================================== @@ -58,10 +58,10 @@ //| //| Read a short series of pulses:: //| -//| import nativeio +//| import pulseio //| import board //| -//| with nativeio.PulseIn(board.D7) as pulses: +//| with pulseio.PulseIn(board.D7) as pulses: //| # Wait for an active pulse //| while len(pulses) == 0: //| pass @@ -78,7 +78,7 @@ //| # Resume with an 80 microsecond active pulse //| pulse_in.resume(80) //| -STATIC mp_obj_t nativeio_pulsein_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { +STATIC mp_obj_t pulseio_pulsein_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, 1, MP_OBJ_FUN_ARGS_MAX, true); mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); @@ -94,10 +94,10 @@ STATIC mp_obj_t nativeio_pulsein_make_new(const mp_obj_type_t *type, size_t n_ar const mcu_pin_obj_t* pin = MP_OBJ_TO_PTR(args[ARG_pin].u_obj); assert_pin_free(pin); - nativeio_pulsein_obj_t *self = m_new_obj(nativeio_pulsein_obj_t); - self->base.type = &nativeio_pulsein_type; + pulseio_pulsein_obj_t *self = m_new_obj(pulseio_pulsein_obj_t); + self->base.type = &pulseio_pulsein_type; - common_hal_nativeio_pulsein_construct(self, pin, args[ARG_maxlen].u_int, + common_hal_pulseio_pulsein_construct(self, pin, args[ARG_maxlen].u_int, args[ARG_idle_state].u_bool); return MP_OBJ_FROM_PTR(self); @@ -107,12 +107,12 @@ STATIC mp_obj_t nativeio_pulsein_make_new(const mp_obj_type_t *type, size_t n_ar //| //| Deinitialises the PulseIn and releases any hardware resources for reuse. //| -STATIC mp_obj_t nativeio_pulsein_deinit(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_pulsein_deinit(self); +STATIC mp_obj_t pulseio_pulsein_deinit(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_pulseio_pulsein_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_deinit_obj, nativeio_pulsein_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_deinit_obj, pulseio_pulsein_deinit); //| .. method:: __enter__() //| @@ -124,24 +124,24 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_deinit_obj, nativeio_pulsein_d //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_pulsein_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t pulseio_pulsein_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_pulsein_deinit(args[0]); + common_hal_pulseio_pulsein_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pulsein___exit___obj, 4, 4, nativeio_pulsein_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pulsein___exit___obj, 4, 4, pulseio_pulsein_obj___exit__); //| .. method:: pause() //| //| Pause pulse capture //| -STATIC mp_obj_t nativeio_pulsein_obj_pause(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t pulseio_pulsein_obj_pause(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_pulsein_pause(self); + common_hal_pulseio_pulsein_pause(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_pause_obj, nativeio_pulsein_obj_pause); +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_pause_obj, pulseio_pulsein_obj_pause); //| .. method:: resume(trigger_duration=0) //| @@ -154,8 +154,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_pause_obj, nativeio_pulsein_obj_pause //| //| :param int trigger_duration: trigger pulse duration in microseconds //| -STATIC mp_obj_t nativeio_pulsein_obj_resume(mp_obj_t self_in, mp_obj_t duration_obj) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t pulseio_pulsein_obj_resume(mp_obj_t self_in, mp_obj_t duration_obj) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); uint16_t trigger_duration = 0; if (MP_OBJ_IS_SMALL_INT(duration_obj)) { trigger_duration = MP_OBJ_SMALL_INT_VALUE(duration_obj); @@ -163,47 +163,47 @@ STATIC mp_obj_t nativeio_pulsein_obj_resume(mp_obj_t self_in, mp_obj_t duration_ mp_raise_TypeError("trigger_duration must be int"); } - common_hal_nativeio_pulsein_resume(self, trigger_duration); + common_hal_pulseio_pulsein_resume(self, trigger_duration); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pulsein_resume_obj, nativeio_pulsein_obj_resume); +MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pulsein_resume_obj, pulseio_pulsein_obj_resume); //| .. method:: clear() //| //| Clears all captured pulses //| -STATIC mp_obj_t nativeio_pulsein_obj_clear(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t pulseio_pulsein_obj_clear(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_pulsein_clear(self); + common_hal_pulseio_pulsein_clear(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_clear_obj, nativeio_pulsein_obj_clear); +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_clear_obj, pulseio_pulsein_obj_clear); //| .. method:: popleft() //| //| Removes and returns the oldest read pulse. //| -STATIC mp_obj_t nativeio_pulsein_obj_popleft(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pulsein_popleft(self)); +STATIC mp_obj_t pulseio_pulsein_obj_popleft(mp_obj_t self_in) { + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_popleft(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_popleft_obj, nativeio_pulsein_obj_popleft); +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_popleft_obj, pulseio_pulsein_obj_popleft); //| .. attribute:: maxlen //| //| Returns the maximum length of the PulseIn. When len() is equal to maxlen, //| it is unclear which pulses are active and which are idle. //| -STATIC mp_obj_t nativeio_pulsein_obj_get_maxlen(mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pulsein_get_maxlen(self)); +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); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_get_maxlen(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulsein_get_maxlen_obj, nativeio_pulsein_obj_get_maxlen); +MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_get_maxlen_obj, pulseio_pulsein_obj_get_maxlen); -const mp_obj_property_t nativeio_pulsein_maxlen_obj = { +const mp_obj_property_t pulseio_pulsein_maxlen_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_pulsein_get_maxlen_obj, + .proxy = {(mp_obj_t)&pulseio_pulsein_get_maxlen_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj}, }; @@ -214,12 +214,12 @@ const mp_obj_property_t nativeio_pulsein_maxlen_obj = { //| //| This allows you to:: //| -//| pulses = nativeio.PulseIn(pin) +//| pulses = pulseio.PulseIn(pin) //| print(len(pulses)) //| STATIC mp_obj_t pulsein_unary_op(mp_uint_t op, mp_obj_t self_in) { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); - uint16_t len = common_hal_nativeio_pulsein_get_len(self); + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + uint16_t len = common_hal_pulseio_pulsein_get_len(self); switch (op) { case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0); case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len); @@ -233,7 +233,7 @@ STATIC mp_obj_t pulsein_unary_op(mp_uint_t op, mp_obj_t self_in) { //| //| This allows you to:: //| -//| pulses = nativeio.PulseIn(pin) +//| pulses = pulseio.PulseIn(pin) //| print(pulses[0]) //| STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t value) { @@ -241,7 +241,7 @@ STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t va // delete item mp_raise_AttributeError("Cannot delete values"); } else { - nativeio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); + pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); if (MP_OBJ_IS_TYPE(index_obj, &mp_type_slice)) { mp_raise_NotImplementedError("Slices not supported"); } else { @@ -253,7 +253,7 @@ STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t va } if (value == MP_OBJ_SENTINEL) { // load - return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pulsein_get_item(self, index)); + return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_get_item(self, index)); } else { mp_raise_AttributeError("Read-only"); } @@ -262,24 +262,24 @@ STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t va return mp_const_none; } -STATIC const mp_rom_map_elem_t nativeio_pulsein_locals_dict_table[] = { +STATIC const mp_rom_map_elem_t pulseio_pulsein_locals_dict_table[] = { // Methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pulsein_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pulseio_pulsein_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_pulsein___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_pause), MP_ROM_PTR(&nativeio_pulsein_pause_obj) }, - { MP_ROM_QSTR(MP_QSTR_resume), MP_ROM_PTR(&nativeio_pulsein_resume_obj) }, - { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&nativeio_pulsein_clear_obj) }, - { MP_ROM_QSTR(MP_QSTR_popleft), MP_ROM_PTR(&nativeio_pulsein_popleft_obj) }, - { MP_ROM_QSTR(MP_QSTR_maxlen), MP_ROM_PTR(&nativeio_pulsein_maxlen_obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&pulseio_pulsein___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_pause), MP_ROM_PTR(&pulseio_pulsein_pause_obj) }, + { MP_ROM_QSTR(MP_QSTR_resume), MP_ROM_PTR(&pulseio_pulsein_resume_obj) }, + { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&pulseio_pulsein_clear_obj) }, + { MP_ROM_QSTR(MP_QSTR_popleft), MP_ROM_PTR(&pulseio_pulsein_popleft_obj) }, + { MP_ROM_QSTR(MP_QSTR_maxlen), MP_ROM_PTR(&pulseio_pulsein_maxlen_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_pulsein_locals_dict, nativeio_pulsein_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(pulseio_pulsein_locals_dict, pulseio_pulsein_locals_dict_table); -const mp_obj_type_t nativeio_pulsein_type = { +const mp_obj_type_t pulseio_pulsein_type = { { &mp_type_type }, .name = MP_QSTR_PulseIn, - .make_new = nativeio_pulsein_make_new, + .make_new = pulseio_pulsein_make_new, .subscr = pulsein_subscr, .unary_op = pulsein_unary_op, - .locals_dict = (mp_obj_dict_t*)&nativeio_pulsein_locals_dict, + .locals_dict = (mp_obj_dict_t*)&pulseio_pulsein_locals_dict, }; diff --git a/shared-bindings/nativeio/PulseIn.h b/shared-bindings/pulseio/PulseIn.h index cdb5e95c5..5ffab1d67 100644 --- a/shared-bindings/nativeio/PulseIn.h +++ b/shared-bindings/pulseio/PulseIn.h @@ -24,23 +24,23 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEIN_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEIN_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEIN_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEIN_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/pulseio/PulseIn.h" -extern const mp_obj_type_t nativeio_pulsein_type; +extern const mp_obj_type_t pulseio_pulsein_type; -extern void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self, +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_nativeio_pulsein_deinit(nativeio_pulsein_obj_t* self); -extern void common_hal_nativeio_pulsein_pause(nativeio_pulsein_obj_t* self); -extern void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self, uint16_t trigger_duration); -extern void common_hal_nativeio_pulsein_clear(nativeio_pulsein_obj_t* self); -extern uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self); -extern uint16_t common_hal_nativeio_pulsein_get_maxlen(nativeio_pulsein_obj_t* self); -extern uint16_t common_hal_nativeio_pulsein_get_len(nativeio_pulsein_obj_t* self); -extern uint16_t common_hal_nativeio_pulsein_get_item(nativeio_pulsein_obj_t* self, int16_t index); +extern void common_hal_pulseio_pulsein_deinit(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); +extern uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self); +extern uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self); +extern uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self); +extern uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEIN_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEIN_H__ diff --git a/shared-bindings/nativeio/PulseOut.c b/shared-bindings/pulseio/PulseOut.c index 79f189293..20e04e8fb 100644 --- a/shared-bindings/nativeio/PulseOut.c +++ b/shared-bindings/pulseio/PulseOut.c @@ -30,10 +30,10 @@ #include "py/objproperty.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/PulseOut.h" -#include "shared-bindings/nativeio/PWMOut.h" +#include "shared-bindings/pulseio/PulseOut.h" +#include "shared-bindings/pulseio/PWMOut.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: pulseio //| //| :class:`PulseOut` -- Output a pulse train //| ======================================================== @@ -46,16 +46,16 @@ //| //| Create a PulseOut object associated with the given PWM out experience. //| -//| :param ~nativeio.PWMOut carrier: PWMOut that is set to output on the desired pin. +//| :param ~pulseio.PWMOut carrier: PWMOut that is set to output on the desired pin. //| //| Send a short series of pulses:: //| //| import array -//| import nativeio +//| import pulseio //| import board //| -//| with nativeio.PWMOut(board.D13, duty_cycle=2 ** 15) as pwm: -//| pulse = nativeio.PulseOut(pwm) +//| with pulseio.PWMOut(board.D13, duty_cycle=2 ** 15) as pwm: +//| pulse = pulseio.PulseOut(pwm) //| # on off on off on //| pulses = array.array('h', [65000, 1000, 65000, 65000, 1000]) //| pulse.send(pulses) @@ -64,19 +64,19 @@ //| pulses[0] = 200 //| pulse.send(pulses) //| -STATIC mp_obj_t nativeio_pulseout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t pulseio_pulseout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 1, 1, true); mp_obj_t carrier_obj = args[0]; - if (!MP_OBJ_IS_TYPE(carrier_obj, &nativeio_pwmout_type)) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", nativeio_pwmout_type.name)); + if (!MP_OBJ_IS_TYPE(carrier_obj, &pulseio_pwmout_type)) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", pulseio_pwmout_type.name)); } // create Pulse object from the given pin - nativeio_pulseout_obj_t *self = m_new_obj(nativeio_pulseout_obj_t); - self->base.type = &nativeio_pulseout_type; + pulseio_pulseout_obj_t *self = m_new_obj(pulseio_pulseout_obj_t); + self->base.type = &pulseio_pulseout_type; - common_hal_nativeio_pulseout_construct(self, (nativeio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj)); + common_hal_pulseio_pulseout_construct(self, (pulseio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj)); return MP_OBJ_FROM_PTR(self); } @@ -85,12 +85,12 @@ STATIC mp_obj_t nativeio_pulseout_make_new(const mp_obj_type_t *type, size_t n_a //| //| Deinitialises the PulseOut and releases any hardware resources for reuse. //| -STATIC mp_obj_t nativeio_pulseout_deinit(mp_obj_t self_in) { - nativeio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_pulseout_deinit(self); +STATIC mp_obj_t pulseio_pulseout_deinit(mp_obj_t self_in) { + pulseio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_pulseio_pulseout_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulseout_deinit_obj, nativeio_pulseout_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulseout_deinit_obj, pulseio_pulseout_deinit); //| .. method:: __enter__() //| @@ -102,12 +102,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pulseout_deinit_obj, nativeio_pulseout //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_pulseout_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t pulseio_pulseout_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_pulseout_deinit(args[0]); + common_hal_pulseio_pulseout_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pulseout___exit___obj, 4, 4, nativeio_pulseout_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pulseout___exit___obj, 4, 4, pulseio_pulseout_obj___exit__); //| .. method:: send(pulses) //| @@ -120,31 +120,31 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pulseout___exit___obj, 4, 4, //| //| :param array.array pulses: pulse durations in microseconds //| -STATIC mp_obj_t nativeio_pulseout_obj_send(mp_obj_t self_in, mp_obj_t pulses) { - nativeio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in); +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); mp_buffer_info_t bufinfo; mp_get_buffer_raise(pulses, &bufinfo, MP_BUFFER_READ); if (bufinfo.typecode != 'H') { mp_raise_TypeError("Array must contain halfwords (type 'H')"); } - common_hal_nativeio_pulseout_send(self, (uint16_t *)bufinfo.buf, bufinfo.len / 2); + common_hal_pulseio_pulseout_send(self, (uint16_t *)bufinfo.buf, bufinfo.len / 2); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pulseout_send_obj, nativeio_pulseout_obj_send); +MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pulseout_send_obj, pulseio_pulseout_obj_send); -STATIC const mp_rom_map_elem_t nativeio_pulseout_locals_dict_table[] = { +STATIC const mp_rom_map_elem_t pulseio_pulseout_locals_dict_table[] = { // Methods - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pulseout_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pulseio_pulseout_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, - { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_pulseout___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&nativeio_pulseout_send_obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&pulseio_pulseout___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&pulseio_pulseout_send_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_pulseout_locals_dict, nativeio_pulseout_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(pulseio_pulseout_locals_dict, pulseio_pulseout_locals_dict_table); -const mp_obj_type_t nativeio_pulseout_type = { +const mp_obj_type_t pulseio_pulseout_type = { { &mp_type_type }, .name = MP_QSTR_PulseOut, - .make_new = nativeio_pulseout_make_new, - .locals_dict = (mp_obj_dict_t*)&nativeio_pulseout_locals_dict, + .make_new = pulseio_pulseout_make_new, + .locals_dict = (mp_obj_dict_t*)&pulseio_pulseout_locals_dict, }; diff --git a/shared-bindings/nativeio/PulseOut.h b/shared-bindings/pulseio/PulseOut.h index 23fb8efed..5365fd1f5 100644 --- a/shared-bindings/nativeio/PulseOut.h +++ b/shared-bindings/pulseio/PulseOut.h @@ -24,18 +24,19 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEOUT_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEOUT_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEOUT_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEOUT_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/pulseio/PulseOut.h" +#include "common-hal/pulseio/PWMOut.h" -extern const mp_obj_type_t nativeio_pulseout_type; +extern const mp_obj_type_t pulseio_pulseout_type; -extern void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self, - const nativeio_pwmout_obj_t* carrier); -extern void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self); -extern void common_hal_nativeio_pulseout_send(nativeio_pulseout_obj_t* self, +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 void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t len); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PULSEOUT_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PULSEOUT_H__ diff --git a/shared-bindings/pulseio/__init__.c b/shared-bindings/pulseio/__init__.c new file mode 100644 index 000000000..27d978ec5 --- /dev/null +++ b/shared-bindings/pulseio/__init__.c @@ -0,0 +1,87 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 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 <stdint.h> + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/pulseio/__init__.h" +#include "shared-bindings/pulseio/PulseIn.h" +#include "shared-bindings/pulseio/PulseOut.h" +#include "shared-bindings/pulseio/PWMOut.h" + +//| :mod:`pulseio` --- Support for pulse based protocols +//| ================================================= +//| +//| .. module:: pulseio +//| :synopsis: Support for pulse based protocols +//| :platform: SAMD21, ESP8266 +//| +//| The `pulseio` module contains classes to provide access to basic pulse IO. +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| PulseIn +//| PulseOut +//| PWMOut +//| +//| All libraries change hardware state and should be deinitialized when they +//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a +//| context manager. +//| +//| For example:: +//| +//| import pulseio +//| import time +//| from board import * +//| +//| with pulseio.PWMOut(D13) as pin: +//| pin.duty_cycle = 2 ** 15 +//| time.sleep(0.1) +//| +//| This example will initialize the the device, set +//| :py:data:`~pulseio.PWMOut.duty_cycle`, sleep 0.1 seconds and then +//| :py:meth:`~pulseio.PWMOut.deinit` the hardware. +//| + +STATIC const mp_rom_map_elem_t pulseio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pulseio) }, + { MP_ROM_QSTR(MP_QSTR_PulseIn), MP_ROM_PTR(&pulseio_pulsein_type) }, + { MP_ROM_QSTR(MP_QSTR_PulseOut), MP_ROM_PTR(&pulseio_pulseout_type) }, + { MP_ROM_QSTR(MP_QSTR_PWMOut), MP_ROM_PTR(&pulseio_pwmout_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(pulseio_module_globals, pulseio_module_globals_table); + +const mp_obj_module_t pulseio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&pulseio_module_globals, +}; diff --git a/shared-bindings/pulseio/__init__.h b/shared-bindings/pulseio/__init__.h new file mode 100644 index 000000000..26bb9f7b4 --- /dev/null +++ b/shared-bindings/pulseio/__init__.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_PULSEIO___INIT___H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO___INIT___H__ + +#include "py/obj.h" + +// Nothing now. + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO___INIT___H__ diff --git a/shared-bindings/nativeio/TouchIn.c b/shared-bindings/touchio/TouchIn.c index 7b391499c..f4e82994c 100644 --- a/shared-bindings/nativeio/TouchIn.c +++ b/shared-bindings/touchio/TouchIn.c @@ -33,19 +33,19 @@ #include "py/objproperty.h" #include "py/runtime.h" #include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/nativeio/TouchIn.h" +#include "shared-bindings/touchio/TouchIn.h" -//| .. currentmodule:: nativeio +//| .. currentmodule:: touchio //| //| :class:`TouchIn` -- Read the state of a capacitive touch sensor //| =================================================================== //| //| Usage:: //| -//| import nativeio +//| import touchio //| from board import * //| -//| with nativeio.TouchIn(A1) as touch: +//| with touchio.TouchIn(A1) as touch: //| if touch.value: //| print("touched!") //| @@ -56,7 +56,7 @@ //| //| :param ~microcontroller.Pin pin: the pin to read from //| -STATIC mp_obj_t nativeio_touchin_make_new(const mp_obj_type_t *type, +STATIC mp_obj_t touchio_touchin_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { // check number of arguments mp_arg_check_num(n_args, n_kw, 1, 1, false); @@ -65,10 +65,10 @@ STATIC mp_obj_t nativeio_touchin_make_new(const mp_obj_type_t *type, mp_obj_t pin_obj = args[0]; assert_pin(pin_obj, false); - nativeio_touchin_obj_t *self = m_new_obj(nativeio_touchin_obj_t); - self->base.type = &nativeio_touchin_type; + touchio_touchin_obj_t *self = m_new_obj(touchio_touchin_obj_t); + self->base.type = &touchio_touchin_type; const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj); - common_hal_nativeio_touchin_construct(self, pin); + common_hal_touchio_touchin_construct(self, pin); return (mp_obj_t) self; } @@ -77,12 +77,12 @@ STATIC mp_obj_t nativeio_touchin_make_new(const mp_obj_type_t *type, //| //| Deinitialises the TouchIn and releases any hardware resources for reuse. //| -STATIC mp_obj_t nativeio_touchin_deinit(mp_obj_t self_in) { - nativeio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_nativeio_touchin_deinit(self); +STATIC mp_obj_t touchio_touchin_deinit(mp_obj_t self_in) { + touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_touchio_touchin_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_touchin_deinit_obj, nativeio_touchin_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_deinit_obj, touchio_touchin_deinit); //| .. method:: __enter__() //| @@ -94,12 +94,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_touchin_deinit_obj, nativeio_touchin_d //| //| Automatically deinitializes the hardware when exiting a context. //| -STATIC mp_obj_t nativeio_touchin_obj___exit__(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t touchio_touchin_obj___exit__(size_t n_args, const mp_obj_t *args) { (void)n_args; - common_hal_nativeio_touchin_deinit(args[0]); + common_hal_touchio_touchin_deinit(args[0]); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_touchin___exit___obj, 4, 4, nativeio_touchin_obj___exit__); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(touchio_touchin___exit___obj, 4, 4, touchio_touchin_obj___exit__); //| .. attribute:: value //| @@ -108,32 +108,32 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_touchin___exit___obj, 4, 4, //| :return: True when touched, False otherwise. //| :rtype: bool //| -STATIC mp_obj_t nativeio_touchin_obj_get_value(mp_obj_t self_in) { - nativeio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(common_hal_nativeio_touchin_get_value(self)); +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)); } -MP_DEFINE_CONST_FUN_OBJ_1(nativeio_touchin_get_value_obj, nativeio_touchin_obj_get_value); +MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_value_obj, touchio_touchin_obj_get_value); -const mp_obj_property_t nativeio_touchin_value_obj = { +const mp_obj_property_t touchio_touchin_value_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&nativeio_touchin_get_value_obj, + .proxy = {(mp_obj_t)&touchio_touchin_get_value_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj}, }; -STATIC const mp_rom_map_elem_t nativeio_touchin_locals_dict_table[] = { +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(&nativeio_touchin___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_touchin_deinit_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(&nativeio_touchin_value_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&touchio_touchin_value_obj)}, }; -STATIC MP_DEFINE_CONST_DICT(nativeio_touchin_locals_dict, nativeio_touchin_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(touchio_touchin_locals_dict, touchio_touchin_locals_dict_table); -const mp_obj_type_t nativeio_touchin_type = { +const mp_obj_type_t touchio_touchin_type = { { &mp_type_type }, .name = MP_QSTR_TouchIn, - .make_new = nativeio_touchin_make_new, - .locals_dict = (mp_obj_t)&nativeio_touchin_locals_dict, + .make_new = touchio_touchin_make_new, + .locals_dict = (mp_obj_t)&touchio_touchin_locals_dict, }; diff --git a/shared-bindings/nativeio/TouchIn.h b/shared-bindings/touchio/TouchIn.h index b22f655e5..04a72835e 100644 --- a/shared-bindings/nativeio/TouchIn.h +++ b/shared-bindings/touchio/TouchIn.h @@ -24,16 +24,16 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_TOUCHIN_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_TOUCHIN_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO_TOUCHIN_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO_TOUCHIN_H__ #include "common-hal/microcontroller/types.h" -#include "common-hal/nativeio/types.h" +#include "common-hal/touchio/TouchIn.h" -extern const mp_obj_type_t nativeio_touchin_type; +extern const mp_obj_type_t touchio_touchin_type; -void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, const mcu_pin_obj_t *pin); -void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self); -bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self); +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_get_value(touchio_touchin_obj_t *self); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_TOUCHIN_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO_TOUCHIN_H__ diff --git a/shared-bindings/touchio/__init__.c b/shared-bindings/touchio/__init__.c new file mode 100644 index 000000000..a930c1af7 --- /dev/null +++ b/shared-bindings/touchio/__init__.c @@ -0,0 +1,82 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 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 <stdint.h> + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/touchio/__init__.h" +#include "shared-bindings/touchio/TouchIn.h" + +#include "py/runtime.h" + +//| :mod:`touchio` --- Touch related IO +//| ================================================= +//| +//| .. module:: touchio +//| :synopsis: Hardware accelerated behavior +//| :platform: SAMD21 +//| +//| The `touchio` module contains classes to provide access to touch IO typically +//| accelerated by hardware on the onboard microcontroller. +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| TouchIn +//| +//| All libraries change hardware state and should be deinitialized when they +//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a +//| context manager. +//| +//| For example:: +//| +//| import touchio +//| from board import * +//| +//| with touchio.TouchIn(D6) as touch_pin: +//| print(touch_pin.value) +//| +//| This example will initialize the the device, run +//| :py:data:`~touchio.TouchIn.value` and then :py:meth:`~touchio.TouchIn.deinit` +//| the hardware. +//| + +STATIC const mp_rom_map_elem_t touchio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_touchio) }, + { MP_ROM_QSTR(MP_QSTR_TouchIn), MP_ROM_PTR(&touchio_touchin_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(touchio_module_globals, touchio_module_globals_table); + +const mp_obj_module_t touchio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&touchio_module_globals, +}; diff --git a/shared-bindings/touchio/__init__.h b/shared-bindings/touchio/__init__.h new file mode 100644 index 000000000..fc40e462b --- /dev/null +++ b/shared-bindings/touchio/__init__.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * 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_TOUCHIO___INIT___H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO___INIT___H__ + +#include "py/obj.h" + +// Nothing now. + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO___INIT___H__ diff --git a/shared-bindings/usb_hid/Device.h b/shared-bindings/usb_hid/Device.h index 08713874e..234d7ebc2 100644 --- a/shared-bindings/usb_hid/Device.h +++ b/shared-bindings/usb_hid/Device.h @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ -#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_USB_HID_DEVICE_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_USB_HID_DEVICE_H__ #include "common-hal/usb_hid/types.h" @@ -35,4 +35,4 @@ void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t* uint8_t common_hal_usb_hid_device_get_usage_page(usb_hid_device_obj_t *self); uint8_t common_hal_usb_hid_device_get_usage(usb_hid_device_obj_t *self); -#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_USB_HID_DEVICE_H__ diff --git a/shared-bindings/usb_hid/__init__.h b/shared-bindings/usb_hid/__init__.h index 576a3301b..ff478d083 100644 --- a/shared-bindings/usb_hid/__init__.h +++ b/shared-bindings/usb_hid/__init__.h @@ -30,8 +30,6 @@ #include <stdint.h> #include <stdbool.h> -#include "common-hal/nativeio/types.h" - // TODO(tannewt): Make this a mp_obj_tuple_t when it is dynamically allocated. // until then we hard code it to two entries so LTO is happy. extern mp_obj_tuple2_t common_hal_usb_hid_devices; |
