Basic MicroPython bindings for BME68X

The BME68X library is *linked* against the MicroPython bindings, rather than compiled directly in.
This saves specifing the list of target files twice.
pull/143/head
Phil Howard 2021-05-21 11:14:41 +01:00
rodzic 22d39faf5f
commit c402d92e0f
13 zmienionych plików z 216 dodań i 17 usunięć

Wyświetl plik

@ -9,8 +9,6 @@ target_sources(${DRIVER_NAME} INTERFACE
target_include_directories(${DRIVER_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(${DRIVER_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR}/src)
# Pull in pico libraries that we need
target_link_libraries(${DRIVER_NAME} INTERFACE pico_stdlib hardware_i2c pimoroni_i2c)
# We can't control the uninitialized result variables in the BME68X API
# so demote unitialized to a warning for this target.

Wyświetl plik

@ -2,7 +2,7 @@
#include "pico/stdlib.h"
namespace pimoroni {
bool BME68x::init() {
bool BME68X::init() {
int8_t result = 0;
if(interrupt != PIN_UNUSED) {
@ -32,7 +32,7 @@ namespace pimoroni {
return true;
}
bool BME68x::configure(uint8_t filter, uint8_t odr, uint8_t os_humidity, uint8_t os_pressure, uint8_t os_temp) {
bool BME68X::configure(uint8_t filter, uint8_t odr, uint8_t os_humidity, uint8_t os_pressure, uint8_t os_temp) {
int8_t result;
conf.filter = filter;
@ -48,7 +48,7 @@ namespace pimoroni {
return true;
}
bool BME68x::read_forced(bme68x_data *data) {
bool BME68X::read_forced(bme68x_data *data) {
int8_t result = 0;
uint8_t n_fields;
uint32_t delay_period;
@ -79,7 +79,7 @@ namespace pimoroni {
Will read profile_length results with the given temperatures and duration multipliers into the results array.
Blocks until it has a valid result for each temp/duration, and returns the entire set in the given order.
*/
bool BME68x::read_parallel(bme68x_data *results, uint16_t *profile_temps, uint16_t *profile_durations, size_t profile_length) {
bool BME68X::read_parallel(bme68x_data *results, uint16_t *profile_temps, uint16_t *profile_durations, size_t profile_length) {
int8_t result;
bme68x_data data[3]; // Parallel & Sequential mode read 3 simultaneous fields
uint8_t n_fields;

Wyświetl plik

@ -8,7 +8,7 @@
#include "stdio.h"
namespace pimoroni {
class BME68x {
class BME68X {
public:
static const uint8_t DEFAULT_I2C_ADDRESS = 0x76;
static const uint8_t ALTERNATE_I2C_ADDRESS = 0x77;
@ -25,13 +25,16 @@ namespace pimoroni {
bool read_forced(bme68x_data *data);
bool read_parallel(bme68x_data *results, uint16_t *profile_temps, uint16_t *profile_durations, size_t profile_length);
BME68x() : BME68x(new I2C()) {}
BME68x(uint8_t address, uint interrupt = PIN_UNUSED) : BME68x(new I2C(), address, interrupt) {}
BME68x(I2C *i2c, uint8_t address = DEFAULT_I2C_ADDRESS, uint interrupt = PIN_UNUSED) : i2c(i2c), address(address), interrupt(interrupt) {}
BME68X() : BME68X(new I2C()) {}
BME68X(uint8_t address, uint interrupt = PIN_UNUSED) : BME68X(new I2C(), address, interrupt) {}
BME68X(I2C *i2c, uint8_t address = DEFAULT_I2C_ADDRESS, uint interrupt = PIN_UNUSED) : i2c(i2c), address(address), interrupt(interrupt) {}
I2C *get_i2c() {return i2c;}
uint get_int() {return PIN_UNUSED;}
// Bindings for bme68x_dev
static int write_bytes(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr) {
BME68x::i2c_intf_ptr* i2c = (BME68x::i2c_intf_ptr *)intf_ptr;
BME68X::i2c_intf_ptr* i2c = (BME68X::i2c_intf_ptr *)intf_ptr;
uint8_t buffer[length + 1];
buffer[0] = reg_addr;
@ -45,7 +48,7 @@ namespace pimoroni {
};
static int read_bytes(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr) {
BME68x::i2c_intf_ptr* i2c = (BME68x::i2c_intf_ptr *)intf_ptr;
BME68X::i2c_intf_ptr* i2c = (BME68X::i2c_intf_ptr *)intf_ptr;
int result = i2c->i2c->write_blocking(i2c->address, &reg_addr, 1, true);
result = i2c->i2c->read_blocking(i2c->address, reg_data, length, false);

Wyświetl plik

@ -6,7 +6,7 @@ add_executable(
)
# Pull in pico libraries that we need
target_link_libraries(${OUTPUT_NAME} pico_stdlib bme68x)
target_link_libraries(${OUTPUT_NAME} pico_stdlib hardware_i2c pimoroni_i2c bme68x)
# create map/bin/hex file etc.
pico_add_extra_outputs(${OUTPUT_NAME})

Wyświetl plik

@ -12,7 +12,7 @@ Read a single reading from the BME688
using namespace pimoroni;
I2C i2c(BOARD::BREAKOUT_GARDEN);
BME68x bme68x(&i2c);
BME68X bme68x(&i2c);
int main() {
gpio_init(PICO_DEFAULT_LED_PIN);

Wyświetl plik

@ -6,7 +6,7 @@ add_executable(
)
# Pull in pico libraries that we need
target_link_libraries(${OUTPUT_NAME} pico_stdlib bme68x)
target_link_libraries(${OUTPUT_NAME} pico_stdlib hardware_i2c pimoroni_i2c bme68x)
# create map/bin/hex file etc.
pico_add_extra_outputs(${OUTPUT_NAME})

Wyświetl plik

@ -13,7 +13,7 @@ Reading the full batch of readings will take some time. This seems to take ~10se
using namespace pimoroni;
I2C i2c(BOARD::BREAKOUT_GARDEN);
BME68x bme68x(&i2c);
BME68X bme68x(&i2c);
constexpr uint16_t profile_length = 10;

Wyświetl plik

@ -0,0 +1,46 @@
#include "breakout_bme68x.h"
////////////////////////////////////////////////////////////////////////////////////////////////////
// BreakoutBME68X Class
////////////////////////////////////////////////////////////////////////////////////////////////////
/***** Methods *****/
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutBME68X_read_obj, BreakoutBME68X_read);
/***** Binding of Methods *****/
STATIC const mp_rom_map_elem_t BreakoutBME68X_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&BreakoutBME68X_read_obj) },
};
STATIC MP_DEFINE_CONST_DICT(BreakoutBME68X_locals_dict, BreakoutBME68X_locals_dict_table);
/***** Class Definition *****/
const mp_obj_type_t breakout_bme68x_BreakoutBME68X_type = {
{ &mp_type_type },
.name = MP_QSTR_breakout_bme68x,
.print = BreakoutBME68X_print,
.make_new = BreakoutBME68X_make_new,
.locals_dict = (mp_obj_dict_t*)&BreakoutBME68X_locals_dict,
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// breakout_bme68x Module
////////////////////////////////////////////////////////////////////////////////////////////////////
/***** Globals Table *****/
STATIC const mp_map_elem_t breakout_bme68x_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_breakout_bme68x) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_BreakoutBME68X), (mp_obj_t)&breakout_bme68x_BreakoutBME68X_type },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_breakout_bme68x_globals, breakout_bme68x_globals_table);
/***** Module Definition *****/
const mp_obj_module_t breakout_bme68x_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_breakout_bme68x_globals,
};
////////////////////////////////////////////////////////////////////////////////////////////////////
MP_REGISTER_MODULE(MP_QSTR_breakout_bme68x, breakout_bme68x_user_cmodule, MODULE_BREAKOUT_BME68X_ENABLED);
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

Wyświetl plik

@ -0,0 +1,105 @@
#include "drivers/bme68x/bme68x.hpp"
#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_bme68x.h"
#include "pimoroni_i2c.h"
/***** I2C Struct *****/
typedef struct _PimoroniI2C_obj_t {
mp_obj_base_t base;
I2C *i2c;
} _PimoroniI2C_obj_t;
/***** Variables Struct *****/
typedef struct _breakout_bme68x_BreakoutBME68X_obj_t {
mp_obj_base_t base;
BME68X *breakout;
} breakout_bme68x_BreakoutBME68X_obj_t;
/***** Print *****/
void BreakoutBME68X_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind; //Unused input parameter
breakout_bme68x_BreakoutBME68X_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_bme68x_BreakoutBME68X_obj_t);
BME68X* breakout = self->breakout;
mp_print_str(print, "BreakoutBME68X(");
mp_print_str(print, "i2c = ");
mp_obj_print_helper(print, mp_obj_new_int((breakout->get_i2c()->get_i2c() == i2c0) ? 0 : 1), PRINT_REPR);
mp_print_str(print, ", sda = ");
mp_obj_print_helper(print, mp_obj_new_int(breakout->get_i2c()->get_sda()), PRINT_REPR);
mp_print_str(print, ", scl = ");
mp_obj_print_helper(print, mp_obj_new_int(breakout->get_i2c()->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 BreakoutBME68X_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
breakout_bme68x_BreakoutBME68X_obj_t *self = nullptr;
enum { ARG_i2c, ARG_address, ARG_int };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} },
{ MP_QSTR_address, MP_ARG_INT, {.u_int = BME68X::DEFAULT_I2C_ADDRESS} },
{ 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);
if(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) {
mp_raise_ValueError(MP_ERROR_TEXT("BreakoutBME68X: Bad i2C object"));
return mp_const_none;
}
_PimoroniI2C_obj_t *i2c = (_PimoroniI2C_obj_t *)MP_OBJ_TO_PTR(args[ARG_i2c].u_obj);
self = m_new_obj(breakout_bme68x_BreakoutBME68X_obj_t);
self->base.type = &breakout_bme68x_BreakoutBME68X_type;
self->breakout = new BME68X(i2c->i2c, args[ARG_address].u_int, args[ARG_int].u_int);
if(!self->breakout->init()) {
mp_raise_msg(&mp_type_RuntimeError, "BreakoutBME68X: breakout not found when initialising");
}
return MP_OBJ_FROM_PTR(self);
}
mp_obj_t BreakoutBME68X_read(mp_obj_t self_in) {
breakout_bme68x_BreakoutBME68X_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_bme68x_BreakoutBME68X_obj_t);
bme68x_data result;
if(self->breakout->read_forced(&result)){
mp_obj_t tuple[7];
tuple[0] = mp_obj_new_float(result.temperature);
tuple[1] = mp_obj_new_float(result.pressure);
tuple[2] = mp_obj_new_float(result.humidity);
tuple[3] = mp_obj_new_float(result.gas_resistance);
tuple[4] = mp_obj_new_float(result.status);
tuple[5] = mp_obj_new_float(result.gas_index);
tuple[6] = mp_obj_new_float(result.meas_index);
return mp_obj_new_tuple(7, tuple);
}
else {
mp_raise_msg(&mp_type_RuntimeError, "BreakoutBME68X: failed read_forced");
return mp_const_none;
}
}
}

Wyświetl plik

@ -0,0 +1,12 @@
// Include MicroPython API.
#include "py/runtime.h"
/***** Constants *****/
/***** Extern of Class Definition *****/
extern const mp_obj_type_t breakout_bme68x_BreakoutBME68X_type;
/***** Extern of Class Methods *****/
extern void BreakoutBME68X_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
extern mp_obj_t BreakoutBME68X_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 BreakoutBME68X_read(mp_obj_t self_in);

Wyświetl plik

@ -0,0 +1,21 @@
set(MOD_NAME breakout_bme68x)
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
)
include(drivers/bme68x/bme68x)
target_link_libraries(usermod_${MOD_NAME} INTERFACE bme68x)
target_include_directories(usermod_${MOD_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}
)
target_compile_definitions(usermod_${MOD_NAME} INTERFACE
MODULE_${MOD_NAME_UPPER}_ENABLED=1
)
target_link_libraries(usermod INTERFACE usermod_${MOD_NAME})

Wyświetl plik

@ -0,0 +1,13 @@
set(MOD_NAME breakout_bme68x)
BREAKOUT_MOD_DIR := $(USERMOD_DIR)
# Add our source files to the respective variables.
SRC_USERMOD += $(BREAKOUT_MOD_DIR)/${MOD_NAME}.c
SRC_USERMOD_CXX += $(BREAKOUT_MOD_DIR)/${MOD_NAME}.cpp
# Add our module directory to the include path.
CFLAGS_USERMOD += -I$(BREAKOUT_MOD_DIR)
CXXFLAGS_USERMOD += -I$(BREAKOUT_MOD_DIR)
# We use C++ features so have to link against the standard library.
LDFLAGS_USERMOD += -lstdc++

Wyświetl plik

@ -22,6 +22,7 @@ include(breakout_trackball/micropython)
include(breakout_sgp30/micropython)
include(breakout_colourlcd240x240/micropython)
include(breakout_bh1745/micropython)
include(breakout_bme68x/micropython)
include(pico_scroll/micropython)
include(pico_rgb_keypad/micropython)
@ -29,4 +30,4 @@ include(pico_unicorn/micropython)
include(pico_display/micropython)
include(pico_explorer/micropython)
include(pico_wireless/micropython)
include(ulab/code/micropython)
include(ulab/code/micropython)