33#include " lorawan_settings.hpp"
44#include " utils.hpp"
55#include " ota.hpp"
6+ #include " debug.hpp"
67#include < cstring>
78#include < cctype>
89#include " lorawan_settings.hpp"
910#include < Adafruit_MAX1704X.h>
1011#include < array>
1112
1213// PROGMEM string constants for LoRaWAN
13- const char PROGMEM msg_lmic_opmode[] = " LMIC.opmode: " ;
14- const char PROGMEM msg_lmic_seqno_up[] = " LMIC.seqnoUp = " ;
15- const char PROGMEM msg_lmic_global_duty_rate[] = " LMIC.globalDutyRate = " ;
14+ const char PROGMEM msg_lmic_opmode[] = " LMIC.opmode: " ;
15+ const char PROGMEM msg_lmic_seqno_up[] = " LMIC.seqnoUp = " ;
16+ const char PROGMEM msg_lmic_global_duty_rate[] = " LMIC.globalDutyRate = " ;
1617const char PROGMEM msg_lmic_global_duty_avail[] = " LMIC.globalDutyAvail = " ;
1718const char PROGMEM msg_lmic_band_plan_next_tx[] = " LMICbandplan_nextTx = " ;
18- const char PROGMEM msg_os_get_time[] = " os_getTime = " ;
19- const char PROGMEM msg_lmic_txend[] = " LMIC.txend = " ;
20- const char PROGMEM msg_lmic_txchnl[] = " LMIC.txChnl = " ;
21- const char PROGMEM msg_lmic_version[] = " LMIC: " ;
22- const char PROGMEM msg_oxticks[] = " osTicks, " ;
23- const char PROGMEM msg_sec[] = " sec" ;
24- const char PROGMEM msg_separator[] = " -----" ;
25- const char PROGMEM msg_do_send[] = " do_send" ;
26- const char PROGMEM msg_lmic_opmode_equals[] = " LMIC.opmode= " ;
27- const char PROGMEM msg_app_eui[] = " app_eui: " ;
28- const char PROGMEM msg_dev_eui[] = " dev_eui: " ;
29- const char PROGMEM msg_app_key[] = " app_key: " ;
30- const char PROGMEM msg_charge_rate[] = " ----ChargeRate: " ;
31- const char PROGMEM msg_x_format[] = " X: %f" ;
32- const char PROGMEM msg_moisture_format[] = " Moisture ADC: %f, Moisture Percentage: %f, vBat %f\n\n " ;
33- const char PROGMEM msg_error_app_eui[] = " ERROR: app_eui string missing or too short" ;
34- const char PROGMEM msg_error_app_eui_hex[] = " ERROR: app_eui contains non-hex digits" ;
35- const char PROGMEM msg_error_dev_eui[] = " ERROR: dev_eui string missing or too short" ;
36- const char PROGMEM msg_error_dev_eui_hex[] = " ERROR: dev_eui contains non-hex digits" ;
37- const char PROGMEM msg_error_app_key[] = " ERROR: app_key string missing or too short" ;
38- const char PROGMEM msg_error_app_key_hex[] = " ERROR: app_key contains non-hex digits" ;
19+ const char PROGMEM msg_os_get_time[] = " os_getTime = " ;
20+ const char PROGMEM msg_lmic_txend[] = " LMIC.txend = " ;
21+ const char PROGMEM msg_lmic_txchnl[] = " LMIC.txChnl = " ;
22+ const char PROGMEM msg_lmic_version[] = " LMIC: " ;
23+ const char PROGMEM msg_oxticks[] = " osTicks, " ;
24+ const char PROGMEM msg_sec[] = " sec" ;
25+ const char PROGMEM msg_separator[] = " -----" ;
26+ const char PROGMEM msg_do_send[] = " do_send" ;
27+ const char PROGMEM msg_lmic_opmode_equals[] = " LMIC.opmode= " ;
28+ const char PROGMEM msg_app_eui[] = " app_eui: " ;
29+ const char PROGMEM msg_dev_eui[] = " dev_eui: " ;
30+ const char PROGMEM msg_app_key[] = " app_key: " ;
31+ const char PROGMEM msg_charge_rate[] = " ----ChargeRate: " ;
32+ const char PROGMEM msg_x_format[] = " X: %f" ;
33+ const char PROGMEM msg_moisture_format[] = " Moisture ADC: %f, Moisture Percentage: %f, vBat %f\n\n " ;
34+ const char PROGMEM msg_error_app_eui[] = " ERROR: app_eui string missing or too short" ;
35+ const char PROGMEM msg_error_app_eui_hex[] = " ERROR: app_eui contains non-hex digits" ;
36+ const char PROGMEM msg_error_dev_eui[] = " ERROR: dev_eui string missing or too short" ;
37+ const char PROGMEM msg_error_dev_eui_hex[] = " ERROR: dev_eui contains non-hex digits" ;
38+ const char PROGMEM msg_error_app_key[] = " ERROR: app_key string missing or too short" ;
39+ const char PROGMEM msg_error_app_key_hex[] = " ERROR: app_key contains non-hex digits" ;
3940
4041sensorData sd;
4142
@@ -91,50 +92,48 @@ void LoraWANPrintLMICOpmode(void) {
9192 }
9293}
9394
94- #if !defined(UNIT_TEST)
9595void LoraWANDebug (const lmic_t & lmic_check) {
96- # ifdef DEBUG
97- LoraWANPrintLMICOpmode ( );
98- Serial. println ( " " );
99- Serial. println ( " ----- " );
100-
101- Serial. print ( F ( " LMIC.seqnoUp = " ));
102- Serial. println (lmic_check .seqnoUp );
103-
104- Serial. print ( F ( " LMIC.globalDutyRate = " ));
105- Serial. print (lmic_check .globalDutyRate );
106- Serial. print ( F ( " osTicks, " ));
107- Serial. print ( osticks2ms (lmic_check. globalDutyRate ) / 1000 );
108- Serial. println ( F ( " sec " ));
109-
110- Serial. print ( F ( " LMIC.globalDutyAvail = " ));
111- Serial. print (lmic_check .globalDutyAvail );
112- Serial. print ( F ( " osTicks, " ));
113- Serial. print ( osticks2ms (lmic_check. globalDutyAvail ) / 1000 );
114- Serial. println ( F ( " sec " ));
115-
116- Serial. print ( F ( " LMICbandplan_nextTx = " ));
117- Serial. print ( LMICbandplan_nextTx ( os_getTime ()) );
118- Serial. print ( F ( " osTicks, " ));
119- Serial. print ( osticks2ms ( LMICbandplan_nextTx ( os_getTime ())) / 1000 );
120- Serial. println ( F ( " sec " ));
121-
122- Serial. print ( F ( " os_getTime = " ));
123- Serial. print ( os_getTime () );
124- Serial. print ( F ( " osTicks, " ));
125- Serial. print ( osticks2ms ( os_getTime ()) / 1000 );
126- Serial. println ( F ( " sec " ));
127-
128- Serial. print ( F ( " LMIC.txend = " ));
129- Serial. println (lmic_check .txend );
130- Serial. print ( F ( " LMIC.txChnl = " ));
131- Serial. println (lmic_check .txChnl );
132-
133- Serial. println ( " " );
134- Serial. println (" " );
135- # endif
96+ // Debug output now compile-time disabled for optimized builds
97+ DEBUG_PRINT ( " LMIC.opmode: " );
98+ // LoraWANPrintLMICOpmode() is now disabled via debug macros
99+ DEBUG_PRINTLN ( " " );
100+ DEBUG_PRINTLN ( " ----- " );
101+
102+ DEBUG_PRINT ( " LMIC .seqnoUp = " );
103+ DEBUG_PRINTLN ( String (lmic_check. seqnoUp ));
104+
105+ DEBUG_PRINT ( " LMIC .globalDutyRate = " );
106+ DEBUG_PRINT ( String (lmic_check. globalDutyRate ));
107+ DEBUG_PRINT ( " osTicks, " );
108+ DEBUG_PRINT ( String ( osticks2ms (lmic_check. globalDutyRate ) / 1000 ));
109+ DEBUG_PRINTLN ( " sec " );
110+
111+ DEBUG_PRINT ( " LMIC .globalDutyAvail = " );
112+ DEBUG_PRINT ( String (lmic_check. globalDutyAvail ));
113+ DEBUG_PRINT ( " osTicks, " );
114+ DEBUG_PRINT ( String ( osticks2ms (lmic_check. globalDutyAvail ) / 1000 ));
115+ DEBUG_PRINTLN ( " sec " );
116+
117+ DEBUG_PRINT ( " LMICbandplan_nextTx = " );
118+ DEBUG_PRINT ( String ( LMICbandplan_nextTx ( os_getTime ()) ));
119+ DEBUG_PRINT ( " osTicks, " );
120+ DEBUG_PRINT ( String ( osticks2ms ( LMICbandplan_nextTx ( os_getTime ())) / 1000 ));
121+ DEBUG_PRINTLN ( " sec " );
122+
123+ DEBUG_PRINT ( " os_getTime = " );
124+ DEBUG_PRINT ( String ( os_getTime () ));
125+ DEBUG_PRINT ( " osTicks, " );
126+ DEBUG_PRINT ( String ( osticks2ms ( os_getTime ()) / 1000 ));
127+ DEBUG_PRINTLN ( " sec " );
128+
129+ DEBUG_PRINT ( " LMIC .txend = " );
130+ DEBUG_PRINTLN ( String (lmic_check. txend ));
131+ DEBUG_PRINT ( " LMIC .txChnl = " );
132+ DEBUG_PRINTLN ( String (lmic_check. txChnl ));
133+
134+ DEBUG_PRINTLN (" " );
135+ DEBUG_PRINTLN ( " " );
136136}
137- #endif // !UNIT_TEST
138137
139138void PrintLMICVersion () {
140139 Serial.print (F (" LMIC: " ));
@@ -215,11 +214,11 @@ void onEvent(ev_t ev) {
215214 if (LMIC.txrxFlags & TXRX_PORT) {
216215 fPort = LMIC.frame [LMIC.dataBeg - 1 ];
217216 }
218-
217+
219218 // Handle OTA update messages on port 1
220219 if (fPort >= 1 && fPort <= OTA_MAX_CHUNKS) {
221220 uint8_t * downlinkData = &LMIC.frame [LMIC.dataBeg ];
222- uint8_t downlinkLen = LMIC.dataLen ;
221+ uint8_t downlinkLen = LMIC.dataLen ;
223222 handleDownlinkMessage (downlinkData, downlinkLen, fPort );
224223 }
225224 }
@@ -297,7 +296,7 @@ void do_send(osjob_t* /* j */) {
297296}
298297
299298// ToDo: Refactor hex string to u1_t array conversion
300- void os_getArtEui (u1_t * buf) {
299+ void os_getArtEui (u1_t * buf) {
301300 const String cfg = settings_get_string (" app_eui" );
302301
303302 if (cfg.isEmpty () || cfg.length () < 16 ) {
@@ -315,8 +314,8 @@ void os_getArtEui(u1_t *buf) {
315314 return ;
316315 }
317316
318- std::string t = cfg.substring (i, i + 2 ).c_str ();
319- app_eui[c] = static_cast <u1_t >(strtoul (t.c_str (), nullptr , 16 ));
317+ std::string t = cfg.substring (i, i + 2 ).c_str ();
318+ app_eui[c] = static_cast <u1_t >(strtoul (t.c_str (), nullptr , 16 ));
320319 }
321320
322321 Serial.print (FPSTR (msg_app_eui));
@@ -328,7 +327,7 @@ void os_getArtEui(u1_t *buf) {
328327 memcpy_P (buf, app_eui.data (), 8 );
329328}
330329
331- void os_getDevEui (u1_t * buf) {
330+ void os_getDevEui (u1_t * buf) {
332331 const String cfg = settings_get_string (" dev_eui" );
333332
334333 if (cfg.isEmpty () || cfg.length () < 16 ) {
@@ -346,8 +345,8 @@ void os_getDevEui(u1_t *buf) {
346345 return ;
347346 }
348347
349- std::string t = cfg.substring (i, i + 2 ).c_str ();
350- dev_eui[c] = static_cast <u1_t >(strtoul (t.c_str (), nullptr , 16 ));
348+ std::string t = cfg.substring (i, i + 2 ).c_str ();
349+ dev_eui[c] = static_cast <u1_t >(strtoul (t.c_str (), nullptr , 16 ));
351350 }
352351
353352 Serial.print (" dev_eui: " );
@@ -359,7 +358,7 @@ void os_getDevEui(u1_t *buf) {
359358 memcpy_P (buf, dev_eui.data (), 8 );
360359}
361360
362- void os_getDevKey (u1_t * buf) {
361+ void os_getDevKey (u1_t * buf) {
363362 const String cfg = settings_get_string (" app_key" );
364363
365364 // Validate the expected length (32 hex chars → 16 bytes)
@@ -401,10 +400,8 @@ void ReadSensors() {
401400 sd.vBat = maxlipo.cellVoltage ();
402401 sd.batPercent = maxlipo.cellPercent ();
403402 sd.batRate = maxlipo.chargeRate ();
404- #ifdef DEBUG
405- Serial.print (FPSTR (msg_charge_rate));
406- Serial.println (sd.batRate );
407- #endif
403+ DEBUG_PRINT (" ----ChargeRate: " );
404+ DEBUG_PRINTLN (String (sd.batRate ));
408405 }
409406 for (int i = 0 ; i < MAX_SENSOR_READ; i++) {
410407 float a = static_cast <float >(analogRead (config::SoilSensorPin));
@@ -413,19 +410,17 @@ void ReadSensors() {
413410 }
414411 float t = sd.soilMoistureValue / static_cast <float >(MAX_SENSOR_READ);
415412 sd.soilMoistureValue = t;
416- float x = static_cast <float >(map (static_cast <long >(sd.soilMoistureValue ),
413+ float x = static_cast <float >(map (static_cast <long >(sd.soilMoistureValue ),
417414 get_calibration_air_value (),
418415 get_calibration_water_value (), 0 , 100 ));
419416 sd.soilMoisturePercentage = abs (x);
420- #ifdef DEBUG
421- Serial.print (F (" X: " ));
422- Serial.println (x);
423- Serial.print (F (" Moisture ADC: " ));
424- Serial.print (sd.soilMoistureValue );
425- Serial.print (F (" , Moisture Percentage: " ));
426- Serial.print (sd.soilMoisturePercentage );
427- Serial.print (F (" , vBat " ));
428- Serial.println (sd.vBat );
429- Serial.println ();
430- #endif
417+ DEBUG_PRINT (" X: " );
418+ DEBUG_PRINTLN (String (x));
419+ DEBUG_PRINT (" Moisture ADC: " );
420+ DEBUG_PRINT (String (sd.soilMoistureValue ));
421+ DEBUG_PRINT (" , Moisture Percentage: " );
422+ DEBUG_PRINT (String (sd.soilMoisturePercentage ));
423+ DEBUG_PRINT (" , vBat " );
424+ DEBUG_PRINTLN (String (sd.vBat ));
425+ DEBUG_PRINTLN (" " );
431426}
0 commit comments