More code formatting improvements

This commit is contained in:
Djuri Baars 2023-11-30 22:56:50 +01:00
parent 4aab02a040
commit 86d299fd75
25 changed files with 417 additions and 467 deletions

View file

@ -177,8 +177,7 @@ void setBlockHeight(uint newBlockHeight) {
} }
bool isBlockNotifyConnected() { bool isBlockNotifyConnected() {
if (blockNotifyClient == NULL) if (blockNotifyClient == NULL) return false;
return false;
return esp_websocket_client_is_connected(blockNotifyClient); return esp_websocket_client_is_connected(blockNotifyClient);
} }

View file

@ -1,15 +1,17 @@
#pragma once #pragma once
#include <Arduino.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <esp_timer.h>
#include <esp_websocket_client.h>
#include <cstring>
#include <string>
#include "lib/led_handler.hpp" #include "lib/led_handler.hpp"
#include "lib/screen_handler.hpp" #include "lib/screen_handler.hpp"
#include "lib/shared.hpp" #include "lib/shared.hpp"
#include <Arduino.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <cstring>
#include <esp_timer.h>
#include <esp_websocket_client.h>
#include <string>
// using namespace websockets; // using namespace websockets;

View file

@ -1,8 +1,9 @@
#pragma once #pragma once
#include <Arduino.h>
#include "lib/screen_handler.hpp" #include "lib/screen_handler.hpp"
#include "lib/shared.hpp" #include "lib/shared.hpp"
#include <Arduino.h>
extern TaskHandle_t buttonTaskHandle; extern TaskHandle_t buttonTaskHandle;

View file

