Skip to content

Commit 1023cad

Browse files
authored
Merge pull request #874 from adafruit/v1-esp8266-build-sz
Reduce firmware size on ESP8266
2 parents 8cad3f6 + 9d6e9b8 commit 1023cad

28 files changed

+174
-145
lines changed

Doxyfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,7 @@ ENABLE_PREPROCESSING = YES
20342034
# The default value is: NO.
20352035
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
20362036

2037-
MACRO_EXPANSION = NO
2037+
MACRO_EXPANSION = YES
20382038

20392039
# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES then
20402040
# the macro expansion is limited to the macros specified with the PREDEFINED and
@@ -2074,7 +2074,7 @@ INCLUDE_FILE_PATTERNS =
20742074
# recursively expanded use the := operator instead of the = operator.
20752075
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
20762076

2077-
PREDEFINED = __cdecl=, ARDUINO_ARCH_ESP32=1, ARDUINO_FUNHOUSE_ESP32S2=1
2077+
PREDEFINED = __cdecl=, ARDUINO_ARCH_ESP32=1, ARDUINO_FUNHOUSE_ESP32S2=1, PROGMEM=
20782078

20792079
# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
20802080
# tag can be used to specify a list of macro names that should be expanded. The

examples/Wippersnapper_demo/Wippersnapper_demo.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void setup() {
2626
Serial.begin(115200);
2727
//while (!Serial) delay(10);
2828

29+
2930
wipper.connect();
3031

3132
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit WipperSnapper
2-
version=1.0.0-beta.123
2+
version=1.0.0-beta.124
33
author=Adafruit
44
maintainer=Adafruit <adafruitio@adafruit.com>
55
sentence=Arduino application for Adafruit.io WipperSnapper

platformio.ini

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,12 +508,11 @@ build_flags =
508508
-DSSL_ALL
509509
-DMMU_3232
510510
-DNON32XFER_FAST
511-
-DDEBUG_DISABLED
512-
-DDEBUG_LEVEL_NONE
511+
-DDEBUG_ENABLED
512+
-DDEBUG_LEVEL_CORE
513513
-DPIO_FRAMEWORK_ARDUINO_MMU_CACHE16_IRAM48 ; avoid section `.text1' will not fit in region `iram1_0_seg'
514514
board_build.eesz=4M2M
515515
board_build.filesystem = littlefs
516-
upload_port = COM19
517516

518517
; SAMD51 Boards ;
519518

src/Wippersnapper.cpp

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ void Wippersnapper::provision() {
116116
#else
117117
set_user_key(); // non-fs-backed, sets global credentials within network iface
118118
#endif
119+
119120
// Set the status pixel's brightness
120121
setStatusLEDBrightness(WS._config.status_pixel_brightness);
122+
121123
// Set device's wireless credentials
122124
set_ssid_pass();
123125
}
@@ -405,7 +407,7 @@ bool cbSignalMsg(pb_istream_t *stream, const pb_field_t *field, void **arg) {
405407

406408
pb_size_t arr_sz = field->array_size;
407409
WS_DEBUG_PRINT("Sub-messages found: ");
408-
WS_DEBUG_PRINTLN(arr_sz);
410+
WS_DEBUG_PRINTLNVAR(arr_sz);
409411

410412
if (field->tag ==
411413
wippersnapper_signal_v1_CreateSignalRequest_pin_configs_tag) {
@@ -492,7 +494,7 @@ bool Wippersnapper::decodeSignalMsg(
492494
/**************************************************************************/
493495
void cbSignalTopic(char *data, uint16_t len) {
494496
WS_DEBUG_PRINTLN("cbSignalTopic: New Msg on Signal Topic");
495-
WS_DEBUG_PRINT(len);
497+
WS_DEBUG_PRINTVAR(len);
496498
WS_DEBUG_PRINTLN(" bytes.");
497499
// zero-out current buffer
498500
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -877,7 +879,7 @@ bool cbDecodeSignalRequestI2C(pb_istream_t *stream, const pb_field_t *field,
877879
/**************************************************************************/
878880
void cbSignalI2CReq(char *data, uint16_t len) {
879881
WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Signal-I2C]: ");
880-
WS_DEBUG_PRINT(len);
882+
WS_DEBUG_PRINTVAR(len);
881883
WS_DEBUG_PRINTLN(" bytes.");
882884
// zero-out current buffer
883885
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -936,11 +938,11 @@ bool cbDecodeServoMsg(pb_istream_t *stream, const pb_field_t *field,
936938
attached = false;
937939
} else {
938940
WS_DEBUG_PRINT("ATTACHED servo w/minPulseWidth: ");
939-
WS_DEBUG_PRINT(msgServoAttachReq.min_pulse_width);
941+
WS_DEBUG_PRINTVAR(msgServoAttachReq.min_pulse_width);
940942
WS_DEBUG_PRINT(" uS and maxPulseWidth: ");
941-
WS_DEBUG_PRINT(msgServoAttachReq.min_pulse_width);
943+
WS_DEBUG_PRINTVAR(msgServoAttachReq.min_pulse_width);
942944
WS_DEBUG_PRINT("uS on pin: ");
943-
WS_DEBUG_PRINTLN(servoPin);
945+
WS_DEBUG_PRINTLNVAR(servoPin);
944946
}
945947

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

988990
WS_DEBUG_PRINT("Writing pulse width of ");
989-
WS_DEBUG_PRINT((int)msgServoWriteReq.pulse_width);
991+
WS_DEBUG_PRINTVAR((int)msgServoWriteReq.pulse_width);
990992
WS_DEBUG_PRINT("uS to servo on pin#: ");
991-
WS_DEBUG_PRINTLN(servoPin);
993+
WS_DEBUG_PRINTLNVAR(servoPin);
992994
WS._servoComponent->servo_write(atoi(servoPin),
993995
(int)msgServoWriteReq.pulse_width);
994996
} else if (field->tag ==
@@ -1008,7 +1010,7 @@ bool cbDecodeServoMsg(pb_istream_t *stream, const pb_field_t *field,
10081010
// execute servo detach request
10091011
char *servoPin = msgServoDetachReq.servo_pin + 1;
10101012
WS_DEBUG_PRINT("Detaching servo from pin ");
1011-
WS_DEBUG_PRINTLN(servoPin);
1013+
WS_DEBUG_PRINTLNVAR(servoPin);
10121014
WS._servoComponent->servo_detach(atoi(servoPin));
10131015
} else {
10141016
WS_DEBUG_PRINTLN("Unable to decode servo message type!");
@@ -1029,7 +1031,7 @@ bool cbDecodeServoMsg(pb_istream_t *stream, const pb_field_t *field,
10291031
/**************************************************************************/
10301032
void cbServoMsg(char *data, uint16_t len) {
10311033
WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Servo]: ");
1032-
WS_DEBUG_PRINT(len);
1034+
WS_DEBUG_PRINTVAR(len);
10331035
WS_DEBUG_PRINTLN(" bytes.");
10341036
// zero-out current buffer
10351037
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -1143,9 +1145,9 @@ bool cbPWMDecodeMsg(pb_istream_t *stream, const pb_field_t *field, void **arg) {
11431145
// execute PWM pin duty cycle write request
11441146
char *pwmPin = msgPWMWriteFreqRequest.pin + 1;
11451147
WS_DEBUG_PRINT("Writing frequency: ");
1146-
WS_DEBUG_PRINT(msgPWMWriteFreqRequest.frequency);
1148+
WS_DEBUG_PRINTVAR(msgPWMWriteFreqRequest.frequency);
11471149
WS_DEBUG_PRINT("Hz to pin ");
1148-
WS_DEBUG_PRINTLN(atoi(pwmPin));
1150+
WS_DEBUG_PRINTLNVAR(atoi(pwmPin));
11491151
WS._pwmComponent->writeTone(atoi(pwmPin), msgPWMWriteFreqRequest.frequency);
11501152
} else if (field->tag ==
11511153
wippersnapper_signal_v1_PWMRequest_write_duty_request_tag) {
@@ -1184,7 +1186,7 @@ bool cbPWMDecodeMsg(pb_istream_t *stream, const pb_field_t *field, void **arg) {
11841186
/**************************************************************************/
11851187
void cbPWMMsg(char *data, uint16_t len) {
11861188
WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: PWM]: ");
1187-
WS_DEBUG_PRINT(len);
1189+
WS_DEBUG_PRINTVAR(len);
11881190
WS_DEBUG_PRINTLN(" bytes.");
11891191
// zero-out current buffer
11901192
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -1271,7 +1273,7 @@ bool cbDecodeDs18x20Msg(pb_istream_t *stream, const pb_field_t *field,
12711273
/**************************************************************************/
12721274
void cbSignalDSReq(char *data, uint16_t len) {
12731275
WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Signal-DS]: ");
1274-
WS_DEBUG_PRINT(len);
1276+
WS_DEBUG_PRINTVAR(len);
12751277
WS_DEBUG_PRINTLN(" bytes.");
12761278
// zero-out current buffer
12771279
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -1384,7 +1386,7 @@ bool cbDecodePixelsMsg(pb_istream_t *stream, const pb_field_t *field,
13841386
/**************************************************************************/
13851387
void cbPixelsMsg(char *data, uint16_t len) {
13861388
WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Pixels]: ");
1387-
WS_DEBUG_PRINT(len);
1389+
WS_DEBUG_PRINTVAR(len);
13881390
WS_DEBUG_PRINTLN(" bytes.");
13891391
// zero-out current buffer
13901392
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -1509,7 +1511,7 @@ bool cbDecodeUARTMessage(pb_istream_t *stream, const pb_field_t *field,
15091511
/**************************************************************************/
15101512
void cbSignalUARTReq(char *data, uint16_t len) {
15111513
WS_DEBUG_PRINTLN("* NEW MESSAGE on Signal of type UART: ");
1512-
WS_DEBUG_PRINT(len);
1514+
WS_DEBUG_PRINTVAR(len);
15131515
WS_DEBUG_PRINTLN(" bytes.");
15141516
// zero-out current buffer
15151517
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -1627,7 +1629,7 @@ bool cbDecodeDisplayMsg(pb_istream_t *stream, const pb_field_t *field,
16271629
*/
16281630
void cbDisplayMessage(char *data, uint16_t len) {
16291631
WS_DEBUG_PRINTLN("* NEW MESSAGE [Topic: Display]: ");
1630-
WS_DEBUG_PRINT(len);
1632+
WS_DEBUG_PRINTVAR(len);
16311633
WS_DEBUG_PRINTLN(" bytes.");
16321634
// zero-out current buffer
16331635
memset(WS._buffer, 0, sizeof(WS._buffer));
@@ -1708,7 +1710,7 @@ void cbRegistrationStatus(char *data, uint16_t len) {
17081710
void cbErrorTopic(char *errorData, uint16_t len) {
17091711
(void)len; // marking unused parameter to avoid compiler warning
17101712
WS_DEBUG_PRINT("IO Ban Error: ");
1711-
WS_DEBUG_PRINTLN(errorData);
1713+
WS_DEBUG_PRINTLNVAR(errorData);
17121714
// Disconnect client from broker
17131715
WS_DEBUG_PRINT("Disconnecting from MQTT..");
17141716
if (!WS._mqtt->disconnect()) {
@@ -1732,7 +1734,7 @@ void cbErrorTopic(char *errorData, uint16_t len) {
17321734
void cbThrottleTopic(char *throttleData, uint16_t len) {
17331735
(void)len; // marking unused parameter to avoid compiler warning
17341736
WS_DEBUG_PRINT("IO Throttle Error: ");
1735-
WS_DEBUG_PRINTLN(throttleData);
1737+
WS_DEBUG_PRINTLNVAR(throttleData);
17361738
char *throttleMessage;
17371739
// Parse out # of seconds from message buffer
17381740
throttleMessage = strtok(throttleData, ",");
@@ -1741,7 +1743,7 @@ void cbThrottleTopic(char *throttleData, uint16_t len) {
17411743
int throttleDuration = atoi(throttleMessage) * 1000;
17421744

17431745
WS_DEBUG_PRINT("Device is throttled for ");
1744-
WS_DEBUG_PRINT(throttleDuration);
1746+
WS_DEBUG_PRINTVAR(throttleDuration);
17451747
WS_DEBUG_PRINTLN("ms and blocking command execution.");
17461748

17471749
// If throttle duration is less than the keepalive interval, delay for the
@@ -2346,7 +2348,7 @@ bool Wippersnapper::generateWSTopics() {
23462348
_topic_signal_display_sub =
23472349
new Adafruit_MQTT_Subscribe(WS._mqtt, WS._topic_signal_display_brkr, 1);
23482350
WS_DEBUG_PRINTLN("Subscribing to DISPLAY topic: ");
2349-
WS_DEBUG_PRINTLN(WS._topic_signal_display_brkr);
2351+
WS_DEBUG_PRINTLNVAR(WS._topic_signal_display_brkr);
23502352
WS._mqtt->subscribe(_topic_signal_display_sub);
23512353
WS_DEBUG_PRINTLN("Subscribed to DISPLAY topic!");
23522354
_topic_signal_display_sub->setCallback(cbDisplayMessage);
@@ -2387,7 +2389,7 @@ bool Wippersnapper::generateWSTopics() {
23872389
/**************************************************************************/
23882390
void Wippersnapper::errorWriteHang(String error) {
23892391
// Print error
2390-
WS_DEBUG_PRINTLN(error);
2392+
WS_DEBUG_PRINTLNVAR(error);
23912393
#ifdef USE_TINYUSB
23922394
_fileSystem->writeToBootOut(error.c_str());
23932395
TinyUSBDevice.attach();
@@ -2396,7 +2398,7 @@ void Wippersnapper::errorWriteHang(String error) {
23962398
// Signal and hang forever
23972399
while (1) {
23982400
WS_DEBUG_PRINTLN("ERROR: Halted execution");
2399-
WS_DEBUG_PRINTLN(error.c_str());
2401+
WS_DEBUG_PRINTLNVAR(error.c_str());
24002402
WS.feedWDT();
24012403
statusLEDBlink(WS_LED_STATUS_ERROR_RUNTIME);
24022404
delay(1000);
@@ -2451,7 +2453,7 @@ void Wippersnapper::runNetFSM() {
24512453
feedWDT();
24522454
// attempt to connect
24532455
WS_DEBUG_PRINT("Connecting to WiFi (attempt #");
2454-
WS_DEBUG_PRINT(5 - maxAttempts);
2456+
WS_DEBUG_PRINTVAR(5 - maxAttempts);
24552457
WS_DEBUG_PRINTLN(")");
24562458
WS_PRINTER.flush();
24572459
feedWDT();
@@ -2477,11 +2479,11 @@ void Wippersnapper::runNetFSM() {
24772479
maxAttempts = 5;
24782480
while (maxAttempts > 0) {
24792481
WS_DEBUG_PRINT("Connecting to AIO MQTT (attempt #");
2480-
WS_DEBUG_PRINT(5 - maxAttempts);
2482+
WS_DEBUG_PRINTVAR(5 - maxAttempts);
24812483
WS_DEBUG_PRINTLN(")");
24822484
WS_PRINTER.flush();
24832485
WS_DEBUG_PRINT("WiFi Status: ");
2484-
WS_DEBUG_PRINTLN(networkStatus());
2486+
WS_DEBUG_PRINTLNVAR(networkStatus());
24852487
WS_PRINTER.flush();
24862488
feedWDT();
24872489
statusLEDBlink(WS_LED_STATUS_MQTT_CONNECTING);
@@ -2493,8 +2495,8 @@ void Wippersnapper::runNetFSM() {
24932495
break;
24942496
}
24952497
WS_DEBUG_PRINT("MQTT Connection Error: ");
2496-
WS_DEBUG_PRINTLN(mqttRC);
2497-
WS_DEBUG_PRINTLN(WS._mqtt->connectErrorString(mqttRC));
2498+
WS_DEBUG_PRINTLNVAR(mqttRC);
2499+
WS_DEBUG_PRINTLNVAR(WS._mqtt->connectErrorString(mqttRC));
24982500
WS_DEBUG_PRINTLN(
24992501
"Unable to connect to Adafruit IO MQTT, retrying in 3 seconds...");
25002502
delay(3000);
@@ -2536,9 +2538,9 @@ void Wippersnapper::haltError(String error, ws_led_status_t ledStatusColor,
25362538

25372539
for (int i = 0;; i++) {
25382540
WS_DEBUG_PRINT("ERROR [WDT RESET IN ");
2539-
WS_DEBUG_PRINT(seconds_until_reboot - i);
2541+
WS_DEBUG_PRINTVAR(seconds_until_reboot - i);
25402542
WS_DEBUG_PRINTLN("]: ");
2541-
WS_DEBUG_PRINTLN(error);
2543+
WS_DEBUG_PRINTLNVAR(error);
25422544
// let the WDT fail out and reset!
25432545
statusLEDSolid(ledStatusColor);
25442546
#ifndef ARDUINO_ARCH_ESP8266
@@ -2610,7 +2612,7 @@ void Wippersnapper::pingBroker() {
26102612
}
26112613
_prv_ping = millis();
26122614
WS_DEBUG_PRINT("WiFi RSSI: ");
2613-
WS_DEBUG_PRINTLN(getRSSI());
2615+
WS_DEBUG_PRINTLNVAR(getRSSI());
26142616
}
26152617
// blink status LED every STATUS_LED_KAT_BLINK_TIME millis
26162618
if (millis() > (_prvKATBlink + STATUS_LED_KAT_BLINK_TIME)) {
@@ -2756,21 +2758,21 @@ void printDeviceInfo() {
27562758
WS_DEBUG_PRINT("Board ID: ");
27572759
WS_DEBUG_PRINTLN(BOARD_ID);
27582760
WS_DEBUG_PRINT("Adafruit.io User: ");
2759-
WS_DEBUG_PRINTLN(WS._config.aio_user);
2761+
WS_DEBUG_PRINTLNVAR(WS._config.aio_user);
27602762
if (strncmp(WS._config.aio_url, "io.adafruit.com", 16) != 0) {
27612763
WS_DEBUG_PRINT("Adafruit.io URL: ");
2762-
WS_DEBUG_PRINTLN(WS._config.aio_url);
2764+
WS_DEBUG_PRINTLNVAR(WS._config.aio_url);
27632765
WS_DEBUG_PRINT("Adafruit.io Port: ");
2764-
WS_DEBUG_PRINTLN(WS._config.io_port);
2766+
WS_DEBUG_PRINTLNVAR(WS._config.io_port);
27652767
}
27662768
WS_DEBUG_PRINT("WiFi Network: ");
2767-
WS_DEBUG_PRINTLN(WS._config.network.ssid);
2769+
WS_DEBUG_PRINTLNVAR(WS._config.network.ssid);
27682770

27692771
char sMAC[18] = {0};
27702772
sprintf(sMAC, "%02X:%02X:%02X:%02X:%02X:%02X", WS._macAddr[0], WS._macAddr[1],
27712773
WS._macAddr[2], WS._macAddr[3], WS._macAddr[4], WS._macAddr[5]);
27722774
WS_DEBUG_PRINT("MAC Address: ");
2773-
WS_DEBUG_PRINTLN(sMAC);
2775+
WS_DEBUG_PRINTLNVAR(sMAC);
27742776
WS_DEBUG_PRINTLN("-------------------------------");
27752777

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

src/Wippersnapper.h

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,45 @@
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
88+
#define WS_DEBUG_HEAP(label) \
89+
{} ///< Disabled heap debug output
6290
#endif
6391

6492
#define WS_DELAY_WITH_WDT(timeout) \
@@ -139,7 +167,7 @@
139167
#endif
140168

141169
#define WS_VERSION \
142-
"1.0.0-beta.122" ///< WipperSnapper app. version (semver-formatted)
170+
"1.0.0-beta.124" ///< WipperSnapper app. version (semver-formatted)
143171

144172
// Reserved Adafruit IO MQTT topics
145173
#define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic

0 commit comments

Comments
 (0)