2021-05-14 11:49:48 +00:00
|
|
|
#include "libraries/breakout_encoder/breakout_encoder.hpp"
|
2021-05-14 14:14:47 +00:00
|
|
|
#include <cstdio>
|
2021-05-11 19:02:18 +00:00
|
|
|
|
|
|
|
#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o))
|
|
|
|
|
|
|
|
// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins.
|
|
|
|
#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c))
|
|
|
|
#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c))
|
|
|
|
|
|
|
|
|
|
|
|
using namespace pimoroni;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "breakout_encoder.h"
|
2021-05-17 17:09:39 +00:00
|
|
|
#include "pimoroni_i2c.h"
|
|
|
|
|
|
|
|
/***** I2C Struct *****/
|
|
|
|
typedef struct _PimoroniI2C_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
I2C *i2c;
|
|
|
|
} _PimoroniI2C_obj_t;
|
2021-05-11 19:02:18 +00:00
|
|
|
|
|
|
|
/***** Variables Struct *****/
|
|
|
|
typedef struct _breakout_encoder_BreakoutEncoder_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
BreakoutEncoder *breakout;
|
|
|
|
} breakout_encoder_BreakoutEncoder_obj_t;
|
|
|
|
|
|
|
|
/***** Print *****/
|
|
|
|
void BreakoutEncoder_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
(void)kind; //Unused input parameter
|
|
|
|
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_encoder_BreakoutEncoder_obj_t);
|
|
|
|
BreakoutEncoder* breakout = self->breakout;
|
|
|
|
mp_print_str(print, "BreakoutEncoder(");
|
|
|
|
|
|
|
|
mp_print_str(print, "i2c = ");
|
|
|
|
mp_obj_print_helper(print, mp_obj_new_int((breakout->get_i2c() == i2c0) ? 0 : 1), PRINT_REPR);
|
|
|
|
|
2021-05-14 14:14:47 +00:00
|
|
|
mp_print_str(print, ", address = 0x");
|
|
|
|
char buf[3];
|
|
|
|
sprintf(buf, "%02X", breakout->get_address());
|
|
|
|
mp_print_str(print, buf);
|
|
|
|
|
2021-05-11 19:02:18 +00:00
|
|
|
mp_print_str(print, ", sda = ");
|
|
|
|
mp_obj_print_helper(print, mp_obj_new_int(breakout->get_sda()), PRINT_REPR);
|
|
|
|
|
|
|
|
mp_print_str(print, ", scl = ");
|
|
|
|
mp_obj_print_helper(print, mp_obj_new_int(breakout->get_scl()), PRINT_REPR);
|
|
|
|
|
|
|
|
mp_print_str(print, ", int = ");
|
|
|
|
mp_obj_print_helper(print, mp_obj_new_int(breakout->get_int()), PRINT_REPR);
|
|
|
|
|
|
|
|
mp_print_str(print, ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
/***** Constructor *****/
|
|
|
|
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;
|
|
|
|
|
2021-05-17 17:09:39 +00:00
|
|
|
enum { ARG_i2c, ARG_address, ARG_interrupt };
|
2021-05-14 16:14:07 +00:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2021-05-17 17:09:39 +00:00
|
|
|
{ MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} },
|
2021-05-14 16:14:07 +00:00
|
|
|
{ MP_QSTR_address, MP_ARG_INT, {.u_int = BreakoutEncoder::DEFAULT_I2C_ADDRESS} },
|
2021-05-17 10:08:23 +00:00
|
|
|
{ MP_QSTR_interrupt, MP_ARG_INT, {.u_int = PIN_UNUSED} },
|
2021-05-14 16:14:07 +00: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-11 19:02:18 +00:00
|
|
|
|
2021-05-17 17:09:39 +00:00
|
|
|
if(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) {
|
2021-05-18 10:18:41 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("BreakoutEncoder: Bad i2C object"));
|
2021-05-17 17:09:39 +00:00
|
|
|
return mp_const_none;
|
2021-05-14 16:14:07 +00:00
|
|
|
}
|
2021-05-11 19:14:13 +00:00
|
|
|
|
2021-05-17 17:09:39 +00:00
|
|
|
_PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj);
|
2021-05-11 19:02:18 +00:00
|
|
|
|
2021-05-14 16:14:07 +00:00
|
|
|
self = m_new_obj(breakout_encoder_BreakoutEncoder_obj_t);
|
|
|
|
self->base.type = &breakout_encoder_BreakoutEncoder_type;
|
|
|
|
|
2021-05-17 17:09:39 +00:00
|
|
|
self->breakout = new BreakoutEncoder(i2c->i2c, args[ARG_address].u_int, args[ARG_interrupt].u_int);
|
2021-05-14 16:14:07 +00:00
|
|
|
|
2021-05-11 20:38:44 +00:00
|
|
|
if(!self->breakout->init()) {
|
2021-05-18 10:18:41 +00:00
|
|
|
mp_raise_msg(&mp_type_RuntimeError, "BreakoutEncoder: breakout not found when initialising");
|
2021-05-11 20:38:44 +00:00
|
|
|
}
|
2021-05-11 19:02:18 +00:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***** Methods *****/
|
2021-05-11 20:58:44 +00:00
|
|
|
mp_obj_t BreakoutEncoder_set_address(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2021-05-11 19:02:18 +00:00
|
|
|
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-11 19:14:13 +00:00
|
|
|
|
2021-05-11 19:02:18 +00:00
|
|
|
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_encoder_BreakoutEncoder_obj_t);
|
|
|
|
|
2021-05-11 20:58:44 +00:00
|
|
|
self->breakout->set_address(args[ARG_address].u_int);
|
2021-05-11 19:02:18 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2021-05-12 19:03:50 +00:00
|
|
|
mp_obj_t BreakoutEncoder_get_interrupt_flag(mp_obj_t self_in) {
|
|
|
|
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_encoder_BreakoutEncoder_obj_t);
|
|
|
|
return mp_obj_new_bool(self->breakout->get_interrupt_flag());
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutEncoder_clear_interrupt_flag(mp_obj_t self_in) {
|
|
|
|
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_encoder_BreakoutEncoder_obj_t);
|
|
|
|
self->breakout->clear_interrupt_flag();
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2021-05-11 19:02:18 +00:00
|
|
|
mp_obj_t BreakoutEncoder_get_direction(mp_obj_t self_in) {
|
|
|
|
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_encoder_BreakoutEncoder_obj_t);
|
|
|
|
return mp_obj_new_bool(self->breakout->get_direction());
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutEncoder_set_direction(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_self, ARG_clockwise };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_clockwise, MP_ARG_REQUIRED | MP_ARG_BOOL },
|
|
|
|
};
|
|
|
|
|
|
|
|
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-11 19:14:13 +00:00
|
|
|
|
2021-05-11 19:02:18 +00:00
|
|
|
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_encoder_BreakoutEncoder_obj_t);
|
|
|
|
|
2021-05-12 17:27:03 +00:00
|
|
|
self->breakout->set_direction(args[ARG_clockwise].u_bool ? BreakoutEncoder::DIRECTION_CW : BreakoutEncoder::DIRECTION_CCW);
|
2021-05-11 19:02:18 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutEncoder_set_brightness(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_self, ARG_brightness };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_brightness, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
};
|
|
|
|
|
|
|
|
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_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_encoder_BreakoutEncoder_obj_t);
|
|
|
|
|
|
|
|
float brightness = mp_obj_get_float(args[ARG_brightness].u_obj);
|
|
|
|
if(brightness < 0 || brightness > 1.0f)
|
|
|
|
mp_raise_ValueError("brightness out of range. Expected 0.0 to 1.0");
|
|
|
|
else
|
2021-05-12 16:51:54 +00:00
|
|
|
self->breakout->set_brightness(brightness);
|
2021-05-11 19:02:18 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutEncoder_set_led(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_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-11 19:14:13 +00:00
|
|
|
|
2021-05-11 19:02:18 +00:00
|
|
|
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_encoder_BreakoutEncoder_obj_t);
|
|
|
|
|
|
|
|
int r = args[ARG_r].u_int;
|
|
|
|
int g = args[ARG_g].u_int;
|
|
|
|
int b = args[ARG_b].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
|
|
|
|
self->breakout->set_led(r, g, b);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutEncoder_available(mp_obj_t self_in) {
|
|
|
|
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_encoder_BreakoutEncoder_obj_t);
|
|
|
|
return mp_obj_new_bool(self->breakout->available());
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutEncoder_read(mp_obj_t self_in) {
|
|
|
|
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_encoder_BreakoutEncoder_obj_t);
|
|
|
|
return mp_obj_new_int(self->breakout->read());
|
|
|
|
}
|
2021-08-24 14:35:14 +00:00
|
|
|
|
|
|
|
mp_obj_t BreakoutEncoder_clear(mp_obj_t self_in) {
|
|
|
|
breakout_encoder_BreakoutEncoder_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_encoder_BreakoutEncoder_obj_t);
|
|
|
|
self->breakout->clear();
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2021-05-11 19:02:18 +00:00
|
|
|
}
|