Better solution to keywords and constructors. Just for I2C for now

This commit is contained in:
ZodiusInfuser 2021-05-14 17:14:07 +01:00
parent 09fc7ca4a7
commit f2ee2a4f8d
19 changed files with 513 additions and 676 deletions

View File

@ -7,7 +7,7 @@
namespace pimoroni {
void MSA301::init() {
bool MSA301::init() {
i2c_init(i2c, 400000);
gpio_set_function(sda, GPIO_FUNC_I2C); gpio_pull_up(sda);
@ -23,6 +23,8 @@ namespace pimoroni {
set_power_mode(PowerMode::NORMAL);
set_range_and_resolution(Range::G_2, Resolution::BITS_14);
return true;
}
void MSA301::reset() {

View File

@ -129,7 +129,7 @@ namespace pimoroni {
// Methods
//--------------------------------------------------
public:
void init();
bool init();
void reset();
i2c_inst_t* get_i2c() const;

View File

@ -1,19 +1,22 @@
#include "breakout_matrix11x7.hpp"
namespace pimoroni {
void BreakoutMatrix11x7::init() {
IS31FL3731::init();
enable({
0b01111111, 0b01111111,
0b01111111, 0b01111111,
0b01111111, 0b01111111,
0b01111111, 0b01111111,
0b01111111, 0b01111111,
0b01111111, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
}, 0);
bool BreakoutMatrix11x7::init() {
bool success = IS31FL3731::init();
if(success) {
enable({
0b01111111, 0b01111111,
0b01111111, 0b01111111,
0b01111111, 0b01111111,
0b01111111, 0b01111111,
0b01111111, 0b01111111,
0b01111111, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
}, 0);
}
return success;
}
uint8_t BreakoutMatrix11x7::lookup_pixel(uint8_t index) {

View File

@ -10,7 +10,7 @@ namespace pimoroni {
static constexpr int8_t DEFAULT_I2C_ADDRESS = 0x75;
static constexpr int8_t ALTERNATE_I2C_ADDRESS = 0x77;
void init();
bool init();
BreakoutMatrix11x7() : IS31FL3731(DEFAULT_I2C_ADDRESS) {};
BreakoutMatrix11x7(uint8_t address) : IS31FL3731(address) {};

View File

@ -2,19 +2,22 @@
namespace pimoroni {
void BreakoutRGBMatrix5x5::init() {
IS31FL3731::init();
enable({
0b00000000, 0b10111111,
0b00111110, 0b00111110,
0b00111111, 0b10111110,
0b00000111, 0b10000110,
0b00110000, 0b00110000,
0b00111111, 0b10111110,
0b00111111, 0b10111110,
0b01111111, 0b11111110,
0b01111111, 0b00000000
}, 0);
bool BreakoutRGBMatrix5x5::init() {
bool success = IS31FL3731::init();
if(success) {
enable({
0b00000000, 0b10111111,
0b00111110, 0b00111110,
0b00111111, 0b10111110,
0b00000111, 0b10000110,
0b00110000, 0b00110000,
0b00111111, 0b10111110,
0b00111111, 0b10111110,
0b01111111, 0b11111110,
0b01111111, 0b00000000
}, 0);
}
return success;
}
RGBLookup BreakoutRGBMatrix5x5::lookup_pixel(uint8_t index) {

View File

@ -16,7 +16,7 @@ namespace pimoroni {
static constexpr int8_t DEFAULT_I2C_ADDRESS = 0x74;
static constexpr int8_t ALTERNATE_I2C_ADDRESS = 0x77;
void init();
bool init();
void set_pixel(uint8_t x, uint8_t y, uint8_t r, uint8_t g, uint8_t b);
BreakoutRGBMatrix5x5() : IS31FL3731(DEFAULT_I2C_ADDRESS) {};

View File

@ -44,49 +44,47 @@ void BreakoutAS7262_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
mp_obj_t BreakoutAS7262_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_as7262_BreakoutAS7262_obj_t *self = nullptr;
if(n_args + n_kw == 0) {
mp_arg_check_num(n_args, n_kw, 0, 0, true);
self = m_new_obj(breakout_as7262_BreakoutAS7262_obj_t);
self->base.type = &breakout_as7262_BreakoutAS7262_type;
self->breakout = new BreakoutAS7262();
enum { ARG_i2c, ARG_sda, ARG_scl, ARG_int };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_sda, MP_ARG_INT, {.u_int = 20} },
{ MP_QSTR_scl, MP_ARG_INT, {.u_int = 21} },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutAS7262::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
int sda = args[ARG_sda].u_int;
int scl = args[ARG_scl].u_int;
if(i2c_id == -1) {
i2c_id = sda & 1;
}
else {
enum { ARG_i2c, ARG_sda, ARG_scl, ARG_int };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_int, MP_ARG_INT, {.u_int = BreakoutAS7262::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
int sda = args[ARG_sda].u_int;
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
int scl = args[ARG_scl].u_int;
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_as7262_BreakoutAS7262_obj_t);
self->base.type = &breakout_as7262_BreakoutAS7262_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutAS7262(i2c, sda, scl, args[ARG_int].u_int);
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
self->breakout->init();
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_as7262_BreakoutAS7262_obj_t);
self->base.type = &breakout_as7262_BreakoutAS7262_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutAS7262(i2c, sda, scl, args[ARG_int].u_int);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "AS7262 breakout not found when initialising");
}
return MP_OBJ_FROM_PTR(self);
}

View File

@ -47,64 +47,47 @@ void BreakoutDotMatrix_print(const mp_print_t *print, mp_obj_t self_in, mp_print
mp_obj_t BreakoutDotMatrix_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_dotmatrix_BreakoutDotMatrix_obj_t *self = nullptr;
if(n_args + n_kw == 0) {
mp_arg_check_num(n_args, n_kw, 0, 0, true);
self = m_new_obj(breakout_dotmatrix_BreakoutDotMatrix_obj_t);
self->base.type = &breakout_dotmatrix_BreakoutDotMatrix_type;
self->breakout = new BreakoutDotMatrix();
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutDotMatrix::DEFAULT_I2C_ADDRESS} },
{ MP_QSTR_sda, MP_ARG_INT, {.u_int = 20} },
{ MP_QSTR_scl, MP_ARG_INT, {.u_int = 21} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
int sda = args[ARG_sda].u_int;
int scl = args[ARG_scl].u_int;
if(i2c_id == -1) {
i2c_id = sda & 1;
}
else if(n_args + n_kw == 1) {
enum { ARG_address };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
self = m_new_obj(breakout_dotmatrix_BreakoutDotMatrix_obj_t);
self->base.type = &breakout_dotmatrix_BreakoutDotMatrix_type;
self->breakout = new BreakoutDotMatrix(args[ARG_address].u_int);
}
else {
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
int sda = args[ARG_sda].u_int;
if (!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
int scl = args[ARG_scl].u_int;
if (!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_dotmatrix_BreakoutDotMatrix_obj_t);
self->base.type = &breakout_dotmatrix_BreakoutDotMatrix_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutDotMatrix(i2c, args[ARG_address].u_int, sda, scl);
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
self->breakout->init();
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_dotmatrix_BreakoutDotMatrix_obj_t);
self->base.type = &breakout_dotmatrix_BreakoutDotMatrix_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutDotMatrix(i2c, args[ARG_address].u_int, sda, scl);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "DotMatrix breakout not found when initialising");
}
return MP_OBJ_FROM_PTR(self);
}

View File

@ -50,64 +50,45 @@ void BreakoutEncoder_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
mp_obj_t BreakoutEncoder_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_encoder_BreakoutEncoder_obj_t *self = nullptr;
if(n_args + n_kw == 0) {
mp_arg_check_num(n_args, n_kw, 0, 0, true);
self = m_new_obj(breakout_encoder_BreakoutEncoder_obj_t);
self->base.type = &breakout_encoder_BreakoutEncoder_type;
self->breakout = new BreakoutEncoder();
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutEncoder::DEFAULT_I2C_ADDRESS} },
{ MP_QSTR_sda, MP_ARG_INT, {.u_int = 20} },
{ MP_QSTR_scl, MP_ARG_INT, {.u_int = 21} },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutEncoder::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
int sda = args[ARG_sda].u_int;
int scl = args[ARG_scl].u_int;
if(i2c_id == -1) {
i2c_id = sda & 1;
}
else if(n_args + n_kw == 1) {
enum { ARG_address };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
self = m_new_obj(breakout_encoder_BreakoutEncoder_obj_t);
self->base.type = &breakout_encoder_BreakoutEncoder_type;
self->breakout = new BreakoutEncoder(args[ARG_address].u_int);
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
else {
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutEncoder::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
int sda = args[ARG_sda].u_int;
if (!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
int scl = args[ARG_scl].u_int;
if (!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_encoder_BreakoutEncoder_obj_t);
self->base.type = &breakout_encoder_BreakoutEncoder_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutEncoder(i2c, args[ARG_address].u_int, sda, scl, args[ARG_interrupt].u_int);
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_encoder_BreakoutEncoder_obj_t);
self->base.type = &breakout_encoder_BreakoutEncoder_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutEncoder(i2c, args[ARG_address].u_int, sda, scl, args[ARG_interrupt].u_int);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "Encoder breakout not found when initialising");
}

View File

@ -50,64 +50,45 @@ void BreakoutIOExpander_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
mp_obj_t BreakoutIOExpander_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_ioexpander_BreakoutIOExpander_obj_t *self = nullptr;
if(n_args + n_kw == 0) {
mp_arg_check_num(n_args, n_kw, 0, 0, true);
self = m_new_obj(breakout_ioexpander_BreakoutIOExpander_obj_t);
self->base.type = &breakout_ioexpander_BreakoutIOExpander_type;
self->breakout = new BreakoutIOExpander();
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutIOExpander::DEFAULT_I2C_ADDRESS} },
{ MP_QSTR_sda, MP_ARG_INT, {.u_int = 20} },
{ MP_QSTR_scl, MP_ARG_INT, {.u_int = 21} },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutIOExpander::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
int sda = args[ARG_sda].u_int;
int scl = args[ARG_scl].u_int;
if(i2c_id == -1) {
i2c_id = sda & 1;
}
else if(n_args + n_kw == 1) {
enum { ARG_address };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
self = m_new_obj(breakout_ioexpander_BreakoutIOExpander_obj_t);
self->base.type = &breakout_ioexpander_BreakoutIOExpander_type;
self->breakout = new BreakoutIOExpander(args[ARG_address].u_int);
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
else {
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutIOExpander::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
int sda = args[ARG_sda].u_int;
if (!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
int scl = args[ARG_scl].u_int;
if (!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_ioexpander_BreakoutIOExpander_obj_t);
self->base.type = &breakout_ioexpander_BreakoutIOExpander_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutIOExpander(i2c, args[ARG_address].u_int, sda, scl, args[ARG_interrupt].u_int);
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_ioexpander_BreakoutIOExpander_obj_t);
self->base.type = &breakout_ioexpander_BreakoutIOExpander_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutIOExpander(i2c, args[ARG_address].u_int, sda, scl, args[ARG_interrupt].u_int);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "IOExpander breakout not found when initialising");
}

View File

@ -50,65 +50,48 @@ void BreakoutLTR559_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
mp_obj_t BreakoutLTR559_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_ltr559_BreakoutLTR559_obj_t *self = nullptr;
if(n_args + n_kw == 0) {
mp_arg_check_num(n_args, n_kw, 0, 0, true);
self = m_new_obj(breakout_ltr559_BreakoutLTR559_obj_t);
self->base.type = &breakout_ltr559_BreakoutLTR559_type;
self->breakout = new BreakoutLTR559();
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutLTR559::DEFAULT_I2C_ADDRESS} },
{ MP_QSTR_sda, MP_ARG_INT, {.u_int = 20} },
{ MP_QSTR_scl, MP_ARG_INT, {.u_int = 21} },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutLTR559::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
int sda = args[ARG_sda].u_int;
int scl = args[ARG_scl].u_int;
if(i2c_id == -1) {
i2c_id = sda & 1;
}
else if(n_args + n_kw == 1) {
enum { ARG_address };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
self = m_new_obj(breakout_ltr559_BreakoutLTR559_obj_t);
self->base.type = &breakout_ltr559_BreakoutLTR559_type;
self->breakout = new BreakoutLTR559(args[ARG_address].u_int);
}
else {
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutLTR559::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
int sda = args[ARG_sda].u_int;
if (!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
int scl = args[ARG_scl].u_int;
if (!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_ltr559_BreakoutLTR559_obj_t);
self->base.type = &breakout_ltr559_BreakoutLTR559_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutLTR559(i2c, args[ARG_address].u_int, sda, scl, args[ARG_interrupt].u_int);
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
self->breakout->init();
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_ltr559_BreakoutLTR559_obj_t);
self->base.type = &breakout_ltr559_BreakoutLTR559_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutLTR559(i2c, args[ARG_address].u_int, sda, scl, args[ARG_interrupt].u_int);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "LTR559 breakout not found when initialising");
}
return MP_OBJ_FROM_PTR(self);
}

View File

@ -47,64 +47,47 @@ void BreakoutMatrix11x7_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
mp_obj_t BreakoutMatrix11x7_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_matrix11x7_BreakoutMatrix11x7_obj_t *self = nullptr;
if(n_args + n_kw == 0) {
mp_arg_check_num(n_args, n_kw, 0, 0, true);
self = m_new_obj(breakout_matrix11x7_BreakoutMatrix11x7_obj_t);
self->base.type = &breakout_matrix11x7_BreakoutMatrix11x7_type;
self->breakout = new BreakoutMatrix11x7();
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutMatrix11x7::DEFAULT_I2C_ADDRESS} },
{ MP_QSTR_sda, MP_ARG_INT, {.u_int = 20} },
{ MP_QSTR_scl, MP_ARG_INT, {.u_int = 21} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
int sda = args[ARG_sda].u_int;
int scl = args[ARG_scl].u_int;
if(i2c_id == -1) {
i2c_id = sda & 1;
}
else if(n_args + n_kw == 1) {
enum { ARG_address };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
self = m_new_obj(breakout_matrix11x7_BreakoutMatrix11x7_obj_t);
self->base.type = &breakout_matrix11x7_BreakoutMatrix11x7_type;
self->breakout = new BreakoutMatrix11x7(args[ARG_address].u_int);
}
else {
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
int sda = args[ARG_sda].u_int;
if (!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
int scl = args[ARG_scl].u_int;
if (!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_matrix11x7_BreakoutMatrix11x7_obj_t);
self->base.type = &breakout_matrix11x7_BreakoutMatrix11x7_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutMatrix11x7(i2c, args[ARG_address].u_int, sda, scl);
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
self->breakout->init();
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_matrix11x7_BreakoutMatrix11x7_obj_t);
self->base.type = &breakout_matrix11x7_BreakoutMatrix11x7_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutMatrix11x7(i2c, args[ARG_address].u_int, sda, scl);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "Matrix11x7 breakout not found when initialising");
}
return MP_OBJ_FROM_PTR(self);
}

View File

@ -50,64 +50,45 @@ void BreakoutMICS6814_print(const mp_print_t *print, mp_obj_t self_in, mp_print_
mp_obj_t BreakoutMICS6814_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_mics6814_BreakoutMICS6814_obj_t *self = nullptr;
if(n_args + n_kw == 0) {
mp_arg_check_num(n_args, n_kw, 0, 0, true);
self = m_new_obj(breakout_mics6814_BreakoutMICS6814_obj_t);
self->base.type = &breakout_mics6814_BreakoutMICS6814_type;
self->breakout = new BreakoutMICS6814();
}
else if(n_args + n_kw == 1) {
enum { ARG_address };
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
};
{ MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutMICS6814::DEFAULT_I2C_ADDRESS} },
{ MP_QSTR_sda, MP_ARG_INT, {.u_int = 20} },
{ MP_QSTR_scl, MP_ARG_INT, {.u_int = 21} },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutMICS6814::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
self = m_new_obj(breakout_mics6814_BreakoutMICS6814_obj_t);
self->base.type = &breakout_mics6814_BreakoutMICS6814_type;
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
int sda = args[ARG_sda].u_int;
int scl = args[ARG_scl].u_int;
self->breakout = new BreakoutMICS6814(args[ARG_address].u_int);
if(i2c_id == -1) {
i2c_id = sda & 1;
}
else {
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutMICS6814::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
int sda = args[ARG_sda].u_int;
if (!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
int scl = args[ARG_scl].u_int;
if (!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_mics6814_BreakoutMICS6814_obj_t);
self->base.type = &breakout_mics6814_BreakoutMICS6814_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutMICS6814(i2c, args[ARG_address].u_int, sda, scl, args[ARG_interrupt].u_int);
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_mics6814_BreakoutMICS6814_obj_t);
self->base.type = &breakout_mics6814_BreakoutMICS6814_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutMICS6814(i2c, args[ARG_address].u_int, sda, scl, args[ARG_interrupt].u_int);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "MICS6814 breakout not found when initialising");
}

View File

@ -44,49 +44,47 @@ void BreakoutMSA301_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
mp_obj_t BreakoutMSA301_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_msa301_BreakoutMSA301_obj_t *self = nullptr;
if(n_args + n_kw == 0) {
mp_arg_check_num(n_args, n_kw, 0, 0, true);
self = m_new_obj(breakout_msa301_BreakoutMSA301_obj_t);
self->base.type = &breakout_msa301_BreakoutMSA301_type;
self->breakout = new BreakoutMSA301();
enum { ARG_i2c, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_sda, MP_ARG_INT, {.u_int = 20} },
{ MP_QSTR_scl, MP_ARG_INT, {.u_int = 21} },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutMSA301::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
int sda = args[ARG_sda].u_int;
int scl = args[ARG_scl].u_int;
if(i2c_id == -1) {
i2c_id = sda & 1;
}
else {
enum { ARG_i2c, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutMSA301::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
int sda = args[ARG_sda].u_int;
if (!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
int scl = args[ARG_scl].u_int;
if (!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_msa301_BreakoutMSA301_obj_t);
self->base.type = &breakout_msa301_BreakoutMSA301_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutMSA301(i2c, sda, scl, args[ARG_interrupt].u_int);
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
self->breakout->init();
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_msa301_BreakoutMSA301_obj_t);
self->base.type = &breakout_msa301_BreakoutMSA301_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutMSA301(i2c, sda, scl, args[ARG_interrupt].u_int);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "MSA301 breakout not found when initialising");
}
return MP_OBJ_FROM_PTR(self);
}

View File

@ -50,64 +50,45 @@ void BreakoutPotentiometer_print(const mp_print_t *print, mp_obj_t self_in, mp_p
mp_obj_t BreakoutPotentiometer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_potentiometer_BreakoutPotentiometer_obj_t *self = nullptr;
if(n_args + n_kw == 0) {
mp_arg_check_num(n_args, n_kw, 0, 0, true);
self = m_new_obj(breakout_potentiometer_BreakoutPotentiometer_obj_t);
self->base.type = &breakout_potentiometer_BreakoutPotentiometer_type;
self->breakout = new BreakoutPotentiometer();
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutPotentiometer::DEFAULT_I2C_ADDRESS} },
{ MP_QSTR_sda, MP_ARG_INT, {.u_int = 20} },
{ MP_QSTR_scl, MP_ARG_INT, {.u_int = 21} },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutPotentiometer::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
int sda = args[ARG_sda].u_int;
int scl = args[ARG_scl].u_int;
if(i2c_id == -1) {
i2c_id = sda & 1;
}
else if(n_args + n_kw == 1) {
enum { ARG_address };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
self = m_new_obj(breakout_potentiometer_BreakoutPotentiometer_obj_t);
self->base.type = &breakout_potentiometer_BreakoutPotentiometer_type;
self->breakout = new BreakoutPotentiometer(args[ARG_address].u_int);
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
else {
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutPotentiometer::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
int sda = args[ARG_sda].u_int;
if (!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
int scl = args[ARG_scl].u_int;
if (!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_potentiometer_BreakoutPotentiometer_obj_t);
self->base.type = &breakout_potentiometer_BreakoutPotentiometer_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutPotentiometer(i2c, args[ARG_address].u_int, sda, scl, args[ARG_interrupt].u_int);
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_potentiometer_BreakoutPotentiometer_obj_t);
self->base.type = &breakout_potentiometer_BreakoutPotentiometer_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutPotentiometer(i2c, args[ARG_address].u_int, sda, scl, args[ARG_interrupt].u_int);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "Potentiometer breakout not found when initialising");
}

View File

@ -47,64 +47,47 @@ void BreakoutRGBMatrix5x5_print(const mp_print_t *print, mp_obj_t self_in, mp_pr
mp_obj_t BreakoutRGBMatrix5x5_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_rgbmatrix5x5_BreakoutRGBMatrix5x5_obj_t *self = nullptr;
if(n_args + n_kw == 0) {
mp_arg_check_num(n_args, n_kw, 0, 0, true);
self = m_new_obj(breakout_rgbmatrix5x5_BreakoutRGBMatrix5x5_obj_t);
self->base.type = &breakout_rgbmatrix5x5_BreakoutRGBMatrix5x5_type;
self->breakout = new BreakoutRGBMatrix5x5();
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutRGBMatrix5x5::DEFAULT_I2C_ADDRESS} },
{ MP_QSTR_sda, MP_ARG_INT, {.u_int = 20} },
{ MP_QSTR_scl, MP_ARG_INT, {.u_int = 21} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
int sda = args[ARG_sda].u_int;
int scl = args[ARG_scl].u_int;
if(i2c_id == -1) {
i2c_id = sda & 1;
}
else if(n_args + n_kw == 1) {
enum { ARG_address };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
self = m_new_obj(breakout_rgbmatrix5x5_BreakoutRGBMatrix5x5_obj_t);
self->base.type = &breakout_rgbmatrix5x5_BreakoutRGBMatrix5x5_type;
self->breakout = new BreakoutRGBMatrix5x5(args[ARG_address].u_int);
}
else {
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
int sda = args[ARG_sda].u_int;
if (!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
int scl = args[ARG_scl].u_int;
if (!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_rgbmatrix5x5_BreakoutRGBMatrix5x5_obj_t);
self->base.type = &breakout_rgbmatrix5x5_BreakoutRGBMatrix5x5_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutRGBMatrix5x5(i2c, args[ARG_address].u_int, sda, scl);
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
self->breakout->init();
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_rgbmatrix5x5_BreakoutRGBMatrix5x5_obj_t);
self->base.type = &breakout_rgbmatrix5x5_BreakoutRGBMatrix5x5_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutRGBMatrix5x5(i2c, args[ARG_address].u_int, sda, scl);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "RGBMatrix5x5 breakout not found when initialising");
}
return MP_OBJ_FROM_PTR(self);
}

