2021-05-13 12:06:01 +01:00
|
|
|
#include "libraries/breakout_trackball/breakout_trackball.hpp"
|
2022-05-20 15:45:25 +01:00
|
|
|
#include "micropython/modules/util.hpp"
|
2021-05-14 15:14:47 +01:00
|
|
|
#include <cstdio>
|
2021-05-04 22:29:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
using namespace pimoroni;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "breakout_trackball.h"
|
2021-05-18 11:18:41 +01:00
|
|
|
#include "pimoroni_i2c.h"
|
|
|
|
|
2021-05-04 22:29:53 +01:00
|
|
|
/***** Variables Struct *****/
|
|
|
|
typedef struct _breakout_trackball_BreakoutTrackball_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
BreakoutTrackball *breakout;
|
2022-05-17 18:21:09 +01:00
|
|
|
_PimoroniI2C_obj_t *i2c;
|
2021-05-04 22:29:53 +01:00
|
|
|
} breakout_trackball_BreakoutTrackball_obj_t;
|
|
|
|
|
|
|
|
|
|
|
|
/***** Constructor *****/
|
|
|
|
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;
|
|
|
|
|
2021-05-18 11:18:41 +01:00
|
|
|
enum { ARG_i2c, ARG_address, ARG_interrupt };
|
2021-05-14 17:14:07 +01:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2021-05-18 11:18:41 +01:00
|
|
|
{ MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} },
|
2021-05-14 17:14:07 +01:00
|
|
|
{ MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutTrackball::DEFAULT_I2C_ADDRESS} },
|
2021-05-17 11:08:23 +01:00
|
|
|
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = PIN_UNUSED} },
|
2021-05-14 17:14:07 +01:00
|
|
|
};
|
2021-05-04 22:29:53 +01:00
|
|
|
|
2021-05-14 17:14:07 +01:00
|
|
|
// 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);
|
2021-05-04 22:29:53 +01:00
|
|
|
|
2021-05-14 17:14:07 +01:00
|
|
|
self = m_new_obj(breakout_trackball_BreakoutTrackball_obj_t);
|
|
|
|
self->base.type = &breakout_trackball_BreakoutTrackball_type;
|
|
|
|
|
2022-05-17 18:21:09 +01:00
|
|
|
self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj);
|
|
|
|
|
2022-05-20 15:45:25 +01:00
|
|
|
self->breakout = m_new_class(BreakoutTrackball, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_interrupt].u_int);
|
2021-05-14 17:14:07 +01:00
|
|
|
|
|
|
|
if(!self->breakout->init()) {
|
|
|
|
mp_raise_msg(&mp_type_RuntimeError, "Trackball breakout not found when initialising");
|
|
|
|
}
|
2021-05-04 22:29:53 +01:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***** Methods *****/
|
|
|
|
mp_obj_t BreakoutTrackball_change_address(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_self, ARG_address };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2021-05-14 14:30:08 +01:00
|
|
|
|
2021-05-04 22:29:53 +01:00
|
|
|
breakout_trackball_BreakoutTrackball_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_trackball_BreakoutTrackball_obj_t);
|
|
|
|
|
|
|
|
self->breakout->change_address(args[ARG_address].u_int);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutTrackball_enable_interrupt(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_self, ARG_interrupt };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_interrupt, MP_ARG_BOOL, {.u_bool = false} },
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2021-05-14 14:30:08 +01:00
|
|
|
|
2021-05-04 22:29:53 +01:00
|
|
|
breakout_trackball_BreakoutTrackball_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_trackball_BreakoutTrackball_obj_t);
|
|
|
|
|
|
|
|
self->breakout->enable_interrupt(args[ARG_interrupt].u_bool);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutTrackball_get_interrupt(mp_obj_t self_in) {
|
|
|
|
breakout_trackball_BreakoutTrackball_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_trackball_BreakoutTrackball_obj_t);
|
|
|
|
return mp_obj_new_bool(self->breakout->get_interrupt());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mp_obj_t BreakoutTrackball_set_rgbw(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_self, ARG_r, ARG_g, ARG_b, ARG_w };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_r, MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
{ MP_QSTR_g, MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
{ MP_QSTR_b, MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
{ MP_QSTR_w, MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
};
|
|
|
|
|
|
|
|
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_trackball_BreakoutTrackball_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_trackball_BreakoutTrackball_obj_t);
|
|
|
|
|
|
|
|
int r = args[ARG_r].u_int;
|
|
|
|
int g = args[ARG_g].u_int;
|
|
|
|
int b = args[ARG_b].u_int;
|
|
|
|
int w = args[ARG_w].u_int;
|
|
|
|
|
|
|
|
if(r < 0 || r > 255)
|
|
|
|
mp_raise_ValueError("r out of range. Expected 0 to 255");
|
|
|
|
else if(g < 0 || g > 255)
|
|
|
|
mp_raise_ValueError("g out of range. Expected 0 to 255");
|
|
|
|
else if(b < 0 || b > 255)
|
|
|
|
mp_raise_ValueError("b out of range. Expected 0 to 255");
|
|
|
|
else if(w < 0 || w > 255)
|
|
|
|
mp_raise_ValueError("w out of range. Expected 0 to 255");
|
|
|
|
else
|
|
|
|
self->breakout->set_rgbw(r, g, b, w);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutTrackball_set_red(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_self, ARG_value };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_value, MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2021-05-14 14:30:08 +01:00
|
|
|
|
2021-05-04 22:29:53 +01:00
|
|
|
breakout_trackball_BreakoutTrackball_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_trackball_BreakoutTrackball_obj_t);
|
|
|
|
|
|
|
|
int value = args[ARG_value].u_int;
|
|
|
|
|
|
|
|
if(value < 0 || value > 255)
|
|
|
|
mp_raise_ValueError("value out of range. Expected 0 to 255");
|
|
|
|
else
|
|
|
|
self->breakout->set_red(value);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutTrackball_set_green(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_self, ARG_value };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_value, MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2021-05-14 14:30:08 +01:00
|
|
|
|
2021-05-04 22:29:53 +01:00
|
|
|
breakout_trackball_BreakoutTrackball_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_trackball_BreakoutTrackball_obj_t);
|
|
|
|
|
|
|
|
int value = args[ARG_value].u_int;
|
|
|
|
|
|
|
|
if(value < 0 || value > 255)
|
|
|
|
mp_raise_ValueError("value out of range. Expected 0 to 255");
|
|
|
|
else
|
|
|
|
self->breakout->set_green(value);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutTrackball_set_blue(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_self, ARG_value };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_value, MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2021-05-14 14:30:08 +01:00
|
|
|
|
2021-05-04 22:29:53 +01:00
|
|
|
breakout_trackball_BreakoutTrackball_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_trackball_BreakoutTrackball_obj_t);
|
|
|
|
|
|
|
|
int value = args[ARG_value].u_int;
|
|
|
|
|
|
|
|
if(value < 0 || value > 255)
|
|
|
|
mp_raise_ValueError("value out of range. Expected 0 to 255");
|
|
|
|
else
|
|
|
|
self->breakout->set_blue(value);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutTrackball_set_white(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_self, ARG_value };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_value, MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2021-05-14 14:30:08 +01:00
|
|
|
|
2021-05-04 22:29:53 +01:00
|
|
|
breakout_trackball_BreakoutTrackball_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_trackball_BreakoutTrackball_obj_t);
|
|
|
|
|
|
|
|
int value = args[ARG_value].u_int;
|
|
|
|
|
|
|
|
if(value < 0 || value > 255)
|
|
|
|
mp_raise_ValueError("value out of range. Expected 0 to 255");
|
|
|
|
else
|
|
|
|
self->breakout->set_white(value);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutTrackball_read(mp_obj_t self_in) {
|
|
|
|
breakout_trackball_BreakoutTrackball_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_trackball_BreakoutTrackball_obj_t);
|
|
|
|
Trackball::State state = self->breakout->read();
|
|
|
|
|
|
|
|
mp_obj_t tuple[6];
|
|
|
|
tuple[LEFT] = mp_obj_new_int(state.left);
|
|
|
|
tuple[RIGHT] = mp_obj_new_int(state.right);
|
|
|
|
tuple[UP] = mp_obj_new_int(state.up);
|
|
|
|
tuple[DOWN] = mp_obj_new_int(state.down);
|
|
|
|
tuple[SW_CHANGED] = mp_obj_new_int(state.sw_changed);
|
|
|
|
tuple[SW_PRESSED] = mp_obj_new_int(state.sw_pressed);
|
2021-05-14 14:30:08 +01:00
|
|
|
|
2021-05-04 22:29:53 +01:00
|
|
|
return mp_obj_new_tuple(6, tuple);
|
|
|
|
}
|
|
|
|
}
|