Skip to content

Commit 0511abd

Browse files
committed
Reduce binary size, use F string macros for esp8266 build
1 parent 8cad3f6 commit 0511abd

File tree

2 files changed

+65
-39
lines changed

2 files changed

+65
-39
lines changed

src/Wippersnapper.cpp

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ bool cbSignalMsg(pb_istream_t *stream, const pb_field_t *field, void **arg) {
405405

406406
pb_size_t arr_sz = field->array_size;
407407
WS_DEBUG_PRINT("Sub-messages found: ");
408-
WS_DEBUG_PRINTLN(arr_sz);
408+
WS_DEBUG_PRINTLNVAR(arr_sz);
409409

410410
if (field->tag ==
411411
wippersnapper_signal_v1_CreateSignalRequest_pin_configs_tag) {
@@ -492,7 +492,7 @@ bool Wippersnapper::decodeSignalMsg(
492492
/**************************************************************************/
493493
void cbSignalTopic(char *data, uint16_t len) {
494494
WS_DEBUG_PRINTLN("cbSignalTopic: New Msg on Signal Topic");
495-
WS_DEBUG_PRINT(len);
495+
WS_DEBUG_PRINTVAR(len);
496496
WS_DEBUG_PRINTLN(" bytes.");
497497
// zero-out current buffer
498498
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -877,7 +877,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field,
877877
/**************************************************************************/
878878
void cbSignalI2CReq(char *data, uint16_t len) {
879879
WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Signal-I2C]: ");
880-
WS_DEBUG_PRINT(len);
880+
WS_DEBUG_PRINTVAR(len);
881881
WS_DEBUG_PRINTLN(" bytes.");
882882
// zero-out current buffer
883883
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -936,11 +936,11 @@ bool cbDecodeServoMsg(pb_istream_t *stream, const pb_field_t *field,
936936
attached = false;
937937
} else {
938938
WS_DEBUG_PRINT("ATTACHED servo w/minPulseWidth: ");
939-
WS_DEBUG_PRINT(msgServoAttachReq.min_pulse_width);
939+
WS_DEBUG_PRINTVAR(msgServoAttachReq.min_pulse_width);
940940
WS_DEBUG_PRINT(" uS and maxPulseWidth: ");
941-
WS_DEBUG_PRINT(msgServoAttachReq.min_pulse_width);
941+
WS_DEBUG_PRINTVAR(msgServoAttachReq.min_pulse_width);
942942
WS_DEBUG_PRINT("uS on pin: ");
943-
WS_DEBUG_PRINTLN(servoPin);
943+
WS_DEBUG_PRINTLNVAR(servoPin);
944944
}
945945

946946
// Create and fill a servo response message
@@ -986,9 +986,9 @@ bool cbDecodeServoMsg(pb_istream_t *stream, const pb_field_t *field,
986986
char *servoPin = msgServoWriteReq.servo_pin + 1;
987987

988988
WS_DEBUG_PRINT("Writing pulse width of ");
989-
WS_DEBUG_PRINT((int)msgServoWriteReq.pulse_width);
989+
WS_DEBUG_PRINTVAR((int)msgServoWriteReq.pulse_width);
990990
WS_DEBUG_PRINT("uS to servo on pin#: ");
991-
WS_DEBUG_PRINTLN(servoPin);
991+
WS_DEBUG_PRINTLNVAR(servoPin);
992992
WS._servoComponent->servo_write(atoi(servoPin),
993993
(int)msgServoWriteReq.pulse_width);
994994
} else if (field->tag ==
@@ -1008,7 +1008,7 @@ bool cbDecodeServoMsg(pb_istream_t *stream, const pb_field_t *field,
10081008
// execute servo detach request
10091009
char *servoPin = msgServoDetachReq.servo_pin + 1;
10101010
WS_DEBUG_PRINT("Detaching servo from pin ");
1011-
WS_DEBUG_PRINTLN(servoPin);
1011+
WS_DEBUG_PRINTLNVAR(servoPin);
10121012
WS._servoComponent->servo_detach(atoi(servoPin));
10131013
} else {
10141014
WS_DEBUG_PRINTLN("Unable to decode servo message type!");
@@ -1029,7 +1029,7 @@ bool cbDecodeServoMsg(pb_istream_t *stream, const pb_field_t *field,
10291029
/**************************************************************************/
10301030
void cbServoMsg(char *data, uint16_t len) {
10311031
WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Servo]: ");
1032-
WS_DEBUG_PRINT(len);
1032+
WS_DEBUG_PRINTVAR(len);
10331033
WS_DEBUG_PRINTLN(" bytes.");
10341034
// zero-out current buffer
10351035
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -1143,9 +1143,9 @@ bool cbPWMDecodeMsg(pb_istream_t *stream, const pb_field_t *field, void **arg) {
11431143
// execute PWM pin duty cycle write request
11441144
char *pwmPin = msgPWMWriteFreqRequest.pin + 1;
11451145
WS_DEBUG_PRINT("Writing frequency: ");
1146-
WS_DEBUG_PRINT(msgPWMWriteFreqRequest.frequency);
1146+
WS_DEBUG_PRINTVAR(msgPWMWriteFreqRequest.frequency);
11471147
WS_DEBUG_PRINT("Hz to pin ");
1148-
WS_DEBUG_PRINTLN(atoi(pwmPin));
1148+
WS_DEBUG_PRINTLNVAR(atoi(pwmPin));
11491149
WS._pwmComponent->writeTone(atoi(pwmPin), msgPWMWriteFreqRequest.frequency);
11501150
} else if (field->tag ==
11511151
wippersnapper_signal_v1_PWMRequest_write_duty_request_tag) {
@@ -1184,7 +1184,7 @@ bool cbPWMDecodeMsg(pb_istream_t *stream, const pb_field_t *field, void **arg) {
11841184
/**************************************************************************/
11851185
void cbPWMMsg(char *data, uint16_t len) {
11861186
WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: PWM]: ");
1187-
WS_DEBUG_PRINT(len);
1187+
WS_DEBUG_PRINTVAR(len);
11881188
WS_DEBUG_PRINTLN(" bytes.");
11891189
// zero-out current buffer
11901190
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -1271,7 +1271,7 @@ bool cbDecodeDs18x20Msg(pb_istream_t *stream, const pb_field_t *field,
12711271
/**************************************************************************/
12721272
void cbSignalDSReq(char *data, uint16_t len) {
12731273
WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Signal-DS]: ");
1274-
WS_DEBUG_PRINT(len);
1274+
WS_DEBUG_PRINTVAR(len);
12751275
WS_DEBUG_PRINTLN(" bytes.");
12761276
// zero-out current buffer
12771277
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -1384,7 +1384,7 @@ bool cbDecodePixelsMsg(pb_istream_t *stream, const pb_field_t *field,
13841384
/**************************************************************************/
13851385
void cbPixelsMsg(char *data, uint16_t len) {
13861386
WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Pixels]: ");
1387-
WS_DEBUG_PRINT(len);
1387+
WS_DEBUG_PRINTVAR(len);
13881388
WS_DEBUG_PRINTLN(" bytes.");
13891389
// zero-out current buffer
13901390
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -1509,7 +1509,7 @@ bool cbDecodeUARTMessage(pb_istream_t *stream, const pb_field_t *field,
15091509
/**************************************************************************/
15101510
void cbSignalUARTReq(char *data, uint16_t len) {
15111511
WS_DEBUG_PRINTLN("* NEW MESSAGE on Signal of type UART: ");
1512-
WS_DEBUG_PRINT(len);
1512+
WS_DEBUG_PRINTVAR(len);
15131513
WS_DEBUG_PRINTLN(" bytes.");
15141514
// zero-out current buffer
15151515
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -1627,7 +1627,7 @@ bool cbDecodeDisplayMsg(pb_istream_t *stream, const pb_field_t *field,
16271627
*/
16281628
void cbDisplayMessage(char *data, uint16_t len) {
16291629
WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Display]: ");
1630-
WS_DEBUG_PRINT(len);
1630+
WS_DEBUG_PRINTVAR(len);
16311631
WS_DEBUG_PRINTLN(" bytes.");
16321632
// zero-out current buffer
16331633
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -1708,7 +1708,7 @@ void cbRegistrationStatus(char *data, uint16_t len) {
17081708
void cbErrorTopic(char *errorData, uint16_t len) {
17091709
(void)len; // marking unused parameter to avoid compiler warning
17101710
WS_DEBUG_PRINT("IO Ban Error: ");
1711-
WS_DEBUG_PRINTLN(errorData);
1711+
WS_DEBUG_PRINTLNVAR(errorData);
17121712
// Disconnect client from broker
17131713
WS_DEBUG_PRINT("Disconnecting from MQTT..");
17141714
if (!WS._mqtt->disconnect()) {
@@ -1732,7 +1732,7 @@ void cbErrorTopic(char *errorData, uint16_t len) {
17321732
void cbThrottleTopic(char *throttleData, uint16_t len) {
17331733
(void)len; // marking unused parameter to avoid compiler warning
17341734
WS_DEBUG_PRINT("IO Throttle Error: ");
1735-
WS_DEBUG_PRINTLN(throttleData);
1735+
WS_DEBUG_PRINTLNVAR(throttleData);
17361736
char *throttleMessage;
17371737
// Parse out # of seconds from message buffer
17381738
throttleMessage = strtok(throttleData, ",");
@@ -1741,7 +1741,7 @@ void cbThrottleTopic(char *throttleData, uint16_t len) {
17411741
int throttleDuration = atoi(throttleMessage) * 1000;
17421742

17431743
WS_DEBUG_PRINT("Device is throttled for ");
1744-
WS_DEBUG_PRINT(throttleDuration);
1744+
WS_DEBUG_PRINTVAR(throttleDuration);
17451745
WS_DEBUG_PRINTLN("ms and blocking command execution.");
17461746

17471747
// If throttle duration is less than the keepalive interval, delay for the
@@ -2346,7 +2346,7 @@ bool Wippersnapper::generateWSTopics() {
23462346
_topic_signal_display_sub =
23472347
new Adafruit_MQTT_Subscribe(WS._mqtt, WS._topic_signal_display_brkr, 1);
23482348
WS_DEBUG_PRINTLN("Subscribing to DISPLAY topic: ");
2349-
WS_DEBUG_PRINTLN(WS._topic_signal_display_brkr);
2349+
WS_DEBUG_PRINTLNVAR(WS._topic_signal_display_brkr);
23502350
WS._mqtt->subscribe(_topic_signal_display_sub);
23512351
WS_DEBUG_PRINTLN("Subscribed to DISPLAY topic!");
23522352
_topic_signal_display_sub->setCallback(cbDisplayMessage);
@@ -2387,7 +2387,7 @@ bool Wippersnapper::generateWSTopics() {
23872387
/**************************************************************************/
23882388
void Wippersnapper::errorWriteHang(String error) {
23892389
// Print error
2390-
WS_DEBUG_PRINTLN(error);
2390+
WS_DEBUG_PRINTLNVAR(error);
23912391
#ifdef USE_TINYUSB
23922392
_fileSystem->writeToBootOut(error.c_str());
23932393
TinyUSBDevice.attach();
@@ -2396,7 +2396,7 @@ void Wippersnapper::errorWriteHang(String error) {
23962396
// Signal and hang forever
23972397
while (1) {
23982398
WS_DEBUG_PRINTLN("ERROR: Halted execution");
2399-
WS_DEBUG_PRINTLN(error.c_str());
2399+
WS_DEBUG_PRINTLNVAR(error.c_str());
24002400
WS.feedWDT();
24012401
statusLEDBlink(WS_LED_STATUS_ERROR_RUNTIME);
24022402
delay(1000);
@@ -2451,7 +2451,7 @@ void Wippersnapper::runNetFSM() {
24512451
feedWDT();
24522452
// attempt to connect
24532453
WS_DEBUG_PRINT("Connecting to WiFi (attempt #");
2454-
WS_DEBUG_PRINT(5 - maxAttempts);
2454+
WS_DEBUG_PRINTVAR(5 - maxAttempts);
24552455
WS_DEBUG_PRINTLN(")");
24562456
WS_PRINTER.flush();
24572457
feedWDT();
@@ -2477,11 +2477,11 @@ void Wippersnapper::runNetFSM() {
24772477
maxAttempts = 5;
24782478
while (maxAttempts > 0) {
24792479
WS_DEBUG_PRINT("Connecting to AIO MQTT (attempt #");
2480-
WS_DEBUG_PRINT(5 - maxAttempts);
2480+
WS_DEBUG_PRINTVAR(5 - maxAttempts);
24812481
WS_DEBUG_PRINTLN(")");
24822482
WS_PRINTER.flush();
24832483
WS_DEBUG_PRINT("WiFi Status: ");
2484-
WS_DEBUG_PRINTLN(networkStatus());
2484+
WS_DEBUG_PRINTLNVAR(networkStatus());
24852485
WS_PRINTER.flush();
24862486
feedWDT();
24872487
statusLEDBlink(WS_LED_STATUS_MQTT_CONNECTING);
@@ -2493,8 +2493,8 @@ void Wippersnapper::runNetFSM() {
24932493
break;
24942494
}
24952495
WS_DEBUG_PRINT("MQTT Connection Error: ");
2496-
WS_DEBUG_PRINTLN(mqttRC);
2497-
WS_DEBUG_PRINTLN(WS._mqtt->connectErrorString(mqttRC));
2496+
WS_DEBUG_PRINTLNVAR(mqttRC);
2497+
WS_DEBUG_PRINTLNVAR(WS._mqtt->connectErrorString(mqttRC));
24982498
WS_DEBUG_PRINTLN(
24992499
"Unable to connect to Adafruit IO MQTT, retrying in 3 seconds...");
25002500
delay(3000);
@@ -2536,9 +2536,9 @@ void Wippersnapper::haltError(String error, ws_led_status_t ledStatusColor,
25362536

25372537
for (int i = 0;; i++) {
25382538
WS_DEBUG_PRINT("ERROR [WDT RESET IN ");
2539-
WS_DEBUG_PRINT(seconds_until_reboot - i);
2539+
WS_DEBUG_PRINTVAR(seconds_until_reboot - i);
25402540
WS_DEBUG_PRINTLN("]: ");
2541-
WS_DEBUG_PRINTLN(error);
2541+
WS_DEBUG_PRINTLNVAR(error);
25422542
// let the WDT fail out and reset!
25432543
statusLEDSolid(ledStatusColor);
25442544
#ifndef ARDUINO_ARCH_ESP8266
@@ -2610,7 +2610,7 @@ void Wippersnapper::pingBroker() {
26102610
}
26112611
_prv_ping = millis();
26122612
WS_DEBUG_PRINT("WiFi RSSI: ");
2613-
WS_DEBUG_PRINTLN(getRSSI());
2613+
WS_DEBUG_PRINTLNVAR(getRSSI());
26142614
}
26152615
// blink status LED every STATUS_LED_KAT_BLINK_TIME millis
26162616
if (millis() > (_prvKATBlink + STATUS_LED_KAT_BLINK_TIME)) {
@@ -2756,21 +2756,21 @@ void printDeviceInfo() {
27562756
WS_DEBUG_PRINT("Board ID: ");
27572757
WS_DEBUG_PRINTLN(BOARD_ID);
27582758
WS_DEBUG_PRINT("Adafruit.io User: ");
2759-
WS_DEBUG_PRINTLN(WS._config.aio_user);
2759+
WS_DEBUG_PRINTLNVAR(WS._config.aio_user);
27602760
if (strncmp(WS._config.aio_url, "io.adafruit.com", 16) != 0) {
27612761
WS_DEBUG_PRINT("Adafruit.io URL: ");
2762-
WS_DEBUG_PRINTLN(WS._config.aio_url);
2762+
WS_DEBUG_PRINTLNVAR(WS._config.aio_url);
27632763
WS_DEBUG_PRINT("Adafruit.io Port: ");
2764-
WS_DEBUG_PRINTLN(WS._config.io_port);
2764+
WS_DEBUG_PRINTLNVAR(WS._config.io_port);
27652765
}
27662766
WS_DEBUG_PRINT("WiFi Network: ");
2767-
WS_DEBUG_PRINTLN(WS._config.network.ssid);
2767+
WS_DEBUG_PRINTLNVAR(WS._config.network.ssid);
27682768

27692769
char sMAC[18] = {0};
27702770
sprintf(sMAC, "%02X:%02X:%02X:%02X:%02X:%02X", WS._macAddr[0], WS._macAddr[1],
27712771
WS._macAddr[2], WS._macAddr[3], WS._macAddr[4], WS._macAddr[5]);
27722772
WS_DEBUG_PRINT("MAC Address: ");
2773-
WS_DEBUG_PRINTLN(sMAC);
2773+
WS_DEBUG_PRINTLNVAR(sMAC);
27742774
WS_DEBUG_PRINTLN("-------------------------------");
27752775

27762776
// (ESP32-Only) Print reason why device was reset

src/Wippersnapper.h

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,43 @@
4848

4949
// Define actual debug output functions when necessary.
5050
#ifdef WS_DEBUG
51+
52+
#ifdef ARDUINO_ARCH_ESP8266
53+
// ESP8266: Use F() macro to store string literals in Flash (PROGMEM) instead
54+
// of RAM. This saves precious RAM on memory-constrained ESP8266 devices.
55+
// NOTE: WS_DEBUG_PRINT/PRINTLN only accept string literals on ESP8266.
56+
// Use WS_DEBUG_PRINTVAR/PRINTLNVAR for variables.
57+
#define WS_DEBUG_PRINT(x) \
58+
{ WS_PRINTER.print(F(x)); } ///< Prints string literal from Flash
59+
#define WS_DEBUG_PRINTLN(x) \
60+
{ WS_PRINTER.println(F(x)); } ///< Prints string literal line from Flash
61+
#else
62+
// Other platforms: Standard variadic macros
5163
#define WS_DEBUG_PRINT(...) \
5264
{ WS_PRINTER.print(__VA_ARGS__); } ///< Prints debug output.
5365
#define WS_DEBUG_PRINTLN(...) \
5466
{ WS_PRINTER.println(__VA_ARGS__); } ///< Prints line from debug output.
67+
#endif
68+
69+
// Variable printing macros - use for non-string-literal arguments
70+
#define WS_DEBUG_PRINTVAR(...) \
71+
{ WS_PRINTER.print(__VA_ARGS__); } ///< Prints variable (any type)
72+
#define WS_DEBUG_PRINTLNVAR(...) \
73+
{ WS_PRINTER.println(__VA_ARGS__); } ///< Prints variable with newline
5574
#define WS_DEBUG_PRINTHEX(...) \
56-
{ WS_PRINTER.print(__VA_ARGS__, HEX); } ///< Prints debug output.
75+
{ WS_PRINTER.print(__VA_ARGS__, HEX); } ///< Prints in hexadecimal
76+
5777
#else
5878
#define WS_DEBUG_PRINT(...) \
59-
{} ///< Prints debug output
79+
{} ///< Disabled debug output
6080
#define WS_DEBUG_PRINTLN(...) \
61-
{} ///< Prints line from debug output.
81+
{} ///< Disabled debug output
82+
#define WS_DEBUG_PRINTVAR(...) \
83+
{} ///< Disabled debug output
84+
#define WS_DEBUG_PRINTLNVAR(...) \
85+
{} ///< Disabled debug output
86+
#define WS_DEBUG_PRINTHEX(...) \
87+
{} ///< Disabled debug output
6288
#endif
6389

6490
#define WS_DELAY_WITH_WDT(timeout) \

0 commit comments

Comments
 (0)