View File

@ -47,49 +47,47 @@ void BreakoutRTC_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_
mp_obj_t BreakoutRTC_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_rtc_BreakoutRTC_obj_t *self = nullptr;
if(n_args + n_kw == 0) {
mp_arg_check_num(n_args, n_kw, 0, 0, true);
self = m_new_obj(breakout_rtc_BreakoutRTC_obj_t);
self->base.type = &breakout_rtc_BreakoutRTC_type;
self->breakout = new BreakoutRTC();
enum { ARG_i2c, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_sda, MP_ARG_INT, {.u_int = 20} },
{ MP_QSTR_scl, MP_ARG_INT, {.u_int = 21} },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutRTC::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
int sda = args[ARG_sda].u_int;
int scl = args[ARG_scl].u_int;
if(i2c_id == -1) {
i2c_id = sda & 1;
}
else {
enum { ARG_i2c, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutRTC::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
int sda = args[ARG_sda].u_int;
if (!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
int scl = args[ARG_scl].u_int;
if (!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_rtc_BreakoutRTC_obj_t);
self->base.type = &breakout_rtc_BreakoutRTC_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutRTC(i2c, sda, scl, args[ARG_interrupt].u_int);
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
self->breakout->init();
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_rtc_BreakoutRTC_obj_t);
self->base.type = &breakout_rtc_BreakoutRTC_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutRTC(i2c, sda, scl, args[ARG_interrupt].u_int);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "RTC breakout not found when initialising");
}
return MP_OBJ_FROM_PTR(self);
}

