From f2ee2a4f8d35ccdadea8228a2b63b930d78c5f89 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Fri, 14 May 2021 17:14:07 +0100 Subject: [PATCH] Better solution to keywords and constructors. Just for I2C for now --- drivers/msa301/msa301.cpp | 4 +- drivers/msa301/msa301.hpp | 2 +- .../breakout_matrix11x7.cpp | 29 +++--- .../breakout_matrix11x7.hpp | 2 +- .../breakout_rgbmatrix5x5.cpp | 29 +++--- .../breakout_rgbmatrix5x5.hpp | 2 +- .../breakout_as7262/breakout_as7262.cpp | 78 ++++++++------- .../breakout_dotmatrix/breakout_dotmatrix.cpp | 93 ++++++++---------- .../breakout_encoder/breakout_encoder.cpp | 87 +++++++---------- .../breakout_ioexpander.cpp | 87 +++++++---------- .../breakout_ltr559/breakout_ltr559.cpp | 95 ++++++++----------- .../breakout_matrix11x7.cpp | 93 ++++++++---------- .../breakout_mics6814/breakout_mics6814.cpp | 83 +++++++--------- .../breakout_msa301/breakout_msa301.cpp | 78 ++++++++------- .../breakout_potentiometer.cpp | 87 +++++++---------- .../breakout_rgbmatrix5x5.cpp | 93 ++++++++---------- .../modules/breakout_rtc/breakout_rtc.cpp | 78 ++++++++------- .../modules/breakout_sgp30/breakout_sgp30.cpp | 74 +++++++-------- .../breakout_trackball/breakout_trackball.cpp | 95 ++++++++----------- 19 files changed, 513 insertions(+), 676 deletions(-) diff --git a/drivers/msa301/msa301.cpp b/drivers/msa301/msa301.cpp index 3eec6912..a190de51 100644 --- a/drivers/msa301/msa301.cpp +++ b/drivers/msa301/msa301.cpp @@ -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() { diff --git a/drivers/msa301/msa301.hpp b/drivers/msa301/msa301.hpp index 2198af93..afdbc8ca 100644 --- a/drivers/msa301/msa301.hpp +++ b/drivers/msa301/msa301.hpp @@ -129,7 +129,7 @@ namespace pimoroni { // Methods //-------------------------------------------------- public: - void init(); + bool init(); void reset(); i2c_inst_t* get_i2c() const; diff --git a/libraries/breakout_matrix11x7/breakout_matrix11x7.cpp b/libraries/breakout_matrix11x7/breakout_matrix11x7.cpp index ed4b19f3..75ec353b 100644 --- a/libraries/breakout_matrix11x7/breakout_matrix11x7.cpp +++ b/libraries/breakout_matrix11x7/breakout_matrix11x7.cpp @@ -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) { diff --git a/libraries/breakout_matrix11x7/breakout_matrix11x7.hpp b/libraries/breakout_matrix11x7/breakout_matrix11x7.hpp index 4ebebd19..cef3d646 100644 --- a/libraries/breakout_matrix11x7/breakout_matrix11x7.hpp +++ b/libraries/breakout_matrix11x7/breakout_matrix11x7.hpp @@ -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) {}; diff --git a/libraries/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp b/libraries/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp index 072cdf5f..b9262afe 100644 --- a/libraries/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp +++ b/libraries/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp @@ -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) { diff --git a/libraries/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.hpp b/libraries/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.hpp index e98dd447..e9b1e3be 100644 --- a/libraries/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.hpp +++ b/libraries/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.hpp @@ -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) {}; diff --git a/micropython/modules/breakout_as7262/breakout_as7262.cpp b/micropython/modules/breakout_as7262/breakout_as7262.cpp index 88ee017c..deecafc3 100644 --- a/micropython/modules/breakout_as7262/breakout_as7262.cpp +++ b/micropython/modules/breakout_as7262/breakout_as7262.cpp @@ -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); } diff --git a/micropython/modules/breakout_dotmatrix/breakout_dotmatrix.cpp b/micropython/modules/breakout_dotmatrix/breakout_dotmatrix.cpp index 698866b6..0f961745 100644 --- a/micropython/modules/breakout_dotmatrix/breakout_dotmatrix.cpp +++ b/micropython/modules/breakout_dotmatrix/breakout_dotmatrix.cpp @@ -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); } diff --git a/micropython/modules/breakout_encoder/breakout_encoder.cpp b/micropython/modules/breakout_encoder/breakout_encoder.cpp index 0af2758a..8b8fe3de 100644 --- a/micropython/modules/breakout_encoder/breakout_encoder.cpp +++ b/micropython/modules/breakout_encoder/breakout_encoder.cpp @@ -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"); } diff --git a/micropython/modules/breakout_ioexpander/breakout_ioexpander.cpp b/micropython/modules/breakout_ioexpander/breakout_ioexpander.cpp index 227fff2a..ef4135aa 100644 --- a/micropython/modules/breakout_ioexpander/breakout_ioexpander.cpp +++ b/micropython/modules/breakout_ioexpander/breakout_ioexpander.cpp @@ -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"); } diff --git a/micropython/modules/breakout_ltr559/breakout_ltr559.cpp b/micropython/modules/breakout_ltr559/breakout_ltr559.cpp index 7d82ea8a..1130c83a 100644 --- a/micropython/modules/breakout_ltr559/breakout_ltr559.cpp +++ b/micropython/modules/breakout_ltr559/breakout_ltr559.cpp @@ -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); } diff --git a/micropython/modules/breakout_matrix11x7/breakout_matrix11x7.cpp b/micropython/modules/breakout_matrix11x7/breakout_matrix11x7.cpp index 42995e29..ea5dd72c 100644 --- a/micropython/modules/breakout_matrix11x7/breakout_matrix11x7.cpp +++ b/micropython/modules/breakout_matrix11x7/breakout_matrix11x7.cpp @@ -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); } diff --git a/micropython/modules/breakout_mics6814/breakout_mics6814.cpp b/micropython/modules/breakout_mics6814/breakout_mics6814.cpp index 96bef2e1..800a5440 100644 --- a/micropython/modules/breakout_mics6814/breakout_mics6814.cpp +++ b/micropython/modules/breakout_mics6814/breakout_mics6814.cpp @@ -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"); } diff --git a/micropython/modules/breakout_msa301/breakout_msa301.cpp b/micropython/modules/breakout_msa301/breakout_msa301.cpp index b7bf8cca..7f7db729 100644 --- a/micropython/modules/breakout_msa301/breakout_msa301.cpp +++ b/micropython/modules/breakout_msa301/breakout_msa301.cpp @@ -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); } diff --git a/micropython/modules/breakout_potentiometer/breakout_potentiometer.cpp b/micropython/modules/breakout_potentiometer/breakout_potentiometer.cpp index 4cd285b9..5cbfb3e3 100644 --- a/micropython/modules/breakout_potentiometer/breakout_potentiometer.cpp +++ b/micropython/modules/breakout_potentiometer/breakout_potentiometer.cpp @@ -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"); } diff --git a/micropython/modules/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp b/micropython/modules/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp index 5eaf566b..d2531281 100644 --- a/micropython/modules/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp +++ b/micropython/modules/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp @@ -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); } diff --git a/micropython/modules/breakout_rtc/breakout_rtc.cpp b/micropython/modules/breakout_rtc/breakout_rtc.cpp index f7e7edce..4c55b897 100644 --- a/micropython/modules/breakout_rtc/breakout_rtc.cpp +++ b/micropython/modules/breakout_rtc/breakout_rtc.cpp @@ -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); } diff --git a/micropython/modules/breakout_sgp30/breakout_sgp30.cpp b/micropython/modules/breakout_sgp30/breakout_sgp30.cpp index 880b185e..590c6c14 100644 --- a/micropython/modules/breakout_sgp30/breakout_sgp30.cpp +++ b/micropython/modules/breakout_sgp30/breakout_sgp30.cpp @@ -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); diff --git a/micropython/modules/breakout_trackball/breakout_trackball.cpp b/micropython/modules/breakout_trackball/breakout_trackball.cpp index f7692465..357b9a7a 100644 --- a/micropython/modules/breakout_trackball/breakout_trackball.cpp +++ b/micropython/modules/breakout_trackball/breakout_trackball.cpp @@ -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); }