2021-05-14 11:49:48 +00:00
|
|
|
#include "libraries/breakout_mics6814/breakout_mics6814.hpp"
|
2022-05-20 14:45:25 +00:00
|
|
|
#include "micropython/modules/util.hpp"
|
2021-05-14 14:14:47 +00:00
|
|
|
#include <cstdio>
|
2021-05-11 19:56:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
using namespace pimoroni;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "breakout_mics6814.h"
|
2021-05-18 10:18:41 +00:00
|
|
|
#include "pimoroni_i2c.h"
|
|
|
|
|
2021-05-11 19:56:35 +00:00
|
|
|
/***** Variables Struct *****/
|
|
|
|
typedef struct _breakout_mics6814_BreakoutMICS6814_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
BreakoutMICS6814 *breakout;
|
2022-05-17 17:21:09 +00:00
|
|
|
_PimoroniI2C_obj_t *i2c;
|
2021-05-11 19:56:35 +00:00
|
|
|
} breakout_mics6814_BreakoutMICS6814_obj_t;
|
|
|
|
|
|
|
|
|
|
|
|
/***** Constructor *****/
|
|
|
|
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;
|
|
|
|
|
2021-05-18 10:18:41 +00:00
|
|
|
enum { ARG_i2c, ARG_address, ARG_interrupt };
|
2021-05-11 19:56:35 +00:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2021-05-18 10:18:41 +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 = BreakoutMICS6814::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);
|
|
|
|
|
2025-03-24 11:50:45 +00:00
|
|
|
self = mp_obj_malloc(breakout_mics6814_BreakoutMICS6814_obj_t, &breakout_mics6814_BreakoutMICS6814_type);
|
2021-05-14 16:14:07 +00:00
|
|
|
|
2022-05-17 17:21:09 +00:00
|
|
|
self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj);
|
|
|
|
|
2022-05-20 14:45:25 +00:00
|
|
|
self->breakout = m_new_class(BreakoutMICS6814, (pimoroni::I2C *)(self->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()) {
|
2025-03-18 18:05:24 +00:00
|
|
|
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("BreakoutMICS6814: breakout not found when initialising"));
|
2021-05-11 20:38:44 +00:00
|
|
|
}
|
2021-05-11 19:56:35 +00:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***** Methods *****/
|
2021-05-11 20:58:44 +00:00
|
|
|
mp_obj_t BreakoutMICS6814_set_address(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2021-05-11 19:56:35 +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);
|
|
|
|
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
|
2021-05-11 20:58:44 +00:00
|
|
|
self->breakout->set_address(args[ARG_address].u_int);
|
2021-05-11 19:56:35 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_get_adc_vref(mp_obj_t self_in) {
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
return mp_obj_new_float(self->breakout->get_adc_vref());
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_set_adc_vref(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_self, ARG_vref };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_vref, 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_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
|
|
|
|
float vref = mp_obj_get_float(args[ARG_vref].u_obj);
|
|
|
|
self->breakout->set_adc_vref(vref);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_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_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
|
|
|
|
float brightness = mp_obj_get_float(args[ARG_brightness].u_obj);
|
|
|
|
if(brightness < 0 || brightness > 1.0f)
|
2025-03-18 17:33:11 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("brightness out of range. Expected 0.0 to 1.0"));
|
2021-05-11 19:56:35 +00:00
|
|
|
else
|
2021-05-12 16:51:54 +00:00
|
|
|
self->breakout->set_brightness(brightness);
|
2021-05-11 19:56:35 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_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);
|
|
|
|
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_mics6814_BreakoutMICS6814_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)
|
2025-03-18 17:33:11 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("r out of range. Expected 0 to 255"));
|
2021-05-11 19:56:35 +00:00
|
|
|
else if(g < 0 || g > 255)
|
2025-03-18 17:33:11 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("g out of range. Expected 0 to 255"));
|
2021-05-11 19:56:35 +00:00
|
|
|
else if(b < 0 || b > 255)
|
2025-03-18 17:33:11 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("b out of range. Expected 0 to 255"));
|
2021-05-11 19:56:35 +00:00
|
|
|
else
|
|
|
|
self->breakout->set_led(r, g, b);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_set_heater(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_self, ARG_on };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_on, 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);
|
|
|
|
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
|
|
|
|
self->breakout->set_heater(args[ARG_on].u_bool);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_disable_heater(mp_obj_t self_in) {
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
self->breakout->disable_heater();
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_get_raw_ref(mp_obj_t self_in) {
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
return mp_obj_new_float(self->breakout->get_raw_ref());
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_get_raw_red(mp_obj_t self_in) {
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
return mp_obj_new_float(self->breakout->get_raw_red());
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_get_raw_nh3(mp_obj_t self_in) {
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
return mp_obj_new_float(self->breakout->get_raw_nh3());
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_get_raw_oxd(mp_obj_t self_in) {
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
return mp_obj_new_float(self->breakout->get_raw_oxd());
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_read_all(mp_obj_t self_in) {
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
BreakoutMICS6814::Reading reading = self->breakout->read_all();
|
|
|
|
|
|
|
|
mp_obj_t tuple[4];
|
|
|
|
tuple[REF] = mp_obj_new_float(reading.ref);
|
|
|
|
tuple[REDUCING] = mp_obj_new_float(reading.reducing);
|
|
|
|
tuple[NH3] = mp_obj_new_float(reading.nh3);
|
|
|
|
tuple[OXIDISING] = mp_obj_new_float(reading.oxidising);
|
2021-05-14 13:30:08 +00:00
|
|
|
|
2021-05-11 19:56:35 +00:00
|
|
|
return mp_obj_new_tuple(4, tuple);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_read_ref(mp_obj_t self_in) {
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
return mp_obj_new_float(self->breakout->read_ref());
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_read_reducing(mp_obj_t self_in) {
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
return mp_obj_new_float(self->breakout->read_reducing());
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_read_nh3(mp_obj_t self_in) {
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
return mp_obj_new_float(self->breakout->read_nh3());
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t BreakoutMICS6814_read_oxidising(mp_obj_t self_in) {
|
|
|
|
breakout_mics6814_BreakoutMICS6814_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_mics6814_BreakoutMICS6814_obj_t);
|
|
|
|
return mp_obj_new_float(self->breakout->read_oxidising());
|
|
|
|
}
|
|
|
|
}
|