Add get_sensors and get_uptime

This commit is contained in:
fvanroie 2021-06-26 19:17:15 +02:00
parent ef8413234f
commit 2d31165fe0
13 changed files with 115 additions and 9 deletions

View File

@ -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;

View File

@ -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)

View File

@ -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;

View File

@ -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));
}
//------------------------------------------------------------

View File

@ -16,6 +16,7 @@ class LanbonL8 : public Esp32Device {
public:
void init();
void loop_5s();
void get_sensors(JsonDocument& doc);
};
} // namespace dev

View File

@ -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

View File

@ -13,6 +13,7 @@ namespace dev {
class M5StackCore2 : public Esp32Device {
public:
void init() override;
void get_sensors(JsonDocument& doc);
};
} // namespace dev

View File

@ -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;

View File

@ -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;

View File

@ -5,6 +5,7 @@
#include <cstdint>
#include <sys/utsname.h>
#include <sys/sysinfo.h> // 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;

View File

@ -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;

View File

@ -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;

View File

@ -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;