Indentation correction, const correctess, header guards and small improvements for readability

This commit is contained in:
Unal 2025-08-20 14:13:42 +01:00
parent 3be72bcafa
commit e8e7ae44f2
11 changed files with 142 additions and 156 deletions

View file

@ -1,10 +1,10 @@
#include <WebSocketsClient.h>
#include "OTA.h"
#include "Print.h"
#include "Config.h"
#include "AudioTools.h"
// #include "AudioTools/Concurrency/RTOS.h"
#include "AudioTools/AudioCodecs/CodecOpus.h"
#include <WebSocketsClient.h>
#include "Audio.h"
#include "PitchShift.h"
@ -31,7 +31,7 @@ const int BITS_PER_SAMPLE = 16; // 16-bit audio
// AUDIO OUTPUT
class BufferPrint : public Print {
public:
BufferPrint(BufferRTOS<uint8_t>& buf) : _buffer(buf) {}
explicit BufferPrint(BufferRTOS<uint8_t>& buf) : _buffer(buf) {}
// networkTask -> webSocket.loop() -> webSocketEvent(WStype_BIN, ...) -> opusDecoder.write() -> bufferPrint.write()
virtual size_t write(uint8_t data) override {
@ -230,7 +230,7 @@ void micTask(void *parameter) {
micToWsCopier.setDelayOnNoData(0);
while (1) {
if ( i2sInputFlushScheduled ) {
if (i2sInputFlushScheduled) {
i2sInputFlushScheduled = false;
i2sInput.flush();
}
@ -249,7 +249,7 @@ void micTask(void *parameter) {
// WEBSOCKET EVENTS
// networkTask -> webSocket.loop() -> webSocketEvent()
void webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
void webSocketEvent(WStype_t type, const uint8_t *payload, size_t length)
{
switch (type)
{
@ -367,9 +367,9 @@ void webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
}
// wifiTask -> WIFIMANAGER::loop() -> WIFIMANAGER::tryConnect() -> connectCb() -> websocketSetup()
void websocketSetup(String server_domain, int port, String path)
void websocketSetup(const String& server_domain, int port, const String& path)
{
String headers = "Authorization: Bearer " + String(authTokenGlobal);
const String headers = "Authorization: Bearer " + String(authTokenGlobal);
xSemaphoreTake(wsMutex, portMAX_DELAY);

View file

@ -1,10 +1,12 @@
#ifndef AUDIO_H
#define AUDIO_H
#include "Print.h"
#include "Config.h"
#include "AudioTools.h"
// #include "AudioTools/Concurrency/RTOS.h"
#include "AudioTools/AudioCodecs/CodecOpus.h"
#include <WebSocketsClient.h>
#include "Print.h"
#include "AudioTools.h"
#include "AudioTools/AudioCodecs/CodecOpus.h"
// #include "AudioTools/Concurrency/RTOS.h"
#include "Config.h"
extern SemaphoreHandle_t wsMutex;
extern WebSocketsClient webSocket;
@ -44,8 +46,8 @@ extern StreamCopy micToWsCopier;
extern volatile bool i2sInputFlushScheduled;
// WEBSOCKET
void webSocketEvent(WStype_t type, uint8_t *payload, size_t length);
void websocketSetup(String server_domain, int port, String path);
void webSocketEvent(WStype_t type, const uint8_t *payload, size_t length);
void websocketSetup(const String& server_domain, int port, const String& path);
void networkTask(void *parameter);
// AUDIO OUTPUT
@ -53,4 +55,6 @@ unsigned long getSpeakingDuration();
void audioStreamTask(void *parameter);
// AUDIO INPUT
void micTask(void *parameter);
void micTask(void *parameter);
#endif

View file

@ -1,5 +1,5 @@
#include "Config.h"
#include <nvs_flash.h>
#include "Config.h"
// ! define preferences
Preferences preferences;

View file

@ -1,6 +1,9 @@
#include <Config.h>
#ifndef FACTORYRESET_H
#define FACTORYRESET_H
#include <nvs_flash.h>
#include <ESPAsyncWebServer.h> //https://github.com/me-no-dev/ESPAsyncWebServer using the latest dev version from @me-no-dev
#include "Config.h"
void setResetComplete() {
HTTPClient http;
@ -62,19 +65,21 @@ void setFactoryResetStatusInNVS(bool status)
}
void factoryResetDevice() {
Serial.println("Factory reset device");
// Erase the NVS partition
esp_err_t err = nvs_flash_erase();
if (err != ESP_OK) {
Serial.printf("Error erasing NVS: %d\n", err);
return;
}
// Reinitialize NVS
err = nvs_flash_init();
if (err != ESP_OK) {
Serial.printf("Error initializing NVS: %d\n", err);
return;
}
}
Serial.println("Factory reset device");
// Erase the NVS partition
esp_err_t err = nvs_flash_erase();
if (err != ESP_OK) {
Serial.printf("Error erasing NVS: %d\n", err);
return;
}
// Reinitialize NVS
err = nvs_flash_init();
if (err != ESP_OK) {
Serial.printf("Error initializing NVS: %d\n", err);
return;
}
}
#endif

View file

@ -12,7 +12,7 @@ void setLEDColor(uint8_t r, uint8_t g, uint8_t b)
analogWrite(BLUE_LED_PIN, b);
}
enum class StaticColor
enum class StaticColor : uint8_t
{
RED,
GREEN,
@ -22,41 +22,44 @@ enum class StaticColor
CYAN,
};
struct RGB {
bool red;
bool green;
bool blue;
};
void setStaticColor(StaticColor color)
{
RGB colorMap;
switch (color)
{
case StaticColor::RED:
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(BLUE_LED_PIN, HIGH);
colorMap = {LOW, HIGH, HIGH};
break;
case StaticColor::GREEN:
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, HIGH);
colorMap = {HIGH, LOW, HIGH};
break;
case StaticColor::BLUE:
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(BLUE_LED_PIN, LOW);
colorMap = {HIGH, HIGH, LOW};
break;
case StaticColor::YELLOW:
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, HIGH);
colorMap = {LOW, LOW, HIGH};
break;
case StaticColor::MAGENTA:
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(BLUE_LED_PIN, LOW);
colorMap = {LOW, HIGH, LOW};
break;
case StaticColor::CYAN:
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
colorMap = {HIGH, LOW, LOW};
break;
default:
colorMap = {HIGH, HIGH, HIGH};
break;
}
digitalWrite(RED_LED_PIN, colorMap.red);
digitalWrite(GREEN_LED_PIN, colorMap.green);
digitalWrite(BLUE_LED_PIN, colorMap.blue);
}
void loopCyanPinkYellow()
@ -122,47 +125,26 @@ void pulseBlue()
void blinkWhite()
{
if (ledState)
{
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(BLUE_LED_PIN, HIGH);
}
else
{
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
}
int ledState = ledState ? HIGH : LOW;
digitalWrite(RED_LED_PIN, ledState);
digitalWrite(GREEN_LED_PIN, ledState);
digitalWrite(BLUE_LED_PIN, ledState);
}
void blinkGreen()
{
int ledState = ledState ? HIGH : LOW;
digitalWrite(BLUE_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
if (ledState)
{
digitalWrite(GREEN_LED_PIN, HIGH);
}
else
{
digitalWrite(GREEN_LED_PIN, LOW);
}
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, ledState);
}
void blinkYellow()
{
int ledState = ledState ? HIGH : LOW;
digitalWrite(BLUE_LED_PIN, LOW);
if (ledState)
{
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, HIGH);
}
else
{
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
}
digitalWrite(RED_LED_PIN, ledState);
digitalWrite(GREEN_LED_PIN, ledState);
}
void turnOffLED()
@ -199,12 +181,29 @@ void blinkCyanPulse()
}
}
const uint8_t colorSequence[][3] = {
void blinkBlue()
{
int ledState = ledState ? HIGH : LOW;
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, ledState);
}
void staticYellow()
{
digitalWrite(BLUE_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, HIGH);
}
static const uint8_t colorSequence[][3] = {
{0, 255, 255}, // Cyan (R=0, G=255, B=255)
{255, 0, 255}, // Pink (R=255, G=0, B=255)
{255, 255, 0}, // Yellow (R=255, G=255, B=0)
};
const int NUM_COLORS = sizeof(colorSequence) / sizeof(colorSequence[0]);
static const int NUM_COLORS = sizeof(colorSequence) / sizeof(colorSequence[0]);
void loopCyanPinkYellowPulse(unsigned long currentTime)
{
@ -268,27 +267,6 @@ void loopCyanPinkYellowPulse(unsigned long currentTime)
}
}
void blinkBlue()
{
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
if (ledState)
{
digitalWrite(BLUE_LED_PIN, HIGH);
}
else
{
digitalWrite(BLUE_LED_PIN, LOW);
}
}
void staticYellow()
{
digitalWrite(BLUE_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, HIGH);
}
void ledTask(void *parameter)
{
setupRGBLED();
@ -304,7 +282,7 @@ void ledTask(void *parameter)
lastToggle = currentTime;
}
switch (deviceState)
switch (deviceState)
{
case IDLE:
setStaticColor(StaticColor::GREEN);

View file

@ -47,7 +47,7 @@ void markOTAUpdateComplete() {
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
Serial.println("OTA status updated successfully");
setOTAStatusInNVS(OTA_IDLE);
setOTAStatusInNVS(OTA_IDLE);
} else {
Serial.printf("OTA status update failed with code: %d\n", httpCode);
}

View file

@ -1,5 +1,6 @@
#ifndef OTA_H
#define OTA_H
#include "Config.h"
extern const char *server_certificate;

View file

@ -1,4 +1,5 @@
#pragma once
#ifndef PITCHSHIFT_H
#define PITCHSHIFT_H
#include <math.h>
#include <stdio.h>
@ -9,7 +10,7 @@
//pitch shift effect with interpolaion fixed to 1.5 frequency factor, fixed delay, int16_t, 1 channel
class PitchShiftFixedOutput : public AudioOutput {
public:
PitchShiftFixedOutput(Print &out) { p_out = &out; }
explicit PitchShiftFixedOutput(Print &out) { p_out = &out; }
PitchShiftInfo defaultConfig() {
PitchShiftInfo result;
@ -41,4 +42,6 @@ protected:
int16_t pitchShift(int16_t value);
uint32_t pitchMul;
unsigned long secondaryOffset;
};
};
#endif

View file

@ -205,7 +205,7 @@ void WIFIMANAGER::fallbackToSoftAp(const bool state) {
* @return true
* @return false
*/
bool WIFIMANAGER::getFallbackState() {
bool WIFIMANAGER::getFallbackState() const {
return createFallbackAP;
}
@ -349,7 +349,7 @@ bool WIFIMANAGER::delWifi(String apName) {
* @return true if one or more SSIDs stored
* @return false if no configuration is available
*/
bool WIFIMANAGER::configAvailable() {
bool WIFIMANAGER::configAvailable() const {
return configuredSSIDs != 0;
}

View file

@ -42,7 +42,7 @@ class WIFIMANAGER {
String uiPrefix = "/wifi"; // Prefix for all UI endpionts
Preferences preferences; // Used to store AP credentials to NVS
char * NVS; // Name used for NVS preferences
char* NVS; // Name used for NVS preferences
struct apCredentials_t {
String apName; // Name of the AP SSID
@ -83,7 +83,7 @@ class WIFIMANAGER {
void fallbackToSoftAp(bool state = true);
// Get the current fallback state
bool getFallbackState();
bool getFallbackState() const;
// Call to run the Task in the background
void startBackgroundTask(String apName = "", String apPass = "");
@ -110,7 +110,7 @@ class WIFIMANAGER {
bool tryConnect();
// Check if a SSID is stored in the config
bool configAvailable();
bool configAvailable() const;
// Preconfigure the SoftAP
void configueSoftAp(String apName = "", String apPass = "");

View file

@ -1,12 +1,7 @@
#include "OTA.h"
#include <Arduino.h>
#include <driver/rtc_io.h>
#include "LEDHandler.h"
#include "Config.h"
#include "SPIFFS.h"
#include "WifiManager.h"
#include <driver/touch_sensor.h>
#include "Button.h"
#include "OTA.h"
#include "WifiManager.h"
#include "LEDHandler.h"
#include "FactoryReset.h"
#define TOUCH_THRESHOLD 28000
@ -131,43 +126,43 @@ void setupWiFi()
}
void touchTask(void* parameter) {
touch_pad_init();
touch_pad_config(TOUCH_PAD_NUM2);
touch_pad_init();
touch_pad_config(TOUCH_PAD_NUM2);
bool touched = false;
unsigned long pressStartTime = 0;
unsigned long lastTouchTime = 0;
const unsigned long LONG_PRESS_DURATION = 500; // 500ms for long press
bool touched = false;
unsigned long pressStartTime = 0;
unsigned long lastTouchTime = 0;
const unsigned long LONG_PRESS_DURATION = 500; // 500ms for long press
while (1) {
// Read the touch sensor
uint32_t touchValue = touchRead(TOUCH_PAD_NUM2);
bool isTouched = (touchValue > TOUCH_THRESHOLD);
unsigned long currentTime = millis();
while (1) {
// Read the touch sensor
uint32_t touchValue = touchRead(TOUCH_PAD_NUM2);
bool isTouched = (touchValue > TOUCH_THRESHOLD);
unsigned long currentTime = millis();
// Initial touch detection
if (isTouched && !touched && (currentTime - lastTouchTime > TOUCH_DEBOUNCE_DELAY)) {
touched = true;
pressStartTime = currentTime; // Start timing the press
lastTouchTime = currentTime;
// Initial touch detection
if (isTouched && !touched && (currentTime - lastTouchTime > TOUCH_DEBOUNCE_DELAY)) {
touched = true;
pressStartTime = currentTime; // Start timing the press
lastTouchTime = currentTime;
}
// Check for long press while touched
if (touched && isTouched) {
if (currentTime - pressStartTime >= LONG_PRESS_DURATION) {
sleepRequested = true; // Only enter sleep after 500ms of continuous touch
}
}
// Release detection
if (!isTouched && touched) {
touched = false;
pressStartTime = 0; // Reset the press timer
}
vTaskDelay(20); // Reduced from 50ms to 20ms for better responsiveness
}
// Check for long press while touched
if (touched && isTouched) {
if (currentTime - pressStartTime >= LONG_PRESS_DURATION) {
sleepRequested = true; // Only enter sleep after 500ms of continuous touch
}
}
// Release detection
if (!isTouched && touched) {
touched = false;
pressStartTime = 0; // Reset the press timer
}
vTaskDelay(20); // Reduced from 50ms to 20ms for better responsiveness
}
vTaskDelete(NULL);
vTaskDelete(NULL);
}
void setupDeviceMetadata() {