diff --git a/micropython/examples/breakout_as7262/demo.py b/micropython/examples/breakout_as7262/demo.py index f6fdd8d7..da100888 100644 --- a/micropython/examples/breakout_as7262/demo.py +++ b/micropython/examples/breakout_as7262/demo.py @@ -1,7 +1,12 @@ import time +from pimoroni_i2c import PimoroniI2C from breakout_as7262 import BreakoutAS7262 -as7262 = BreakoutAS7262() +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +as7262 = BreakoutAS7262(i2c) dev_type = as7262.device_type() hw_version = as7262.hardware_version() diff --git a/micropython/examples/breakout_dotmatrix/bargraph.py b/micropython/examples/breakout_dotmatrix/bargraph.py index 665e1a6f..e80f4aec 100644 --- a/micropython/examples/breakout_dotmatrix/bargraph.py +++ b/micropython/examples/breakout_dotmatrix/bargraph.py @@ -2,9 +2,14 @@ import time import math import random +from pimoroni_i2c import PimoroniI2C from breakout_dotmatrix import BreakoutDotMatrix -display = BreakoutDotMatrix() +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +display = BreakoutDotMatrix(i2c) width = display.WIDTH height = display.HEIGHT diff --git a/micropython/examples/breakout_dotmatrix/eyes.py b/micropython/examples/breakout_dotmatrix/eyes.py index 7d0b0484..e625f1e4 100644 --- a/micropython/examples/breakout_dotmatrix/eyes.py +++ b/micropython/examples/breakout_dotmatrix/eyes.py @@ -1,10 +1,15 @@ -import math import time +import math import random +from pimoroni_i2c import PimoroniI2C from breakout_dotmatrix import BreakoutDotMatrix -display = BreakoutDotMatrix() +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +display = BreakoutDotMatrix(i2c) start_time = time.time() diff --git a/micropython/examples/breakout_dotmatrix/image.py b/micropython/examples/breakout_dotmatrix/image.py index f1af07ae..31e7acfe 100644 --- a/micropython/examples/breakout_dotmatrix/image.py +++ b/micropython/examples/breakout_dotmatrix/image.py @@ -1,9 +1,14 @@ import time import math +from pimoroni_i2c import PimoroniI2C from breakout_dotmatrix import BreakoutDotMatrix -display = BreakoutDotMatrix() +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +display = BreakoutDotMatrix(i2c) # Left Image Padding Right Image Padding image = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, diff --git a/micropython/examples/breakout_dotmatrix/timer.py b/micropython/examples/breakout_dotmatrix/timer.py index c32db6cf..d420f4ec 100644 --- a/micropython/examples/breakout_dotmatrix/timer.py +++ b/micropython/examples/breakout_dotmatrix/timer.py @@ -1,11 +1,16 @@ import time +from pimoroni_i2c import PimoroniI2C from breakout_dotmatrix import BreakoutDotMatrix +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + address = 0x61 start_time = time.time() -display = BreakoutDotMatrix(address=address) +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +display = BreakoutDotMatrix(i2c, address=address) display.clear() display.show() diff --git a/micropython/examples/breakout_encoder/demo.py b/micropython/examples/breakout_encoder/demo.py index c8337453..848e4192 100644 --- a/micropython/examples/breakout_encoder/demo.py +++ b/micropython/examples/breakout_encoder/demo.py @@ -1,8 +1,13 @@ +from pimoroni_i2c import PimoroniI2C from breakout_encoder import BreakoutEncoder +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + steps_per_rev = 24 -enc = BreakoutEncoder() +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +enc = BreakoutEncoder(i2c) enc.set_brightness(1.0) # enc.set_direction(BreakoutEncoder.DIRECTION_CCW) # Uncomment this to flip the direction diff --git a/micropython/examples/breakout_ioexpander/adc.py b/micropython/examples/breakout_ioexpander/adc.py index 290a83ba..f66a9274 100644 --- a/micropython/examples/breakout_ioexpander/adc.py +++ b/micropython/examples/breakout_ioexpander/adc.py @@ -1,9 +1,14 @@ import time +from pimoroni_i2c import PimoroniI2C from breakout_ioexpander import BreakoutIOExpander +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + ioe_adc_pin = 10 -ioe = BreakoutIOExpander(0x18) +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +ioe = BreakoutIOExpander(i2c, address=0x18) ioe.set_mode(ioe_adc_pin, BreakoutIOExpander.PIN_ADC) diff --git a/micropython/examples/breakout_ioexpander/button.py b/micropython/examples/breakout_ioexpander/button.py index 624306a6..1955073b 100644 --- a/micropython/examples/breakout_ioexpander/button.py +++ b/micropython/examples/breakout_ioexpander/button.py @@ -1,9 +1,14 @@ import time +from pimoroni_i2c import PimoroniI2C from breakout_ioexpander import BreakoutIOExpander +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + ioe_button_pin = 14 -ioe = BreakoutIOExpander(0x18) +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +ioe = BreakoutIOExpander(i2c, address=0x18) ioe.set_mode(ioe_button_pin, BreakoutIOExpander.PIN_IN_PU) diff --git a/micropython/examples/breakout_ioexpander/servo.py b/micropython/examples/breakout_ioexpander/servo.py index 7ca012fc..622fe0ab 100644 --- a/micropython/examples/breakout_ioexpander/servo.py +++ b/micropython/examples/breakout_ioexpander/servo.py @@ -1,7 +1,11 @@ import time import math +from pimoroni_i2c import PimoroniI2C from breakout_ioexpander import BreakoutIOExpander +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + ioe_servo_pin = 1 # Settings to produce a 50Hz output from the 24MHz clock. @@ -12,7 +16,8 @@ period = 60000 cycle_time = 5.0 servo_range = 1000.0 # Between 1000 and 2000us (1-2ms) -ioe = BreakoutIOExpander(0x18) +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +ioe = BreakoutIOExpander(i2c, address=0x18) ioe.set_pwm_period(period) ioe.set_pwm_control(divider) diff --git a/micropython/examples/breakout_ltr559/demo.py b/micropython/examples/breakout_ltr559/demo.py index 7f79f67d..a027de45 100644 --- a/micropython/examples/breakout_ltr559/demo.py +++ b/micropython/examples/breakout_ltr559/demo.py @@ -1,7 +1,12 @@ import time +from pimoroni_i2c import PimoroniI2C from breakout_ltr559 import BreakoutLTR559 -ltr = BreakoutLTR559() +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +ltr = BreakoutLTR559(i2c) part_id = ltr.part_id() print("Found LTR559. Part ID: 0x", '{:02x}'.format(part_id), sep="") diff --git a/micropython/examples/breakout_matrix11x7/demo.py b/micropython/examples/breakout_matrix11x7/demo.py index 47c83677..07554c51 100644 --- a/micropython/examples/breakout_matrix11x7/demo.py +++ b/micropython/examples/breakout_matrix11x7/demo.py @@ -1,9 +1,14 @@ import time +from pimoroni_i2c import PimoroniI2C from breakout_matrix11x7 import BreakoutMatrix11x7 on_brightness = 64 -matrix = BreakoutMatrix11x7() +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +matrix = BreakoutMatrix11x7(i2c) x = 0 y = 0 diff --git a/micropython/examples/breakout_mics6814/demo.py b/micropython/examples/breakout_mics6814/demo.py index b8af41c6..cdd8c08a 100644 --- a/micropython/examples/breakout_mics6814/demo.py +++ b/micropython/examples/breakout_mics6814/demo.py @@ -1,9 +1,14 @@ import time +from pimoroni_i2c import PimoroniI2C from breakout_mics6814 import BreakoutMICS6814 OUTPUT_FREQUENCY = 0.5 -gas = BreakoutMICS6814() +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +gas = BreakoutMICS6814(i2c) gas.set_brightness(1.0) diff --git a/micropython/examples/breakout_msa301/demo.py b/micropython/examples/breakout_msa301/demo.py index 703915a3..cb2c9792 100644 --- a/micropython/examples/breakout_msa301/demo.py +++ b/micropython/examples/breakout_msa301/demo.py @@ -1,7 +1,12 @@ import time +from pimoroni_i2c import PimoroniI2C from breakout_msa301 import BreakoutMSA301 -msa = BreakoutMSA301() +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +msa = BreakoutMSA301(i2c) part_id = msa.part_id() print("Found MSA301. Part ID: 0x", '{:02x}'.format(part_id), sep="") diff --git a/micropython/examples/breakout_potentiometer/demo.py b/micropython/examples/breakout_potentiometer/demo.py index ec354b72..81cbfb2b 100644 --- a/micropython/examples/breakout_potentiometer/demo.py +++ b/micropython/examples/breakout_potentiometer/demo.py @@ -1,7 +1,12 @@ import time +from pimoroni_i2c import PimoroniI2C from breakout_potentiometer import BreakoutPotentiometer -pot = BreakoutPotentiometer() +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +pot = BreakoutPotentiometer(i2c) pot.set_brightness(1.0) # pot.set_direction(BreakoutPotentiometer.DIRECTION_CCW) # Uncomment this to flip the direction diff --git a/micropython/examples/breakout_rgbmatrix5x5/demo.py b/micropython/examples/breakout_rgbmatrix5x5/demo.py index c5862842..5f486b79 100644 --- a/micropython/examples/breakout_rgbmatrix5x5/demo.py +++ b/micropython/examples/breakout_rgbmatrix5x5/demo.py @@ -1,14 +1,19 @@ import time +from pimoroni_i2c import PimoroniI2C from breakout_rgbmatrix5x5 import BreakoutRGBMatrix5x5 +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +matrix = BreakoutRGBMatrix5x5(i2c) + colors = [] colors.append((255, 0, 0)) colors.append((0, 255, 0)) colors.append((0, 0, 255)) colors.append((128, 128, 128)) -matrix = BreakoutRGBMatrix5x5() - x = 0 y = 0 col = 0 diff --git a/micropython/examples/breakout_rtc/demo.py b/micropython/examples/breakout_rtc/demo.py index a1c19fa3..cc24b6ca 100644 --- a/micropython/examples/breakout_rtc/demo.py +++ b/micropython/examples/breakout_rtc/demo.py @@ -1,8 +1,12 @@ +from pimoroni_i2c import PimoroniI2C from breakout_rtc import BreakoutRTC import time -# rtc = BreakoutRTC(sda=4, scl=5) # i2c pins 4, 5 for Breakout Garden -rtc = BreakoutRTC() # Default i2c pins for Pico Explorer +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} # i2c pins 4, 5 for Breakout Garden +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} # Default i2c pins for Pico Explorer + +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +rtc = BreakoutRTC(i2c) if rtc.is_12_hour(): rtc.set_24_hour() diff --git a/micropython/examples/breakout_sgp30/demo.py b/micropython/examples/breakout_sgp30/demo.py index 4894b167..2a316bdb 100644 --- a/micropython/examples/breakout_sgp30/demo.py +++ b/micropython/examples/breakout_sgp30/demo.py @@ -1,8 +1,12 @@ import time +from pimoroni_i2c import PimoroniI2C from breakout_sgp30 import BreakoutSGP30 -# sgp30 = BreakoutSGP30(sda=4, scl=5) # i2c pins 4, 5 for Breakout Garden -sgp30 = BreakoutSGP30() # Default i2c pins for Pico Explorer +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} # i2c pins 4, 5 for Breakout Garden +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} # Default i2c pins for Pico Explorer + +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +sgp30 = BreakoutSGP30(i2c) print("SGP30 initialised - about to start measuring without waiting") diff --git a/micropython/examples/breakout_trackball/demo.py b/micropython/examples/breakout_trackball/demo.py index e846b6c6..5d726ee1 100644 --- a/micropython/examples/breakout_trackball/demo.py +++ b/micropython/examples/breakout_trackball/demo.py @@ -1,12 +1,19 @@ import time +from pimoroni_i2c import PimoroniI2C from breakout_trackball import BreakoutTrackball +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5, "baudrate": 100000} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21, "baudrate": 100000} + sensitivity = 2 -trackball = BreakoutTrackball() +i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) +trackball = BreakoutTrackball(i2c) trackball.set_rgbw(0, 0, 0, 64) +print("Roll the trackball to change colour!") + while True: state = trackball.read() if state[BreakoutTrackball.SW_PRESSED]: diff --git a/micropython/modules/breakout_as7262/breakout_as7262.cpp b/micropython/modules/breakout_as7262/breakout_as7262.cpp index 62dd291b..7942a0b1 100644 --- a/micropython/modules/breakout_as7262/breakout_as7262.cpp +++ b/micropython/modules/breakout_as7262/breakout_as7262.cpp @@ -11,6 +11,13 @@ using namespace pimoroni; extern "C" { #include "breakout_as7262.h" +#include "pimoroni_i2c.h" + +/***** I2C Struct *****/ +typedef struct _PimoroniI2C_obj_t { + mp_obj_base_t base; + I2C *i2c; +} _PimoroniI2C_obj_t; /***** Variables Struct *****/ typedef struct _breakout_as7262_BreakoutAS7262_obj_t { @@ -44,11 +51,9 @@ 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; - enum { ARG_i2c, ARG_sda, ARG_scl, ARG_int }; + enum { ARG_i2c, 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 = I2C_DEFAULT_SDA} }, - { MP_QSTR_scl, MP_ARG_INT, {.u_int = I2C_DEFAULT_SCL} }, + { MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} }, { MP_QSTR_interrupt, MP_ARG_INT, {.u_int = PIN_UNUSED} }, }; @@ -56,33 +61,21 @@ mp_obj_t BreakoutAS7262_make_new(const mp_obj_type_t *type, size_t n_args, size_ 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) & 0b1; // If no i2c specified, choose the one for the given SDA pin - } - 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(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutAS7262: Bad i2C object")); + return mp_const_none; } - 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")); - } + _PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj); self = m_new_obj(breakout_as7262_BreakoutAS7262_obj_t); self->base.type = &breakout_as7262_BreakoutAS7262_type; - self->breakout = new BreakoutAS7262(sda, scl, args[ARG_int].u_int); + self->breakout = new BreakoutAS7262(i2c->i2c, args[ARG_int].u_int); if(!self->breakout->init()) { - mp_raise_msg(&mp_type_RuntimeError, "AS7262 breakout not found when initialising"); + mp_raise_msg(&mp_type_RuntimeError, "BreakoutAS7262: 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 6a66a7f6..de9fca75 100644 --- a/micropython/modules/breakout_dotmatrix/breakout_dotmatrix.cpp +++ b/micropython/modules/breakout_dotmatrix/breakout_dotmatrix.cpp @@ -12,6 +12,13 @@ using namespace pimoroni; extern "C" { #include "breakout_dotmatrix.h" +#include "pimoroni_i2c.h" + +/***** I2C Struct *****/ +typedef struct _PimoroniI2C_obj_t { + mp_obj_base_t base; + I2C *i2c; +} _PimoroniI2C_obj_t; /***** Variables Struct *****/ typedef struct _breakout_dotmatrix_BreakoutDotMatrix_obj_t { @@ -47,42 +54,27 @@ 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; - enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl }; + enum { ARG_i2c, ARG_address }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} }, { MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutDotMatrix::DEFAULT_I2C_ADDRESS} }, - { MP_QSTR_sda, MP_ARG_INT, {.u_int = I2C_DEFAULT_SDA} }, - { MP_QSTR_scl, MP_ARG_INT, {.u_int = I2C_DEFAULT_SCL} }, }; // 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) & 0b1; // If no i2c specified, choose the one for the given SDA pin - } - 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(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutDotMatrix: Bad i2C object")); + return mp_const_none; } - 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")); - } + _PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj); 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, sda, scl); + self->breakout = new BreakoutDotMatrix(i2c->i2c, args[ARG_address].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "DotMatrix breakout not found when initialising"); diff --git a/micropython/modules/breakout_encoder/breakout_encoder.cpp b/micropython/modules/breakout_encoder/breakout_encoder.cpp index 2bd3b86d..314bf86b 100644 --- a/micropython/modules/breakout_encoder/breakout_encoder.cpp +++ b/micropython/modules/breakout_encoder/breakout_encoder.cpp @@ -69,7 +69,7 @@ mp_obj_t BreakoutEncoder_make_new(const mp_obj_type_t *type, size_t n_args, size mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); if(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { - mp_raise_ValueError(MP_ERROR_TEXT("bad i2C object")); + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutEncoder: Bad i2C object")); return mp_const_none; } @@ -81,7 +81,7 @@ mp_obj_t BreakoutEncoder_make_new(const mp_obj_type_t *type, size_t n_args, size self->breakout = new BreakoutEncoder(i2c->i2c, args[ARG_address].u_int, args[ARG_interrupt].u_int); if(!self->breakout->init()) { - mp_raise_msg(&mp_type_RuntimeError, "Encoder breakout not found when initialising"); + mp_raise_msg(&mp_type_RuntimeError, "BreakoutEncoder: breakout not found when initialising"); } return MP_OBJ_FROM_PTR(self); diff --git a/micropython/modules/breakout_ioexpander/breakout_ioexpander.cpp b/micropython/modules/breakout_ioexpander/breakout_ioexpander.cpp index 6ae34a67..d1d8d356 100644 --- a/micropython/modules/breakout_ioexpander/breakout_ioexpander.cpp +++ b/micropython/modules/breakout_ioexpander/breakout_ioexpander.cpp @@ -12,6 +12,13 @@ using namespace pimoroni; extern "C" { #include "breakout_ioexpander.h" +#include "pimoroni_i2c.h" + +/***** I2C Struct *****/ +typedef struct _PimoroniI2C_obj_t { + mp_obj_base_t base; + I2C *i2c; +} _PimoroniI2C_obj_t; /***** Variables Struct *****/ typedef struct _breakout_ioexpander_BreakoutIOExpander_obj_t { @@ -50,12 +57,10 @@ 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; - enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt }; + enum { ARG_i2c, ARG_address, ARG_interrupt }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} }, { MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutIOExpander::DEFAULT_I2C_ADDRESS} }, - { MP_QSTR_sda, MP_ARG_INT, {.u_int = I2C_DEFAULT_SDA} }, - { MP_QSTR_scl, MP_ARG_INT, {.u_int = I2C_DEFAULT_SCL} }, { MP_QSTR_interrupt, MP_ARG_INT, {.u_int = PIN_UNUSED} }, }; @@ -64,32 +69,20 @@ mp_obj_t BreakoutIOExpander_make_new(const mp_obj_type_t *type, size_t n_args, s 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) & 0b1; // If no i2c specified, choose the one for the given SDA pin - } - 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(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutIOExpander: Bad i2C object")); + return mp_const_none; } - 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")); - } + _PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj); 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, sda, scl, args[ARG_interrupt].u_int); + self->breakout = new BreakoutIOExpander(i2c->i2c, args[ARG_address].u_int, args[ARG_interrupt].u_int); if(!self->breakout->init()) { - mp_raise_msg(&mp_type_RuntimeError, "IOExpander breakout not found when initialising"); + mp_raise_msg(&mp_type_RuntimeError, "BreakoutIOExpander: breakout not found when initialising"); } return MP_OBJ_FROM_PTR(self); diff --git a/micropython/modules/breakout_ltr559/breakout_ltr559.cpp b/micropython/modules/breakout_ltr559/breakout_ltr559.cpp index 7d3661c4..735a5845 100644 --- a/micropython/modules/breakout_ltr559/breakout_ltr559.cpp +++ b/micropython/modules/breakout_ltr559/breakout_ltr559.cpp @@ -12,6 +12,13 @@ using namespace pimoroni; extern "C" { #include "breakout_ltr559.h" +#include "pimoroni_i2c.h" + +/***** I2C Struct *****/ +typedef struct _PimoroniI2C_obj_t { + mp_obj_base_t base; + I2C *i2c; +} _PimoroniI2C_obj_t; /***** Variables Struct *****/ typedef struct _breakout_ltr559_BreakoutLTR559_obj_t { @@ -50,45 +57,30 @@ 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; - enum { ARG_i2c, ARG_sda, ARG_scl, ARG_interrupt }; + enum { ARG_i2c, 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 = I2C_DEFAULT_SDA} }, - { MP_QSTR_scl, MP_ARG_INT, {.u_int = I2C_DEFAULT_SCL} }, - { MP_QSTR_interrupt, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} }, + { MP_QSTR_interrupt, MP_ARG_INT, {.u_int = 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) & 0b1; // If no i2c specified, choose the one for the given SDA pin - } - 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(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutLTR559: Bad i2C object")); + return mp_const_none; } - 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")); - } + _PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj); self = m_new_obj(breakout_ltr559_BreakoutLTR559_obj_t); self->base.type = &breakout_ltr559_BreakoutLTR559_type; - self->breakout = new BreakoutLTR559(sda, scl, args[ARG_interrupt].u_int); + self->breakout = new BreakoutLTR559(i2c->i2c, args[ARG_interrupt].u_int); if(!self->breakout->init()) { - mp_raise_msg(&mp_type_RuntimeError, "LTR559 breakout not found when initialising"); + mp_raise_msg(&mp_type_RuntimeError, "BreakoutLTR559: 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 5952fcdf..06d89c90 100644 --- a/micropython/modules/breakout_matrix11x7/breakout_matrix11x7.cpp +++ b/micropython/modules/breakout_matrix11x7/breakout_matrix11x7.cpp @@ -12,6 +12,13 @@ using namespace pimoroni; extern "C" { #include "breakout_matrix11x7.h" +#include "pimoroni_i2c.h" + +/***** I2C Struct *****/ +typedef struct _PimoroniI2C_obj_t { + mp_obj_base_t base; + I2C *i2c; +} _PimoroniI2C_obj_t; /***** Variables Struct *****/ typedef struct _breakout_matrix11x7_BreakoutMatrix11x7_obj_t { @@ -47,12 +54,10 @@ 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; - enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl }; + enum { ARG_i2c, ARG_address }; 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 = I2C_DEFAULT_SDA} }, - { MP_QSTR_scl, MP_ARG_INT, {.u_int = I2C_DEFAULT_SCL} }, + { MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} }, + { MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutMatrix11x7::DEFAULT_I2C_ADDRESS} } }; // Parse args. @@ -60,33 +65,20 @@ mp_obj_t BreakoutMatrix11x7_make_new(const mp_obj_type_t *type, size_t n_args, s 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) & 0b1; // If no i2c specified, choose the one for the given SDA pin - } - 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(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutMatrix11x7: Bad i2C object")); + return mp_const_none; } - 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")); - } + _PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj); 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); + self->breakout = new BreakoutMatrix11x7(i2c->i2c, args[ARG_address].u_int); if(!self->breakout->init()) { - mp_raise_msg(&mp_type_RuntimeError, "Matrix11x7 breakout not found when initialising"); + mp_raise_msg(&mp_type_RuntimeError, "BreakoutMatrix11x7: 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 d8b47141..3994faac 100644 --- a/micropython/modules/breakout_mics6814/breakout_mics6814.cpp +++ b/micropython/modules/breakout_mics6814/breakout_mics6814.cpp @@ -12,6 +12,13 @@ using namespace pimoroni; extern "C" { #include "breakout_mics6814.h" +#include "pimoroni_i2c.h" + +/***** I2C Struct *****/ +typedef struct _PimoroniI2C_obj_t { + mp_obj_base_t base; + I2C *i2c; +} _PimoroniI2C_obj_t; /***** Variables Struct *****/ typedef struct _breakout_mics6814_BreakoutMICS6814_obj_t { @@ -50,12 +57,10 @@ 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; - enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt }; + enum { ARG_i2c, ARG_address, ARG_interrupt }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} }, { MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutMICS6814::DEFAULT_I2C_ADDRESS} }, - { MP_QSTR_sda, MP_ARG_INT, {.u_int = I2C_DEFAULT_SDA} }, - { MP_QSTR_scl, MP_ARG_INT, {.u_int = I2C_DEFAULT_SCL} }, { MP_QSTR_interrupt, MP_ARG_INT, {.u_int = PIN_UNUSED} }, }; @@ -64,32 +69,20 @@ mp_obj_t BreakoutMICS6814_make_new(const mp_obj_type_t *type, size_t n_args, siz 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) & 0b1; // If no i2c specified, choose the one for the given SDA pin - } - 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(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutMICS6814: Bad i2C object")); + return mp_const_none; } - 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")); - } + _PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj); self = m_new_obj(breakout_mics6814_BreakoutMICS6814_obj_t); self->base.type = &breakout_mics6814_BreakoutMICS6814_type; - self->breakout = new BreakoutMICS6814(args[ARG_address].u_int, sda, scl, args[ARG_interrupt].u_int); + self->breakout = new BreakoutMICS6814(i2c->i2c, args[ARG_address].u_int, args[ARG_interrupt].u_int); if(!self->breakout->init()) { - mp_raise_msg(&mp_type_RuntimeError, "MICS6814 breakout not found when initialising"); + mp_raise_msg(&mp_type_RuntimeError, "BreakoutMICS6814: breakout not found when initialising"); } return MP_OBJ_FROM_PTR(self); diff --git a/micropython/modules/breakout_msa301/breakout_msa301.cpp b/micropython/modules/breakout_msa301/breakout_msa301.cpp index bd1cea90..bb21fed3 100644 --- a/micropython/modules/breakout_msa301/breakout_msa301.cpp +++ b/micropython/modules/breakout_msa301/breakout_msa301.cpp @@ -11,6 +11,13 @@ using namespace pimoroni; extern "C" { #include "breakout_msa301.h" +#include "pimoroni_i2c.h" + +/***** I2C Struct *****/ +typedef struct _PimoroniI2C_obj_t { + mp_obj_base_t base; + I2C *i2c; +} _PimoroniI2C_obj_t; /***** Variables Struct *****/ typedef struct _breakout_msa301_BreakoutMSA301_obj_t { @@ -44,11 +51,9 @@ 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; - enum { ARG_i2c, ARG_sda, ARG_scl, ARG_interrupt }; + enum { ARG_i2c, 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 = I2C_DEFAULT_SDA} }, - { MP_QSTR_scl, MP_ARG_INT, {.u_int = I2C_DEFAULT_SCL} }, + { MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} }, { MP_QSTR_interrupt, MP_ARG_INT, {.u_int = PIN_UNUSED} }, }; @@ -56,34 +61,20 @@ mp_obj_t BreakoutMSA301_make_new(const mp_obj_type_t *type, size_t n_args, size_ 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) & 0b1; // If no i2c specified, choose the one for the given SDA pin - } - 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(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutMSA301: Bad i2C object")); + return mp_const_none; } - 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")); - } + _PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj); 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); + + self->breakout = new BreakoutMSA301(i2c->i2c, args[ARG_interrupt].u_int); if(!self->breakout->init()) { - mp_raise_msg(&mp_type_RuntimeError, "MSA301 breakout not found when initialising"); + mp_raise_msg(&mp_type_RuntimeError, "BreakoutMSA301: 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 05c2820e..90fc24d3 100644 --- a/micropython/modules/breakout_potentiometer/breakout_potentiometer.cpp +++ b/micropython/modules/breakout_potentiometer/breakout_potentiometer.cpp @@ -69,7 +69,7 @@ mp_obj_t BreakoutPotentiometer_make_new(const mp_obj_type_t *type, size_t n_args mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); if(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { - mp_raise_ValueError(MP_ERROR_TEXT("bad i2C object")); + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutPotentiometer: Bad i2C object")); return mp_const_none; } @@ -81,7 +81,7 @@ mp_obj_t BreakoutPotentiometer_make_new(const mp_obj_type_t *type, size_t n_args self->breakout = new BreakoutPotentiometer(i2c->i2c, args[ARG_interrupt].u_int); if(!self->breakout->init()) { - mp_raise_msg(&mp_type_RuntimeError, "Potentiometer breakout not found when initialising"); + mp_raise_msg(&mp_type_RuntimeError, "BreakoutPotentiometer: breakout not found when initialising"); } return MP_OBJ_FROM_PTR(self); diff --git a/micropython/modules/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp b/micropython/modules/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp index 3e4859a9..c1c80676 100644 --- a/micropython/modules/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp +++ b/micropython/modules/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp @@ -12,6 +12,13 @@ using namespace pimoroni; extern "C" { #include "breakout_rgbmatrix5x5.h" +#include "pimoroni_i2c.h" + +/***** I2C Struct *****/ +typedef struct _PimoroniI2C_obj_t { + mp_obj_base_t base; + I2C *i2c; +} _PimoroniI2C_obj_t; /***** Variables Struct *****/ typedef struct _breakout_rgbmatrix5x5_BreakoutRGBMatrix5x5_obj_t { @@ -47,12 +54,10 @@ 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; - enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl }; + enum { ARG_i2c, ARG_address }; 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 = I2C_DEFAULT_SDA} }, - { MP_QSTR_scl, MP_ARG_INT, {.u_int = I2C_DEFAULT_SCL} }, + { MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} }, + { MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutRGBMatrix5x5::DEFAULT_I2C_ADDRESS} } }; // Parse args. @@ -60,33 +65,20 @@ mp_obj_t BreakoutRGBMatrix5x5_make_new(const mp_obj_type_t *type, size_t n_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) & 0b1; // If no i2c specified, choose the one for the given SDA pin - } - 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(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutRGBMatrix5x5: Bad i2C object")); + return mp_const_none; } - 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")); - } + _PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj); 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); + + self->breakout = new BreakoutRGBMatrix5x5(i2c->i2c, args[ARG_address].u_int); if(!self->breakout->init()) { - mp_raise_msg(&mp_type_RuntimeError, "RGBMatrix5x5 breakout not found when initialising"); + mp_raise_msg(&mp_type_RuntimeError, "BreakoutRGBMatrix5x5: 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 7ab6793d..22afbf42 100644 --- a/micropython/modules/breakout_rtc/breakout_rtc.cpp +++ b/micropython/modules/breakout_rtc/breakout_rtc.cpp @@ -14,6 +14,13 @@ using namespace pimoroni; extern "C" { #include "breakout_rtc.h" +#include "pimoroni_i2c.h" + +/***** I2C Struct *****/ +typedef struct _PimoroniI2C_obj_t { + mp_obj_base_t base; + I2C *i2c; +} _PimoroniI2C_obj_t; /***** Variables Struct *****/ typedef struct _breakout_rtc_BreakoutRTC_obj_t { @@ -47,11 +54,9 @@ 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; - enum { ARG_i2c, ARG_sda, ARG_scl, ARG_interrupt }; + enum { ARG_i2c, 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 = I2C_DEFAULT_SDA} }, - { MP_QSTR_scl, MP_ARG_INT, {.u_int = I2C_DEFAULT_SCL} }, + { MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} }, { MP_QSTR_interrupt, MP_ARG_INT, {.u_int = PIN_UNUSED} }, }; @@ -60,33 +65,20 @@ mp_obj_t BreakoutRTC_make_new(const mp_obj_type_t *type, size_t n_args, size_t n 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) & 0b1; // If no i2c specified, choose the one for the given SDA pin - } - 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(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutRTC: Bad i2C object")); + return mp_const_none; } - 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")); - } + _PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj); 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); + self->breakout = new BreakoutRTC(i2c->i2c, args[ARG_interrupt].u_int); if(!self->breakout->init()) { - mp_raise_msg(&mp_type_RuntimeError, "RTC breakout not found when initialising"); + mp_raise_msg(&mp_type_RuntimeError, "BreakoutRTC: 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 10c6a42d..41ff3029 100644 --- a/micropython/modules/breakout_sgp30/breakout_sgp30.cpp +++ b/micropython/modules/breakout_sgp30/breakout_sgp30.cpp @@ -11,6 +11,13 @@ using namespace pimoroni; extern "C" { #include "breakout_sgp30.h" +#include "pimoroni_i2c.h" + +/***** I2C Struct *****/ +typedef struct _PimoroniI2C_obj_t { + mp_obj_base_t base; + I2C *i2c; +} _PimoroniI2C_obj_t; /***** Variables Struct *****/ typedef struct _breakout_sgp30_BreakoutSGP30_obj_t { @@ -41,11 +48,9 @@ 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; - enum { ARG_i2c, ARG_sda, ARG_scl }; + enum { ARG_i2c }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} }, - { MP_QSTR_sda, MP_ARG_INT, {.u_int = I2C_DEFAULT_SDA} }, - { MP_QSTR_scl, MP_ARG_INT, {.u_int = I2C_DEFAULT_SCL} }, + { MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} } }; // Parse args. @@ -53,33 +58,20 @@ mp_obj_t BreakoutSGP30_make_new(const mp_obj_type_t *type, size_t n_args, size_t 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) & 0b1; // If no i2c specified, choose the one for the given SDA pin - } - 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(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutSGP30: Bad i2C object")); + return mp_const_none; } - 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")); - } + _PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj); 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); + + self->breakout = new BreakoutSGP30(i2c->i2c); if(!self->breakout->init()) { - mp_raise_msg(&mp_type_RuntimeError, "SGP30 breakout not found when initialising"); + mp_raise_msg(&mp_type_RuntimeError, "BreakoutSGP30: 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 660c9630..eb62513e 100644 --- a/micropython/modules/breakout_trackball/breakout_trackball.cpp +++ b/micropython/modules/breakout_trackball/breakout_trackball.cpp @@ -12,6 +12,13 @@ using namespace pimoroni; extern "C" { #include "breakout_trackball.h" +#include "pimoroni_i2c.h" + +/***** I2C Struct *****/ +typedef struct _PimoroniI2C_obj_t { + mp_obj_base_t base; + I2C *i2c; +} _PimoroniI2C_obj_t; /***** Variables Struct *****/ typedef struct _breakout_trackball_BreakoutTrackball_obj_t { @@ -50,12 +57,10 @@ 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; - enum { ARG_i2c, ARG_address, ARG_sda, ARG_scl, ARG_interrupt }; + enum { ARG_i2c, ARG_address, ARG_interrupt }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_i2c, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} }, { MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutTrackball::DEFAULT_I2C_ADDRESS} }, - { MP_QSTR_sda, MP_ARG_INT, {.u_int = I2C_DEFAULT_SDA} }, - { MP_QSTR_scl, MP_ARG_INT, {.u_int = I2C_DEFAULT_SCL} }, { MP_QSTR_interrupt, MP_ARG_INT, {.u_int = PIN_UNUSED} }, }; @@ -64,30 +69,17 @@ mp_obj_t BreakoutTrackball_make_new(const mp_obj_type_t *type, size_t n_args, si 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) & 0b1; // If no i2c specified, choose the one for the given SDA pin - } - 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(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutSGP30: Bad i2C object")); + return mp_const_none; } - 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")); - } + _PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj); 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); + self->breakout = new BreakoutTrackball(i2c->i2c, args[ARG_address].u_int, args[ARG_interrupt].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "Trackball breakout not found when initialising"); diff --git a/micropython/modules/pimoroni_i2c/pimoroni_i2c.cpp b/micropython/modules/pimoroni_i2c/pimoroni_i2c.cpp index 1d0c2dee..f8924fd2 100644 --- a/micropython/modules/pimoroni_i2c/pimoroni_i2c.cpp +++ b/micropython/modules/pimoroni_i2c/pimoroni_i2c.cpp @@ -50,10 +50,11 @@ mp_obj_t PimoroniI2C___del__(mp_obj_t self_in) { mp_obj_t PimoroniI2C_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { _PimoroniI2C_obj_t *self = nullptr; - enum { ARG_sda, ARG_scl }; + enum { ARG_sda, ARG_scl, ARG_baudrate }; static const mp_arg_t allowed_args[] = { { MP_QSTR_sda, MP_ARG_INT, {.u_int = I2C_DEFAULT_SDA} }, { MP_QSTR_scl, MP_ARG_INT, {.u_int = I2C_DEFAULT_SCL} }, + { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = I2C_DEFAULT_BAUDRATE} }, }; // Parse args. @@ -63,6 +64,7 @@ mp_obj_t PimoroniI2C_make_new(const mp_obj_type_t *type, size_t n_args, size_t n // Get I2C bus. int sda = args[ARG_sda].u_int; int scl = args[ARG_scl].u_int; + int baud = args[ARG_baudrate].u_int; int i2c_id = (sda >> 1) & 0b1; // i2c bus for given SDA pin if(!IS_VALID_SDA(i2c_id, sda)) { @@ -76,7 +78,7 @@ mp_obj_t PimoroniI2C_make_new(const mp_obj_type_t *type, size_t n_args, size_t n self = m_new_obj_with_finaliser(_PimoroniI2C_obj_t); self->base.type = &PimoroniI2C_type; - self->i2c = new I2C(sda, scl); + self->i2c = new I2C(sda, scl, baud); return MP_OBJ_FROM_PTR(self); }