diff --git a/src/dev/device.h b/src/dev/device.h index f6ed7ae2..708c7981 100644 --- a/src/dev/device.h +++ b/src/dev/device.h @@ -107,6 +107,8 @@ class BaseDevice { } virtual void get_info(JsonDocument& doc) {} + virtual void get_sensors(JsonDocument& doc) + {} virtual bool is_system_pin(uint8_t pin) { return false; diff --git a/src/dev/esp32/esp32.cpp b/src/dev/esp32/esp32.cpp index 6fcdc0a2..d3f763ee 100644 --- a/src/dev/esp32/esp32.cpp +++ b/src/dev/esp32/esp32.cpp @@ -16,8 +16,12 @@ #include "esp32.h" #include "hasp_debug.h" +#include "../../drv/tft/tft_driver.h" // for haspTft + #define BACKLIGHT_CHANNEL 0 +uint8_t temprature_sens_read(); + namespace dev { static String esp32ResetReason(uint8_t cpuid) @@ -205,11 +209,31 @@ bool Esp32Device::get_backlight_power() void Esp32Device::update_backlight() { - if(_backlight_pin < GPIO_NUM_MAX) { - uint32_t duty = _backlight_power ? map(_backlight_level, 0, 255, 0, 4095) : 0; - if(_backlight_invert) duty = 4095 - duty; - ledcWrite(BACKLIGHT_CHANNEL, duty); // ledChannel and value - } + // if(_backlight_pin < GPIO_NUM_MAX) { + // uint32_t duty = _backlight_power ? map(_backlight_level, 0, 255, 0, 4095) : 0; + // if(_backlight_invert) duty = 4095 - duty; + // ledcWrite(BACKLIGHT_CHANNEL, duty); // ledChannel and value + // } + + haspTft.tft.writecommand(0x53); // Write CTRL Display + if(_backlight_power) + haspTft.tft.writedata(0x24); // BL on, show image + else + haspTft.tft.writedata(0x20); // BL off, white screen + + // if(_backlight_power) + // haspTft.tft.writecommand(0x29); // BL on, show image + // else + // haspTft.tft.writecommand(0x28); // BL off, white screen + + // haspTft.tft.writecommand(0x55); // Write Content Adaptive Brightness Control and Color Enhancement + // haspTft.tft.writedata(0x0); // Off + + haspTft.tft.writecommand(0x5E); // minimum brightness + haspTft.tft.writedata(_backlight_level); // 0-255 + + haspTft.tft.writecommand(0x51); // Write Display Brightness + haspTft.tft.writedata(_backlight_level); // 0-255 } size_t Esp32Device::get_free_max_block() @@ -276,6 +300,18 @@ void Esp32Device::get_info(JsonDocument& doc) info[F(D_INFO_SKETCH_FREE)] = size_buf; } +void Esp32Device::get_sensors(JsonDocument& doc) +{ + JsonObject sensor = doc.createNestedObject(F("ESP32")); + uint32_t temp = (temprature_sens_read() - 32) * 100 / 1.8; + sensor[F("Temperature")] = serialized(String(1.0f * temp / 100, 2)); +} + +long Esp32Device::get_uptime() +{ + return esp_timer_get_time() / 1000000U; +} + } // namespace dev #if defined(LANBONL8) diff --git a/src/dev/esp32/esp32.h b/src/dev/esp32/esp32.h index 38bf9db2..0e936da4 100644 --- a/src/dev/esp32/esp32.h +++ b/src/dev/esp32/esp32.h @@ -9,6 +9,16 @@ #if defined(ESP32) +#ifdef __cplusplus +extern "C" { +#endif + +uint8_t temprature_sens_read(); + +#ifdef __cplusplus +} +#endif + namespace dev { class Esp32Device : public BaseDevice { @@ -34,6 +44,8 @@ class Esp32Device : public BaseDevice { uint8_t get_heap_fragmentation() override; uint16_t get_cpu_frequency() override; void get_info(JsonDocument& doc) override; + void get_sensors(JsonDocument& doc) override; + long get_uptime(); bool is_system_pin(uint8_t pin) override; diff --git a/src/dev/esp32/lanbonl8.cpp b/src/dev/esp32/lanbonl8.cpp index 215779ce..7ea4d7d6 100644 --- a/src/dev/esp32/lanbonl8.cpp +++ b/src/dev/esp32/lanbonl8.cpp @@ -39,6 +39,8 @@ hw_timer_t* timer = NULL; // Instancia do timer esp_adc_cal_characteristics_t* adc_chars = new esp_adc_cal_characteristics_t; // adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t)); +int16_t watt_10 = 0; + namespace dev { static void check_efuse(void) @@ -130,10 +132,24 @@ void LanbonL8::loop_5s() uint32_t newPulses = OverflowCounter * 10000 + PulseCounter; uint32_t delta = newPulses - totalPulses; totalPulses = newPulses; - int16_t watt_10 = DEC / 5 * delta * MEASURED_WATTS / MEASURED_PULSES_PER_SECOND; + watt_10 = DEC / 5 * delta * MEASURED_WATTS / MEASURED_PULSES_PER_SECOND; int16_t kwh_10 = DEC * totalPulses * MEASURED_WATTS / MEASURED_PULSES_PER_SECOND / 3600 / 1000; - LOG_VERBOSE(TAG_DEV, F("Pulse Counter %d.%d W / %d / %d.%d kWh"), watt_10 / DEC, watt_10 % DEC, totalPulses, - kwh_10 / DEC, kwh_10 % DEC); + // LOG_VERBOSE(TAG_DEV, F("Pulse Counter %d.%d W / %d / %d.%d kWh"), watt_10 / DEC, watt_10 % DEC, totalPulses, + // kwh_10 / DEC, kwh_10 % DEC); + // uint32_t temp = (temprature_sens_read() - 32) * 100 / 1.8; + // LOG_VERBOSE(TAG_DEV, F("Temperature %d C"), temp); +} + +void LanbonL8::get_sensors(JsonDocument& doc) +{ + Esp32Device::get_sensors(doc); + + JsonObject sensor = doc.createNestedObject(F("Energy")); + + // int16_t kwh_10 = DEC * totalPulses * MEASURED_WATTS / MEASURED_PULSES_PER_SECOND / 3600 / 1000; + + /* Pulse counter Stats */ + sensor[F("Power")] = serialized(String(1.0f * watt_10 / DEC, 2)); } //------------------------------------------------------------ diff --git a/src/dev/esp32/lanbonl8.h b/src/dev/esp32/lanbonl8.h index b4e205cd..40375ec5 100644 --- a/src/dev/esp32/lanbonl8.h +++ b/src/dev/esp32/lanbonl8.h @@ -16,6 +16,7 @@ class LanbonL8 : public Esp32Device { public: void init(); void loop_5s(); + void get_sensors(JsonDocument& doc); }; } // namespace dev diff --git a/src/dev/esp32/m5stackcore2.cpp b/src/dev/esp32/m5stackcore2.cpp index 11785afe..6fa16ee4 100644 --- a/src/dev/esp32/m5stackcore2.cpp +++ b/src/dev/esp32/m5stackcore2.cpp @@ -8,12 +8,13 @@ #include "AXP192.h" // Power Mgmt #include "dev/esp32/esp32.h" +AXP192 Axp; + // AXP192 Axp; namespace dev { void M5StackCore2::init(void) { - AXP192 Axp; Wire.begin(TOUCH_SDA, TOUCH_SCL); Axp.begin(); @@ -43,8 +44,22 @@ void M5StackCore2::init(void) Axp.SetLed(1); } +void M5StackCore2::get_sensors(JsonDocument& doc) +{ + Esp32Device::get_sensors(doc); + + JsonObject sensor = doc.createNestedObject(F("AXP192")); + sensor[F("BattVoltage")] = Axp.GetBatVoltage(); + sensor[F("BattPower")] = Axp.GetBatPower(); + // sensor[F("Batt%")] = Axp.getBattPercentage(); + sensor[F("BattChargeCurrent")] = Axp.GetBatChargeCurrent(); + sensor[F("Temperature")] = Axp.GetTempInAXP192(); + sensor[F("Charging")] = Axp.isCharging(); +} + } // namespace dev dev::M5StackCore2 haspDevice; #endif + diff --git a/src/dev/esp32/m5stackcore2.h b/src/dev/esp32/m5stackcore2.h index 7bc48dd3..b602c37a 100644 --- a/src/dev/esp32/m5stackcore2.h +++ b/src/dev/esp32/m5stackcore2.h @@ -13,6 +13,7 @@ namespace dev { class M5StackCore2 : public Esp32Device { public: void init() override; + void get_sensors(JsonDocument& doc); }; } // namespace dev diff --git a/src/dev/esp8266/esp8266.cpp b/src/dev/esp8266/esp8266.cpp index 0fc52514..cd2a87e9 100644 --- a/src/dev/esp8266/esp8266.cpp +++ b/src/dev/esp8266/esp8266.cpp @@ -140,6 +140,11 @@ bool Esp8266Device::is_system_pin(uint8_t pin) return false; } +long Esp8266Device::get_uptime() +{ + return millis() / 1000U; +} + } // namespace dev dev::Esp8266Device haspDevice; diff --git a/src/dev/esp8266/esp8266.h b/src/dev/esp8266/esp8266.h index ee829fd0..a37cd94e 100644 --- a/src/dev/esp8266/esp8266.h +++ b/src/dev/esp8266/esp8266.h @@ -35,6 +35,7 @@ class Esp8266Device : public BaseDevice { size_t get_free_heap() override; uint8_t get_heap_fragmentation() override; uint16_t get_cpu_frequency() override; + long get_uptime(); bool is_system_pin(uint8_t pin) override; diff --git a/src/dev/posix/hasp_posix.cpp b/src/dev/posix/hasp_posix.cpp index 47fb2d09..ca9cec40 100644 --- a/src/dev/posix/hasp_posix.cpp +++ b/src/dev/posix/hasp_posix.cpp @@ -5,6 +5,7 @@ #include #include +#include // uptime #include "hasp_posix.h" @@ -161,6 +162,15 @@ bool PosixDevice::is_system_pin(uint8_t pin) return false; } +long PosixDevice::get_uptime() +{ + struct sysinfo s_info; + if(sysinfo(&s_info) == 0) + return s_info.uptime; + else + return 0; +} + } // namespace dev dev::PosixDevice haspDevice; diff --git a/src/dev/posix/hasp_posix.h b/src/dev/posix/hasp_posix.h index 589bd16e..98b5d9e5 100644 --- a/src/dev/posix/hasp_posix.h +++ b/src/dev/posix/hasp_posix.h @@ -50,6 +50,7 @@ class PosixDevice : public BaseDevice { size_t get_free_heap(); uint8_t get_heap_fragmentation(); uint16_t get_cpu_frequency(); + long get_uptime(); bool is_system_pin(uint8_t pin) override; diff --git a/src/dev/win32/hasp_win32.cpp b/src/dev/win32/hasp_win32.cpp index a69fd4a3..bccad64d 100644 --- a/src/dev/win32/hasp_win32.cpp +++ b/src/dev/win32/hasp_win32.cpp @@ -132,6 +132,11 @@ bool Win32Device::is_system_pin(uint8_t pin) return false; } +long Win32Device::get_uptime() +{ + return GetTickCount64() / 1000; +} + } // namespace dev dev::Win32Device haspDevice; diff --git a/src/dev/win32/hasp_win32.h b/src/dev/win32/hasp_win32.h index a222b39e..d179d85e 100644 --- a/src/dev/win32/hasp_win32.h +++ b/src/dev/win32/hasp_win32.h @@ -71,6 +71,7 @@ class Win32Device : public BaseDevice { size_t get_free_heap(); uint8_t get_heap_fragmentation(); uint16_t get_cpu_frequency(); + long get_uptime(); bool is_system_pin(uint8_t pin) override;