@ -222,7 +222,6 @@ void setupTimers() {
} }
void finishSetup() { void finishSetup() {
if (preferences.getBool("ledStatus", false)) { if (preferences.getBool("ledStatus", false)) {
restoreLedState(); restoreLedState();
} else { } else {
@ -389,8 +388,8 @@ bool onImprovCommandCallback(improv::ImprovCommand cmd) {
if (improv_connectWifi(cmd.ssid, cmd.password)) { if (improv_connectWifi(cmd.ssid, cmd.password)) {
queueLedEffect(LED_EFFECT_WIFI_CONNECT_SUCCESS); queueLedEffect(LED_EFFECT_WIFI_CONNECT_SUCCESS);
// std::array<String, NUM_SCREENS> epdContent = {"S", "U", "C", "C", "E", // std::array<String, NUM_SCREENS> epdContent = {"S", "U", "C", "C",
// "S", "S"}; setEpdContent(epdContent); // "E", "S", "S"}; setEpdContent(epdContent);
preferences.putBool("wifiConfigured", true); preferences.putBool("wifiConfigured", true);
@ -452,8 +451,7 @@ void improv_set_state(improv::State state) {
data[9] = state; data[9] = state;
uint8_t checksum = 0x00; uint8_t checksum = 0x00;
for (uint8_t d : data) for (uint8_t d : data) checksum += d;
checksum += d;
data[10] = checksum; data[10] = checksum;
Serial.write(data.data(), data.size()); Serial.write(data.data(), data.size());
@ -468,8 +466,7 @@ void improv_send_response(std::vector<uint8_t> &response) {
data.insert(data.end(), response.begin(), response.end()); data.insert(data.end(), response.begin(), response.end());
uint8_t checksum = 0x00; uint8_t checksum = 0x00;
for (uint8_t d : data) for (uint8_t d : data) checksum += d;
checksum += d;
data.push_back(checksum); data.push_back(checksum);
Serial.write(data.data(), data.size()); Serial.write(data.data(), data.size());
@ -484,8 +481,7 @@ void improv_set_error(improv::Error error) {
data[9] = error; data[9] = error;
uint8_t checksum = 0x00; uint8_t checksum = 0x00;
for (uint8_t d : data) for (uint8_t d : data) checksum += d;
checksum += d;
data[10] = checksum; data[10] = checksum;
Serial.write(data.data(), data.size()); Serial.write(data.data(), data.size());
@ -529,8 +525,7 @@ void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP: { case ARDUINO_EVENT_WIFI_STA_GOT_IP: {
Serial.print("Obtained IP address: "); Serial.print("Obtained IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
if (!first_connect) if (!first_connect) queueLedEffect(LED_EFFECT_WIFI_CONNECT_SUCCESS);
queueLedEffect(LED_EFFECT_WIFI_CONNECT_SUCCESS);
first_connect = false; first_connect = false;
break; break;
} }

View file

@ -6,6 +6,7 @@
#include <WiFiManager.h> #include <WiFiManager.h>
#include <base64.h> #include <base64.h>
#include <esp_task_wdt.h> #include <esp_task_wdt.h>
#include <map> #include <map>
#include "lib/block_notify.hpp" #include "lib/block_notify.hpp"

View file

@ -185,7 +185,6 @@ void prepareDisplayUpdateTask(void *pvParameters) {
} else if (epdContent[epdIndex].length() > 5) { } else if (epdContent[epdIndex].length() > 5) {
renderText(epdIndex, epdContent[epdIndex], updatePartial); renderText(epdIndex, epdContent[epdIndex], updatePartial);
} else { } else {
if (epdContent[epdIndex].length() > 1) { if (epdContent[epdIndex].length() > 1) {
showChars(epdIndex, epdContent[epdIndex], updatePartial, showChars(epdIndex, epdContent[epdIndex], updatePartial,
&FONT_MEDIUM); &FONT_MEDIUM);
@ -237,8 +236,7 @@ extern "C" void updateDisplay(void *pvParameters) noexcept {
if (displays[epdIndex].displayWithReturn(updatePartial)) { if (displays[epdIndex].displayWithReturn(updatePartial)) {
displays[epdIndex].powerOff(); displays[epdIndex].powerOff();
currentEpdContent[epdIndex] = epdContent[epdIndex]; currentEpdContent[epdIndex] = epdContent[epdIndex];
if (!updatePartial) if (!updatePartial) lastFullRefresh[epdIndex] = millis();
lastFullRefresh[epdIndex] = millis();
if (eventSourceTaskHandle != NULL) if (eventSourceTaskHandle != NULL)
xTaskNotifyGive(eventSourceTaskHandle); xTaskNotifyGive(eventSourceTaskHandle);

View file

@ -1,16 +1,18 @@
#pragma once #pragma once
#include "fonts/fonts.hpp"
#include "lib/config.hpp"
#include "lib/shared.hpp"
#include <Fonts/FreeSans9pt7b.h> #include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSansBold9pt7b.h> #include <Fonts/FreeSansBold9pt7b.h>
#include <GxEPD2_BW.h> #include <GxEPD2_BW.h>
#include <mcp23x17_pin.hpp> #include <mcp23x17_pin.hpp>
#include <mutex> #include <mutex>
#include <native_pin.hpp> #include <native_pin.hpp>
#include <regex> #include <regex>
#include "fonts/fonts.hpp"
#include "lib/config.hpp"
#include "lib/shared.hpp"
#ifdef USE_QR #ifdef USE_QR
#include "qrcodegen.h" #include "qrcodegen.h"
#endif #endif

View file

@ -54,35 +54,25 @@ bool parse_improv_serial_byte(size_t position, uint8_t byte,
const uint8_t *buffer, const uint8_t *buffer,
std::function<bool(ImprovCommand)> &&callback, std::function<bool(ImprovCommand)> &&callback,
std::function<void(Error)> &&on_error) { std::function<void(Error)> &&on_error) {
if (position == 0) if (position == 0) return byte == 'I';
return byte == 'I'; if (position == 1) return byte == 'M';
if (position == 1) if (position == 2) return byte == 'P';
return byte == 'M'; if (position == 3) return byte == 'R';
if (position == 2) if (position == 4) return byte == 'O';
return byte == 'P'; if (position == 5) return byte == 'V';
if (position == 3)
return byte == 'R';
if (position == 4)
return byte == 'O';
if (position == 5)
return byte == 'V';
if (position == 6) if (position == 6) return byte == IMPROV_SERIAL_VERSION;
return byte == IMPROV_SERIAL_VERSION;
if (position <= 8) if (position <= 8) return true;
return true;
uint8_t type = buffer[7]; uint8_t type = buffer[7];
uint8_t data_len = buffer[8]; uint8_t data_len = buffer[8];
if (position <= 8 + data_len) if (position <= 8 + data_len) return true;
return true;
if (position == 8 + data_len + 1) { if (position == 8 + data_len + 1) {
uint8_t checksum = 0x00; uint8_t checksum = 0x00;
for (size_t i = 0; i < position; i++) for (size_t i = 0; i < position; i++) checksum += buffer[i];
checksum += buffer[i];
if (checksum != byte) { if (checksum != byte) {
on_error(ERROR_INVALID_RPC); on_error(ERROR_INVALID_RPC);

View file

@ -76,8 +76,7 @@ void ledTask(void *parameter) {
for (int i = NEOPIXEL_COUNT; i >= 0; i--) { for (int i = NEOPIXEL_COUNT; i >= 0; i--) {
for (int j = NEOPIXEL_COUNT; j >= 0; j--) { for (int j = NEOPIXEL_COUNT; j >= 0; j--) {
uint32_t c = pixels.Color(0, 0, 0); uint32_t c = pixels.Color(0, 0, 0);
if (i == j) if (i == j) c = pixels.Color(0, 255, 0);
c = pixels.Color(0, 255, 0);
pixels.setPixelColor(j, c); pixels.setPixelColor(j, c);
} }
@ -101,11 +100,9 @@ void ledTask(void *parameter) {
delay(900); delay(900);
for (int i = NEOPIXEL_COUNT; i--; i > 0) { for (int i = NEOPIXEL_COUNT; i--; i > 0) {
for (int j = NEOPIXEL_COUNT; j--; j > 0) { for (int j = NEOPIXEL_COUNT; j--; j > 0) {
uint32_t c = pixels.Color(0, 0, 0); uint32_t c = pixels.Color(0, 0, 0);
if (i == j) if (i == j) c = pixels.Color(0, 255, 0);
c = pixels.Color(0, 255, 0);
pixels.setPixelColor(j, c); pixels.setPixelColor(j, c);
} }
@ -155,7 +152,6 @@ void setupLedTask() {
void blinkDelay(int d, int times) { void blinkDelay(int d, int times) {
for (int j = 0; j < times; j++) { for (int j = 0; j < times; j++) {
pixels.setPixelColor(0, pixels.Color(255, 0, 0)); pixels.setPixelColor(0, pixels.Color(255, 0, 0));
pixels.setPixelColor(1, pixels.Color(0, 255, 0)); pixels.setPixelColor(1, pixels.Color(0, 255, 0));
pixels.setPixelColor(2, pixels.Color(255, 0, 0)); pixels.setPixelColor(2, pixels.Color(255, 0, 0));
@ -218,7 +214,6 @@ void clearLeds() {
void setLights(int r, int g, int b) { setLights(pixels.Color(r, g, b)); } void setLights(int r, int g, int b) { setLights(pixels.Color(r, g, b)); }
void setLights(uint32_t color) { void setLights(uint32_t color) {
bool ledStatus = true; bool ledStatus = true;
for (int i = 0; i < NEOPIXEL_COUNT; i++) { for (int i = 0; i < NEOPIXEL_COUNT; i++) {

View file

@ -1,12 +1,13 @@
#pragma once #pragma once
#include "lib/shared.hpp"
#include "lib/webserver.hpp"
#include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
#include <Arduino.h> #include <Arduino.h>
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h> #include <freertos/task.h>
#include "lib/shared.hpp"
#include "lib/webserver.hpp"
#ifndef NEOPIXEL_PIN #ifndef NEOPIXEL_PIN
#define NEOPIXEL_PIN 34 #define NEOPIXEL_PIN 34
#endif #endif

View file

@ -1,8 +1,9 @@
#include "lib/config.hpp"
#include "lib/shared.hpp"
#include <Arduino.h> #include <Arduino.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include "lib/config.hpp"
#include "lib/shared.hpp"
void setupOTA(); void setupOTA();
void onOTAStart(); void onOTAStart();
void handleOTATask(void *parameter); void handleOTATask(void *parameter);

View file

@ -1,6 +1,7 @@
#include "price_fetch.hpp" #include "price_fetch.hpp"
const PROGMEM char *cgApiUrl = "https://api.coingecko.com/api/v3/simple/" const PROGMEM char *cgApiUrl =
"https://api.coingecko.com/api/v3/simple/"
"price?ids=bitcoin&vs_currencies=usd%2Ceur"; "price?ids=bitcoin&vs_currencies=usd%2Ceur";
TaskHandle_t priceFetchTaskHandle; TaskHandle_t priceFetchTaskHandle;

View file

@ -1,8 +1,9 @@
#include "lib/config.hpp"
#include "lib/shared.hpp"
#include <Arduino.h> #include <Arduino.h>
#include <HTTPClient.h> #include <HTTPClient.h>
#include "lib/config.hpp"
#include "lib/shared.hpp"
extern TaskHandle_t priceFetchTaskHandle; extern TaskHandle_t priceFetchTaskHandle;
void setupPriceFetchTask(); void setupPriceFetchTask();

View file

@ -79,7 +79,6 @@ void onWebsocketPriceMessage(esp_websocket_event_data_t *event_data) {
if (doc.containsKey("bitcoin")) { if (doc.containsKey("bitcoin")) {
if (currentPrice != doc["bitcoin"].as<long>()) { if (currentPrice != doc["bitcoin"].as<long>()) {
uint minSecPriceUpd = preferences.getUInt( uint minSecPriceUpd = preferences.getUInt(
"minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE); "minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE);
uint currentTime = esp_timer_get_time() / 1000000; uint currentTime = esp_timer_get_time() / 1000000;
@ -108,8 +107,7 @@ uint getPrice() { return currentPrice; }
void setPrice(uint newPrice) { currentPrice = newPrice; } void setPrice(uint newPrice) { currentPrice = newPrice; }
bool isPriceNotifyConnected() { bool isPriceNotifyConnected() {
if (clientPrice == NULL) if (clientPrice == NULL) return false;
return false;
return esp_websocket_client_is_connected(clientPrice); return esp_websocket_client_is_connected(clientPrice);
} }

View file

@ -2,13 +2,12 @@
#include <Arduino.h> #include <Arduino.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <esp_websocket_client.h>
#include <string> #include <string>
#include <esp_websocket_client.h>
#include "lib/screen_handler.hpp" #include "lib/screen_handler.hpp"
// using namespace websockets;
void setupPriceNotify(); void setupPriceNotify();
void onWebsocketPriceEvent(void *handler_args, esp_event_base_t base, void onWebsocketPriceEvent(void *handler_args, esp_event_base_t base,

View file

@ -12,19 +12,6 @@ std::array<std::string, NUM_SCREENS> taskEpdContent = {"", "", "", "",
"", "", ""}; "", "", ""};
std::string priceString; std::string priceString;
// typedef enum
// {
// TASK_PRICE_UPDATE,
// TASK_BLOCK_UPDATE,
// TASK_TIME_UPDATE
// } TaskType;
// typedef struct
// {
// TaskType type;
// unsigned long data;
// } WorkItem;
#define WORK_QUEUE_SIZE 10 #define WORK_QUEUE_SIZE 10
QueueHandle_t workQueue = NULL; QueueHandle_t workQueue = NULL;
@ -85,7 +72,8 @@ void workerTask(void *pvParameters) {
minute = "0" + minute; minute = "0" + minute;
} }
timeString = std::to_string(timeinfo.tm_hour) + ":" + minute.c_str(); timeString =
std::to_string(timeinfo.tm_hour) + ":" + minute.c_str();
timeString.insert(timeString.begin(), timeString.insert(timeString.begin(),
NUM_SCREENS - timeString.length(), ' '); NUM_SCREENS - timeString.length(), ' ');
taskEpdContent[0] = std::to_string(timeinfo.tm_mday) + "/" + taskEpdContent[0] = std::to_string(timeinfo.tm_mday) + "/" +
@ -145,11 +133,6 @@ void IRAM_ATTR screenRotateTimerISR(void *arg) {
void setupTasks() { void setupTasks() {
workQueue = xQueueCreate(WORK_QUEUE_SIZE, sizeof(WorkItem)); workQueue = xQueueCreate(WORK_QUEUE_SIZE, sizeof(WorkItem));
// xTaskCreate(taskPriceUpdate, "updatePrice", 1024, NULL, tskIDLE_PRIORITY,
// &priceUpdateTaskHandle); xTaskCreate(taskBlockUpdate, "updateBlock", 1024,
// NULL, tskIDLE_PRIORITY, &blockUpdateTaskHandle);
// xTaskCreate(taskTimeUpdate, "updateTime", 1024, NULL, tskIDLE_PRIORITY,
// &timeUpdateTaskHandle);
xTaskCreate(workerTask, "workerTask", 4096, NULL, tskIDLE_PRIORITY, xTaskCreate(workerTask, "workerTask", 4096, NULL, tskIDLE_PRIORITY,
&workerTaskHandle); &workerTaskHandle);
@ -214,8 +197,7 @@ void setTimerActive(bool status) {
preferences.putBool("timerActive", false); preferences.putBool("timerActive", false);
} }
if (eventSourceTaskHandle != NULL) if (eventSourceTaskHandle != NULL) xTaskNotifyGive(eventSourceTaskHandle);
xTaskNotifyGive(eventSourceTaskHandle);
} }
void toggleTimerActive() { setTimerActive(!isTimerActive()); } void toggleTimerActive() { setTimerActive(!isTimerActive()); }
@ -253,8 +235,7 @@ void setCurrentScreen(uint newScreen) {
} }
} }
if (eventSourceTaskHandle != NULL) if (eventSourceTaskHandle != NULL) xTaskNotifyGive(eventSourceTaskHandle);
xTaskNotifyGive(eventSourceTaskHandle);
} }
void nextScreen() { void nextScreen() {
@ -269,7 +250,6 @@ void nextScreen() {
} }
void previousScreen() { void previousScreen() {
int newCurrentScreen = modulo(getCurrentScreen() - 1, SCREEN_COUNT); int newCurrentScreen = modulo(getCurrentScreen() - 1, SCREEN_COUNT);
String key = "screen" + String(newCurrentScreen) + "Visible"; String key = "screen" + String(newCurrentScreen) + "Visible";

View file

@ -1,11 +1,11 @@
#pragma once #pragma once
#include <esp_timer.h> #include <esp_timer.h>
#include <data_handler.hpp>
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h> #include <freertos/task.h>
#include <data_handler.hpp>
#include "lib/epd.hpp" #include "lib/epd.hpp"
#include "lib/price_fetch.hpp" #include "lib/price_fetch.hpp"
#include "lib/shared.hpp" #include "lib/shared.hpp"

View file

@ -5,8 +5,8 @@
#include <Preferences.h> #include <Preferences.h>
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h> #include <freertos/task.h>
#include <mutex>
#include <mutex>
#include <utils.hpp> #include <utils.hpp>
extern Adafruit_MCP23X17 mcp1; extern Adafruit_MCP23X17 mcp1;

View file

@ -5,7 +5,6 @@ AsyncEventSource events("/events");
TaskHandle_t eventSourceTaskHandle; TaskHandle_t eventSourceTaskHandle;
void setupWebserver() { void setupWebserver() {
events.onConnect([](AsyncEventSourceClient *client) { events.onConnect([](AsyncEventSourceClient *client) {
client->send("welcome", NULL, millis(), 1000); client->send("welcome", NULL, millis(), 1000);
}); });
@ -139,8 +138,7 @@ StaticJsonDocument<512> getLedStatusObject() {
} }
void eventSourceUpdate() { void eventSourceUpdate() {
if (!events.count()) if (!events.count()) return;
return;
StaticJsonDocument<768> root = getStatusObject(); StaticJsonDocument<768> root = getStatusObject();
JsonArray data = root.createNestedArray("data"); JsonArray data = root.createNestedArray("data");
@ -369,8 +367,7 @@ void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json) {
void onApiRestart(AsyncWebServerRequest *request) { void onApiRestart(AsyncWebServerRequest *request) {
request->send(200); request->send(200);
if (events.count()) if (events.count()) events.send("closing");
events.send("closing");
delay(500); delay(500);
@ -742,7 +739,6 @@ void onApiLightsSetJson(AsyncWebServerRequest *request, JsonVariant &json) {
if (lights[i].containsKey("red") && lights[i].containsKey("green") && if (lights[i].containsKey("red") && lights[i].containsKey("green") &&
lights[i].containsKey("blue")) { lights[i].containsKey("blue")) {
red = lights[i]["red"].as<uint>(); red = lights[i]["red"].as<uint>();
green = lights[i]["green"].as<uint>(); green = lights[i]["green"].as<uint>();
blue = lights[i]["blue"].as<uint>(); blue = lights[i]["blue"].as<uint>();

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
// Keep order of includes because of conflicts
#include "WebServer.h" #include "WebServer.h"
#include "ESPAsyncWebServer.h" #include "ESPAsyncWebServer.h"
#include <ArduinoJson.h> #include <ArduinoJson.h>
@ -8,20 +9,10 @@
#include "AsyncJson.h" #include "AsyncJson.h"
#include <iostream> #include <iostream>
// #include "AsyncJson.h"
// #include "ESPAsyncWebServer.h"
// #include "WebServer.h"
// #include <ArduinoJson.h>
// #include <ESPmDNS.h>
// #include <LittleFS.h>
// #include <iostream>
#include "lib/block_notify.hpp" #include "lib/block_notify.hpp"
#include "lib/led_handler.hpp" #include "lib/led_handler.hpp"
#include "lib/price_notify.hpp" #include "lib/price_notify.hpp"
#include "lib/screen_handler.hpp" #include "lib/screen_handler.hpp"
#include "webserver/OneParamRewrite.hpp" #include "webserver/OneParamRewrite.hpp"
extern TaskHandle_t eventSourceTaskHandle; extern TaskHandle_t eventSourceTaskHandle;

View file

@ -2,7 +2,6 @@
OneParamRewrite::OneParamRewrite(const char *from, const char *to) OneParamRewrite::OneParamRewrite(const char *from, const char *to)
: AsyncWebRewrite(from, to) { : AsyncWebRewrite(from, to) {
_paramIndex = _from.indexOf('{'); _paramIndex = _from.indexOf('{');
if (_paramIndex >= 0 && _from.endsWith("}")) { if (_paramIndex >= 0 && _from.endsWith("}")) {

View file

@ -3,12 +3,12 @@
#include "ESPAsyncWebServer.h" #include "ESPAsyncWebServer.h"
class OneParamRewrite : public AsyncWebRewrite { class OneParamRewrite : public AsyncWebRewrite {
protected: protected:
String _urlPrefix; String _urlPrefix;
int _paramIndex; int _paramIndex;
String _paramsBackup; String _paramsBackup;
public: public:
OneParamRewrite(const char *from, const char *to); OneParamRewrite(const char *from, const char *to);
bool match(AsyncWebServerRequest *request) override; bool match(AsyncWebServerRequest *request) override;
}; };