View File

@ -41,49 +41,45 @@ void BreakoutSGP30_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kin
mp_obj_t BreakoutSGP30_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_sgp30_BreakoutSGP30_obj_t *self = nullptr;
if(n_args + n_kw == 0) {
mp_arg_check_num(n_args, n_kw, 0, 0, true);
self = m_new_obj(breakout_sgp30_BreakoutSGP30_obj_t);
self->base.type = &breakout_sgp30_BreakoutSGP30_type;
self->breakout = new BreakoutSGP30();
enum { ARG_i2c, ARG_sda, ARG_scl };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_sda, MP_ARG_INT, {.u_int = 20} },
{ MP_QSTR_scl, MP_ARG_INT, {.u_int = 21} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
int sda = args[ARG_sda].u_int;
int scl = args[ARG_scl].u_int;
if(i2c_id == -1) {
i2c_id = sda & 1;
}
else {
enum { ARG_i2c, ARG_sda, ARG_scl };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
int sda = args[ARG_sda].u_int;
if (!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
int scl = args[ARG_scl].u_int;
if (!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_sgp30_BreakoutSGP30_obj_t);
self->base.type = &breakout_sgp30_BreakoutSGP30_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutSGP30(i2c, sda, scl);
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_sgp30_BreakoutSGP30_obj_t);
self->base.type = &breakout_sgp30_BreakoutSGP30_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutSGP30(i2c, sda, scl);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "SGP30 not found when initialising");
mp_raise_msg(&mp_type_RuntimeError, "SGP30 breakout not found when initialising");
}
return MP_OBJ_FROM_PTR(self);

