Skip to content

Commit 89561ee

Browse files
committed
bug fixes
1 parent 18a9c35 commit 89561ee

24 files changed

Lines changed: 202 additions & 30 deletions

HomeController/BME280Controller.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88
#include <Adafruit_Sensor.h>
99
#include <Adafruit_BME280.h>
1010

11+
1112
//REGISTER_CONTROLLER(BME280Controller)
1213
#ifndef ESP8266
1314
REGISTER_CONTROLLER_FACTORY(BME280Controller)
1415
#endif
1516

1617
//BME280ControllerFactory* ff = new BME280ControllerFactory();
1718

19+
String GetStateStringForHistory(BMEState state) {
20+
21+
return "";
22+
}
23+
1824
const size_t bufferSize = JSON_OBJECT_SIZE(20);
1925

2026
BME280Controller::BME280Controller() {
@@ -34,6 +40,8 @@ BME280Controller::BME280Controller() {
3440
this->hap_hum=NULL;
3541
this->hap_press=NULL;
3642
#endif
43+
this->usehistory = false;
44+
this->pHistory = NULL;
3745
}
3846
String BME280Controller::serializestate() {
3947

@@ -75,6 +83,8 @@ void BME280Controller::loadconfig(JsonObject& json) {
7583
uselegacy = json["uselegacy"];
7684
loadif(temp_corr, json, "temp_corr");
7785
loadif(hum_corr, json, "hum_corr");
86+
loadif(usehistory, json, "usehistory");
87+
7888
}
7989
void BME280Controller::getdefaultconfig(JsonObject& json) {
8090
json[FPSTR(szi2caddr)]= i2caddr;
@@ -91,6 +101,11 @@ void BME280Controller::getdefaultconfig(JsonObject& json) {
91101
#define BME_CS 14
92102
void BME280Controller::setup() {
93103
DBG_OUTPUT_PORT.println("BME280Controller setup");
104+
if (usehistory) {
105+
pHistory = new BME280History();
106+
pHistory->SetSize(MAX_HISTORY_RECORDS);
107+
pHistory->SetGetStringFn(GetStateStringForHistory);
108+
}
94109
if (this->uselegacy) {
95110
DBG_OUTPUT_PORT.println("Init Adafruit_BME280");
96111
this->pbme = new Adafruit_BME280();

HomeController/BME280Controller.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
#include <Arduino.h>
44
#include <ArduinoJson.h>
55
#include "BaseController.h"
6+
#include "BMEHistory.h"
7+
8+
#define PERIOD_HISTORY_MS 36000
9+
#define MAX_HISTORY_RECORDS 20
10+
611
#ifdef ENABLE_NATIVE_HAP
712
extern "C"{
813
#include "homeintegration.h"
@@ -16,6 +21,7 @@ struct BMEState
1621
double temp=0.0;
1722
double hum = 0.0;
1823
double pres = 0.0;
24+
long gtime = 0;
1925
};
2026
enum BMECMD {
2127
BMEOn = 1,
@@ -25,6 +31,7 @@ enum BMECMD {
2531
BMERestore = 2048
2632
};
2733
class BME280Controller;
34+
typedef CBMEHistory<BMEState> BME280History;
2835
typedef CController<BME280Controller, BMEState, BMECMD> BME;
2936
class BME280Controller : public BME
3037
{
@@ -52,6 +59,7 @@ class BME280Controller : public BME
5259
void write8(byte reg, byte value);
5360
uint8_t i2caddr;
5461
bool uselegacy;
62+
bool usehistory;
5563
bool isinit;
5664
double temp_corr;
5765
double hum_corr;
@@ -66,6 +74,8 @@ class BME280Controller : public BME
6674
homekit_service_t* hapservice_press;
6775
homekit_characteristic_t * hap_press;
6876
#endif
77+
BME280History* pHistory;
78+
6979
};
7080
DEFINE_CONTROLLER_FACTORY(BME280Controller)
7181

HomeController/BME680Controller.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ BME680Controller::BME680Controller() {
2929
this->hum_corr = 0.0;
3030
this->gas_lower_limit = 10000; // Bad air quality limit
3131
this->gas_upper_limit = 300000; // Good air quality limit
32+
this->coreMode = Core;
33+
this->core = 0;
3234
#ifdef ENABLE_NATIVE_HAP
3335
this->ishap=true;
3436
this->hapservice_temp=NULL;
@@ -171,7 +173,10 @@ void BME680Controller::run() {
171173
command newcmd;
172174
newcmd.mode = BME680Measure;
173175
this->meassure(newcmd.state);
176+
174177
#ifdef BME680CONTROLLER_DEBUG
178+
//DBG_OUTPUT_PORT.println("Start delay test");
179+
//delay(30000);
175180
DBG_OUTPUT_PORT.print("BME680Controller->");
176181
DBG_OUTPUT_PORT.print("Temperature in Celsius : ");
177182
DBG_OUTPUT_PORT.print(newcmd.state.temp);
@@ -226,6 +231,12 @@ void BME680Controller::meassure(BME680State& state) {
226231
state.gas_resistance = ReadGasReference();
227232
state.gas = getpsevdoIaqGasScore(state.gas_resistance);
228233
state.last_measure_ms = millis();
234+
#ifdef BME680CONTROLLER_DEBUG
235+
DBG_OUTPUT_PORT.print("gas_resistance");
236+
DBG_OUTPUT_PORT.println(state.gas_resistance);
237+
DBG_OUTPUT_PORT.print("gas");
238+
DBG_OUTPUT_PORT.println(state.gas);
239+
#endif
229240
// DBG_OUTPUT_PORT.println(state.gas_resistance);
230241
// DBG_OUTPUT_PORT.println(state.gas);
231242

@@ -305,9 +316,9 @@ int BME680Controller::getpsevdoIaqGasScore(int val) {
305316
//Calculate gas contribution to IAQ index
306317
const int maxiaq = 500;
307318
if (val > gas_upper_limit)
308-
return maxiaq;
309-
if (val < gas_lower_limit)
310319
return 0;
320+
if (val < gas_lower_limit)
321+
return maxiaq;
311322
int gas_score = maxiaq -( (val - gas_lower_limit) * maxiaq )/ (gas_upper_limit - gas_lower_limit);
312323

313324
return gas_score;

HomeController/BMEHistory.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "BMEHistory.h"
2+
3+
4+

HomeController/BMEHistory.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
#include "Arduino.h"
3+
4+
#include "Array.h"
5+
#include <FS.h>
6+
7+
#if !defined(ESP8266)
8+
#include <SPIFFS.h>
9+
#endif
10+
11+
template<class T>
12+
class CBMEHistory :public CSimpleArray <T>{
13+
public:
14+
typedef void(*WriteElementFN)(Stream* ps,T elem);
15+
typedef String(*GetStringElement)( T elem);
16+
void WriteToStream(Stream* ps) {
17+
for (int i = 0; i < this->GetSize(); i++) {
18+
pfn_write_element(ps,this->GetAt(i));
19+
}
20+
}
21+
bool WriteToFile(String fileName) {
22+
File file = SPIFFS.open(fileName, "w");
23+
if (!file)
24+
return false;
25+
file.print("[");
26+
for (int i = 0; i < this->GetSize(); i++) {
27+
String s = pfn_get_string_element(this->GetAt(i));
28+
file.print(s);
29+
if (i == (this->GetSize() - 1)) {
30+
file.print("\r\n");
31+
}
32+
else {
33+
file.print(",\r\n");
34+
}
35+
36+
}
37+
file.print("]");
38+
file.close();
39+
return true;
40+
}
41+
void SetWriteFn(WriteElementFN fn) { pfn_write_element = fn; };
42+
void SetGetStringFn(GetStringElement fn) { pfn_get_string_element = fn; };
43+
private:
44+
WriteElementFN pfn_write_element;
45+
GetStringElement pfn_get_string_element;
46+
};
47+
48+

HomeController/BaseController.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,11 @@ void runcoreloop(void*param)
376376

377377
if (self != NULL ) // && self->shouldRun())
378378
self->runcore();
379-
if (self->get_coremode() == Core && self->shouldRun()) {
379+
if (self != NULL && self->get_coremode() == Core && self->shouldRun()) {
380380
self->run();
381+
Controllers* pc = Controllers::getInstance();
382+
pc->run_trigger_for(self);
383+
381384
//DBG_OUTPUT_PORT.println("CBaseController::run from core");
382385
}
383386
// https://github.com/espressif/arduino-esp32/issues/595

HomeController/BaseController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ void runcoreloop(void*param);
4646
#endif
4747
#define CONTROLLER_MAX_COMMANDS 99
4848
class CBaseController;
49+
4950
enum ControllerEvent {
5051
SleepStart = 0,
5152
SleepUp = 1

HomeController/CWeatherDisplay.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void WeatherDisplayController::run() {
174174
break;
175175
case WDSetCurrentData:
176176
newState.data = cmd.state.data;
177-
177+
178178
bweather = dispMode == MainWether;
179179
bInfo=bAllClear= dispMode == Info;
180180

@@ -707,7 +707,7 @@ if(disptype==ST7735){
707707
pDisplay->println(format_doublestr("Press %.f",wdata.pressure));
708708
}else{
709709
pDisplay->setTextSize(2);
710-
if (wdata.gas > 0) {
710+
if (wdata.gas_resistance > 0) {
711711
pDisplay->setCursor(180, main_offset_y_wetaher);
712712
pDisplay->setTextColor(COLOR_LIGHTBLUE);
713713
pDisplay->print(format_doublestr("%.f %%", wdata.humidity));

HomeController/CWeatherDisplay.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ struct WeatherData{
7474
double temp=0;
7575
double pressure=0;
7676
double humidity=0;
77-
double gas = 0;
77+
double gas = 0;
78+
uint32_t gas_resistance = 0;
7879

7980

8081
};

HomeController/Controllers.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,16 @@ void Controllers::handleloops() {
526526
}
527527

528528
}
529+
void Controllers::run_trigger_for(CBaseController* ctl) {
530+
for (int j = 0; j < this->triggers.GetSize(); j++) {
531+
Trigger* tr = this->triggers.GetAt(j);
532+
533+
if (strcmp(ctl->get_name(), tr->get_src()) == 0) {
534+
535+
tr->handleloop(ctl, this);
536+
}
537+
}
538+
}
529539
void Controllers::set_monitor_state(uint channel, bool isOn, long mask, uint masklen, uint duration) {
530540

531541
if (pMonitor) {

0 commit comments

Comments
 (0)