AS7343: MicroPython bindings.
This commit is contained in:
parent
6803f00b31
commit
6fcbaf5616
|
@ -15,6 +15,13 @@ namespace pimoroni {
|
|||
gpio_pull_up(interrupt);
|
||||
}
|
||||
|
||||
uint8_t aux_id, revision_id, hardware_id;
|
||||
get_version(aux_id, revision_id, hardware_id);
|
||||
|
||||
if(hardware_id != HARDWARE_ID) {
|
||||
return false;
|
||||
}
|
||||
|
||||
reset();
|
||||
|
||||
bank_select(0);
|
||||
|
|
|
@ -112,6 +112,8 @@ namespace pimoroni {
|
|||
FDATA = 0xFE // 0xFFFF
|
||||
};
|
||||
|
||||
constexpr uint8_t HARDWARE_ID = 0b10000001;
|
||||
|
||||
constexpr uint8_t CFG0_LOW_POWER = 0b00100000;
|
||||
constexpr uint8_t CFG0_BANK = 0b00010000;
|
||||
constexpr uint8_t CFG0_WLONG = 0b00000100;
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
#include "breakout_as7343.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// BreakoutAS7343 Class
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/***** Methods *****/
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutAS7343_reset_obj, BreakoutAS7343_reset);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutAS7343_version_obj, BreakoutAS7343_version);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutAS7343_read_obj, BreakoutAS7343_read);
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(BreakoutAS7343_set_gain_obj, BreakoutAS7343_set_gain);
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(BreakoutAS7343_set_integration_time_obj, BreakoutAS7343_set_integration_time);
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(BreakoutAS7343_set_measurement_time_obj, BreakoutAS7343_set_measurement_time);
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(BreakoutAS7343_set_illumination_current_obj, BreakoutAS7343_set_illumination_current);
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(BreakoutAS7343_set_illumination_led_obj, BreakoutAS7343_set_illumination_led);
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(BreakoutAS7343_set_channels_obj, BreakoutAS7343_set_channels);
|
||||
|
||||
/***** Binding of Methods *****/
|
||||
STATIC const mp_rom_map_elem_t BreakoutAS7343_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&BreakoutAS7343_reset_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&BreakoutAS7343_version_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&BreakoutAS7343_read_obj) },
|
||||
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_gain), MP_ROM_PTR(&BreakoutAS7343_set_gain_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_measurement_time), MP_ROM_PTR(&BreakoutAS7343_set_measurement_time_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_integration_time), MP_ROM_PTR(&BreakoutAS7343_set_integration_time_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_illumination_current), MP_ROM_PTR(&BreakoutAS7343_set_illumination_current_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_illumination_led), MP_ROM_PTR(&BreakoutAS7343_set_illumination_led_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_channels), MP_ROM_PTR(&BreakoutAS7343_set_channels_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(BreakoutAS7343_locals_dict, BreakoutAS7343_locals_dict_table);
|
||||
|
||||
/***** Class Definition *****/
|
||||
#ifdef MP_DEFINE_CONST_OBJ_TYPE
|
||||
MP_DEFINE_CONST_OBJ_TYPE(
|
||||
breakout_as7343_BreakoutAS7343_type,
|
||||
MP_QSTR_BreakoutAS7343,
|
||||
MP_TYPE_FLAG_NONE,
|
||||
make_new, BreakoutAS7343_make_new,
|
||||
locals_dict, (mp_obj_dict_t*)&BreakoutAS7343_locals_dict
|
||||
);
|
||||
#else
|
||||
const mp_obj_type_t breakout_as7343_BreakoutAS7343_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_BreakoutAS7343,
|
||||
.make_new = BreakoutAS7343_make_new,
|
||||
.locals_dict = (mp_obj_dict_t*)&BreakoutAS7343_locals_dict,
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// breakout_as7343 Module
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/***** Globals Table *****/
|
||||
STATIC const mp_map_elem_t breakout_as7343_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_breakout_as7343) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_BreakoutAS7343), (mp_obj_t)&breakout_as7343_BreakoutAS7343_type },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_breakout_as7343_globals, breakout_as7343_globals_table);
|
||||
|
||||
/***** Module Definition *****/
|
||||
const mp_obj_module_t breakout_as7343_user_cmodule = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mp_module_breakout_as7343_globals,
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#if MICROPY_VERSION <= 70144
|
||||
MP_REGISTER_MODULE(MP_QSTR_breakout_as7343, breakout_as7343_user_cmodule, MODULE_BREAKOUT_AS7343_ENABLED);
|
||||
#else
|
||||
MP_REGISTER_MODULE(MP_QSTR_breakout_as7343, breakout_as7343_user_cmodule);
|
||||
#endif
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
@ -0,0 +1,140 @@
|
|||
#include "drivers/as7343/as7343.hpp"
|
||||
#include "common/pimoroni_i2c.hpp"
|
||||
#include "micropython/modules/util.hpp"
|
||||
|
||||
|
||||
using namespace pimoroni;
|
||||
|
||||
extern "C" {
|
||||
#include "breakout_as7343.h"
|
||||
#include "pimoroni_i2c.h"
|
||||
|
||||
/***** Variables Struct *****/
|
||||
typedef struct _breakout_as7343_BreakoutAS7343_obj_t {
|
||||
mp_obj_base_t base;
|
||||
AS7343 *breakout;
|
||||
_PimoroniI2C_obj_t *i2c;
|
||||
} breakout_as7343_BreakoutAS7343_obj_t;
|
||||
|
||||
|
||||
|
||||
/***** Constructor *****/
|
||||
mp_obj_t BreakoutAS7343_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
||||
breakout_as7343_BreakoutAS7343_obj_t *self = nullptr;
|
||||
|
||||
enum { ARG_i2c, ARG_int };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ 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);
|
||||
|
||||
self = m_new_obj(breakout_as7343_BreakoutAS7343_obj_t);
|
||||
self->base.type = &breakout_as7343_BreakoutAS7343_type;
|
||||
|
||||
self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj);
|
||||
|
||||
self->breakout = m_new_class(AS7343, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_int].u_int);
|
||||
|
||||
if(!self->breakout->init()) {
|
||||
mp_raise_msg(&mp_type_RuntimeError, "BreakoutAS7343: breakout not found when initialising");
|
||||
}
|
||||
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
/***** Methods *****/
|
||||
mp_obj_t BreakoutAS7343_reset(mp_obj_t self_in) {
|
||||
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
|
||||
self->breakout->reset();
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutAS7343_version(mp_obj_t self_in) {
|
||||
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
|
||||
|
||||
uint8_t aux_id, revision_id, hardware_id;
|
||||
self->breakout->get_version(aux_id, revision_id, hardware_id);
|
||||
|
||||
mp_obj_t tuple[3];
|
||||
tuple[0] = mp_obj_new_int(aux_id);
|
||||
tuple[1] = mp_obj_new_int(revision_id);
|
||||
tuple[2] = mp_obj_new_int(hardware_id);
|
||||
return mp_obj_new_tuple(3, tuple);
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutAS7343_read(mp_obj_t self_in) {
|
||||
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
|
||||
AS7343::reading reading = self->breakout->read();
|
||||
|
||||
mp_obj_t tuple[12];
|
||||
tuple[0] = mp_obj_new_float(reading.FZ);
|
||||
tuple[1] = mp_obj_new_float(reading.FY);
|
||||
tuple[2] = mp_obj_new_float(reading.FXL);
|
||||
tuple[3] = mp_obj_new_float(reading.NIR);
|
||||
|
||||
tuple[4] = mp_obj_new_float(reading.F2);
|
||||
tuple[5] = mp_obj_new_float(reading.F3);
|
||||
tuple[6] = mp_obj_new_float(reading.F4);
|
||||
tuple[7] = mp_obj_new_float(reading.F6);
|
||||
|
||||
tuple[8] = mp_obj_new_float(reading.F1);
|
||||
tuple[9] = mp_obj_new_float(reading.F5);
|
||||
tuple[10] = mp_obj_new_float(reading.F7);
|
||||
tuple[11] = mp_obj_new_float(reading.F8);
|
||||
|
||||
return mp_obj_new_tuple(12, tuple);
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutAS7343_set_gain(mp_obj_t self_in, mp_obj_t value_in) {
|
||||
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
|
||||
|
||||
self->breakout->set_gain(mp_obj_get_int(value_in));
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutAS7343_set_measurement_time(mp_obj_t self_in, mp_obj_t value_in) {
|
||||
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
|
||||
|
||||
self->breakout->set_measurement_time(mp_obj_get_float(value_in));
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutAS7343_set_integration_time(mp_obj_t self_in, mp_obj_t value_in) {
|
||||
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
|
||||
|
||||
self->breakout->set_integration_time(mp_obj_get_float(value_in));
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutAS7343_set_illumination_current(mp_obj_t self_in, mp_obj_t value_in) {
|
||||
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
|
||||
|
||||
self->breakout->set_illumination_current(mp_obj_get_int(value_in));
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutAS7343_set_illumination_led(mp_obj_t self_in, mp_obj_t value_in) {
|
||||
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
|
||||
|
||||
self->breakout->set_illumination_led(value_in == mp_const_true);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutAS7343_set_channels(mp_obj_t self_in, mp_obj_t value_in) {
|
||||
breakout_as7343_BreakoutAS7343_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_as7343_BreakoutAS7343_obj_t);
|
||||
|
||||
self->breakout->set_channels((AS7343::channel_count)mp_obj_get_int(value_in));
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
// Include MicroPython API.
|
||||
#include "py/runtime.h"
|
||||
|
||||
/***** Constants *****/
|
||||
|
||||
|
||||
/***** Extern of Class Definition *****/
|
||||
extern const mp_obj_type_t breakout_as7343_BreakoutAS7343_type;
|
||||
|
||||
/***** Extern of Class Methods *****/
|
||||
extern mp_obj_t BreakoutAS7343_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
|
||||
extern mp_obj_t BreakoutAS7343_reset(mp_obj_t self_in);
|
||||
extern mp_obj_t BreakoutAS7343_version(mp_obj_t self_in);
|
||||
extern mp_obj_t BreakoutAS7343_read(mp_obj_t self_in);
|
||||
|
||||
extern mp_obj_t BreakoutAS7343_set_gain(mp_obj_t self_in, mp_obj_t value_in);
|
||||
extern mp_obj_t BreakoutAS7343_set_measurement_time(mp_obj_t self_in, mp_obj_t value_in);
|
||||
extern mp_obj_t BreakoutAS7343_set_integration_time(mp_obj_t self_in, mp_obj_t value_in);
|
||||
|
||||
extern mp_obj_t BreakoutAS7343_set_illumination_current(mp_obj_t self_in, mp_obj_t value_in);
|
||||
extern mp_obj_t BreakoutAS7343_set_illumination_led(mp_obj_t self_in, mp_obj_t value_in);
|
||||
|
||||
extern mp_obj_t BreakoutAS7343_set_channels(mp_obj_t self_in, mp_obj_t value_in);
|
|
@ -0,0 +1,19 @@
|
|||
set(MOD_NAME breakout_as7343)
|
||||
string(TOUPPER ${MOD_NAME} MOD_NAME_UPPER)
|
||||
add_library(usermod_${MOD_NAME} INTERFACE)
|
||||
|
||||
target_sources(usermod_${MOD_NAME} INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../../drivers/as7343/as7343.cpp
|
||||
)
|
||||
|
||||
target_include_directories(usermod_${MOD_NAME} INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
target_compile_definitions(usermod_${MOD_NAME} INTERFACE
|
||||
-DMODULE_${MOD_NAME_UPPER}_ENABLED=1
|
||||
)
|
||||
|
||||
target_link_libraries(usermod INTERFACE usermod_${MOD_NAME})
|
|
@ -4,6 +4,7 @@ include(breakout_encoder_wheel/micropython)
|
|||
include(breakout_ioexpander/micropython)
|
||||
include(breakout_ltr559/micropython)
|
||||
include(breakout_as7262/micropython)
|
||||
include(breakout_as7343/micropython)
|
||||
include(breakout_rgbmatrix5x5/micropython)
|
||||
include(breakout_matrix11x7/micropython)
|
||||
include(breakout_msa301/micropython)
|
||||
|
|
Loading…
Reference in New Issue