View File

@ -50,65 +50,48 @@ void BreakoutTrackball_print(const mp_print_t *print, mp_obj_t self_in, mp_print
mp_obj_t BreakoutTrackball_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_trackball_BreakoutTrackball_obj_t *self = nullptr;
if(n_args + n_kw == 0) {
mp_arg_check_num(n_args, n_kw, 0, 0, true);
self = m_new_obj(breakout_trackball_BreakoutTrackball_obj_t);
self->base.type = &breakout_trackball_BreakoutTrackball_type;
self->breakout = new BreakoutTrackball();
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutTrackball::DEFAULT_I2C_ADDRESS} },
{ MP_QSTR_sda, MP_ARG_INT, {.u_int = 20} },
{ MP_QSTR_scl, MP_ARG_INT, {.u_int = 21} },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutTrackball::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
int sda = args[ARG_sda].u_int;
int scl = args[ARG_scl].u_int;
if(i2c_id == -1) {
i2c_id = sda & 1;
}
else if(n_args + n_kw == 1) {
enum { ARG_address };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
self = m_new_obj(breakout_trackball_BreakoutTrackball_obj_t);
self->base.type = &breakout_trackball_BreakoutTrackball_type;
self->breakout = new BreakoutTrackball(args[ARG_address].u_int);
}
else {
enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = BreakoutTrackball::PIN_UNUSED} },
};
// Parse args.
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Get I2C bus.
int i2c_id = args[ARG_i2c].u_int;
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
int sda = args[ARG_sda].u_int;
if (!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
int scl = args[ARG_scl].u_int;
if (!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_trackball_BreakoutTrackball_obj_t);
self->base.type = &breakout_trackball_BreakoutTrackball_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutTrackball(i2c, args[ARG_address].u_int, sda, scl, args[ARG_interrupt].u_int);
if(i2c_id < 0 || i2c_id > 1) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
}
self->breakout->init();
if(!IS_VALID_SDA(i2c_id, sda)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
}
if(!IS_VALID_SCL(i2c_id, scl)) {
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
}
self = m_new_obj(breakout_trackball_BreakoutTrackball_obj_t);
self->base.type = &breakout_trackball_BreakoutTrackball_type;
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
self->breakout = new BreakoutTrackball(i2c, args[ARG_address].u_int, sda, scl, args[ARG_interrupt].u_int);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "Trackball breakout not found when initialising");
}
return MP_OBJ_FROM_PTR(self);
}