From f291db7158e9c430d811e6f2c2e3e908700fa4ae Mon Sep 17 00:00:00 2001 From: Yukti Date: Thu, 30 Apr 2026 03:11:38 +0530 Subject: [PATCH 1/2] add runtime debug level selectable via LCD menu (Debug Lvl field) and serial command d0-d3 --- Firmware/GPAD_API/GPAD_API/GPAD_API.ino | 43 ++++---------- Firmware/GPAD_API/GPAD_API/GPAD_HAL.cpp | 59 +++++++++---------- Firmware/GPAD_API/GPAD_API/GPAD_menu.cpp | 15 ++++- Firmware/GPAD_API/GPAD_API/WiFiManagerOTA.cpp | 5 +- Firmware/GPAD_API/GPAD_API/gpad_debug.cpp | 3 + Firmware/GPAD_API/GPAD_API/gpad_debug.h | 11 ++++ 6 files changed, 67 insertions(+), 69 deletions(-) create mode 100644 Firmware/GPAD_API/GPAD_API/gpad_debug.cpp create mode 100644 Firmware/GPAD_API/GPAD_API/gpad_debug.h diff --git a/Firmware/GPAD_API/GPAD_API/GPAD_API.ino b/Firmware/GPAD_API/GPAD_API/GPAD_API.ino index c1931cc0..28505704 100644 --- a/Firmware/GPAD_API/GPAD_API/GPAD_API.ino +++ b/Firmware/GPAD_API/GPAD_API/GPAD_API.ino @@ -54,6 +54,7 @@ #include "GPAD_HAL.h" #include "gpad_utility.h" #include "gpad_serial.h" +#include "gpad_debug.h" #include "Wink.h" #include @@ -176,9 +177,8 @@ WifiOTA::Manager wifiManager(WiFi, debugSerial); #define DEBUG_SPI 0 -// #define DEBUG 0 -#define DEBUG 1 -// #define DEBUG 4 +// Debug level now set at runtime via LCD menu or serial command d0-d3. +// See gpad_debug.h unsigned long last_command_ms; @@ -333,10 +333,7 @@ void publishOnLineMsg(void) float rssi = WiFi.RSSI(); char rssiString[8]; -#if (DEBUG > 1) - debugSerial.print("Publish RSSI: "); - debugSerial.println(rssi); -#endif + if (gpad_debug_level > 1) { debugSerial.print("Publish RSSI: "); debugSerial.println(rssi); } dtostrf(rssi, 1, 2, rssiString); char onLineMsg[32] = " online, RSSI:"; @@ -1095,11 +1092,7 @@ void callback(char *topic, byte *payload, unsigned int length) } mbuff[m] = '\0'; -#if (DEBUG > 0) - debugSerial.print("|"); - debugSerial.print(mbuff); - debugSerial.println("|"); -#endif + if (gpad_debug_level > 0) { debugSerial.print("|"); debugSerial.print(mbuff); debugSerial.println("|"); } debugSerial.println("Received MQTT Msg."); const String payloadText = String(mbuff); @@ -1634,9 +1627,7 @@ void setup() publish_Ack_Topic[0] = '\0'; macAddressString[0] = '\0'; -#if (DEBUG > 1) - debugSerial.println("Call: GPAD_HAL_setup(&debugSerial)"); -#endif + if (gpad_debug_level > 1) debugSerial.println("Call: GPAD_HAL_setup(&debugSerial)"); // Setup and present LCD splash screen // Setup the SWITCH_MUTE @@ -1650,10 +1641,7 @@ void setup() IPAddress deviceAddress = wifiManager.getAddress(); GPAD_HAL_setup(&debugSerial, wifiManager.getMode(), deviceAddress); -#if (DEBUG > 0) - debugSerial.println("MAC: "); - debugSerial.println(macAddressString); -#endif + if (gpad_debug_level > 0) { debugSerial.println("MAC: "); debugSerial.println(macAddressString); } debugSerial.setTimeout(SERIAL_TIMEOUT_MS); strncpy(mqtt_broker_name, DEFAULT_MQTT_BROKER_NAME, MQTT_BROKER_MAX_LEN - 1); @@ -1665,9 +1653,7 @@ void setup() client.setServer(mqtt_broker_name, 1883); // Default MQTT port, this is a TCP port. client.setCallback(callback); -#if (DEBUG > 0) - debugSerial.println("Starting WiFi as STA"); -#endif + if (gpad_debug_level > 0) debugSerial.println("Starting WiFi as STA"); // Note: On Krake SN#3 only, performing this // while the Splash is on causes a reset, presumably @@ -1691,11 +1677,7 @@ void setup() char buff[13]; sprintf(buff, MACSTR_PLN, MAC2STR(mac)); -#if (DEBUG > 0) - printf("My mac is " MACSTR "\n", MAC2STR(mac)); - debugSerial.print("MAC as char array: "); - debugSerial.println(buff); -#endif + if (gpad_debug_level > 0) { printf("My mac is " MACSTR "\n", MAC2STR(mac)); debugSerial.print("MAC as char array: "); debugSerial.println(buff); } strcpy(macAddressString, buff); macAddressString[12] = '\0'; @@ -1707,12 +1689,7 @@ void setup() loadMqttConfig(); client.setServer(mqtt_broker_name, 1883); -#if (DEBUG > 1) - debugSerial.println("XXXXXXX"); - debugSerial.println(subscribe_Alarm_Topic); - debugSerial.println(publish_Ack_Topic); - debugSerial.println("XXXXXXX"); -#endif + if (gpad_debug_level > 1) { debugSerial.println("XXXXXXX"); debugSerial.println(subscribe_Alarm_Topic); debugSerial.println(publish_Ack_Topic); debugSerial.println("XXXXXXX"); } // Setup SSID length is the length of the prefix, 'Krake_', which is 7 // plus the length of the MAC address string, MAC_ADDRESS_STRING_LENGTH const size_t SETUP_SSID_LENGTH = 7 + MAC_ADDRESS_STRING_LENGTH; diff --git a/Firmware/GPAD_API/GPAD_API/GPAD_HAL.cpp b/Firmware/GPAD_API/GPAD_API/GPAD_HAL.cpp index 32e2acee..fe70fc64 100644 --- a/Firmware/GPAD_API/GPAD_API/GPAD_HAL.cpp +++ b/Firmware/GPAD_API/GPAD_API/GPAD_HAL.cpp @@ -22,6 +22,7 @@ #include "GPAD_HAL.h" #include "alarm_api.h" #include "gpad_utility.h" +#include "gpad_debug.h" #include #include "WiFiManagerOTA.h" #include "GPAD_menu.h" @@ -120,16 +121,8 @@ volatile boolean process; byte received_signal_raw_bytes[MAX_BUFFER_SIZE]; -// Local DEBUG defines, GPAD_HAL -#define DEBUG 0 -// #define DEBUG 1 - -#if (DEBUG > 0) -Serial.println("Debug defined >0") -#endif - - const int NUM_PREFICES = 6; -char legal_prefices[NUM_PREFICES] = {'h', 's', 'a', 'u', 'i', 'r'}; + const int NUM_PREFICES = 7; +char legal_prefices[NUM_PREFICES] = {'h', 's', 'a', 'u', 'i', 'r', 'd'}; void setup_spi() { @@ -193,7 +186,7 @@ void receive_byte(byte c) void updateFromSPI() { - if (DEBUG > 0) + if (gpad_debug_level > 0) { if (process) { @@ -210,7 +203,7 @@ void updateFromSPI() event.msg[i] = (char)received_signal_raw_bytes[1 + i]; } - if (DEBUG > 1) + if (gpad_debug_level > 1) { Serial.print(F("LVL: ")); Serial.println(event.lvl); @@ -340,20 +333,14 @@ void GPAD_HAL_setup(Stream *serialport, wifi_mode_t wifiMode, IPAddress &deviceI Real_lcd.init(); -#if (DEBUG > 0) - serialport->println(F("Clear LCD")); -#endif + if (gpad_debug_level > 0) serialport->println(F("Clear LCD")); clearLCD(); delay(100); -#if (DEBUG > 0) - serialport->println(F("Start LCD splash")); -#endif + if (gpad_debug_level > 0) serialport->println(F("Start LCD splash")); splashLCD(wifiMode, deviceIp); -#if (DEBUG > 0) - serialport->println(F("EndLCD splash")); -#endif + if (gpad_debug_level > 0) serialport->println(F("EndLCD splash")); // Setup GPIO pins, Mute and lights pinMode(SWITCH_MUTE, INPUT_PULLUP); // The SWITCH_MUTE is different on Atmega vs ESP32. Is this redundant? @@ -361,10 +348,7 @@ void GPAD_HAL_setup(Stream *serialport, wifi_mode_t wifiMode, IPAddress &deviceI for (int i = 0; i < NUM_LIGHTS; i++) { -#if (DEBUG > 0) - serialport->print(LIGHT[i]); - serialport->print(", "); -#endif + if (gpad_debug_level > 0) { serialport->print(LIGHT[i]); serialport->print(", "); } pinMode(LIGHT[i], OUTPUT); // Rob trying to prevent resets // This is necessary on SN#3 @@ -394,18 +378,14 @@ void GPAD_HAL_setup(Stream *serialport, wifi_mode_t wifiMode, IPAddress &deviceI uartSerial1.begin(UART1_BAUD_RATE, SERIAL_8N1, RXD1, TXD1); // UART setup. On Homework2, LCD goes blank early uartSerial1.flush(); // Clear any Serial1 crud at reset. -#if (DEBUG > 0) - serialport->println(F("uartSerial1 Setup")); -#endif + if (gpad_debug_level > 0) serialport->println(F("uartSerial1 Setup")); #endif // Here initialize the UART2 pinMode(RXD2, INPUT_PULLUP); uartSerial2.begin(UART2_BAUD_RATE, SERIAL_8N1, RXD2, TXD2); // UART setup uartSerial2.flush(); -#if (DEBUG > 0) - serialport->println(F("uartSerial2 Setup")); -#endif + if (gpad_debug_level > 0) serialport->println(F("uartSerial2 Setup")); } // end GPAD_HAL_setup() // This routine should be refactored so that it only "interprets" @@ -440,6 +420,23 @@ void interpretBuffer(char *buf, int rlen, Stream *serialport, PubSubClient *clie return; } + if (buf[0] == 'd') + { + if (rlen >= 2 && buf[1] >= '0' && buf[1] <= '3') + { + gpad_debug_level = buf[1] - '0'; + serialport->print(F("Debug level set to: ")); + serialport->println(gpad_debug_level); + } + else + { + serialport->print(F("Debug level is: ")); + serialport->println(gpad_debug_level); + serialport->println(F("Usage: d0=off d1=basic d2=verbose d3=trace")); + } + return; + } + const auto protocolMessage = gpap_message::GPAPMessage::deserialize(buf, rlen); serialport->print(F("Command: ")); diff --git a/Firmware/GPAD_API/GPAD_API/GPAD_menu.cpp b/Firmware/GPAD_API/GPAD_API/GPAD_menu.cpp index 7900b6fd..84ad0690 100644 --- a/Firmware/GPAD_API/GPAD_API/GPAD_menu.cpp +++ b/Firmware/GPAD_API/GPAD_API/GPAD_menu.cpp @@ -7,6 +7,7 @@ #include "RickmanLiquidCrystal_I2C.h" #include "DFPlayer.h" #include "alarm_api.h" +#include "gpad_debug.h" using namespace Menu; @@ -90,6 +91,16 @@ result action5(eventMask e) +result actionDebugLevel(eventMask e) +{ + if (e == eventMask::enterEvent) + { + Serial.print(F("Debug level set to: ")); + Serial.println(gpad_debug_level); + } + return proceed; +} + result actionResetConfirm(eventMask e) { if (e == eventMask::enterEvent) @@ -111,8 +122,8 @@ MENU(mainMenu, "Krake Menu", Menu::doNothing, Menu::noEvent, Menu::wrapStyle, OP("Dismiss", action2, enterEvent), OP("Shelve", action3, enterEvent), FIELD(volumeDFPlayer, "Volume", "%", 0, 30, 10, 1, action4, anyEvent, wrapStyle), - - SUBMENU(resetConfirmMenu), + FIELD(gpad_debug_level, "Debug Lvl", "", 0, 3, 1, 1, actionDebugLevel, enterEvent, wrapStyle), + SUBMENU(resetConfirmMenu), OP("Exit Menu", action5, enterEvent) ); diff --git a/Firmware/GPAD_API/GPAD_API/WiFiManagerOTA.cpp b/Firmware/GPAD_API/GPAD_API/WiFiManagerOTA.cpp index b28ab2db..8f7b8d8d 100644 --- a/Firmware/GPAD_API/GPAD_API/WiFiManagerOTA.cpp +++ b/Firmware/GPAD_API/GPAD_API/WiFiManagerOTA.cpp @@ -1,5 +1,6 @@ #include "WiFiManagerOTA.h" #include +#include "gpad_debug.h" namespace { @@ -434,9 +435,7 @@ void WifiOTA::initLittleFS() } else { -#if (DEBUG > 1) - Serial.println("LittleFS mounted successfully."); -#endif + if (gpad_debug_level > 1) Serial.println("LittleFS mounted successfully."); } } diff --git a/Firmware/GPAD_API/GPAD_API/gpad_debug.cpp b/Firmware/GPAD_API/GPAD_API/gpad_debug.cpp new file mode 100644 index 00000000..66616452 --- /dev/null +++ b/Firmware/GPAD_API/GPAD_API/gpad_debug.cpp @@ -0,0 +1,3 @@ +#include "gpad_debug.h" + +int gpad_debug_level = 0; diff --git a/Firmware/GPAD_API/GPAD_API/gpad_debug.h b/Firmware/GPAD_API/GPAD_API/gpad_debug.h new file mode 100644 index 00000000..514728e0 --- /dev/null +++ b/Firmware/GPAD_API/GPAD_API/gpad_debug.h @@ -0,0 +1,11 @@ +#ifndef GPAD_DEBUG_H +#define GPAD_DEBUG_H + +// Runtime debug level — can be changed via LCD menu or serial command (d0-d3). +// 0 = off +// 1 = basic (setup info, key events) +// 2 = verbose (detailed message flow) +// 3 = trace (inside functions, SPI, per-loop) +extern int gpad_debug_level; + +#endif From ccdff91540e789463e56e10dd92b3ca98c251d56 Mon Sep 17 00:00:00 2001 From: Yukti Date: Thu, 30 Apr 2026 03:20:48 +0530 Subject: [PATCH 2/2] added instructions for user in gpad_debug.h file --- Firmware/GPAD_API/GPAD_API/gpad_debug.h | 28 ++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/Firmware/GPAD_API/GPAD_API/gpad_debug.h b/Firmware/GPAD_API/GPAD_API/gpad_debug.h index 514728e0..4e3bff8c 100644 --- a/Firmware/GPAD_API/GPAD_API/gpad_debug.h +++ b/Firmware/GPAD_API/GPAD_API/gpad_debug.h @@ -1,11 +1,29 @@ #ifndef GPAD_DEBUG_H #define GPAD_DEBUG_H -// Runtime debug level — can be changed via LCD menu or serial command (d0-d3). -// 0 = off -// 1 = basic (setup info, key events) -// 2 = verbose (detailed message flow) -// 3 = trace (inside functions, SPI, per-loop) +// ----------------------------------------------------------------------------- +// Runtime Debug Level System +// ----------------------------------------------------------------------------- +// Previously, debug output was controlled by #define DEBUG scattered across +// multiple files with different values, requiring a recompile to change. +// This module replaces all of that with a single global variable that can be +// changed while the device is running — no recompile, no reflash needed. +// +// HOW TO CHANGE THE DEBUG LEVEL: +// Via LCD menu : open Krake Menu, navigate to "Debug Lvl", rotate to pick +// a value (0-3), press the encoder button to confirm. +// Via Serial : type d0, d1, d2, or d3 and press enter in Serial Monitor. +// +// DEBUG LEVELS: +// 0 = off — no debug output, normal production mode +// 1 = basic — setup info, MAC address, WiFi start, MQTT payload echo +// 2 = verbose — RSSI values, MQTT topic verification, HAL setup trace +// 3 = trace — SPI raw packets, per-message level and content details +// +// Higher levels always include everything from the levels below them. +// Default is 0 (off) on every boot. +// ----------------------------------------------------------------------------- + extern int gpad_debug_level; #endif