shared-bindings: Ensure pin objects are actually pins.

Fixes #12
This commit is contained in:
Scott Shawcroft 2016-11-29 15:49:50 -08:00
parent 72455e441f
commit 4933fa1c27
11 changed files with 111 additions and 78 deletions

View File

@ -36,8 +36,8 @@
void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self,
const mcu_pin_obj_t *pin) {
if (pin->pin != PIN_PA02) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
"DAC only supported on pin PA02."));
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
"AnalogOut only supported on pin PA02."));
return;
}
struct dac_config config_dac;

View File

@ -28,6 +28,7 @@
// bitbangio.I2C class.
#include "shared-bindings/bitbangio/I2C.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "py/runtime.h"
//| .. currentmodule:: bitbangio
@ -59,6 +60,8 @@ STATIC mp_obj_t bitbangio_i2c_make_new(const mp_obj_type_t *type, size_t n_args,
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
assert_pin(args[ARG_scl].u_obj, false);
assert_pin(args[ARG_sda].u_obj, false);
const mcu_pin_obj_t* scl = MP_OBJ_TO_PTR(args[ARG_scl].u_obj);
const mcu_pin_obj_t* sda = MP_OBJ_TO_PTR(args[ARG_sda].u_obj);
shared_module_bitbangio_i2c_construct(self, scl, sda, args[ARG_freq].u_int);

View File

@ -30,6 +30,7 @@
#include <string.h>
#include "shared-bindings/bitbangio/SPI.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "py/runtime.h"
@ -76,6 +77,9 @@ STATIC mp_obj_t bitbangio_spi_make_new(const mp_obj_type_t *type, size_t n_args,
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
assert_pin(args[ARG_clock].u_obj, false);
assert_pin(args[ARG_MOSI].u_obj, true);
assert_pin(args[ARG_MISO].u_obj, true);
const mcu_pin_obj_t* clock = MP_OBJ_TO_PTR(args[ARG_clock].u_obj);
const mcu_pin_obj_t* mosi = MP_OBJ_TO_PTR(args[ARG_MOSI].u_obj);
const mcu_pin_obj_t* miso = MP_OBJ_TO_PTR(args[ARG_MISO].u_obj);

View File

@ -26,6 +26,9 @@
#include "shared-bindings/microcontroller/Pin.h"
#include "py/nlr.h"
#include "py/obj.h"
//| .. currentmodule:: microcontroller
//|
//| :class:`Pin` --- Pin reference
@ -44,3 +47,9 @@ const mp_obj_type_t mcu_pin_type = {
{ &mp_type_type },
.name = MP_QSTR_Pin,
};
void assert_pin(mp_obj_t obj, bool none_ok) {
if ((obj != mp_const_none || !none_ok) && !MP_OBJ_IS_TYPE(obj, &mcu_pin_type)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Expected a Pin"));
}
}

View File

@ -32,4 +32,6 @@
// Type object used in Python. Should be shared between ports.
extern const mp_obj_type_t mcu_pin_type;
void assert_pin(mp_obj_t obj, bool none_ok);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_PIN_H__

View File

@ -31,6 +31,7 @@
#include "py/nlr.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/AnalogIn.h"
//| .. currentmodule:: nativeio
@ -60,6 +61,7 @@ STATIC mp_obj_t nativeio_analogin_make_new(const mp_obj_type_t *type,
// 1st argument is the pin
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;

View File

@ -29,6 +29,7 @@
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/AnalogOut.h"
//| .. currentmodule:: nativeio
@ -57,11 +58,12 @@ STATIC mp_obj_t nativeio_analogout_make_new(const mp_obj_type_t *type, mp_uint_t
// 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;
const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(args[0]);
common_hal_nativeio_analogout_construct(self, pin);
return self;

View File

@ -33,6 +33,7 @@
#include "py/runtime.h"
#include "py/mphal.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/DigitalInOut.h"
//| .. currentmodule:: nativeio
@ -60,6 +61,7 @@ STATIC mp_obj_t nativeio_digitalinout_make_new(const mp_obj_type_t *type,
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]);
common_hal_nativeio_digitalinout_construct(self, pin);

View File

@ -27,6 +27,7 @@
// This file contains all of the Python API definitions for the
// nativeio.I2C class.
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/I2C.h"
#include "py/runtime.h"
@ -59,6 +60,8 @@ STATIC mp_obj_t nativeio_i2c_make_new(const mp_obj_type_t *type, size_t n_args,
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
assert_pin(args[ARG_scl].u_obj, false);
assert_pin(args[ARG_sda].u_obj, false);
const mcu_pin_obj_t* scl = MP_OBJ_TO_PTR(args[ARG_scl].u_obj);
const mcu_pin_obj_t* sda = MP_OBJ_TO_PTR(args[ARG_sda].u_obj);
common_hal_nativeio_i2c_construct(self, scl, sda, args[ARG_freq].u_int);

View File

@ -28,6 +28,7 @@
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/PWMOut.h"
//| .. currentmodule:: nativeio
@ -48,6 +49,7 @@
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) {
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);
const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj);
// create PWM object from the given pin

View File

@ -29,6 +29,7 @@
#include <string.h>
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/SPI.h"
#include "py/runtime.h"
@ -76,6 +77,9 @@ STATIC mp_obj_t nativeio_spi_make_new(const mp_obj_type_t *type, size_t n_args,
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
assert_pin(args[ARG_clock].u_obj, false);
assert_pin(args[ARG_MOSI].u_obj, true);
assert_pin(args[ARG_MISO].u_obj, true);
const mcu_pin_obj_t* clock = MP_OBJ_TO_PTR(args[ARG_clock].u_obj);
const mcu_pin_obj_t* mosi = MP_OBJ_TO_PTR(args[ARG_MOSI].u_obj);
const mcu_pin_obj_t* miso = MP_OBJ_TO_PTR(args[ARG_MISO].u_obj);