Code clean-up

This commit is contained in:
fvanroie 2020-03-25 23:02:25 +01:00
parent 9a36764411
commit 1b300c7a50
9 changed files with 127 additions and 75 deletions

View File

@ -1044,7 +1044,6 @@ void haspNewObject(const JsonObject & config)
return; return;
} }
lv_obj_t * label;
switch(objid) { switch(objid) {
/* ----- Basic Objects ------ */ /* ----- Basic Objects ------ */
case LV_HASP_BUTTON: { case LV_HASP_BUTTON: {
@ -1053,7 +1052,7 @@ void haspNewObject(const JsonObject & config)
// haspSetToggle(obj, toggle); // haspSetToggle(obj, toggle);
// lv_btn_set_toggle(obj, toggle); // lv_btn_set_toggle(obj, toggle);
// if(config[F("txt")]) { // if(config[F("txt")]) {
label = lv_label_create(obj, NULL); lv_obj_t * label = lv_label_create(obj, NULL);
// lv_label_set_text(label, config[F("txt")].as<String>().c_str()); // lv_label_set_text(label, config[F("txt")].as<String>().c_str());
// haspSetOpacity(obj, LV_OPA_COVER); // haspSetOpacity(obj, LV_OPA_COVER);
//} //}
@ -1276,8 +1275,6 @@ void haspNewObject(const JsonObject & config)
void haspLoadPage(String pages) void haspLoadPage(String pages)
{ {
char msg[128];
if(!SPIFFS.begin()) { if(!SPIFFS.begin()) {
Log.error(F("HASP: FS not mounted. Failed to load %s"), pages.c_str()); Log.error(F("HASP: FS not mounted. Failed to load %s"), pages.c_str());
return; return;
@ -1321,8 +1318,14 @@ bool haspGetConfig(const JsonObject & settings)
return true; return true;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** Set HASP Configuration.
*
* Read the settings from json and sets the application variables.
*
* @note: data pixel should be formated to uint32_t RGBA. Imagemagick requirements.
*
* @param[in] settings JsonObject with the config settings.
**/
bool haspSetConfig(const JsonObject & settings) bool haspSetConfig(const JsonObject & settings)
{ {
configOutput(settings); configOutput(settings);

View File

@ -10,7 +10,6 @@
#endif #endif
#include <WiFiUdp.h> #include <WiFiUdp.h>
// #include "hasp_log.h"
#include "hasp_hal.h" #include "hasp_hal.h"
#include "hasp_mqtt.h" #include "hasp_mqtt.h"
#include "hasp_debug.h" #include "hasp_debug.h"
@ -21,24 +20,46 @@
#include "user_config_override.h" #include "user_config_override.h"
#endif #endif
#if HASP_USE_SYSLOG != 0
#include "Syslog.h"
#if HASP_USE_TELNET != 0 #if HASP_USE_TELNET != 0
#include "hasp_telnet.h" #include "hasp_telnet.h"
//#include "hasp_telnet.cpp" //#include "hasp_telnet.cpp"
#endif #endif
#if HASP_USE_SYSLOG != 0
#include "Syslog.h"
#ifndef SYSLOG_SERVER #ifndef SYSLOG_SERVER
#define SYSLOG_SERVER "" #define SYSLOG_SERVER ""
#endif #endif
#ifndef SYSLOG_PORT #ifndef SYSLOG_PORT
#define SYSLOG_PORT 514 #define SYSLOG_PORT 514
#endif #endif
#ifndef APP_NAME #ifndef APP_NAME
#define APP_NAME "HASP" #define APP_NAME "HASP"
#endif #endif
const char * syslogAppName = APP_NAME;
char debugSyslogHost[32] = SYSLOG_SERVER;
uint16_t debugSyslogPort = SYSLOG_PORT;
uint8_t debugSyslogFacility = 0;
uint8_t debugSyslogProtocol = 0;
extern char mqttNodeName[16];
// A UDP instance to let us send and receive packets over UDP
WiFiUDP syslogClient;
// Create a new syslog instance with LOG_KERN facility
// Syslog syslog(syslogClient, SYSLOG_SERVER, SYSLOG_PORT, MQTT_CLIENT, APP_NAME, LOG_KERN);
// Create a new empty syslog instance
Syslog * syslog;
#endif // USE_SYSLOG
// Serial Settings
uint16_t debugSerialBaud = 11520; // Multiplied by 10
bool debugSerialStarted = false;
//#define TERM_COLOR_Black "\u001b[30m" //#define TERM_COLOR_Black "\u001b[30m"
#define TERM_COLOR_GRAY "\e[37m" #define TERM_COLOR_GRAY "\e[37m"
#define TERM_COLOR_RED "\e[91m" #define TERM_COLOR_RED "\e[91m"
@ -50,28 +71,6 @@
#define TERM_COLOR_WHITE "\e[97m" #define TERM_COLOR_WHITE "\e[97m"
#define TERM_COLOR_RESET "\e[0m" #define TERM_COLOR_RESET "\e[0m"
// unsigned char TERM_COLOR_CYAN[] = {27, '[', '3', '6', 'm'};
// unsigned char TERM_COLOR_RESET[] = {27, '[', '0', 'm'};
const char * syslogAppName = APP_NAME;
char debugSyslogHost[32] = SYSLOG_SERVER;
uint16_t debugSyslogPort = SYSLOG_PORT;
uint8_t debugSyslogFacility = 0;
uint8_t debugSyslogProtocol = 0;
uint16_t debugSerialBaud = 11520; // Multiplied by 10
bool debugSerialStarted = false;
extern char mqttNodeName[16];
// A UDP instance to let us send and receive packets over UDP
WiFiUDP syslogClient;
// Create a new syslog instance with LOG_KERN facility
// Syslog syslog(syslogClient, SYSLOG_SERVER, SYSLOG_PORT, MQTT_CLIENT, APP_NAME, LOG_KERN);
// Create a new empty syslog instance
Syslog * syslog;
#endif
unsigned long debugLastMillis = 0; unsigned long debugLastMillis = 0;
uint16_t debugTelePeriod = 300; uint16_t debugTelePeriod = 300;
@ -216,8 +215,14 @@ bool debugGetConfig(const JsonObject & settings)
return true; return true;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** Set DEBUG Configuration.
*
* Read the settings from json and sets the application variables.
*
* @note: data pixel should be formated to uint32_t RGBA. Imagemagick requirements.
*
* @param[in] settings JsonObject with the config settings.
**/
bool debugSetConfig(const JsonObject & settings) bool debugSetConfig(const JsonObject & settings)
{ {
configOutput(settings); configOutput(settings);
@ -246,7 +251,8 @@ bool debugSetConfig(const JsonObject & settings)
void printTimestamp(int level, Print * _logOutput) void printTimestamp(int level, Print * _logOutput)
{ {
char c[128]; char c[128];
int m = snprintf(c, sizeof(c), PSTR("[%10.3fs] %5u/%5u %2u | "), float(millis()) / 1000, halGetMaxFreeBlock(), /*int m =*/
snprintf(c, sizeof(c), PSTR("[%10.3fs] %5u/%5u %2u | "), float(millis()) / 1000, halGetMaxFreeBlock(),
ESP.getFreeHeap(), halGetHeapFragmentation()); ESP.getFreeHeap(), halGetHeapFragmentation());
#if LV_MEM_CUSTOM == 0 #if LV_MEM_CUSTOM == 0

View File

@ -34,7 +34,6 @@ void eepromWrite(uint16_t addr, std::string & data)
std::string eepromRead(uint16_t addr) std::string eepromRead(uint16_t addr)
{ {
int i;
char data[1024]; // Max 1024 Bytes char data[1024]; // Max 1024 Bytes
int len = 0; int len = 0;
unsigned char k; unsigned char k;

View File

@ -67,13 +67,13 @@ static uint16_t calData[5] = {0, 65535, 0, 65535, 0};
bool guiCheckSleep() bool guiCheckSleep()
{ {
uint32_t idle = lv_disp_get_inactive_time(NULL); uint32_t idle = lv_disp_get_inactive_time(NULL);
if(idle >= (guiSleepTime1 + guiSleepTime2) * 1000) { if(idle >= (guiSleepTime1 + guiSleepTime2) * 1000U) {
if(guiSleeping != 2) { if(guiSleeping != 2) {
dispatchIdle(F("LONG")); dispatchIdle(F("LONG"));
guiSleeping = 2; guiSleeping = 2;
} }
return true; return true;
} else if(idle >= guiSleepTime1 * 1000) { } else if(idle >= guiSleepTime1 * 1000U) {
if(guiSleeping != 1) { if(guiSleeping != 1) {
dispatchIdle(F("SHORT")); dispatchIdle(F("SHORT"));
guiSleeping = 1; guiSleeping = 1;
@ -89,11 +89,24 @@ bool guiCheckSleep()
#if LV_USE_LOG != 0 #if LV_USE_LOG != 0
/* Serial debugging */ /* Serial debugging */
void debugLvgl(lv_log_level_t level, const char * file, uint32_t line, const char * dsc) void debugLvgl(lv_log_level_t level, const char * file, uint32_t line, const char * funcname, const char * descr)
{ {
char buffer[128]; switch(level) {
snprintf(buffer, sizeof(buffer), PSTR("LVGL: %s@%d->%s"), file, line, dsc); case LV_LOG_LEVEL_TRACE:
Log.warning(buffer); Log.trace(F("LVGL: %s:%u %s -> %s"), file, line, funcname, descr);
break;
case LV_LOG_LEVEL_INFO:
Log.notice(F("LVGL: %s:%u %s -> %s"), file, line, funcname, descr);
break;
case LV_LOG_LEVEL_WARN:
Log.warning(F("LVGL: %s:%u %s -> %s"), file, line, funcname, descr);
break;
case LV_LOG_LEVEL_ERROR:
Log.error(F("LVGL: %s:%u %s -> %s"), file, line, funcname, descr);
break;
default:
Log.verbose(F("LVGL: %s:%u %s -> %s"), file, line, funcname, descr);
}
} }
#endif #endif
@ -107,7 +120,7 @@ void tft_espi_flush(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * c
(area->y2 - area->y1 + 1)); /* set the working window */ (area->y2 - area->y1 + 1)); /* set the working window */
if(guiSnapshot != 0) { if(guiSnapshot != 0) {
int i = 0; uint i = 0;
uint8_t pixel[1024]; uint8_t pixel[1024];
for(int y = area->y1; y <= area->y2; y++) { for(int y = area->y1; y <= area->y2; y++) {
@ -312,8 +325,8 @@ void guiSetup(JsonObject settings)
Log.verbose(F("LVGL: VFB size : %d"), (size_t)sizeof(lv_color_t) * guiVDBsize); Log.verbose(F("LVGL: VFB size : %d"), (size_t)sizeof(lv_color_t) * guiVDBsize);
#if LV_USE_LOG != 0 #if LV_USE_LOG != 0
Log.warning(F("LVGL: NOT Registering lvgl logging handler")); Log.warning(F("LVGL: Registering lvgl logging handler"));
// lv_log_register_print_cb(debugLvgl); /* register print function for debugging */ lv_log_register_print_cb(debugLvgl); /* register print function for debugging */
#endif #endif
/* Initialize PNG decoder */ /* Initialize PNG decoder */
@ -479,8 +492,14 @@ bool guiGetConfig(const JsonObject & settings)
return true; return true;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** Set GUI Configuration.
*
* Read the settings from json and sets the application variables.
*
* @note: data pixel should be formated to uint32_t RGBA. Imagemagick requirements.
*
* @param[in] settings JsonObject with the config settings.
**/
bool guiSetConfig(const JsonObject & settings) bool guiSetConfig(const JsonObject & settings)
{ {
configOutput(settings); configOutput(settings);
@ -523,6 +542,13 @@ bool guiSetConfig(const JsonObject & settings)
return changed; return changed;
} }
/** Send Bitmap Header.
*
* Sends a header in BMP format for the size of the screen.
*
* @note: data pixel should be formated to uint32_t RGBA. Imagemagick requirements.
*
**/
void guiSendBmpHeader() void guiSendBmpHeader()
{ {
uint8_t buffer[128]; uint8_t buffer[128];
@ -609,7 +635,7 @@ void guiSendBmpHeader()
} }
} }
/** Flush buffer. /** Take Screenshot.
* *
* Flush buffer into a binary file. * Flush buffer into a binary file.
* *
@ -617,12 +643,12 @@ void guiSendBmpHeader()
* *
* @param[in] pFileName Output binary file name. * @param[in] pFileName Output binary file name.
* *
*/ **/
void guiTakeScreenshot(const char * pFileName) void guiTakeScreenshot(const char * pFileName)
{ {
pFileOut = SPIFFS.open(pFileName, "w"); pFileOut = SPIFFS.open(pFileName, "w");
if(pFileOut == NULL) { if(pFileOut == 0) {
Log.warning(F("GUI: %s cannot be opened"), pFileName); Log.warning(F("GUI: %s cannot be opened"), pFileName);
return; return;
} }

View File

@ -1951,8 +1951,14 @@ bool httpGetConfig(const JsonObject & settings)
return true; return true;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** Set HTTP Configuration.
*
* Read the settings from json and sets the application variables.
*
* @note: data pixel should be formated to uint32_t RGBA. Imagemagick requirements.
*
* @param[in] settings JsonObject with the config settings.
**/
bool httpSetConfig(const JsonObject & settings) bool httpSetConfig(const JsonObject & settings)
{ {
configOutput(settings); configOutput(settings);

View File

@ -81,8 +81,14 @@ bool mdnsGetConfig(const JsonObject & settings)
return true; return true;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** Set MDNS Configuration.
*
* Read the settings from json and sets the application variables.
*
* @note: data pixel should be formated to uint32_t RGBA. Imagemagick requirements.
*
* @param[in] settings JsonObject with the config settings.
**/
bool mdnsSetConfig(const JsonObject & settings) bool mdnsSetConfig(const JsonObject & settings)
{ {
configOutput(settings); configOutput(settings);

View File

@ -310,29 +310,19 @@ void mqttCallback(char * topic, byte * payload, unsigned int length)
char topicBuffer[128]; char topicBuffer[128];
snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic); snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic);
mqttClient.publish(topicBuffer, "ON", true); mqttClient.publish(topicBuffer, "ON", true);
Log.notice(F("MQTT: binary_sensor state: [status] : ON")); Log.notice(F("MQTT: binary_sensor state: [status] : ON"));
// snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("MQTT: binary_sensor state: [%sstatus] : ON"),
// mqttNodeTopic); debugPrintln(topicBuffer);
return; return;
} }
} }
void mqttSubscribeTo(const char * format, const char * data) void mqttSubscribeTo(const char * format, const char * data)
{ {
char buffer[128];
char topic[128]; char topic[128];
snprintf_P(topic, sizeof(topic), format, data); snprintf_P(topic, sizeof(topic), format, data);
if(mqttClient.subscribe(topic)) { if(mqttClient.subscribe(topic)) {
Log.verbose(F("MQTT: * Subscribed to %s"), topic); Log.verbose(F("MQTT: * Subscribed to %s"), topic);
// snprintf_P(buffer, sizeof(buffer), PSTR("MQTT: * Subscribed to %s"), topic);
// debugPrintln(buffer);
} else { } else {
Log.error(F("MQTT: Failed to subscribe to %s"), topic); Log.error(F("MQTT: Failed to subscribe to %s"), topic);
// snprintf_P(buffer, sizeof(buffer), PSTR("MQTT: %%sFailed to subscribe to %s"), topic);
// errorPrintln(buffer);
} }
} }
@ -461,7 +451,6 @@ void mqttSetup(const JsonObject & settings)
mqttClient.setCallback(mqttCallback); mqttClient.setCallback(mqttCallback);
Log.notice(F("MQTT: Setup Complete")); Log.notice(F("MQTT: Setup Complete"));
// debugPrintln(F("MQTT: Setup Complete"));
} }
void mqttLoop() void mqttLoop()
@ -499,7 +488,6 @@ void mqttStop()
mqttClient.disconnect(); mqttClient.disconnect();
Log.notice(F("MQTT: Disconnected from broker")); Log.notice(F("MQTT: Disconnected from broker"));
// debugPrintln(F("MQTT: Disconnected from broker"));
} }
} }
@ -516,8 +504,14 @@ bool mqttGetConfig(const JsonObject & settings)
return true; return true;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** Set MQTT Configuration.
*
* Read the settings from json and sets the application variables.
*
* @note: data pixel should be formated to uint32_t RGBA. Imagemagick requirements.
*
* @param[in] settings JsonObject with the config settings.
**/
bool mqttSetConfig(const JsonObject & settings) bool mqttSetConfig(const JsonObject & settings)
{ {
configOutput(settings); configOutput(settings);

View File

@ -128,7 +128,7 @@ void telnetProcessInput()
dispatchCommand(telnetInputBuffer); dispatchCommand(telnetInputBuffer);
} }
} }
telnetInputIndex = 0; telnetInputIndex = 0; // reset input buffer index
break; break;
default: default:
// If we have room left in our buffer add the current byte // If we have room left in our buffer add the current byte
@ -149,7 +149,7 @@ void telnetSetup(const JsonObject & settings)
telnetClient = new WiFiClient; telnetClient = new WiFiClient;
telnetServer->setNoDelay(true); telnetServer->setNoDelay(true);
telnetServer->begin(); telnetServer->begin();
Log.notice(F("TELNET: Debug console enabled at telnet://%s"), WiFi.localIP().toString().c_str()); Log.notice(F("TELNET: Debug telnet console started"));
} else { } else {
Log.error(F("TELNET: Failed to start telnet server")); Log.error(F("TELNET: Failed to start telnet server"));
} }
@ -197,6 +197,14 @@ bool telnetGetConfig(const JsonObject & settings)
return true; return true;
} }
/** Set TELNET Configuration.
*
* Read the settings from json and sets the application variables.
*
* @note: data pixel should be formated to uint32_t RGBA. Imagemagick requirements.
*
* @param[in] settings JsonObject with the config settings.
**/
bool telnetSetConfig(const JsonObject & settings) bool telnetSetConfig(const JsonObject & settings)
{ {
configOutput(settings); configOutput(settings);

View File

@ -65,7 +65,6 @@ String wifiGetMacAddress(int start, const char * seperator)
void wifiConnected(IPAddress ipaddress) void wifiConnected(IPAddress ipaddress)
{ {
char buffer[128];
bool isConnected = WiFi.status() == WL_CONNECTED; bool isConnected = WiFi.status() == WL_CONNECTED;
Log.notice(F("WIFI: Received IP address %s"), ipaddress.toString().c_str()); Log.notice(F("WIFI: Received IP address %s"), ipaddress.toString().c_str());
@ -81,7 +80,6 @@ void wifiConnected(IPAddress ipaddress)
void wifiDisconnected(const char * ssid, uint8_t reason) void wifiDisconnected(const char * ssid, uint8_t reason)
{ {
char buffer[128];
wifiReconnectCounter++; wifiReconnectCounter++;
if(wifiReconnectCounter > 45) { if(wifiReconnectCounter > 45) {
Log.error(F("WIFI: Retries exceed %u: Rebooting..."), wifiReconnectCounter); Log.error(F("WIFI: Retries exceed %u: Rebooting..."), wifiReconnectCounter);
@ -192,8 +190,14 @@ bool wifiGetConfig(const JsonObject & settings)
return true; return true;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** Set WIFI Configuration.
*
* Read the settings from json and sets the application variables.
*
* @note: data pixel should be formated to uint32_t RGBA. Imagemagick requirements.
*
* @param[in] settings JsonObject with the config settings.
**/
bool wifiSetConfig(const JsonObject & settings) bool wifiSetConfig(const JsonObject & settings)
{ {
configOutput(settings); configOutput(settings);