Extended device classes for ESP32 and ESP8266

This commit is contained in:
fvanroie 2021-02-17 03:39:56 +01:00
parent 8156e8ca33
commit 5692bb8b90
13 changed files with 403 additions and 151 deletions

View File

@ -1,9 +1,9 @@
#include "device.h"
#if defined(LANBONL8)
#warning Lanbon L8
#warning Lanbon L8
#elif defined(M5STACK)
#warning M5 Stack
#warning M5 Stack
#else
dev::BaseDevice haspDevice;
#warning Generic Device
#endif

View File

@ -4,10 +4,19 @@
#ifndef HASP_DEVICE_H
#define HASP_DEVICE_H
#ifdef ARDUINO
#include "Arduino.h"
#endif
namespace dev {
class BaseDevice {
public:
bool has_battery = false;
bool has_backligth_control = true;
virtual void reboot()
{}
virtual void pre_setup()
{}
virtual void post_setup()
@ -16,19 +25,37 @@ class BaseDevice {
{}
virtual void loop_5s()
{}
virtual void set_backlight_pin(uint8_t pin)
{}
virtual void set_backlight_level(uint8_t level)
{}
virtual uint8_t get_backlight_level()
{
return -1;
}
virtual void set_backlight_power(bool power)
{}
virtual bool get_backlight_power()
{
return true;
}
};
} // namespace dev
#if defined(ESP32)
#warning Building for ESP32 Devices
#include "dev/esp32/esp32.h"
#elif defined(ESP8266)
#warning Building for ESP8266 Devices
#include "dev/esp8266/esp8266.h"
#elif defined(STM32F4)
#warning Building for STM32F4xx Devices
#include "dev/stm32f4.h"
#else
#warning Building for Generic Devices
using dev::BaseDevice;
#if defined(LANBONL8)
#warning Lanbon L8
#include "lanbonl8.h"
#elif defined(M5STACK)
#warning M5 Stack
#include "m5stackcore2.h"
#else
extern dev::BaseDevice haspDevice;
#endif
#endif

84
src/dev/esp32/esp32.cpp Normal file
View File

@ -0,0 +1,84 @@
#if defined(ESP32)
#include "Arduino.h"
#include <Esp.h>
#include "esp_system.h"
#include "dev/device.h"
#include "dev/esp32/esp32.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
#include "hasp_conf.h"
#include "hasp_debug.h"
#define BACKLIGHT_CHANNEL 0
namespace dev {
void Esp32Device::reboot()
{
ESP.restart();
}
void Esp32Device::set_backlight_pin(uint8_t pin)
{
Esp32Device::backlight_pin = pin;
/* Setup Backlight Control Pin */
if(pin >= 0) {
LOG_VERBOSE(TAG_GUI, F("Backlight : Pin %d"), pin);
ledcSetup(BACKLIGHT_CHANNEL, 20000, 12);
ledcAttachPin(pin, BACKLIGHT_CHANNEL);
update_backlight();
}
}
void Esp32Device::set_backlight_level(uint8_t level)
{
backlight_level = level >= 0 ? level : 0;
backlight_level = backlight_level <= 100 ? backlight_level : 100;
update_backlight();
}
uint8_t Esp32Device::get_backlight_level()
{
return backlight_level;
}
void Esp32Device::set_backlight_power(bool power)
{
backlight_power = power;
update_backlight();
}
bool Esp32Device::get_backlight_power()
{
return backlight_power != 0;
}
void Esp32Device::update_backlight()
{
if(backlight_pin == -1) return;
if(backlight_power) { // The backlight is ON
ledcWrite(BACKLIGHT_CHANNEL, map(backlight_level, 0, 100, 0, 4095)); // ledChannel and value
}
}
} // namespace dev
#if defined(LANBONL8)
#warning Building for Lanbon L8
#include "dev/esp32/lanbonl8.h"
#elif defined(M5STACK)
#warning Building for M5Stack core2
#include "dev/esp32/m5stackcore2.h"
#else
dev::Esp32Device haspDevice;
#endif
#endif // ESP32

51
src/dev/esp32/esp32.h Normal file
View File

@ -0,0 +1,51 @@
/* MIT License - Copyright (c) 2020 Francis Van Roie
For full license information read the LICENSE file in the project folder */
#ifndef HASP_DEVICE_ESP32_H
#define HASP_DEVICE_ESP32_H
#include "dev/device.h"
#if defined(ESP32)
namespace dev {
class Esp32Device : public BaseDevice {
public:
void reboot() override;
void set_backlight_pin(uint8_t pin) override;
void set_backlight_level(uint8_t val) override;
uint8_t get_backlight_level() override;
void set_backlight_power(bool power) override;
bool get_backlight_power() override;
private:
uint8_t backlight_pin;
uint8_t backlight_level;
uint8_t backlight_power;
void update_backlight();
};
} // namespace dev
using dev::Esp32Device;
#if defined(LANBONL8)
#warning Building for Lanbon L8
#include "dev/esp32/lanbonl8.h"
#elif defined(M5STACK)
#warning Building for M5Stack core2
#include "dev/esp32/m5stackcore2.h"
#else
extern dev::Esp32Device haspDevice;
#endif
#endif // ESP32
#endif // HASP_DEVICE_ESP32_H

View File

@ -1,13 +1,19 @@
#include "lanbonl8.h"
#if defined(LANBONL8)
#include "Arduino.h"
#include "../device.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
#include "Arduino.h"
#include "dev/esp32/esp32.h"
#define REF_VOLTAGE 1100
#include "driver/adc.h"
#include "esp_adc_cal.h"
#include "hasp_conf.h"
#include "hasp_debug.h"
#define BACKLIGHT_CHANNEL 0
#define REF_VOLTAGE 1100
esp_adc_cal_characteristics_t * adc_chars =
new esp_adc_cal_characteristics_t; // adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
@ -55,19 +61,6 @@ void LanbonL8::pre_setup()
print_char_val_type(val_type);
}
void LanbonL8::post_setup()
{}
void LanbonL8::loop()
{}
void LanbonL8::loop_5s()
{
double voltage = esp_adc_cal_raw_to_voltage(analogRead(39), adc_chars);
Serial.print(adc1_get_raw(ADC1_CHANNEL_3));
Serial.print(" - ");
Serial.println(voltage);
}
} // namespace dev
dev::LanbonL8 haspDevice;

