Merge pull request #15 from embedded4ever/submit_cosmetic_and_small_improvements

Indentation correction, const correctess, header guards and small improvements for readability
This commit is contained in:
Akashdeep Deb 2025-08-21 13:20:54 +01:00 committed by GitHub
commit 376ab29202
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 142 additions and 156 deletions

View file

@ -1,10 +1,10 @@
#include <WebSocketsClient.h>
#include "OTA.h" #include "OTA.h"
#include "Print.h" #include "Print.h"
#include "Config.h" #include "Config.h"
#include "AudioTools.h" #include "AudioTools.h"
// #include "AudioTools/Concurrency/RTOS.h" // #include "AudioTools/Concurrency/RTOS.h"
#include "AudioTools/AudioCodecs/CodecOpus.h" #include "AudioTools/AudioCodecs/CodecOpus.h"
#include <WebSocketsClient.h>
#include "Audio.h" #include "Audio.h"
#include "PitchShift.h" #include "PitchShift.h"
@ -31,7 +31,7 @@ const int BITS_PER_SAMPLE = 16; // 16-bit audio
// AUDIO OUTPUT // AUDIO OUTPUT
class BufferPrint : public Print { class BufferPrint : public Print {
public: 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() // networkTask -> webSocket.loop() -> webSocketEvent(WStype_BIN, ...) -> opusDecoder.write() -> bufferPrint.write()
virtual size_t write(uint8_t data) override { virtual size_t write(uint8_t data) override {
@ -230,7 +230,7 @@ void micTask(void *parameter) {
micToWsCopier.setDelayOnNoData(0); micToWsCopier.setDelayOnNoData(0);
while (1) { while (1) {
if ( i2sInputFlushScheduled ) { if (i2sInputFlushScheduled) {
i2sInputFlushScheduled = false; i2sInputFlushScheduled = false;
i2sInput.flush(); i2sInput.flush();
} }
@ -249,7 +249,7 @@ void micTask(void *parameter) {
// WEBSOCKET EVENTS // WEBSOCKET EVENTS
// networkTask -> webSocket.loop() -> webSocketEvent() // 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) switch (type)
{ {
@ -367,9 +367,9 @@ void webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
} }
// wifiTask -> WIFIMANAGER::loop() -> WIFIMANAGER::tryConnect() -> connectCb() -> websocketSetup() // 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); 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 <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 SemaphoreHandle_t wsMutex;
extern WebSocketsClient webSocket; extern WebSocketsClient webSocket;
@ -44,8 +46,8 @@ extern StreamCopy micToWsCopier;
extern volatile bool i2sInputFlushScheduled; extern volatile bool i2sInputFlushScheduled;
// WEBSOCKET // WEBSOCKET
void webSocketEvent(WStype_t type, uint8_t *payload, size_t length); void webSocketEvent(WStype_t type, const uint8_t *payload, size_t length);
void websocketSetup(String server_domain, int port, String path); void websocketSetup(const String& server_domain, int port, const String& path);
void networkTask(void *parameter); void networkTask(void *parameter);
// AUDIO OUTPUT // AUDIO OUTPUT
@ -53,4 +55,6 @@ unsigned long getSpeakingDuration();
void audioStreamTask(void *parameter); void audioStreamTask(void *parameter);
// AUDIO INPUT // 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 <nvs_flash.h>
#include "Config.h"
// ! define preferences // ! define preferences
Preferences preferences; Preferences preferences;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -42,7 +42,7 @@ class WIFIMANAGER {
String uiPrefix = "/wifi"; // Prefix for all UI endpionts String uiPrefix = "/wifi"; // Prefix for all UI endpionts
Preferences preferences; // Used to store AP credentials to NVS 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 { struct apCredentials_t {
String apName; // Name of the AP SSID String apName; // Name of the AP SSID
@ -83,7 +83,7 @@ class WIFIMANAGER {
void fallbackToSoftAp(bool state = true); void fallbackToSoftAp(bool state = true);
// Get the current fallback state // Get the current fallback state
bool getFallbackState(); bool getFallbackState() const;
// Call to run the Task in the background // Call to run the Task in the background
void startBackgroundTask(String apName = "", String apPass = ""); void startBackgroundTask(String apName = "", String apPass = "");
@ -110,7 +110,7 @@ class WIFIMANAGER {
bool tryConnect(); bool tryConnect();
// Check if a SSID is stored in the config // Check if a SSID is stored in the config
bool configAvailable(); bool configAvailable() const;
// Preconfigure the SoftAP // Preconfigure the SoftAP
void configueSoftAp(String apName = "", String apPass = ""); 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 <driver/touch_sensor.h>
#include "Button.h" #include "OTA.h"
#include "WifiManager.h"
#include "LEDHandler.h"
#include "FactoryReset.h" #include "FactoryReset.h"
#define TOUCH_THRESHOLD 28000 #define TOUCH_THRESHOLD 28000
@ -131,43 +126,43 @@ void setupWiFi()
} }
void touchTask(void* parameter) { void touchTask(void* parameter) {
touch_pad_init(); touch_pad_init();
touch_pad_config(TOUCH_PAD_NUM2); touch_pad_config(TOUCH_PAD_NUM2);
bool touched = false; bool touched = false;
unsigned long pressStartTime = 0; unsigned long pressStartTime = 0;
unsigned long lastTouchTime = 0; unsigned long lastTouchTime = 0;
const unsigned long LONG_PRESS_DURATION = 500; // 500ms for long press const unsigned long LONG_PRESS_DURATION = 500; // 500ms for long press
while (1) { while (1) {
// Read the touch sensor // Read the touch sensor
uint32_t touchValue = touchRead(TOUCH_PAD_NUM2); uint32_t touchValue = touchRead(TOUCH_PAD_NUM2);
bool isTouched = (touchValue > TOUCH_THRESHOLD); bool isTouched = (touchValue > TOUCH_THRESHOLD);
unsigned long currentTime = millis(); unsigned long currentTime = millis();
// Initial touch detection // Initial touch detection
if (isTouched && !touched && (currentTime - lastTouchTime > TOUCH_DEBOUNCE_DELAY)) { if (isTouched && !touched && (currentTime - lastTouchTime > TOUCH_DEBOUNCE_DELAY)) {
touched = true; touched = true;
pressStartTime = currentTime; // Start timing the press pressStartTime = currentTime; // Start timing the press
lastTouchTime = currentTime; 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
} }
vTaskDelete(NULL);
// 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);
} }
void setupDeviceMetadata() { void setupDeviceMetadata() {