Skip to content

Commit

Permalink
Merge pull request #153 from pimoroni/patch-keyword-initialisers
Browse files Browse the repository at this point in the history
Fix for keywords not working in MP class constructors, and added address printout for supported breakouts
  • Loading branch information
Gadgetoid authored May 14, 2021
2 parents 13d4aaf + 1ee5101 commit 66a5ab1
Show file tree
Hide file tree
Showing 38 changed files with 633 additions and 702 deletions.
4 changes: 4 additions & 0 deletions drivers/ioexpander/ioexpander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ namespace pimoroni {
return i2c;
}

int IOExpander::get_address() const {
return address;
}

int IOExpander::get_sda() const {
return sda;
}
Expand Down
1 change: 1 addition & 0 deletions drivers/ioexpander/ioexpander.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ namespace pimoroni {

// For print access in micropython
i2c_inst_t* get_i2c() const;
int get_address() const;
int get_sda() const;
int get_scl() const;
int get_int() const;
Expand Down
4 changes: 4 additions & 0 deletions drivers/is31fl3731/is31fl3731.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ namespace pimoroni {
return i2c;
}

int IS31FL3731::get_address() const {
return address;
}

int IS31FL3731::get_sda() const {
return sda;
}
Expand Down
1 change: 1 addition & 0 deletions drivers/is31fl3731/is31fl3731.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace pimoroni {
bool init();

i2c_inst_t* get_i2c() const;
int get_address() const;
int get_sda() const;
int get_scl() const;

Expand Down
4 changes: 4 additions & 0 deletions drivers/ltp305/ltp305.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ namespace pimoroni {
return i2c;
}

int LTP305::get_address() const {
return address;
}

int LTP305::get_sda() const {
return sda;
}
Expand Down
1 change: 1 addition & 0 deletions drivers/ltp305/ltp305.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ namespace pimoroni {
bool init();

i2c_inst_t* get_i2c() const;
int get_address() const;
int get_sda() const;
int get_scl() const;

Expand Down
4 changes: 4 additions & 0 deletions drivers/ltr559/ltr559.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ namespace pimoroni {
return i2c;
}

int LTR559::get_address() const {
return address;
}

int LTR559::get_sda() const {
return sda;
}
Expand Down
1 change: 1 addition & 0 deletions drivers/ltr559/ltr559.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ namespace pimoroni {
void reset();

i2c_inst_t* get_i2c() const;
int get_address() const;
int get_sda() const;
int get_scl() const;
int get_int() const;
Expand Down
4 changes: 3 additions & 1 deletion drivers/msa301/msa301.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion drivers/msa301/msa301.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace pimoroni {
// Methods
//--------------------------------------------------
public:
void init();
bool init();
void reset();

i2c_inst_t* get_i2c() const;
Expand Down
4 changes: 4 additions & 0 deletions drivers/trackball/trackball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ namespace pimoroni {
return i2c;
}

int Trackball::get_address() const {
return address;
}

int Trackball::get_sda() const {
return sda;
}
Expand Down
1 change: 1 addition & 0 deletions drivers/trackball/trackball.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace pimoroni {
bool init();

i2c_inst_t* get_i2c() const;
int get_address() const;
int get_sda() const;
int get_scl() const;
int get_int() const;
Expand Down
4 changes: 4 additions & 0 deletions libraries/breakout_encoder/breakout_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ namespace pimoroni {
return ioe.get_i2c();
}

int BreakoutEncoder::get_address() const {
return ioe.get_address();
}

int BreakoutEncoder::get_sda() const {
return ioe.get_sda();
}
Expand Down
1 change: 1 addition & 0 deletions libraries/breakout_encoder/breakout_encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace pimoroni {

// For print access in micropython
i2c_inst_t* get_i2c() const;
int get_address() const;
int get_sda() const;
int get_scl() const;
int get_int() const;
Expand Down
29 changes: 16 additions & 13 deletions libraries/breakout_matrix11x7/breakout_matrix11x7.cpp
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion libraries/breakout_matrix11x7/breakout_matrix11x7.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {};
Expand Down
4 changes: 4 additions & 0 deletions libraries/breakout_mics6814/breakout_mics6814.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ namespace pimoroni {
return ioe.get_i2c();
}

int BreakoutMICS6814::get_address() const {
return ioe.get_address();
}

int BreakoutMICS6814::get_sda() const {
return ioe.get_sda();
}
Expand Down
1 change: 1 addition & 0 deletions libraries/breakout_mics6814/breakout_mics6814.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace pimoroni {

// For print access in micropython
i2c_inst_t* get_i2c() const;
int get_address() const;
int get_sda() const;
int get_scl() const;
int get_int() const;
Expand Down
4 changes: 4 additions & 0 deletions libraries/breakout_potentiometer/breakout_potentiometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ namespace pimoroni {
return ioe.get_i2c();
}

int BreakoutPotentiometer::get_address() const {
return ioe.get_address();
}

int BreakoutPotentiometer::get_sda() const {
return ioe.get_sda();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ namespace pimoroni {

// For print access in micropython
i2c_inst_t* get_i2c() const;
int get_address() const;
int get_sda() const;
int get_scl() const;
int get_int() const;
Expand Down
29 changes: 16 additions & 13 deletions libraries/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion libraries/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {};
Expand Down
80 changes: 39 additions & 41 deletions micropython/modules/breakout_as7262/breakout_as7262.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 == 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) & 0b1; // If no i2c specified, choose the one for the given SDA pin
}
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);
}

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->breakout->init();
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);
}
Expand Down Expand Up @@ -257,7 +255,7 @@ mp_obj_t BreakoutAS7262_set_leds(size_t n_args, const mp_obj_t *pos_args, mp_map

mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

breakout_as7262_BreakoutAS7262_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_as7262_BreakoutAS7262_obj_t);
self->breakout->set_leds(args[ARG_illumination].u_bool, args[ARG_indicator].u_bool);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void BreakoutColourLCD160x80_print(const mp_print_t *print, mp_obj_t self_in, mp
mp_obj_t BreakoutColourLCD160x80_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_colourlcd160x80_BreakoutColourLCD160x80_obj_t *self = nullptr;

if(n_args <= 1) {
if(n_args + n_kw <= 1) {
enum { ARG_buffer };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
Expand All @@ -69,7 +69,7 @@ mp_obj_t BreakoutColourLCD160x80_make_new(const mp_obj_type_t *type, size_t n_ar

self->breakout = new BreakoutColourLCD160x80((uint16_t *)bufinfo.buf);
}
else if(n_args == 2) {
else if(n_args + n_kw == 2) {
enum { ARG_buffer, ARG_slot };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
Expand Down
Loading

0 comments on commit 66a5ab1

Please sign in to comment.