View File

@ -4,22 +4,17 @@
#ifndef HASP_DEVICE_LANBONL8_H
#define HASP_DEVICE_LANBONL8_H
#include "../device.h"
#include "dev/esp32/esp32.h"
#if defined(LANBONL8)
namespace dev {
class LanbonL8 : public BaseDevice {
class LanbonL8 : public Esp32Device {
public:
void pre_setup() override;
void post_setup() override;
void loop() override;
void loop_5s() override;
void pre_setup();
};
} // namespace dev
extern dev::LanbonL8 haspDevice;

View File

@ -1,8 +1,9 @@
#include "m5stackcore2.h"
#if defined(M5STACK)
#include "AXP192.h" // Power Mgmt
#include "../device.h"
#include "AXP192.h" // Power Mgmt
#include "dev/esp32/esp32.h"
// AXP192 Axp;
namespace dev {
@ -39,14 +40,6 @@ void M5StackCore2::pre_setup(void)
Axp.SetLed(1);
}
void M5StackCore2::post_setup(void)
{}
void M5StackCore2::loop(void)
{}
void M5StackCore2::loop_5s(void)
{}
} // namespace dev
dev::M5StackCore2 haspDevice;

View File

@ -6,16 +6,13 @@
#if defined(M5STACK)
#include "../device.h"
#include "dev/esp32/esp32.h"
namespace dev {
class M5StackCore2 : public BaseDevice {
class M5StackCore2 : public Esp32Device {
public:
void pre_setup() override;
void post_setup() override;
void loop() override;
void loop_5s() override;
};
} // namespace dev

View File

@ -0,0 +1,66 @@
#if defined(ESP8266)
#include "Arduino.h"
#include <Esp.h>
#include "dev/esp8266/esp8266.h"
#include "hasp_conf.h"
#include "hasp_debug.h"
#define BACKLIGHT_CHANNEL 0
namespace dev {
void Esp8266Device::reboot()
{
ESP.restart();
}
void Esp8266Device::set_backlight_pin(uint8_t pin)
{
Esp8266Device::backlight_pin = pin;
/* Setup Backlight Control Pin */
if(pin >= 0) {
LOG_VERBOSE(TAG_GUI, F("Backlight : Pin %d"), pin);
pinMode(backlight_pin, OUTPUT);
update_backlight();
}
}
void Esp8266Device::set_backlight_level(uint8_t level)
{
backlight_level = level >= 0 ? level : 0;
backlight_level = backlight_level <= 100 ? backlight_level : 100;
update_backlight();
}
uint8_t Esp8266Device::get_backlight_level()
{
return backlight_level;
}
void Esp8266Device::set_backlight_power(bool power)
{
backlight_power = power;
update_backlight();
}
bool Esp8266Device::get_backlight_power()
{
return backlight_power != 0;
}
void Esp8266Device::update_backlight()
{
if(backlight_pin == -1) return;
analogWrite(backlight_pin, backlight_power ? map(backlight_level, 0, 100, 0, 1023) : 0);
}
} // namespace dev
dev::Esp8266Device haspDevice;
#endif // ESP8266

45
src/dev/esp8266/esp8266.h Normal file
View File

@ -0,0 +1,45 @@
/* MIT License - Copyright (c) 2020 Francis Van Roie
For full license information read the LICENSE file in the project folder */
#ifndef HASP_DEVICE_ESP8266_H
#define HASP_DEVICE_ESP8266_H
#include "hasp_conf.h"
#include "dev/device.h"
#if defined(ESP8266)
namespace dev {
class Esp8266Device : public BaseDevice {
public:
void reboot() override;
void set_backlight_pin(uint8_t pin) override;
void set_backlight_level(uint8_t val) override;
uint8_t get_backlight_level() override;
void set_backlight_power(bool power) override;
bool get_backlight_power() override;
private:
uint8_t backlight_pin;
uint8_t backlight_level;
uint8_t backlight_power;
void update_backlight();
};
} // namespace dev
using dev::Esp8266Device;
extern dev::Esp8266Device haspDevice;
#endif // ESP8266
#endif // HASP_DEVICE_ESP8266_H

View File

@ -5,18 +5,18 @@
#include "hasp_conf.h"
#if defined(ESP8266)
#include <Esp.h>
#include <ESP8266WiFi.h>
#include <Esp.h>
#include <ESP8266WiFi.h>
#endif
#if defined(ESP32)
#include <Esp.h>
#include <WiFi.h>
#include "esp_system.h"
#include <Esp.h>
#include <WiFi.h>
#include "esp_system.h"
#endif
#if defined(ARDUINO_ARCH_ESP32)
#include <rom/rtc.h> // needed to get the ResetInfo
#include <rom/rtc.h> // needed to get the ResetInfo
// Compatibility function for ESP8266 getRestInfo
String esp32ResetReason(uint8_t cpuid)
@ -152,11 +152,11 @@ String halGetChipModel()
case CHIP_ESP32:
model += F("ESP32");
break;
#ifdef CHIP_ESP32S2
#ifdef CHIP_ESP32S2
case CHIP_ESP32S2:
model += F("ESP32-S2");
break;
#endif
#endif
default:
model = F("Unknown ESP32");
}
@ -174,19 +174,19 @@ String halGetChipModel()
/* Memory Management Functions */
#if defined(STM32F4xx)
#include <malloc.h> // for mallinfo()
#include <unistd.h> // for sbrk()
#include <malloc.h> // for mallinfo()
#include <unistd.h> // for sbrk()
int freeHighMemory()
{
char top;
#ifdef __arm__
#ifdef __arm__
return &top - reinterpret_cast<char *>(sbrk(0));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
return &top - __brkval;
#else // __arm__
#else // __arm__
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif // __arm__
#endif // __arm__
}
#endif
@ -260,14 +260,14 @@ String halGetMacAddress(int start, const char * seperator)
#if defined(STM32F4xx)
uint8_t * mac_p = nullptr;
#if HASP_USE_ETHERNET > 0
#if USE_BUILTIN_ETHERNET > 0
#if HASP_USE_ETHERNET > 0
#if USE_BUILTIN_ETHERNET > 0
mac_p = Ethernet.MACAddress();
for(int i = 0; i < 6; i++) mac[i] = *(mac_p + i);
#else
#else
Ethernet.macAddress(mac);
#endif
#endif
#endif
#endif
#else
WiFi.macAddress(mac);
#endif
@ -293,8 +293,6 @@ uint16_t halGetCpuFreqMHz()
#endif
}
String halDisplayDriverName()
{
#if defined(ILI9341_DRIVER)
@ -342,56 +340,56 @@ String halGpioName(uint8_t gpio)
case PortName::PortB:
ioName = F("PB");
break;
#if defined GPIOC_BASE
#if defined GPIOC_BASE
case PortName::PortC:
ioName = F("PC");
break;
#endif
#if defined GPIOD_BASE
#endif
#if defined GPIOD_BASE
case PortName::PortD:
ioName = F("PD");
break;
#endif
#if defined GPIOE_BASE
#endif
#if defined GPIOE_BASE
case PortName::PortE:
ioName = F("PE");
break;
#endif
#if defined GPIOF_BASE
#endif
#if defined GPIOF_BASE
case PortName::PortF:
ioName = F("PF");
break;
#endif
#if defined GPIOG_BASE
#endif
#if defined GPIOG_BASE
case PortName::PortG:
ioName = F("PG");
break;
#endif
#if defined GPIOH_BASE
#endif
#if defined GPIOH_BASE
case PortName::PortH:
ioName = F("PH");
break;
#endif
#if defined GPIOI_BASE
#endif
#if defined GPIOI_BASE
case PortName::PortI:
ioName = F("PI");
break;
#endif
#if defined GPIOJ_BASE
#endif
#if defined GPIOJ_BASE
case PortName::PortJ:
ioName = F("PJ");
break;
#endif
#if defined GPIOK_BASE
#endif
#if defined GPIOK_BASE
case PortName::PortK:
ioName = F("PK");
break;
#endif
#if defined GPIOZ_BASE
#endif
#if defined GPIOZ_BASE
case PortName::PortZ:
ioName = F("PZ");
break;
#endif
#endif
default:
ioName = F("P?");
}

