diff --git a/drivers/icp10125/icp10125.cpp b/drivers/icp10125/icp10125.cpp index 68e9a087..5a2cf9a3 100644 --- a/drivers/icp10125/icp10125.cpp +++ b/drivers/icp10125/icp10125.cpp @@ -75,6 +75,7 @@ namespace pimoroni { if(results[2].crc8 != crc8((uint8_t *)&results[2].data, 2)) {result.status = CRC_FAIL; return result;}; int temperature = __bswap16(results[0].data); + // Due to all the byte swapping nonsense I'm not sure if I've discarded the LLSB or LMSB here... int pressure = ((int32_t)__bswap16(results[1].data) << 8) | (__bswap16(results[2].data >> 8)); // LLSB is discarded process_data(pressure, temperature, &result.pressure, &result.temperature); diff --git a/drivers/icp10125/icp10125.hpp b/drivers/icp10125/icp10125.hpp index 4bd0cee7..ea90b31d 100644 --- a/drivers/icp10125/icp10125.hpp +++ b/drivers/icp10125/icp10125.hpp @@ -46,7 +46,7 @@ namespace pimoroni { bool init(); int chip_id(); - bool read_otp(); + void reset(); reading measure(meas_command cmd=NORMAL); private: @@ -59,8 +59,7 @@ namespace pimoroni { const float LUT_upper = 11.5 * (1 << 20); const float quadr_factor = 1.0 / 16777216.0; const float offst_factor = 2048.0; - - void reset(); + bool read_otp(); void process_data(const int p_LSB, const int T_LSB, float *pressure, float *temperature); void calculate_conversion_constants(const float *p_Pa, const float *p_LUT, float *out); uint8_t crc8(uint8_t *bytes, size_t length, uint8_t polynomial = 0x31); diff --git a/micropython/examples/breakout_icp10125/normal_demo.py b/micropython/examples/breakout_icp10125/normal_demo.py new file mode 100644 index 00000000..96adc50e --- /dev/null +++ b/micropython/examples/breakout_icp10125/normal_demo.py @@ -0,0 +1,14 @@ +import time + +import breakout_icp10125 +import pimoroni_i2c + +i2c = pimoroni_i2c.PimoroniI2C(4, 5) + +icp10125 = breakout_icp10125.BreakoutICP10125(i2c) + +while True: + t, p, status = icp10125.measure(icp10125.NORMAL) + if status == icp10125.STATUS_OK: + print(t, p) + time.sleep(1.0) diff --git a/micropython/modules/breakout_icp10125/README.md b/micropython/modules/breakout_icp10125/README.md new file mode 100644 index 00000000..804bbeef --- /dev/null +++ b/micropython/modules/breakout_icp10125/README.md @@ -0,0 +1,89 @@ +# ICP1025 High-accuracy Barometric Pressure & Temperature Sensor + +The ICP1025 library is intended to drive the TDK InvenSense ICP10125 temperature and pressure sensor. + +- [Getting Started](#getting-started) +- [Taking A Measurement](#taking-a-measurement) +- [Measurement Types](#measurement-types) + - [Normal](#normal) + - [Low Power](#low-power) + - [Low Noise](#low-noise) + - [Ultra Low Noise](#ultra-low-noise) + +## Getting Started + +Construct a new PimoroniI2C instance for your specific board. Breakout Garden uses pins 4 & 5 and Pico Explorer uses pins 20 & 21. + +```python +import breakout_icp10125 +import pimoroni_i2c + +i2c = pimoroni_i2c.PimoroniI2C(4, 5) + +icp10125 = breakout_icp10125.BreakoutICP10125(i2c) +``` + +## Taking A Measurement + +The `measure` method triggers a measurement, blocks for enough time for the measurement to complete, and returns the result as a tuple with three values: + +1. Temperature (degrees C) +2. Pressure (Pa) +3. Status + +If the `status` is `icp10125.STATUS_OK` then the reading is valid. Otherwise you should probably discard it or attempt another reading. + +For example, the following code will continuously poll the ICP10125 for `NORMAL` readings and print them out if they are valid: + +```python +import breakout_icp10125 +import pimoroni_i2c + +i2c = pimoroni_i2c.PimoroniI2C(4, 5) + +icp10125 = breakout_icp10125.BreakoutICP10125(i2c) + +while True: + t, p, status = icp10125.measure() + if status == icp10125.STATUS_OK: + print(t, p) + time.sleep(1.0) +``` + +## Measurement Types + +The ICP1025 has eight measurement commands. Four are supported by this library since the remaining four are identical save for the order of Temperature/Pressure data readout being reversed. + +Each measurement type has a fixed duration. + +### Normal + +Normal measurements are the default type, and take 7ms to complete offering a good balance of stability and speed. + +```python +result = icp10125.measure(icp10125.NORMAL) +``` + +### Low Power + +Low-power measurements take just 2ms and trade stability for speed/power efficiency. + +```python +result = icp10125.measure(icp10125.LOW_POWER) +``` + +### Low Noise + +Low-noise measurements take 24ms (roughly 3.5x as long as normal) and trade speed for stability. + +```python +result = icp10125.measure(icp10125.LOW_NOISE) +``` + +### Ultra Low Noise + +Ultra Low-noise measurements take 95ms + +```python +result = icp10125.measure(icp10125.ULTRA_LOW_NOISE) +``` diff --git a/micropython/modules/breakout_icp10125/breakout_icp10125.c b/micropython/modules/breakout_icp10125/breakout_icp10125.c new file mode 100644 index 00000000..6c254ffe --- /dev/null +++ b/micropython/modules/breakout_icp10125/breakout_icp10125.c @@ -0,0 +1,54 @@ +#include "breakout_icp10125.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// BreakoutICP10125 Class +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/***** Methods *****/ +MP_DEFINE_CONST_FUN_OBJ_KW(BreakoutICP10125_measure_obj, 1, BreakoutICP10125_measure); +MP_DEFINE_CONST_FUN_OBJ_1(BreakoutICP10125_soft_reset_obj, BreakoutICP10125_soft_reset); + +/***** Binding of Methods *****/ +STATIC const mp_rom_map_elem_t BreakoutICP10125_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_measure), MP_ROM_PTR(&BreakoutICP10125_measure_obj) }, + { MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&BreakoutICP10125_soft_reset_obj) }, + { MP_ROM_QSTR(MP_QSTR_NORMAL), MP_ROM_INT(NORMAL) }, + { MP_ROM_QSTR(MP_QSTR_LOW_POWER), MP_ROM_INT(LOW_POWER) }, + { MP_ROM_QSTR(MP_QSTR_LOW_NOISE), MP_ROM_INT(LOW_NOISE) }, + { MP_ROM_QSTR(MP_QSTR_ULTRA_LOW_NOISE), MP_ROM_INT(ULTRA_LOW_NOISE) }, + { MP_ROM_QSTR(MP_QSTR_STATUS_OK), MP_ROM_INT(OK) }, + { MP_ROM_QSTR(MP_QSTR_STATUS_CRC_FAIL), MP_ROM_INT(CRC_FAIL) }, +}; +STATIC MP_DEFINE_CONST_DICT(BreakoutICP10125_locals_dict, BreakoutICP10125_locals_dict_table); + +/***** Class Definition *****/ +const mp_obj_type_t breakout_icp10125_BreakoutICP10125_type = { + { &mp_type_type }, + .name = MP_QSTR_breakout_matrix11x7, + .print = BreakoutICP10125_print, + .make_new = BreakoutICP10125_make_new, + .locals_dict = (mp_obj_dict_t*)&BreakoutICP10125_locals_dict, +}; + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// breakout_icp10125 Module +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/***** Globals Table *****/ +STATIC const mp_map_elem_t breakout_icp10125_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_breakout_icp10125) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_BreakoutICP10125), (mp_obj_t)&breakout_icp10125_BreakoutICP10125_type }, +}; +STATIC MP_DEFINE_CONST_DICT(mp_module_breakout_icp10125_globals, breakout_icp10125_globals_table); + +/***** Module Definition *****/ +const mp_obj_module_t breakout_icp10125_user_cmodule = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&mp_module_breakout_icp10125_globals, +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +MP_REGISTER_MODULE(MP_QSTR_breakout_icp10125, breakout_icp10125_user_cmodule, MODULE_BREAKOUT_SGP30_ENABLED); +//////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/micropython/modules/breakout_icp10125/breakout_icp10125.cpp b/micropython/modules/breakout_icp10125/breakout_icp10125.cpp new file mode 100644 index 00000000..c02ad8ab --- /dev/null +++ b/micropython/modules/breakout_icp10125/breakout_icp10125.cpp @@ -0,0 +1,95 @@ +#include "drivers/icp10125/icp10125.hpp" + +#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) + +using namespace pimoroni; + +extern "C" { +#include "breakout_icp10125.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_icp10125_BreakoutICP10125_obj_t { + mp_obj_base_t base; + ICP10125 *breakout; +} breakout_icp10125_BreakoutICP10125_obj_t; + +/***** Print *****/ +void BreakoutICP10125_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + (void)kind; //Unused input parameter + (void)self_in; + // breakout_icp10125_BreakoutICP10125_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_icp10125_BreakoutICP10125_obj_t); + // ICP10125* breakout = self->breakout; + // TODO put something useful here? There's no point printing I2C info since that's handled by the I2C object now + mp_print_str(print, "BreakoutICP10125()"); +} + +/***** Constructor *****/ +mp_obj_t BreakoutICP10125_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + breakout_icp10125_BreakoutICP10125_obj_t *self = nullptr; + + enum { ARG_i2c }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_i2c, MP_ARG_OBJ, {.u_obj = nullptr} } + }; + + // 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); + + // Get I2C bus. + if(!MP_OBJ_IS_TYPE(args[ARG_i2c].u_obj, &PimoroniI2C_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("BreakoutICP10125: 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_icp10125_BreakoutICP10125_obj_t); + self->base.type = &breakout_icp10125_BreakoutICP10125_type; + + self->breakout = new ICP10125(i2c->i2c); + + if(!self->breakout->init()) { + mp_raise_msg(&mp_type_RuntimeError, "BreakoutICP10125: breakout not found when initialising"); + } + + return MP_OBJ_FROM_PTR(self); +} + +/***** Methods *****/ + +mp_obj_t BreakoutICP10125_measure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_self, ARG_command }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_command, MP_ARG_INT, {.u_int = NORMAL} }, + }; + + 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_icp10125_BreakoutICP10125_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_icp10125_BreakoutICP10125_obj_t); + auto reading = self->breakout->measure((ICP10125::meas_command)args[ARG_command].u_int); + + mp_obj_t tuple[3]; + tuple[0] = mp_obj_new_float(reading.temperature); + tuple[1] = mp_obj_new_float(reading.pressure); + tuple[2] = mp_obj_new_int(reading.status); + return mp_obj_new_tuple(3, tuple); +} + +mp_obj_t BreakoutICP10125_soft_reset(mp_obj_t self_in) { + breakout_icp10125_BreakoutICP10125_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_icp10125_BreakoutICP10125_obj_t); + self->breakout->reset(); + + return mp_const_none; +} + +} \ No newline at end of file diff --git a/micropython/modules/breakout_icp10125/breakout_icp10125.h b/micropython/modules/breakout_icp10125/breakout_icp10125.h new file mode 100644 index 00000000..607dfed6 --- /dev/null +++ b/micropython/modules/breakout_icp10125/breakout_icp10125.h @@ -0,0 +1,23 @@ +// Include MicroPython API. +#include "py/runtime.h" + +/***** Extern of Class Definition *****/ +extern const mp_obj_type_t breakout_icp10125_BreakoutICP10125_type; + +enum meas_command { + NORMAL = 0x6825, + LOW_POWER = 0x609C, + LOW_NOISE = 0x70DF, + ULTRA_LOW_NOISE = 0x7866, +}; + +enum reading_status { + OK = 0, + CRC_FAIL = 1, +}; + +/***** Extern of Class Methods *****/ +extern void BreakoutICP10125_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind); +extern mp_obj_t BreakoutICP10125_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 BreakoutICP10125_soft_reset(mp_obj_t self_in); +extern mp_obj_t BreakoutICP10125_measure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); \ No newline at end of file diff --git a/micropython/modules/breakout_icp10125/micropython.cmake b/micropython/modules/breakout_icp10125/micropython.cmake new file mode 100644 index 00000000..3897a67b --- /dev/null +++ b/micropython/modules/breakout_icp10125/micropython.cmake @@ -0,0 +1,19 @@ +set(MOD_NAME breakout_icp10125) +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/icp10125/icp10125.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}) \ No newline at end of file diff --git a/micropython/modules/micropython.cmake b/micropython/modules/micropython.cmake index 30044027..4ddde4cc 100644 --- a/micropython/modules/micropython.cmake +++ b/micropython/modules/micropython.cmake @@ -25,6 +25,7 @@ include(breakout_bh1745/micropython) include(breakout_bme68x/micropython) include(breakout_bme280/micropython) include(breakout_bmp280/micropython) +include(breakout_icp10125/micropython) include(pico_scroll/micropython) include(pico_rgb_keypad/micropython)