mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2026-03-21 09:11:03 +03:00
Made refactoring for multi-sensor ability (N same sensors); check AHT21. I cry: I need this pointer in C!!!
This commit is contained in:
@@ -22,11 +22,11 @@
|
||||
#include "aht.h"
|
||||
#include "BMP180.h"
|
||||
#include "i2c.h"
|
||||
#include "sensor.h"
|
||||
#include "sensors_private.h"
|
||||
#include "SI7005.h"
|
||||
|
||||
// NULL-terminated list of all supported sensors
|
||||
static const sensor_t* supported_sensors[] = {&AHT10, &AHT15, &BMP180, &SI7005, NULL};
|
||||
static const sensor_t* supported_sensors[] = {&AHT10, &AHT15, &AHT21, &BMP180, &SI7005, NULL};
|
||||
|
||||
// just two stupid wrappers
|
||||
int sensors_open(const char *dev){
|
||||
@@ -37,9 +37,10 @@ void sensors_close(){
|
||||
}
|
||||
|
||||
// init sensor with optional new address
|
||||
int sensor_init(const sensor_t *s, uint8_t address){
|
||||
int sensor_init(sensor_t *s, uint8_t address){
|
||||
if(!s) return FALSE;
|
||||
address = s->address(address);
|
||||
if(address) s->address = address;
|
||||
else address = s->address; // default
|
||||
if(!i2c_set_slave_address(address)){
|
||||
DBG("Can't set slave address 0x%02x", address);
|
||||
return FALSE;
|
||||
@@ -50,21 +51,32 @@ int sensor_init(const sensor_t *s, uint8_t address){
|
||||
}
|
||||
double t0 = sl_dtime();
|
||||
int result = FALSE;
|
||||
while(sl_dtime() - t0 < I2C_TIMEOUT && !(result = s->init())) usleep(10000);
|
||||
while(sl_dtime() - t0 < I2C_TIMEOUT && !(result = s->init(s))) usleep(10000);
|
||||
return result;
|
||||
}
|
||||
|
||||
// find supported sensor by name
|
||||
const sensor_t* sensor_find(const char *name){
|
||||
// find supported sensor by name and return allocated struct
|
||||
sensor_t *sensor_new(const char *name){
|
||||
if(!name || !*name) return NULL;
|
||||
const sensor_t **p = supported_sensors;
|
||||
while(*p){
|
||||
if(0 == strcmp((*p)->name, name)) return *p;
|
||||
if(0 == strcmp((*p)->name, name)){
|
||||
sensor_t *n = MALLOC(sensor_t, 1);
|
||||
memcpy(n, *p, sizeof(sensor_t));
|
||||
return n;
|
||||
}
|
||||
++p;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void sensor_delete(sensor_t **s){
|
||||
if(!s || !*s) return;
|
||||
if((*s)->privdata) FREE((*s)->privdata);
|
||||
// here could be additional free's
|
||||
FREE((*s));
|
||||
}
|
||||
|
||||
// list all supported sensors
|
||||
void sensors_list(){
|
||||
const sensor_t **p = supported_sensors;
|
||||
@@ -77,10 +89,34 @@ void sensors_list(){
|
||||
}
|
||||
|
||||
// wrapper with timeout
|
||||
int sensor_start(const sensor_t *s){
|
||||
int sensor_start(sensor_t *s){
|
||||
if(!s) return FALSE;
|
||||
double t0 = sl_dtime();
|
||||
int result = FALSE;
|
||||
while(sl_dtime() - t0 < I2C_TIMEOUT && !(result = s->start())) usleep(10000);
|
||||
while(sl_dtime() - t0 < I2C_TIMEOUT && !(result = s->start(s))) usleep(10000);
|
||||
return result;
|
||||
}
|
||||
|
||||
int sensor_getdata(sensor_t *s, sensor_data_t *d){
|
||||
if(!s || !d) return FALSE;
|
||||
if(s->status != SENS_RDY) return FALSE;
|
||||
*d = s->data;
|
||||
s->status = SENS_RELAX;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
sensor_status_t sensor_process(sensor_t *s){
|
||||
if(!s) return FALSE;
|
||||
return s->process(s);
|
||||
}
|
||||
|
||||
sensor_props_t sensor_properties(sensor_t *s){
|
||||
sensor_props_t def = {0};
|
||||
if(!s) return def;
|
||||
return s->properties(s);
|
||||
}
|
||||
|
||||
int sensor_heater(sensor_t *s, int on){
|
||||
if(!s || !s->properties(s).htr || !s->heater) return FALSE;
|
||||
return s->heater(s, on);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user