View File

@ -2,14 +2,14 @@
For full license information read the LICENSE file in the project folder */
#ifdef ARDUINO
#include "ArduinoLog.h"
#include "ArduinoLog.h"
#endif
#include "ArduinoJson.h"
#include "hasp_conf.h"
#if HASP_USE_EEPROM > 0
#include "StreamUtils.h" // For EEPromStream
#include "StreamUtils.h" // For EEPromStream
#endif
#include "lvgl.h"
@ -17,13 +17,13 @@
#include "hasp_conf.h"
#if HASP_USE_DEBUG > 0
#include "../hasp_debug.h"
#include "../hasp_debug.h"
#endif
#if HASP_USE_CONFIG > 0
#include "lv_fs_if.h"
#include "hasp_gui.h"
#include "hasp_config.h"
#include "lv_fs_if.h"
#include "hasp_gui.h"
#include "hasp_config.h"
//#include "hasp_filesystem.h" included in hasp_conf.h
#endif
@ -32,10 +32,11 @@
#include "hasp_attribute.h"
#include "hasp.h"
#include "dev/device.h"
#include "lv_theme_hasp.h"
#if HASP_USE_EEPROM > 0
#include "EEPROM.h"
#include "EEPROM.h"
#endif
//#if LV_USE_HASP
@ -298,7 +299,7 @@ void haspProgressMsg(const char * msg)
#ifdef ARDUINO
// Sets the value string of the global progress bar
void haspProgressMsg(const __FlashStringHelper *msg)
void haspProgressMsg(const __FlashStringHelper * msg)
{
haspProgressMsg(String(msg).c_str());
}
@ -322,7 +323,7 @@ static void custom_font_apply_cb(lv_theme_t * th, lv_obj_t * obj, lv_theme_style
*/
void haspSetup(void)
{
guiSetDim(haspStartDim);
haspDevice.set_backlight_level(haspStartDim);
/******* File System Test ********************************************************************/
// lv_fs_file_t f;
@ -355,7 +356,7 @@ void haspSetup(void)
/* ********** Font Initializations ********** */
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
lv_zifont_init();
if(lv_zifont_font_init(&haspFonts[1], haspZiFontPath, 32) != 0) {
@ -364,7 +365,7 @@ void haspSetup(void)
} else {
// defaultFont = haspFonts[0];
}
#endif
#endif
#endif
// haspFonts[0] = lv_font_load("E:/font_1.fnt");
@ -589,12 +590,12 @@ void haspLoadPage(const char * pagesfile)
LOG_INFO(TAG_HASP, F("File %s loaded"), pagesfile);
#else
#if HASP_USE_EEPROM > 0
#if HASP_USE_EEPROM > 0
LOG_TRACE(TAG_HASP, F("Loading jsonl from EEPROM..."));
EepromStream eepromStream(4096, 1024);
dispatch_parse_jsonl(eepromStream);
LOG_INFO(TAG_HASP, F("Loaded jsonl from EEPROM"));
#endif
#endif
#endif
}

View File

@ -13,33 +13,35 @@
#include "hasp_parser.h"
#include "hasp_attribute.h"
#include "dev/device.h"
//#include "hasp_gui.h"
#if HASP_USE_DEBUG > 0
#include "../hasp_debug.h"
#include "../hasp_debug.h"
#if WINDOWS
#include <iostream>
#include <fstream>
#include <sstream>
#include "../mqtt/hasp_mqtt.h"
#else
#include "StringStream.h"
#include "CharStream.h"
#if WINDOWS
#include <iostream>
#include <fstream>
#include <sstream>
#include "../mqtt/hasp_mqtt.h"
#else
#include "StringStream.h"
#include "CharStream.h"
#include "hasp_oobe.h"
#include "hasp_gui.h" // for screenshot
#include "sys/gpio/hasp_gpio.h"
#include "hal/hasp_hal.h"
#include "hasp_oobe.h"
#include "hasp_gui.h" // for screenshot
#include "sys/gpio/hasp_gpio.h"
#include "hal/hasp_hal.h"
#include "svc/hasp_ota.h"
#include "mqtt/hasp_mqtt.h"
#include "sys/net/hasp_network.h" // for network_get_status()
#endif
#include "svc/hasp_ota.h"
#include "mqtt/hasp_mqtt.h"
#include "sys/net/hasp_network.h" // for network_get_status()
#endif
#endif
#if HASP_USE_CONFIG > 0
#include "hasp_config.h"
#include "hasp_config.h"
#endif
extern uint8_t hasp_sleep_state;
@ -227,14 +229,14 @@ void dispatch_command(const char * topic, const char * payload)
#if HASP_USE_CONFIG > 0
#if HASP_USE_WIFI > 0
#if HASP_USE_WIFI > 0
} else if(!strcmp_P(topic, FP_CONFIG_SSID) || !strcmp_P(topic, FP_CONFIG_PASS)) {
StaticJsonDocument<64> settings;
settings[topic] = payload;
wifiSetConfig(settings.as<JsonObject>());
#endif // HASP_USE_WIFI
#endif // HASP_USE_WIFI
#if HASP_USE_MQTT > 0
#if HASP_USE_MQTT > 0
} else if(!strcmp_P(topic, PSTR("mqtthost")) || !strcmp_P(topic, PSTR("mqttport")) ||
!strcmp_P(topic, PSTR("mqttport")) || !strcmp_P(topic, PSTR("mqttuser")) ||
!strcmp_P(topic, PSTR("hostname"))) {
@ -245,7 +247,7 @@ void dispatch_command(const char * topic, const char * payload)
StaticJsonDocument<64> settings;
settings[topic + 4] = payload;
mqttSetConfig(settings.as<JsonObject>());
#endif // HASP_USE_MQTT
#endif // HASP_USE_MQTT
#endif // HASP_USE_CONFIG
@ -425,42 +427,42 @@ static void dispatch_config(const char * topic, const char * payload)
haspGetConfig(settings);
}
#if HASP_USE_WIFI > 0
#if HASP_USE_WIFI > 0
else if(strcasecmp_P(topic, PSTR("wifi")) == 0) {
if(update)
wifiSetConfig(settings);
else
wifiGetConfig(settings);
}
#if HASP_USE_MQTT > 0
#if HASP_USE_MQTT > 0
else if(strcasecmp_P(topic, PSTR("mqtt")) == 0) {
if(update)
mqttSetConfig(settings);
else
mqttGetConfig(settings);
}
#endif
#if HASP_USE_TELNET > 0
#endif
#if HASP_USE_TELNET > 0
// else if(strcasecmp_P(topic, PSTR("telnet")) == 0)
// telnetGetConfig(settings[F("telnet")]);
#endif
#if HASP_USE_MDNS > 0
#endif
#if HASP_USE_MDNS > 0
else if(strcasecmp_P(topic, PSTR("mdns")) == 0) {
if(update)
mdnsSetConfig(settings);
else
mdnsGetConfig(settings);
}
#endif
#if HASP_USE_HTTP > 0
#endif
#if HASP_USE_HTTP > 0
else if(strcasecmp_P(topic, PSTR("http")) == 0) {
if(update)
httpSetConfig(settings);
else
httpGetConfig(settings);
}
#endif
#endif
#endif
#endif
// Send output
if(!update) {
@ -532,9 +534,9 @@ void dispatch_gpio_input_event(uint8_t pin, uint8_t group, uint8_t eventid)
dispatch_get_event_name(eventid, event, sizeof(event));
snprintf_P(payload, sizeof(payload), PSTR("{\"pin\":%d,\"group\":%d,\"event\":\"%s\"}"), pin, group, event);
#if HASP_USE_MQTT > 0
#if HASP_USE_MQTT > 0
mqtt_send_state(F("input"), payload);
#endif
#endif
// update outputstates
// dispatch_group_onoff(group, dispatch_get_event_state(eventid), NULL);
@ -572,12 +574,12 @@ static inline void dispatch_state_msg(const __FlashStringHelper * subtopic, cons
#if !defined(HASP_USE_MQTT) && !defined(HASP_USE_TASMOTA_SLAVE)
LOG_TRACE(TAG_MSGR, F("%s => %s"), String(subtopic).c_str(), payload);
#else
#if HASP_USE_MQTT > 0
#if HASP_USE_MQTT > 0
mqtt_send_state(subtopic, payload);
#endif
#if HASP_USE_TASMOTA_SLAVE > 0
#endif
#if HASP_USE_TASMOTA_SLAVE > 0
slave_send_state(subtopic, payload);
#endif
#endif
#endif
}
@ -774,10 +776,10 @@ void dispatch_clear_page(const char *, const char * page)
void dispatch_dim(const char *, const char * level)
{
// Set the current state
if(strlen(level) != 0) guiSetDim(atoi(level));
if(strlen(level) != 0) haspDevice.set_backlight_level(atoi(level));
char payload[5];
itoa(guiGetDim(), payload, DEC);
itoa(haspDevice.get_backlight_level(), payload, DEC);
dispatch_state_msg(F("dim"), payload);
}
@ -833,11 +835,11 @@ void dispatch_moodlight(const char * topic, const char * payload)
void dispatch_backlight(const char *, const char * payload)
{
// Set the current state
if(strlen(payload) != 0) guiSetBacklight(Utilities::is_true(payload));
if(strlen(payload) != 0) haspDevice.set_backlight_power(Utilities::is_true(payload));
// Return the current state
char buffer[4];
memcpy_P(buffer, guiGetBacklight() ? PSTR("ON") : PSTR("OFF"), sizeof(buffer));
memcpy_P(buffer, haspDevice.get_backlight_power() ? PSTR("ON") : PSTR("OFF"), sizeof(buffer));
dispatch_state_msg(F("light"), buffer);
}
@ -1012,7 +1014,7 @@ void everySecond()
}
}
#else
#include <chrono>
#include <chrono>
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
void everySecond()
{