[M] Fix Sensors

This commit is contained in:
choelzl 2022-05-28 15:06:59 +02:00
parent f05b6492b0
commit 58ba94a455
Signed by: sora
GPG Key ID: A362EA0491E2EEA0
39 changed files with 14135 additions and 14103 deletions

View File

@ -27,6 +27,10 @@
#define BUTTON_PIN_BITMASK 0x8000 #define BUTTON_PIN_BITMASK 0x8000
void loop_DRAW(void * pvParameters );
void loop_TOUCH(void * pvParameters );
void loop_MEASURE(void * pvParameters );
/* Global Variables -----------------------------------------------------------*/ /* Global Variables -----------------------------------------------------------*/
static WiFiMulti wifiMulti; static WiFiMulti wifiMulti;
static TwoWire I2C = TwoWire(0); static TwoWire I2C = TwoWire(0);
@ -75,9 +79,9 @@ void IRAM_ATTR onSecondTimer() {
} }
view_update = true; view_update = true;
time_update = true; time_update = true;
tf->minuteTick();
} }
tf->tick();
} }
void IRAM_ATTR onTouch(int8_t contacts, GTPoint *points) { void IRAM_ATTR onTouch(int8_t contacts, GTPoint *points) {
if(touch->dev.holding) return; if(touch->dev.holding) return;
@ -109,18 +113,18 @@ void setup_TIMER(){
#define REGISTER_TIMEDFUN(name,code) static void name (){code;} #define REGISTER_TIMEDFUN(name,code) static void name (){code;}
#define REGISTER_TIMEDFUN_CALL(name,min) tf->registerFun(name,min) #define REGISTER_TIMEDFUN_CALL(name,min) tf->registerFun(name,min)
REGISTER_TIMEDFUN(SENSOR_MEASURE_TF, sensor->measure()); REGISTER_TIMEDFUN(SENSOR_MEASURE_TF, do{sensor->measure();view_update = true;}while(0));
REGISTER_TIMEDFUN(OWM_MEASURE_TF, do{uint8_t i = 0;while(owm && owm[i]){owm[i]->update(); ++i;}}while(0)); REGISTER_TIMEDFUN(OWM_MEASURE_TF, do{uint8_t i = 0;while(owm && owm[i]){owm[i]->update(); ++i;}}while(0));
REGISTER_TIMEDFUN(RECORD_LOCAL_TF, iflx->record_local()); REGISTER_TIMEDFUN(RECORD_LOCAL_TF, iflx->record_local());
REGISTER_TIMEDFUN(RECORD_WEATHER_TF, iflx->record_weather()); REGISTER_TIMEDFUN(RECORD_WEATHER_TF, iflx->record_weather());
REGISTER_TIMEDFUN(NTP_TF, ntp->updateNTPtime()); REGISTER_TIMEDFUN(NTP_TF, ntp->updateNTPtime());
void setup_TIMEDFUN() { void setup_TIMEDFUN() {
REGISTER_TIMEDFUN_CALL(NTP_TF, 60); REGISTER_TIMEDFUN_CALL(NTP_TF, 60*60);
REGISTER_TIMEDFUN_CALL(SENSOR_MEASURE_TF,5); REGISTER_TIMEDFUN_CALL(SENSOR_MEASURE_TF,5);
REGISTER_TIMEDFUN_CALL(OWM_MEASURE_TF,5); REGISTER_TIMEDFUN_CALL(OWM_MEASURE_TF,5*60);
REGISTER_TIMEDFUN_CALL(RECORD_LOCAL_TF,5); // REGISTER_TIMEDFUN_CALL(RECORD_LOCAL_TF,1*60);
REGISTER_TIMEDFUN_CALL(RECORD_WEATHER_TF,5); // REGISTER_TIMEDFUN_CALL(RECORD_WEATHER_TF,5*60);
} }
void setup_POWER() { void setup_POWER() {
@ -140,11 +144,12 @@ void setup(){
setup_TOUCH(); setup_TOUCH();
setup_TIMER(); setup_TIMER();
setup_TIMEDFUN(); setup_TIMEDFUN();
sensor->init();
iflx->check(); iflx->check();
Display::setViews(views,views_size); Display::setViews(views,views_size);
tf->updateForce(); tf->updateForce();
tf->setTick(ntp->dt.minute); tf->setTick(ntp->dt.minute*60 + ntp->dt.second);
xTaskCreatePinnedToCore( xTaskCreatePinnedToCore(
loop_MEASURE, "Task LMeasure", loop_MEASURE, "Task LMeasure",
@ -189,8 +194,7 @@ void loop_TOUCH(void * pvParameters ){
void loop_MEASURE(void * pvParameters ){ void loop_MEASURE(void * pvParameters ){
for(;;){ for(;;){
tf->update(); tf->update();
vTaskDelay(500);
vTaskDelay(200);
} }
} }

View File

@ -62,8 +62,14 @@ void Display::setViews(DisplayWrapper** view_tab, int8_t size){
views = view_tab; views = view_tab;
}; };
void Display::nextView(){ view_curr++; view_curr = view_curr%view_count;} void Display::nextView() {
void Display::prevView(){ view_curr--; view_curr = (view_curr+view_count) % view_count;} view_curr++;
view_curr = view_curr%view_count;
}
void Display::prevView() {
view_curr--;
view_curr = (view_curr+view_count) % view_count;
}

View File

@ -168,7 +168,7 @@ unsigned long NTPtime::adjustTimeZone(unsigned long _timeStamp, float _timeZone,
} }
strDateTime NTPtime::getNTPtime(float _timeZone, boolean _DayLightSaving) { strDateTime NTPtime::getNTPtime(float _timeZone, byte _DayLightSaving) {
Serial.println("NTP: Updating Time"); Serial.println("NTP: Updating Time");
int cb; int cb;
@ -234,6 +234,6 @@ strDateTime NTPtime::getNTPtime(float _timeZone, boolean _DayLightSaving) {
void NTPtime::updateNTPtime() { void NTPtime::updateNTPtime() {
strDateTime ndt = getNTPtime(1.0f, true); strDateTime ndt = getNTPtime(1.0f, 1);
if(ndt.valid) dt = ndt; if(ndt.valid) dt = ndt;
}; };

View File

@ -20,7 +20,7 @@ class NTPtime {
NTPtime(String NTPtime); NTPtime(String NTPtime);
strDateTime dt = {0}; strDateTime dt = {0};
strDateTime getNTPtime(float _timeZone, boolean _DayLightSaving); strDateTime getNTPtime(float _timeZone, byte _DayLightSaving);
void updateNTPtime(); void updateNTPtime();

View File

@ -88,15 +88,6 @@ bool BME280::ReadTrim(){
success &= ReadRegister(HUM_DIG_ADDR2, &m_dig[ord], HUM_DIG_ADDR2_LENGTH); success &= ReadRegister(HUM_DIG_ADDR2, &m_dig[ord], HUM_DIG_ADDR2_LENGTH);
ord += HUM_DIG_ADDR2_LENGTH; ord += HUM_DIG_ADDR2_LENGTH;
#ifdef DEBUG_ON
Serial.print("Dig: ");
for(int i = 0; i < 32; ++i){
Serial.print(m_dig[i], HEX);
Serial.print(" ");
}
Serial.println();
#endif
return success && ord == DIG_LENGTH; return success && ord == DIG_LENGTH;
} }
@ -182,7 +173,9 @@ float BME280::CalculatePressure(int32_t raw,int32_t t_fine,PresUnit unit){
var2 = var2 + (((int64_t)dig_P4) << 35); var2 = var2 + (((int64_t)dig_P4) << 35);
var1 = ((var1 * var1 * (int64_t)dig_P3) >> 8) + ((var1 * (int64_t)dig_P2) << 12); var1 = ((var1 * var1 * (int64_t)dig_P3) >> 8) + ((var1 * (int64_t)dig_P2) << 12);
var1 = (((((int64_t)1) << 47) + var1)) * ((int64_t)dig_P1) >> 33; var1 = (((((int64_t)1) << 47) + var1)) * ((int64_t)dig_P1) >> 33;
if (var1 == 0) { return NAN; } // Don't divide by zero. if (var1 == 0) {
return NAN; // Don't divide by zero.
}
pressure = 1048576 - raw; pressure = 1048576 - raw;
pressure = (((pressure << 31) - var2) * 3125)/var1; pressure = (((pressure << 31) - var2) * 3125)/var1;
var1 = (((int64_t)dig_P9) * (pressure >> 13) * (pressure >> 13)) >> 25; var1 = (((int64_t)dig_P9) * (pressure >> 13) * (pressure >> 13)) >> 25;
@ -213,7 +206,9 @@ float BME280::temp(){
if(!m_initialized) return 0; if(!m_initialized) return 0;
int32_t data[8]; int32_t data[8];
int32_t t_fine; int32_t t_fine;
if(!ReadData(data)){ return NAN; } if(!ReadData(data)) {
return NAN;
}
uint32_t rawTemp = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); uint32_t rawTemp = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4);
return CalculateTemperature(rawTemp, t_fine); return CalculateTemperature(rawTemp, t_fine);
} }
@ -223,7 +218,9 @@ float BME280::pres(PresUnit unit){
if(!m_initialized) return 0; if(!m_initialized) return 0;
int32_t data[8]; int32_t data[8];
int32_t t_fine; int32_t t_fine;
if(!ReadData(data)){ return NAN; } if(!ReadData(data)) {
return NAN;
}
uint32_t rawTemp = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); uint32_t rawTemp = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4);
uint32_t rawPressure = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4); uint32_t rawPressure = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4);
CalculateTemperature(rawTemp, t_fine); CalculateTemperature(rawTemp, t_fine);
@ -235,7 +232,9 @@ float BME280::hum(){
if(!m_initialized) return 0; if(!m_initialized) return 0;
int32_t data[8]; int32_t data[8];
int32_t t_fine; int32_t t_fine;
if(!ReadData(data)){ return NAN; } if(!ReadData(data)) {
return NAN;
}
uint32_t rawTemp = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); uint32_t rawTemp = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4);
uint32_t rawHumidity = (data[6] << 8) | data[7]; uint32_t rawHumidity = (data[6] << 8) | data[7];
CalculateTemperature(rawTemp, t_fine); CalculateTemperature(rawTemp, t_fine);

View File

@ -45,13 +45,13 @@ bool LTR390::ReadChipID(){
/****************************************************************/ /****************************************************************/
void LTR390::WriteSettings() { void LTR390::WriteSettings() {
uint8_t ctrlMeas = LTR390_RESOLUTION_16BIT | LTR390_RATE_1000; WriteRegister(MAIN_CTRL, LTR390_ON | LTR390_MODE_UVS);
uint8_t ctrlGain = LTR390_GAIN_3; WriteRegister(MEAS_RATE, LTR390_RESOLUTION_16BIT | LTR390_RATE_1000);
uint8_t ctrlMode = LTR390_ON | LTR390_MODE_UVS; WriteRegister(GAIN, LTR390_GAIN_18);
WriteRegister(MEAS_RATE, ctrlMeas); // WriteRegister(THRESH_LOW, 100);
WriteRegister(GAIN, ctrlGain); // WriteRegister(THRESH_UP, 1000);
WriteRegister(MAIN_CTRL, ctrlMode); // WriteRegister(INT_CFG, 0x30 | 0x04);
} }
@ -89,8 +89,11 @@ bool LTR390::ReadData(int32_t data[SENSOR_DATA_LENGTH], uint32_t addr){
/****************************************************************/ /****************************************************************/
uint32_t LTR390::uv() { uint32_t LTR390::uv() {
if(!m_initialized) return 0; if(!m_initialized) return 0;
int32_t data[SENSOR_DATA_LENGTH]; int32_t data[SENSOR_DATA_LENGTH];
if(!ReadData(data, UVSDATA)){ return 0; } if(!ReadData(data, UVSDATA)) {
return 0;
}
uint32_t rawUV = (data[2] << 16) | (data[1] << 8) | (data[0]); uint32_t rawUV = (data[2] << 16) | (data[1] << 8) | (data[0]);
return rawUV; return rawUV;
} }
@ -98,7 +101,7 @@ uint32_t LTR390::uv(){
void LTR390::read(uint32_t& uvs, uint32_t& uvi) { void LTR390::read(uint32_t& uvs, uint32_t& uvi) {
if(!m_initialized) return; if(!m_initialized) return;
uvs = uv(); uvs = uv();
uvi = (uvs/ (3*0.25))*3; uvi = (uvs/ 2300)*2;
} }

View File

@ -24,7 +24,8 @@ bool PMSA003::ReadChipID(){
uint8_t id = 0; uint8_t id = 0;
ReadRegister(REG_VERSION, &id, 1); ReadRegister(REG_VERSION, &id, 1);
Serial.print("PMSSA DEV: ");Serial.println(id); Serial.print("PMSSA DEV: ");
Serial.println(id);
// switch(id){ // switch(id){
// case ChipModel_PMSA003_REV0: // case ChipModel_PMSA003_REV0:
// m_chip_model = ChipModel_PMSA003_REV0; // m_chip_model = ChipModel_PMSA003_REV0;

View File

@ -1,4 +1,5 @@
#include "sgp40.h" #include "sgp40.h"
#include <Arduino.h>
#define SENSOR_DATA_LENGTH 3 #define SENSOR_DATA_LENGTH 3
@ -11,7 +12,6 @@ bool SGP40::Initialize(){
VocAlgorithm_init(&vocAlgorithmParameters); VocAlgorithm_init(&vocAlgorithmParameters);
if(success) success = WriteSettings(); if(success) success = WriteSettings();
m_initialized = success; m_initialized = success;
return m_initialized; return m_initialized;
} }
@ -26,7 +26,7 @@ bool SGP40::ReadChipID(){
switch(id) { switch(id) {
default: default:
m_chip_model = ChipModel_UNKNOWN; m_chip_model = ChipModel_UNKNOWN;
return false; //return false;
} }
return true; return true;
@ -112,6 +112,7 @@ bool SGP40::ReadData(uint8_t data[SENSOR_DATA_LENGTH], float RH, float T){
int32_t SGP40::voc(float RH, float T) { int32_t SGP40::voc(float RH, float T) {
if(!m_initialized) return 0; if(!m_initialized) return 0;
uint8_t data[SENSOR_DATA_LENGTH]; uint8_t data[SENSOR_DATA_LENGTH];
ReadData(data, RH, T); ReadData(data, RH, T);
uint16_t results = (data[0]<<8) | data[1]; uint16_t results = (data[0]<<8) | data[1];
int32_t voci = 0; int32_t voci = 0;
@ -126,7 +127,6 @@ void SGP40::read(int32_t& voc, float RH, float T){
ReadData(data, RH, T); ReadData(data, RH, T);
uint16_t results = (data[0]<<8) | data[1]; uint16_t results = (data[0]<<8) | data[1];
VocAlgorithm_process(&vocAlgorithmParameters, results, &voc); VocAlgorithm_process(&vocAlgorithmParameters, results, &voc);
return;
} }
/****************************************************************/ /****************************************************************/

View File

@ -1,7 +1,6 @@
#ifndef TG_SGP40_H #ifndef TG_SGP40_H
#define TG_SGP40_H #define TG_SGP40_H
#include <Arduino.h>
#include <Wire.h> #include <Wire.h>
#include "../voc.h" #include "../voc.h"

View File

@ -1,5 +1,6 @@
#include "tsl25911.h" #include "tsl25911.h"
#include <Arduino.h>
#define SENSOR_DATA_LENGTH 4 #define SENSOR_DATA_LENGTH 4
@ -40,6 +41,7 @@ bool TSL25911::ReadChipID(){
switch(id) { switch(id) {
case ChipModel_TSL25911_REV0: case ChipModel_TSL25911_REV0:
m_chip_model = ChipModel_TSL25911_REV0; m_chip_model = ChipModel_TSL25911_REV0;
break;
default: default:
m_chip_model = ChipModel_UNKNOWN; m_chip_model = ChipModel_UNKNOWN;
return false; return false;
@ -51,9 +53,7 @@ bool TSL25911::ReadChipID(){
/****************************************************************/ /****************************************************************/
void TSL25911::WriteSettings() { void TSL25911::WriteSettings() {
WriteRegister(REG_ENABLE, ENABLE_POWERON | ENABLE_AEN); WriteRegister(REG_ENABLE, ENABLE_POWERON | ENABLE_AEN);
WriteRegister(REG_CTRL, integration | gain); WriteRegister(REG_CTRL, integration | gain);
WriteRegister(REG_ENABLE,ENABLE_POWEROFF); WriteRegister(REG_ENABLE,ENABLE_POWEROFF);
} }
@ -73,16 +73,14 @@ bool TSL25911::reset(){
} }
/****************************************************************/ /****************************************************************/
bool TSL25911::ReadData(uint32_t data[SENSOR_DATA_LENGTH]){ bool TSL25911::ReadData(uint32_t* dataf) {
bool success; uint8_t* data = static_cast<uint8_t*>(static_cast<void*>(dataf));
success &= WriteRegister(REG_ENABLE, ENABLE_POWERON | ENABLE_AEN); bool success = WriteRegister(REG_ENABLE, ENABLE_POWERON | ENABLE_AEN);
delay((uint32_t)atime());
for (uint8_t d = 0; d <= integration; d++) // Wait x ms for ADC to complete success &= ReadRegister(CHAN0_LOW,&data[0],2);
delay(120); success &= ReadRegister(CHAN1_LOW,&data[2],2);
success &= ReadRegister(CHAN0_LOW,static_cast<uint8_t*>(static_cast<void*>(&data[0])),2);
success &= ReadRegister(CHAN1_LOW,static_cast<uint8_t*>(static_cast<void*>(&data[2])),2);
success &= WriteRegister(REG_ENABLE,ENABLE_POWEROFF); success &= WriteRegister(REG_ENABLE,ENABLE_POWEROFF);
@ -140,13 +138,12 @@ float TSL25911::lumLux(){
void TSL25911::read(uint16_t& v, uint16_t& ir, uint16_t& f, float& lux) { void TSL25911::read(uint16_t& v, uint16_t& ir, uint16_t& f, float& lux) {
if(!m_initialized) return; if(!m_initialized) return;
uint32_t data; uint32_t data;
if(ReadData(&data)){ if(!ReadData(&data)) return;
v = ((data & 0xFFFF) - (data >> 16)); v = ((data & 0xFFFF) - (data >> 16));
ir = (data >> 16); ir = (data >> 16);
f = (data & 0xFFFF); f = (data & 0xFFFF);
lux = ComputeLux(data&0xFFFF,data>>16,again(),atime()); lux = ComputeLux(data&0xFFFF,data>>16,again(),atime());
} }
}
/****************************************************************/ /****************************************************************/

View File

@ -12,12 +12,22 @@ Sensor::Sensor(TwoWire* i2c){
pms = new PMSA003(i2c); pms = new PMSA003(i2c);
} }
void Sensor::TSL_init(void){tsl->begin();} void Sensor::TSL_init(void) {
void Sensor::BME_init(void){bme->begin();} tsl->begin();
}
void Sensor::BME_init(void) {
bme->begin();
}
void Sensor::ICM_init(void) {} //Nothing to do afaik void Sensor::ICM_init(void) {} //Nothing to do afaik
void Sensor::LTR_init(void){ltr->begin();} void Sensor::LTR_init(void) {
void Sensor::SGP_init(void){sgp->begin();} ltr->begin();
void Sensor::PMS_init(void){pms->begin();} }
void Sensor::SGP_init(void) {
sgp->begin();
}
void Sensor::PMS_init(void) {
pms->begin();
}
void Sensor::BME_measure() { void Sensor::BME_measure() {

View File

@ -50,13 +50,22 @@ class Sensor
void SGP_measure(void); void SGP_measure(void);
void PMS_measure(void); void PMS_measure(void);
void init() {
BME_init();
LTR_init();
TSL_init();
SGP_init();
PMS_init();
ICM_init();
}
void measure() { void measure() {
TSL_measure();
BME_measure(); BME_measure();
ICM_measure();
LTR_measure(); LTR_measure();
TSL_measure();
SGP_measure(); SGP_measure();
PMS_measure(); PMS_measure();
ICM_measure();
} }
private: private:

View File

@ -251,9 +251,11 @@ static fix16_t fix16_exp(fix16_t x) {
// exp(x) for x = +/- {1, 1/8, 1/64, 1/512} // exp(x) for x = +/- {1, 1/8, 1/64, 1/512}
#define NUM_EXP_VALUES 4 #define NUM_EXP_VALUES 4
static const fix16_t exp_pos_values[NUM_EXP_VALUES] = { static const fix16_t exp_pos_values[NUM_EXP_VALUES] = {
F16(2.7182818), F16(1.1331485), F16(1.0157477), F16(1.0019550)}; F16(2.7182818), F16(1.1331485), F16(1.0157477), F16(1.0019550)
};
static const fix16_t exp_neg_values[NUM_EXP_VALUES] = { static const fix16_t exp_neg_values[NUM_EXP_VALUES] = {
F16(0.3678794), F16(0.8824969), F16(0.9844964), F16(0.9980488)}; F16(0.3678794), F16(0.8824969), F16(0.9844964), F16(0.9980488)
};
const fix16_t* exp_values; const fix16_t* exp_values;
fix16_t res, arg; fix16_t res, arg;

View File

@ -21,11 +21,11 @@ class TimedFun {
funmap.push_back(std::pair<timedfun_ptr,uint64_t>(p,s)); funmap.push_back(std::pair<timedfun_ptr,uint64_t>(p,s));
}; };
void minuteTick(){ void tick() {
++counter; ++counter;
} }
void minuteTick(int64_t m){ void tick(int64_t m) {
counter += m; counter += m;
} }

View File

@ -8,7 +8,9 @@ void IRAM_ATTR _gt_irq_handler() {
interrupts(); interrupts();
} }
GT1151::GT1151(TwoWire* i2c){ wire = i2c;} GT1151::GT1151(TwoWire* i2c) {
wire = i2c;
}
void GT1151::setHandler(void (*handler)(int8_t, GTPoint*)) { void GT1151::setHandler(void (*handler)(int8_t, GTPoint*)) {
touchHandler = handler; touchHandler = handler;