diff --git a/firmware-arduino/src/Audio.cpp b/firmware-arduino/src/Audio.cpp index 8774d64..3fb7f71 100644 --- a/firmware-arduino/src/Audio.cpp +++ b/firmware-arduino/src/Audio.cpp @@ -1,10 +1,10 @@ +#include #include "OTA.h" #include "Print.h" #include "Config.h" #include "AudioTools.h" // #include "AudioTools/Concurrency/RTOS.h" #include "AudioTools/AudioCodecs/CodecOpus.h" -#include #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& buf) : _buffer(buf) {} + explicit BufferPrint(BufferRTOS& 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); diff --git a/firmware-arduino/src/Audio.h b/firmware-arduino/src/Audio.h index f6d0c2f..41474a1 100644 --- a/firmware-arduino/src/Audio.h +++ b/firmware-arduino/src/Audio.h @@ -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 +#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); \ No newline at end of file +void micTask(void *parameter); + +#endif \ No newline at end of file diff --git a/firmware-arduino/src/Config.cpp b/firmware-arduino/src/Config.cpp index 67b372d..63f04f2 100644 --- a/firmware-arduino/src/Config.cpp +++ b/firmware-arduino/src/Config.cpp @@ -1,5 +1,5 @@ -#include "Config.h" #include +#include "Config.h" // ! define preferences Preferences preferences; diff --git a/firmware-arduino/src/FactoryReset.h b/firmware-arduino/src/FactoryReset.h index 7f4ce3d..5da67a4 100644 --- a/firmware-arduino/src/FactoryReset.h +++ b/firmware-arduino/src/FactoryReset.h @@ -1,6 +1,9 @@ -#include +#ifndef FACTORYRESET_H +#define FACTORYRESET_H + #include #include //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; - } - } \ No newline at end of file + 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 \ No newline at end of file diff --git a/firmware-arduino/src/LEDHandler.cpp b/firmware-arduino/src/LEDHandler.cpp index 3fee3dd..0f3444b 100644 --- a/firmware-arduino/src/LEDHandler.cpp +++ b/firmware-arduino/src/LEDHandler.cpp @@ -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); diff --git a/firmware-arduino/src/OTA.cpp b/firmware-arduino/src/OTA.cpp index 577dc9a..db29b8d 100644 --- a/firmware-arduino/src/OTA.cpp +++ b/firmware-arduino/src/OTA.cpp @@ -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); } diff --git a/firmware-arduino/src/OTA.h b/firmware-arduino/src/OTA.h index 1445e79..5d48d90 100644 --- a/firmware-arduino/src/OTA.h +++ b/firmware-arduino/src/OTA.h @@ -1,5 +1,6 @@ #ifndef OTA_H #define OTA_H + #include "Config.h" extern const char *server_certificate; diff --git a/firmware-arduino/src/PitchShift.h b/firmware-arduino/src/PitchShift.h index f3f2d4f..37fd409 100644 --- a/firmware-arduino/src/PitchShift.h +++ b/firmware-arduino/src/PitchShift.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef PITCHSHIFT_H +#define PITCHSHIFT_H #include #include @@ -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; -}; \ No newline at end of file +}; + +#endif \ No newline at end of file diff --git a/firmware-arduino/src/WifiManager.cpp b/firmware-arduino/src/WifiManager.cpp index 04dd774..ccc7ce1 100644 --- a/firmware-arduino/src/WifiManager.cpp +++ b/firmware-arduino/src/WifiManager.cpp @@ -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; } diff --git a/firmware-arduino/src/WifiManager.h b/firmware-arduino/src/WifiManager.h index 41102fc..17b5181 100644 --- a/firmware-arduino/src/WifiManager.h +++ b/firmware-arduino/src/WifiManager.h @@ -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 = ""); diff --git a/firmware-arduino/src/main.cpp b/firmware-arduino/src/main.cpp index f29721c..c1160fe 100644 --- a/firmware-arduino/src/main.cpp +++ b/firmware-arduino/src/main.cpp @@ -1,12 +1,7 @@ -#include "OTA.h" -#include -#include -#include "LEDHandler.h" -#include "Config.h" -#include "SPIFFS.h" -#include "WifiManager.h" #include -#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() {