Skip to content

Commit 2f04276

Browse files
authored
Merge pull request #16 from jescarri/size-baseline
Final
2 parents 1dcdb5f + 1a93715 commit 2f04276

13 files changed

Lines changed: 196 additions & 360 deletions

src/config.hpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
namespace config {
1717

1818
// Debug control --------------------------------------------------------------
19-
#ifdef DEBUG_SERIAL
20-
constexpr bool EnableDebug = true;
21-
#else
22-
constexpr bool EnableDebug = false;
23-
#endif
19+
// Debug is now compile-time disabled for optimized builds
20+
constexpr bool EnableDebug = false;
2421

2522
// SX127x / LMIC wiring -------------------------------------------------------
2623
constexpr int PinLmicNss = 18;
@@ -30,12 +27,12 @@ constexpr int PinLmicDio1 = 33;
3027
constexpr int PinLmicDio2 = 32;
3128

3229
// LoRaWAN / payload ----------------------------------------------------------
33-
constexpr int MaxPayloadSize = 200; // Increased to accommodate OTA messages
30+
constexpr int MaxPayloadSize = 200; // Increased to accommodate OTA messages
3431

3532
// Sensors --------------------------------------------------------------------
36-
constexpr int SoilSensorPin = 34; // ADC1_CH6
37-
constexpr int AirValue = 2200; // Dry calibration value
38-
constexpr int WaterValue = 380; // Wet calibration value
33+
constexpr int SoilSensorPin = 34; // ADC1_CH6
34+
constexpr int AirValue = 2200; // Dry calibration value
35+
constexpr int WaterValue = 380; // Wet calibration value
3936

4037
// Misc -----------------------------------------------------------------------
4138
constexpr int MaxSensorRead = 1;
@@ -44,10 +41,9 @@ constexpr int MaxSensorRead = 1;
4441
// The FIRMWARE_VERSION is set by the CI/CD pipeline during compilation
4542
// Format: 3-digit integer (e.g., 100 for v1.0.0, 110 for v1.1.0, 112 for v1.1.2)
4643
#ifdef FIRMWARE_VERSION
47-
constexpr int FirmwareVersionInt = static_cast<int>(FIRMWARE_VERSION);
44+
constexpr int FirmwareVersionInt = static_cast<int>(FIRMWARE_VERSION);
4845
#else
49-
constexpr int FirmwareVersionInt = 0; // Development build
46+
constexpr int FirmwareVersionInt = 0; // Development build
5047
#endif
5148

52-
} // namespace config
53-
49+
} // namespace config

src/debug.hpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22

33
#include "config.hpp"
44

5-
#ifdef DEBUG_SERIAL
6-
#define DEBUG_PRINT(x) Serial.print(x)
7-
#define DEBUG_PRINTLN(x) Serial.println(x)
8-
#define DEBUG_PRINTF(fmt, ...) Serial.printf(fmt, __VA_ARGS__)
9-
#define DEBUG_BEGIN(baud) Serial.begin(baud)
5+
// Debug macros are now compile-time disabled for optimized builds
6+
// These macros compile to nothing, ensuring zero runtime overhead
7+
#if 0 // Always disabled for production builds
8+
#define DEBUG_PRINT(x) Serial.print(x)
9+
#define DEBUG_PRINTLN(x) Serial.println(x)
10+
#define DEBUG_PRINTF(fmt, ...) Serial.printf(fmt, __VA_ARGS__)
11+
#define DEBUG_BEGIN(baud) Serial.begin(baud)
1012
#else
11-
#define DEBUG_PRINT(x)
12-
#define DEBUG_PRINTLN(x)
13-
#define DEBUG_PRINTF(fmt, ...)
14-
#define DEBUG_BEGIN(baud)
13+
#define DEBUG_PRINT(x) \
14+
do { \
15+
} while (0)
16+
#define DEBUG_PRINTLN(x) \
17+
do { \
18+
} while (0)
19+
#define DEBUG_PRINTF(fmt, ...) \
20+
do { \
21+
} while (0)
22+
#define DEBUG_BEGIN(baud) \
23+
do { \
24+
} while (0)
1525
#endif

src/lorawan.cpp

Lines changed: 86 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,40 @@
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 = ";
1617
const char PROGMEM msg_lmic_global_duty_avail[] = "LMIC.globalDutyAvail = ";
1718
const 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

4041
sensorData sd;
4142

@@ -91,50 +92,48 @@ void LoraWANPrintLMICOpmode(void) {
9192
}
9293
}
9394

94-
#if !defined(UNIT_TEST)
9595
void 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

139138
void 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
}

src/lorawan.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extern bool maxLipoFound;
3838
extern Adafruit_MAX17048 maxlipo;
3939

4040
void LoraWANPrintLMICOpmode(void);
41-
void LoraWANDebug(const lmic_t& lmic_check);
41+
void LoraWANDebug(const lmic_t &lmic_check);
4242
void PrintLMICVersion();
4343
void onEvent(ev_t ev);
4444
void do_send(osjob_t *j);

src/lorawan_settings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Preferences &prefs() {
2020
static Preferences instance;
2121
return instance;
2222
}
23-
} // namespace
23+
} // namespace
2424

2525
void lorawan_preferences_init() {
2626
prefs().begin(LMIC_PREF_NS_NAME, RW_MODE);
@@ -32,7 +32,7 @@ bool lmic_init_needed() {
3232

3333
void lmic_save() {
3434
LMIC.globalDutyAvail = 0;
35-
size_t a = prefs().putBytes(LMIC_BYTES_KEY_NAME, &LMIC, sizeof(LMIC));
35+
size_t a = prefs().putBytes(LMIC_BYTES_KEY_NAME, &LMIC, sizeof(LMIC));
3636
Serial.print("Saved: ");
3737
Serial.println(a);
3838
}

src/lorawan_settings.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
#include <hal/hal.h>
77

8-
constexpr const char* LMIC_PREF_NS_NAME = "lmic";
9-
constexpr const char* LMIC_INIT_NEEDED_KEY_NAME = "init";
10-
constexpr const char* LMIC_BYTES_KEY_NAME = "lmic_struct";
11-
constexpr const char* LORAWAN_CONFIG_PRESENT_KEY = "lorawan_config";
12-
constexpr const char* APP_EUID_KEY = "euid";
13-
constexpr const char* RUNTIME_KEY = "runtime";
14-
constexpr int MAX_LORAWAN_CONF_CHAR_LEN = 100;
8+
constexpr const char *LMIC_PREF_NS_NAME = "lmic";
9+
constexpr const char *LMIC_INIT_NEEDED_KEY_NAME = "init";
10+
constexpr const char *LMIC_BYTES_KEY_NAME = "lmic_struct";
11+
constexpr const char *LORAWAN_CONFIG_PRESENT_KEY = "lorawan_config";
12+
constexpr const char *APP_EUID_KEY = "euid";
13+
constexpr const char *RUNTIME_KEY = "runtime";
14+
constexpr int MAX_LORAWAN_CONF_CHAR_LEN = 100;
1515

1616
constexpr bool RW_MODE = false;
1717
constexpr bool RO_MODE = true;

0 commit comments

Comments
 (0)