mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2026-03-22 01:31:16 +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:
128
I2Csensors/aht.c
128
I2Csensors/aht.c
@@ -20,42 +20,58 @@
|
||||
|
||||
#include "aht.h"
|
||||
#include "i2c.h"
|
||||
|
||||
static uint8_t addr = 0x38;
|
||||
|
||||
static sensor_status_t status = SENS_NOTINIT;
|
||||
#include "sensors_private.h"
|
||||
|
||||
enum{
|
||||
ISAHT10,
|
||||
ISAHT15,
|
||||
ISAHT21b
|
||||
ISAHT1x,
|
||||
ISAHT2x
|
||||
};
|
||||
|
||||
static uint32_t rawH = 0, rawT = 0;
|
||||
#define AHT_CMD_INITIALIZE 0xE1
|
||||
#define AHT_CMD_MEASURE 0xAC
|
||||
#define AHT_CMD_SOFT_RESET 0xBA
|
||||
// status - for AHT21
|
||||
#define AHT_CMD_STATUS 0x71
|
||||
// init command bits:
|
||||
// normal/cycle/command modes (bits 6:5) [non-documented!]:
|
||||
#define AHT_INIT_NORMAL_MODE 0x00
|
||||
#define AHT_INIT_CYCLE_MODE 0x20
|
||||
#define AHT_INIT_CMD_MODE 0x40
|
||||
// run calibration
|
||||
#define AHT_INIT_CAL_ON 0x08
|
||||
// zero byte for INIT/START cmd
|
||||
#define AHT_NOP 0
|
||||
// measurement control [non-documented!]
|
||||
#define AHT_MEAS_CTRL 0x33
|
||||
// status bits
|
||||
#define AHT_STATUS_BUSY 0x80
|
||||
#define AHT_STATUS_NORMAL_MODE 0x00
|
||||
#define AHT_STATUS_CYCLE_MODE 0x20
|
||||
#define AHT_STATUS_CMD_MODE 0x40
|
||||
#define AHT_STATUS_CAL_ON 0x08
|
||||
// status bits for AHT2x (both should be ones, or init again)
|
||||
#define AHT_STATUS_CHK 0x18
|
||||
|
||||
#define AHT_CMD_INITIALIZE 0xE1
|
||||
#define AHT_CMD_MEASURE 0xAC
|
||||
#define AHT_CMD_SOFT_RESET 0xBA
|
||||
// max reset time
|
||||
#define RST_TIME (20e-3)
|
||||
#define RST_TIME (20e-3)
|
||||
// max data waiting time
|
||||
#define DATA_TIME (75e-3)
|
||||
#define DATA_TIME (75e-3)
|
||||
|
||||
static sensor_status_t s_poll(){
|
||||
uint8_t b;
|
||||
if(!i2c_read_raw(&b, 1)) return SENS_ERR;
|
||||
#ifdef EBUG
|
||||
if(b & 0x80) printf("BUSY ");
|
||||
if(b & AHT_STATUS_BUSY) printf("BUSY ");
|
||||
static const char *modes[] = {"NOR", "CYC", "CMD", "CMD"};
|
||||
printf("MODE=%s ", modes[(b >> 6)&3]);
|
||||
printf("%sCALIBRATED\n", b & 8 ? "" : "NOT ");
|
||||
printf("%sCALIBRATED\n", b & AHT_STATUS_CAL_ON ? "" : "NOT ");
|
||||
#endif
|
||||
if(b & 0x80) return SENS_BUSY;
|
||||
if(b & AHT_STATUS_BUSY) return SENS_BUSY;
|
||||
return SENS_RELAX;
|
||||
}
|
||||
|
||||
static int s_init(){
|
||||
status = SENS_NOTINIT;
|
||||
static int s_init(sensor_t *s){
|
||||
s->status = SENS_NOTINIT;
|
||||
if(!i2c_write_reg8(AHT_CMD_SOFT_RESET, 0)){
|
||||
DBG("Can't reset");
|
||||
return FALSE;
|
||||
@@ -67,7 +83,7 @@ static int s_init(){
|
||||
}
|
||||
if(t - t0 > RST_TIME) return SENS_ERR;
|
||||
DBG("Reseted");
|
||||
uint8_t data[3] = {AHT_CMD_INITIALIZE, 0x08, 0};
|
||||
uint8_t data[3] = {AHT_CMD_INITIALIZE, AHT_INIT_CAL_ON, AHT_NOP};
|
||||
if(!i2c_write_raw(data, 3)){
|
||||
DBG("Can't init");
|
||||
return FALSE;
|
||||
@@ -79,13 +95,22 @@ static int s_init(){
|
||||
}
|
||||
if(t - t0 > RST_TIME) return SENS_ERR;
|
||||
DBG("Inited");
|
||||
status = SENS_RELAX;
|
||||
s->status = SENS_RELAX;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static int s_start(){
|
||||
if(status != SENS_RELAX) return FALSE;
|
||||
uint8_t data[3] = {AHT_CMD_MEASURE, 0x33, 0};
|
||||
static int s_start(sensor_t *s){
|
||||
if(s->status != SENS_RELAX) return FALSE;
|
||||
uint8_t data[3] = {AHT_CMD_MEASURE, AHT_MEAS_CTRL, AHT_NOP};
|
||||
// the only difference between AHT1x and AHT2x
|
||||
if(s->private == ISAHT2x){ // check status
|
||||
uint8_t b;
|
||||
if(!i2c_read_reg8(AHT_CMD_STATUS, &b)) return FALSE;
|
||||
if((b & AHT_STATUS_CHK) != AHT_STATUS_CHK){
|
||||
DBG("need init");
|
||||
if(!s->init(s)) return FALSE;
|
||||
}
|
||||
}
|
||||
if(!i2c_write_raw(data, 3)){
|
||||
DBG("Can't start measuring");
|
||||
return FALSE;
|
||||
@@ -94,60 +119,61 @@ static int s_start(){
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static sensor_status_t s_process(){
|
||||
sensor_status_t s = s_poll();
|
||||
if(s != SENS_RELAX) return (status = s);
|
||||
static sensor_status_t s_process(sensor_t *s){
|
||||
sensor_status_t st = s_poll();
|
||||
if(st != SENS_RELAX) return (s->status = st);
|
||||
uint8_t data[6];
|
||||
if(!i2c_read_raw(data, 6)) return (status = SENS_ERR);
|
||||
if(!i2c_read_raw(data, 6)) return (s->status = SENS_ERR);
|
||||
DBG("Got @ %.3f", sl_dtime());
|
||||
rawH = ((uint32_t)data[1] << 12) | ((uint32_t)data[2] << 4) | (data[3] >> 4);
|
||||
rawT = ((uint32_t)(data[3] & 0x0F) << 16) | ((uint32_t)data[4] << 8) | data[5];
|
||||
uint32_t rawH = ((uint32_t)data[1] << 12) | ((uint32_t)data[2] << 4) | (data[3] >> 4);
|
||||
uint32_t rawT = ((uint32_t)(data[3] & 0x0F) << 16) | ((uint32_t)data[4] << 8) | data[5];
|
||||
DBG("rawH=%d, rawT=%d", rawH, rawT);
|
||||
return (status = SENS_RDY);
|
||||
s->data.T = rawT * 200.0 / 1048576.0 - 50.0;
|
||||
s->data.H = rawH * 100.0 / 1048576.0;
|
||||
return (s->status = SENS_RDY);
|
||||
}
|
||||
|
||||
static int s_getdata(sensor_data_t *d){
|
||||
if(!d || status != SENS_RDY) return FALSE;
|
||||
d->T = rawT * 200.0 / 1048576.0 - 50.0;
|
||||
d->H = rawH * 100.0 / 1048576.0;
|
||||
status = SENS_RELAX;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static sensor_props_t s_props(){
|
||||
static sensor_props_t s_props(sensor_t _U_ *s){
|
||||
sensor_props_t p = {.T = 1, .H = 1};
|
||||
return p;
|
||||
}
|
||||
|
||||
static uint8_t address(uint8_t new){
|
||||
if(new) addr = new;
|
||||
return addr;
|
||||
}
|
||||
|
||||
static int s_heater(int _U_ on){
|
||||
static int s_heater(sensor_t _U_ *s, int _U_ on){
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
sensor_t AHT10 = {
|
||||
.name = "AHT10",
|
||||
.private = ISAHT10,
|
||||
.address = address,
|
||||
.private = ISAHT1x,
|
||||
.address = 0x38,
|
||||
.status = SENS_NOTINIT,
|
||||
.init = s_init,
|
||||
.start = s_start,
|
||||
.heater = s_heater,
|
||||
.process = s_process,
|
||||
.properties = s_props,
|
||||
.get_data = s_getdata
|
||||
};
|
||||
|
||||
sensor_t AHT15 = {
|
||||
.name = "AHT15",
|
||||
.private = ISAHT15,
|
||||
.address = address,
|
||||
.private = ISAHT1x,
|
||||
.address = 0x38,
|
||||
.status = SENS_NOTINIT,
|
||||
.init = s_init,
|
||||
.start = s_start,
|
||||
.heater = s_heater,
|
||||
.process = s_process,
|
||||
.properties = s_props,
|
||||
};
|
||||
|
||||
sensor_t AHT21 = {
|
||||
.name = "AHT21",
|
||||
.private = ISAHT2x,
|
||||
.address = 0x38,
|
||||
.status = SENS_NOTINIT,
|
||||
.init = s_init,
|
||||
.start = s_start,
|
||||
.heater = s_heater,
|
||||
.process = s_process,
|
||||
.properties = s_props,
|
||||
.get_data = s_getdata
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user