kopia lustrzana https://github.com/pimoroni/pimoroni-pico
Micropython bindings and minor tweaks for SGP30
rodzic
62e085e81c
commit
77ca674ac8
|
@ -26,15 +26,15 @@ namespace pimoroni {
|
|||
|
||||
soft_reset();
|
||||
|
||||
if (!retrieve_unique_id()) {
|
||||
if(!retrieve_unique_id()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Retrieve and check Feature Set
|
||||
uint16_t featureset;
|
||||
if (!read_reg_1_word(GET_FEATURE_SET_VERSION, 10, &featureset))
|
||||
if(!read_reg_1_word(GET_FEATURE_SET_VERSION, 10, &featureset))
|
||||
return false;
|
||||
if ((featureset & 0xF0) != SGP30_REQ_FEATURES)
|
||||
if((featureset & 0xF0) != SGP30_REQ_FEATURES)
|
||||
return false;
|
||||
|
||||
// Start the measurement process
|
||||
|
@ -47,6 +47,18 @@ namespace pimoroni {
|
|||
return true;
|
||||
}
|
||||
|
||||
i2c_inst_t* SGP30::get_i2c() const {
|
||||
return i2c;
|
||||
}
|
||||
|
||||
int SGP30::get_sda() const {
|
||||
return sda;
|
||||
}
|
||||
|
||||
int SGP30::get_scl() const {
|
||||
return scl;
|
||||
}
|
||||
|
||||
// Get the unique ID from the Chip. Will fail if no chip attached
|
||||
bool SGP30::retrieve_unique_id() {
|
||||
// return the Chip ID, in three separate 16-bit values
|
||||
|
@ -75,15 +87,15 @@ namespace pimoroni {
|
|||
bool rc = write_reg(INIT_AIR_QUALITY, 10);
|
||||
|
||||
// Optionally wait up to 20 seconds for the measurement process to initiate
|
||||
if (wait_for_setup) {
|
||||
if( wait_for_setup) {
|
||||
// It takes 15 seconds to start the measurement process but allow 20.
|
||||
// Ignore the first 2 readings completely.
|
||||
uint16_t eCO2, TVOC;
|
||||
uint8_t sec_count = 0;
|
||||
while (sec_count < 20) {
|
||||
while(sec_count < 20) {
|
||||
sleep_ms(988); // Will sleep last 12ms of 1sec in get_air_quality()
|
||||
get_air_quality(&eCO2, &TVOC);
|
||||
if ((sec_count >= 2) && (eCO2 != 400 || TVOC != 0)) {
|
||||
if((sec_count >= 2) && (eCO2 != 400 || TVOC != 0)) {
|
||||
// startup process finished
|
||||
break;
|
||||
}
|
||||
|
@ -104,7 +116,6 @@ namespace pimoroni {
|
|||
return read_reg_2_words(MEASURE_RAW_SIGNALS, 25, rawH2, rawEthanol);
|
||||
}
|
||||
|
||||
|
||||
// Get the baseline compensation values for eCO2 and VOC
|
||||
bool SGP30::get_baseline(uint16_t *eco2, uint16_t *tvoc) {
|
||||
return read_reg_2_words(GET_BASELINE, 10, eco2, tvoc);
|
||||
|
@ -117,19 +128,17 @@ namespace pimoroni {
|
|||
|
||||
// Set the absolute humidity, e.g. from an SHTxx chip
|
||||
bool SGP30::set_humidity(uint32_t absolute_humidity) {
|
||||
if (absolute_humidity > 256000) {
|
||||
if(absolute_humidity > 256000) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t ah_scaled =
|
||||
(uint16_t)(((uint64_t)absolute_humidity * 256 * 16777) >> 24);
|
||||
uint16_t ah_scaled = (uint16_t)(((uint64_t)absolute_humidity * 256 * 16777) >> 24);
|
||||
|
||||
return write_reg_1_word(SET_HUMIDITY, 10, ah_scaled);
|
||||
}
|
||||
|
||||
// Write a single byte globally (not to a specifc I2c address)
|
||||
bool SGP30::write_global(uint16_t reg, uint16_t delayms)
|
||||
{
|
||||
bool SGP30::write_global(uint16_t reg, uint16_t delayms) {
|
||||
uint8_t buffer[1] = { (uint8_t)(reg & 0xFF)};
|
||||
i2c_write_blocking(i2c, 0, buffer, 1, false);
|
||||
sleep_ms(delayms);
|
||||
|
@ -137,8 +146,7 @@ namespace pimoroni {
|
|||
}
|
||||
|
||||
// Write just the register to the i2c address, no parameter
|
||||
bool SGP30::write_reg(uint16_t reg, uint16_t delayms)
|
||||
{
|
||||
bool SGP30::write_reg(uint16_t reg, uint16_t delayms) {
|
||||
uint8_t buffer[2] = { (uint8_t)((reg >> 8) & 0xFF), (uint8_t)(reg & 0xFF)};
|
||||
i2c_write_blocking(i2c, address, buffer, 2, false);
|
||||
sleep_ms(delayms);
|
||||
|
@ -157,8 +165,8 @@ namespace pimoroni {
|
|||
// Write two 16-bit words (+CRC)
|
||||
bool SGP30::write_reg_2_words(uint16_t reg, uint16_t delayms, uint16_t value1, uint16_t value2) {
|
||||
uint8_t buffer[8] = { (uint8_t)((reg >> 8) & 0xFF), (uint8_t)(reg & 0xFF),
|
||||
(uint8_t)((value1 >> 8) & 0xFF), (uint8_t)(value1 & 0xFF), calculate_crc(value1),
|
||||
(uint8_t)((value2 >> 8) & 0xFF), (uint8_t)(value2 & 0xFF), calculate_crc(value2)};
|
||||
(uint8_t)((value1 >> 8) & 0xFF), (uint8_t)(value1 & 0xFF), calculate_crc(value1),
|
||||
(uint8_t)((value2 >> 8) & 0xFF), (uint8_t)(value2 & 0xFF), calculate_crc(value2)};
|
||||
i2c_write_blocking(i2c, address, buffer, 8, false);
|
||||
sleep_ms(delayms);
|
||||
return true;
|
||||
|
@ -171,7 +179,7 @@ namespace pimoroni {
|
|||
i2c_write_blocking(i2c, address, regbuf, 2, true);
|
||||
sleep_ms(delayms);
|
||||
i2c_read_blocking(i2c, address, buffer, 3, false);
|
||||
if (buffer[2] != calculate_crc(buffer[0], buffer[1]))
|
||||
if(buffer[2] != calculate_crc(buffer[0], buffer[1]))
|
||||
return false;
|
||||
*value = (buffer[0] << 8) + buffer[1];
|
||||
return true;
|
||||
|
@ -184,7 +192,7 @@ namespace pimoroni {
|
|||
i2c_write_blocking(i2c, address, regbuf, 2, true);
|
||||
sleep_ms(delayms);
|
||||
i2c_read_blocking(i2c, address, buffer, 6, false);
|
||||
if ((buffer[2] != calculate_crc(buffer[0], buffer[1])) || (buffer[5] != calculate_crc(buffer[3], buffer[4]))) {
|
||||
if((buffer[2] != calculate_crc(buffer[0], buffer[1])) || (buffer[5] != calculate_crc(buffer[3], buffer[4]))) {
|
||||
return false;
|
||||
}
|
||||
*value1 = (buffer[0] << 8) + buffer[1];
|
||||
|
@ -199,7 +207,7 @@ namespace pimoroni {
|
|||
i2c_write_blocking(i2c, address, regbuf, 2, true);
|
||||
sleep_ms(delayms);
|
||||
i2c_read_blocking(i2c, address, buffer, 9, false);
|
||||
if (buffer[2] != calculate_crc(buffer[0], buffer[1])) {
|
||||
if(buffer[2] != calculate_crc(buffer[0], buffer[1])) {
|
||||
return false;
|
||||
}
|
||||
if (buffer[5] != calculate_crc(buffer[3], buffer[4])) {
|
||||
|
@ -225,15 +233,15 @@ namespace pimoroni {
|
|||
uint8_t crc = SGP30_CRC_BASE;
|
||||
|
||||
crc ^= value1;
|
||||
for (uint8_t b = 0; b < 8; b++) {
|
||||
if (crc & 0x80)
|
||||
for(uint8_t b = 0; b < 8; b++) {
|
||||
if(crc & 0x80)
|
||||
crc = (crc << 1) ^ SGP30_CRC_SEED;
|
||||
else
|
||||
crc <<= 1;
|
||||
}
|
||||
crc ^= value2;
|
||||
for (uint8_t b = 0; b < 8; b++) {
|
||||
if (crc & 0x80)
|
||||
for(uint8_t b = 0; b < 8; b++) {
|
||||
if(crc & 0x80)
|
||||
crc = (crc << 1) ^ SGP30_CRC_SEED;
|
||||
else
|
||||
crc <<= 1;
|
||||
|
|
|
@ -19,8 +19,6 @@ namespace pimoroni {
|
|||
static const uint8_t DEFAULT_SDA_PIN = 20;
|
||||
static const uint8_t DEFAULT_SCL_PIN = 21;
|
||||
|
||||
/***** More public constants here *****/
|
||||
|
||||
private:
|
||||
/***** Private constants here *****/
|
||||
const uint16_t SOFT_RESET = 0x0006;
|
||||
|
@ -33,6 +31,7 @@ namespace pimoroni {
|
|||
const uint16_t GET_FEATURE_SET_VERSION = 0x202f;
|
||||
const uint16_t MEASURE_RAW_SIGNALS = 0x2050;
|
||||
const uint16_t GET_SERIAL_ID = 0x3682;
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// Variables
|
||||
|
@ -40,13 +39,10 @@ namespace pimoroni {
|
|||
private:
|
||||
i2c_inst_t *i2c = i2c0;
|
||||
|
||||
// interface pins with our standard defaults where appropriate
|
||||
int8_t address = DEFAULT_I2C_ADDRESS;
|
||||
int8_t sda = DEFAULT_SDA_PIN;
|
||||
int8_t scl = DEFAULT_SCL_PIN;
|
||||
|
||||
/***** More variables here *****/
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// Constructors/Destructor
|
||||
|
@ -62,7 +58,12 @@ namespace pimoroni {
|
|||
// Methods
|
||||
//--------------------------------------------------
|
||||
public:
|
||||
bool init(); //This should be present in all drivers
|
||||
bool init();
|
||||
|
||||
// For print access in micropython
|
||||
i2c_inst_t* get_i2c() const;
|
||||
int get_sda() const;
|
||||
int get_scl() const;
|
||||
|
||||
// uint8_t command(uint8_t command_name, uint8_t parameters/*=None*/);
|
||||
// Get the unique ID from the chip and store locally
|
||||
|
@ -108,7 +109,6 @@ namespace pimoroni {
|
|||
//
|
||||
uint8_t calculate_crc(uint16_t value);
|
||||
uint8_t calculate_crc(uint8_t value1, uint8_t value2);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
#include "breakout_sgp30.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// BreakoutSGP30 Class
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/***** Methods *****/
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutSGP30_retrieve_unique_id_obj, BreakoutSGP30_retrieve_unique_id);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutSGP30_get_unique_id_obj, BreakoutSGP30_get_unique_id);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(BreakoutSGP30_start_measurement_obj, 2, BreakoutSGP30_start_measurement);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutSGP30_get_air_quality_obj, BreakoutSGP30_get_air_quality);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutSGP30_get_air_quality_raw_obj, BreakoutSGP30_get_air_quality_raw);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutSGP30_soft_reset_obj, BreakoutSGP30_soft_reset);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(BreakoutSGP30_get_baseline_obj, BreakoutSGP30_get_baseline);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(BreakoutSGP30_set_baseline_obj, 3,BreakoutSGP30_set_baseline);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(BreakoutSGP30_set_humidity_obj, 2, BreakoutSGP30_set_humidity);
|
||||
|
||||
/***** Binding of Methods *****/
|
||||
STATIC const mp_rom_map_elem_t BreakoutSGP30_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_retrieve_unique_id), MP_ROM_PTR(&BreakoutSGP30_retrieve_unique_id_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_get_unique_id), MP_ROM_PTR(&BreakoutSGP30_get_unique_id_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_start_measurement), MP_ROM_PTR(&BreakoutSGP30_start_measurement_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_get_air_quality), MP_ROM_PTR(&BreakoutSGP30_get_air_quality_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_get_air_quality_raw), MP_ROM_PTR(&BreakoutSGP30_get_air_quality_raw_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&BreakoutSGP30_soft_reset_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_get_baseline), MP_ROM_PTR(&BreakoutSGP30_get_baseline_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_baseline), MP_ROM_PTR(&BreakoutSGP30_set_baseline_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_humidity), MP_ROM_PTR(&BreakoutSGP30_set_humidity_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(BreakoutSGP30_locals_dict, BreakoutSGP30_locals_dict_table);
|
||||
|
||||
/***** Class Definition *****/
|
||||
const mp_obj_type_t breakout_sgp30_BreakoutSGP30_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_breakout_matrix11x7,
|
||||
.print = BreakoutSGP30_print,
|
||||
.make_new = BreakoutSGP30_make_new,
|
||||
.locals_dict = (mp_obj_dict_t*)&BreakoutSGP30_locals_dict,
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// breakout_sgp30 Module
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/***** Globals Table *****/
|
||||
STATIC const mp_map_elem_t breakout_sgp30_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_breakout_sgp30) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_BreakoutSGP30), (mp_obj_t)&breakout_sgp30_BreakoutSGP30_type },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_breakout_sgp30_globals, breakout_sgp30_globals_table);
|
||||
|
||||
/***** Module Definition *****/
|
||||
const mp_obj_module_t breakout_sgp30_user_cmodule = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mp_module_breakout_sgp30_globals,
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
MP_REGISTER_MODULE(MP_QSTR_breakout_sgp30, breakout_sgp30_user_cmodule, MODULE_BREAKOUT_SGP30_ENABLED);
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
@ -0,0 +1,206 @@
|
|||
#include "../../../libraries/breakout_sgp30/breakout_sgp30.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_sgp30.h"
|
||||
|
||||
/***** Variables Struct *****/
|
||||
typedef struct _breakout_sgp30_BreakoutSGP30_obj_t {
|
||||
mp_obj_base_t base;
|
||||
BreakoutSGP30 *breakout;
|
||||
} breakout_sgp30_BreakoutSGP30_obj_t;
|
||||
|
||||
/***** Print *****/
|
||||
void BreakoutSGP30_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
(void)kind; //Unused input parameter
|
||||
breakout_sgp30_BreakoutSGP30_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_sgp30_BreakoutSGP30_obj_t);
|
||||
BreakoutSGP30* breakout = self->breakout;
|
||||
mp_print_str(print, "BreakoutSGP30(");
|
||||
|
||||
mp_print_str(print, "i2c = ");
|
||||
mp_obj_print_helper(print, mp_obj_new_int((breakout->get_i2c() == i2c0) ? 0 : 1), PRINT_REPR);
|
||||
|
||||
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, ")");
|
||||
}
|
||||
|
||||
/***** Constructor *****/
|
||||
mp_obj_t BreakoutSGP30_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
||||
breakout_sgp30_BreakoutSGP30_obj_t *self = nullptr;
|
||||
|
||||
if(n_args == 0) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 0, true);
|
||||
self = m_new_obj(breakout_sgp30_BreakoutSGP30_obj_t);
|
||||
self->base.type = &breakout_sgp30_BreakoutSGP30_type;
|
||||
self->breakout = new BreakoutSGP30();
|
||||
}
|
||||
else {
|
||||
enum { ARG_i2c, ARG_sda, ARG_scl };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// 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.
|
||||
int i2c_id = args[ARG_i2c].u_int;
|
||||
if(i2c_id < 0 || i2c_id > 1) {
|
||||
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("I2C(%d) doesn't exist"), i2c_id);
|
||||
}
|
||||
|
||||
int sda = args[ARG_sda].u_int;
|
||||
if (!IS_VALID_SDA(i2c_id, sda)) {
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("bad SDA pin"));
|
||||
}
|
||||
|
||||
int scl = args[ARG_scl].u_int;
|
||||
if (!IS_VALID_SCL(i2c_id, scl)) {
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("bad SCL pin"));
|
||||
}
|
||||
|
||||
self = m_new_obj(breakout_sgp30_BreakoutSGP30_obj_t);
|
||||
self->base.type = &breakout_sgp30_BreakoutSGP30_type;
|
||||
|
||||
i2c_inst_t *i2c = (i2c_id == 0) ? i2c0 : i2c1;
|
||||
self->breakout = new BreakoutSGP30(i2c, sda, scl);
|
||||
}
|
||||
|
||||
self->breakout->init();
|
||||
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
/***** Methods *****/
|
||||
mp_obj_t BreakoutSGP30_retrieve_unique_id(mp_obj_t self_in) {
|
||||
breakout_sgp30_BreakoutSGP30_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_sgp30_BreakoutSGP30_obj_t);
|
||||
return mp_obj_new_bool(self->breakout->retrieve_unique_id());
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutSGP30_get_unique_id(mp_obj_t self_in) {
|
||||
breakout_sgp30_BreakoutSGP30_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_sgp30_BreakoutSGP30_obj_t);
|
||||
uint64_t id = self->breakout->get_unique_id();
|
||||
|
||||
mp_obj_t tuple[3];
|
||||
tuple[0] = mp_obj_new_int((id >> 32) & 0xffff);
|
||||
tuple[1] = mp_obj_new_int((id >> 16) & 0xffff);
|
||||
tuple[2] = mp_obj_new_int(id & 0xffff);
|
||||
return mp_obj_new_tuple(3, tuple);
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutSGP30_start_measurement(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_wait_for_setup };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_wait_for_setup, 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_sgp30_BreakoutSGP30_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_sgp30_BreakoutSGP30_obj_t);
|
||||
self->breakout->start_measurement(args[ARG_wait_for_setup].u_bool);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutSGP30_get_air_quality(mp_obj_t self_in) {
|
||||
breakout_sgp30_BreakoutSGP30_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_sgp30_BreakoutSGP30_obj_t);
|
||||
|
||||
uint16_t eCO2 = 0, TVOC = 0;
|
||||
if(self->breakout->get_air_quality(&eCO2, &TVOC)) {
|
||||
mp_obj_t tuple[2];
|
||||
tuple[0] = mp_obj_new_int(eCO2);
|
||||
tuple[1] = mp_obj_new_int(TVOC);
|
||||
return mp_obj_new_tuple(2, tuple);
|
||||
}
|
||||
else
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutSGP30_get_air_quality_raw(mp_obj_t self_in) {
|
||||
breakout_sgp30_BreakoutSGP30_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_sgp30_BreakoutSGP30_obj_t);
|
||||
|
||||
uint16_t rawH2 = 0, rawEthanol = 0;
|
||||
if(self->breakout->get_air_quality_raw(&rawH2, &rawEthanol)) {
|
||||
mp_obj_t tuple[2];
|
||||
tuple[0] = mp_obj_new_int(rawH2);
|
||||
tuple[1] = mp_obj_new_int(rawEthanol);
|
||||
return mp_obj_new_tuple(2, tuple);
|
||||
}
|
||||
else
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutSGP30_soft_reset(mp_obj_t self_in) {
|
||||
breakout_sgp30_BreakoutSGP30_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_sgp30_BreakoutSGP30_obj_t);
|
||||
self->breakout->soft_reset();
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutSGP30_get_baseline(mp_obj_t self_in) {
|
||||
breakout_sgp30_BreakoutSGP30_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_sgp30_BreakoutSGP30_obj_t);
|
||||
|
||||
uint16_t eCO2 = 0, TVOC = 0;
|
||||
if(self->breakout->get_baseline(&eCO2, &TVOC)) {
|
||||
mp_obj_t tuple[2];
|
||||
tuple[0] = mp_obj_new_int(eCO2);
|
||||
tuple[1] = mp_obj_new_int(TVOC);
|
||||
return mp_obj_new_tuple(2, tuple);
|
||||
}
|
||||
else
|
||||
return mp_const_none;
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutSGP30_set_baseline(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_eco2, ARG_tvoc };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_eco2, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_tvoc, 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_sgp30_BreakoutSGP30_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_sgp30_BreakoutSGP30_obj_t);
|
||||
self->breakout->set_baseline(args[ARG_eco2].u_int, args[ARG_tvoc].u_int);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t BreakoutSGP30_set_humidity(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_absolute_humidity };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_absolute_humidity, 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_sgp30_BreakoutSGP30_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_sgp30_BreakoutSGP30_obj_t);
|
||||
self->breakout->set_humidity(args[ARG_absolute_humidity].u_int);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
// Include MicroPython API.
|
||||
#include "py/runtime.h"
|
||||
|
||||
/***** Extern of Class Definition *****/
|
||||
extern const mp_obj_type_t breakout_sgp30_BreakoutSGP30_type;
|
||||
|
||||
/***** Extern of Class Methods *****/
|
||||
extern void BreakoutSGP30_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
|
||||
extern mp_obj_t BreakoutSGP30_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 BreakoutSGP30_retrieve_unique_id(mp_obj_t self_in);
|
||||
extern mp_obj_t BreakoutSGP30_get_unique_id(mp_obj_t self_in);
|
||||
extern mp_obj_t BreakoutSGP30_start_measurement(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t BreakoutSGP30_get_air_quality(mp_obj_t self_in);
|
||||
extern mp_obj_t BreakoutSGP30_get_air_quality_raw(mp_obj_t self_in);
|
||||
extern mp_obj_t BreakoutSGP30_soft_reset(mp_obj_t self_in);
|
||||
extern mp_obj_t BreakoutSGP30_get_baseline(mp_obj_t self_in);
|
||||
extern mp_obj_t BreakoutSGP30_set_baseline(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t BreakoutSGP30_set_humidity(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
|
@ -0,0 +1,20 @@
|
|||
set(MOD_NAME breakout_sgp30)
|
||||
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}/../../../libraries/${MOD_NAME}/${MOD_NAME}.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../../drivers/sgp30/sgp30.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})
|
|
@ -0,0 +1,13 @@
|
|||
set(MOD_NAME breakout_sgp30)
|
||||
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++
|
|
@ -4,6 +4,8 @@ include(${CMAKE_CURRENT_LIST_DIR}/breakout_roundlcd/micropython.cmake)
|
|||
include(${CMAKE_CURRENT_LIST_DIR}/breakout_rgbmatrix5x5/micropython.cmake)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/breakout_matrix11x7/micropython.cmake)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/breakout_trackball/micropython.cmake)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/breakout_sgp30/micropython.cmake)
|
||||
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/pico_scroll/micropython.cmake)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/pico_rgb_keypad/micropython.cmake)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/pico_unicorn/micropython.cmake)
|
||||
|
|
Ładowanie…
Reference in New Issue