Skip to content

Commit bb28fb0

Browse files
committed
Refactor F() macro in Commander-API-Commands
1 parent 9130161 commit bb28fb0

File tree

1 file changed

+36
-102
lines changed

1 file changed

+36
-102
lines changed

src/Commander-API-Commands.cpp

Lines changed: 36 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ SOFTWARE.
3333

3434
#include "Commander-API-Commands.hpp"
3535

36+
#if defined(ARDUINO) && defined(__AVR__)
37+
#define FF(s) F(s)
38+
#else
39+
#define FF(s) (s)
40+
#endif
41+
42+
3643
void commander_millis_func( char *args, Stream *response ){
3744

3845
char buff[ 20 ];
@@ -92,27 +99,13 @@ void commander_pinMode_func( char *args, Stream *response ){
9299
argResult = sscanf( args, "%d %d", &pin, &direction );
93100

94101
if( argResult != 2 ){
95-
96-
#ifdef __AVR__
97-
response -> print( F( "Argument error!" ) );
98-
#else
99-
response -> print( (const char*)"Argument error!" );
100-
#endif
101-
102+
response -> print( FF( "Argument error!" ) );
102103
return;
103-
104104
}
105105

106106
if( pin < 0 || direction < 0 ){
107-
108-
#ifdef __AVR__
109-
response -> print( F( "Argument error!" ) );
110-
#else
111-
response -> print( (const char*)"Argument error!" );
112-
#endif
113-
107+
response -> print( FF( "Argument error!" ) );
114108
return;
115-
116109
}
117110

118111
if( direction == 0 ){
@@ -128,13 +121,7 @@ void commander_pinMode_func( char *args, Stream *response ){
128121
}
129122

130123
else{
131-
132-
#ifdef __AVR__
133-
response -> print( F( "Argument error! Second argument has to be 1 or 0!" ) );
134-
#else
135-
response -> print( (const char*)"Argument error! Second argument has to be 1 or 0!" );
136-
#endif
137-
124+
response -> print( FF( "Argument error! Second argument has to be 1 or 0!" ) );
138125
}
139126

140127
}
@@ -150,24 +137,14 @@ void commander_digitalWrite_func( char *args, Stream *response ){
150137

151138
if( argResult != 2 ){
152139

153-
#ifdef __AVR__
154-
response -> print( F( "Argument error!" ) );
155-
#else
156-
response -> print( (const char*)"Argument error!" );
157-
#endif
158-
140+
response -> print( FF( "Argument error!" ) );
159141
return;
160142

161143
}
162144

163145
if( pin < 0 || state < 0 ){
164146

165-
#ifdef __AVR__
166-
response -> print( F( "Argument error!" ) );
167-
#else
168-
response -> print( (const char*)"Argument error!" );
169-
#endif
170-
147+
response -> print( FF( "Argument error!" ) );
171148
return;
172149

173150
}
@@ -185,13 +162,7 @@ void commander_digitalWrite_func( char *args, Stream *response ){
185162
}
186163

187164
else{
188-
189-
#ifdef __AVR__
190-
response -> print( F( "Argument error! Second argument has to be 1 or 0!" ) );
191-
#else
192-
response -> print( (const char*)"Argument error! Second argument has to be 1 or 0!" );
193-
#endif
194-
165+
response -> print( FF( "Argument error! Second argument has to be 1 or 0!" ) );
195166
}
196167

197168
}
@@ -205,27 +176,13 @@ void commander_digitalRead_func( char *args, Stream *response ){
205176
argResult = sscanf( args, "%d", &pin );
206177

207178
if( argResult != 1 ){
208-
209-
#ifdef __AVR__
210-
response -> print( F( "Argument error!" ) );
211-
#else
212-
response -> print( (const char*)"Argument error!" );
213-
#endif
214-
179+
response -> print( FF( "Argument error!" ) );
215180
return;
216-
217181
}
218182

219183
if( pin < 0 ){
220-
221-
#ifdef __AVR__
222-
response -> print( F( "Argument error!" ) );
223-
#else
224-
response -> print( (const char*)"Argument error!" );
225-
#endif
226-
184+
response -> print( FF( "Argument error!" ) );
227185
return;
228-
229186
}
230187

231188
response -> print( digitalRead( pin ) );
@@ -378,14 +335,14 @@ void commander_analogRead_func( char *args, Stream *response ){
378335

379336
if( argResult != 1 ){
380337

381-
response -> print( (const char*)"Argument error!" );
338+
response -> print( "Argument error!" );
382339
return;
383340

384341
}
385342

386343
if( pin < 0 ){
387344

388-
response -> print( (const char*)"Argument error!" );
345+
response -> print( "Argument error!" );
389346
return;
390347

391348
}
@@ -400,24 +357,24 @@ void commander_analogRead_func( char *args, Stream *response ){
400357

401358
void commander_ipconfig_func( char *args, Stream *response ){
402359

403-
response -> println( (const char*)"Wi-Fi:\r\n" );
360+
response -> println( "Wi-Fi:\r\n" );
404361

405-
response -> print( (const char*)"\tIP Address . . : " );
362+
response -> print( "\tIP Address . . : " );
406363
response -> println( WiFi.localIP() );
407364

408-
response -> print( (const char*)"\tSubnet Mask . . : " );
365+
response -> print( "\tSubnet Mask . . : " );
409366
response -> println( WiFi.subnetMask() );
410367

411-
response -> print( (const char*)"\tDefault Gateway : " );
368+
response -> print( "\tDefault Gateway : " );
412369
response -> println( WiFi.gatewayIP() );
413370

414371
}
415372

416373
void commander_wifiStat_func( char *args, Stream *response ){
417374

418-
response -> println( (const char*)"Wi-Fi:\r\n" );
375+
response -> println( "Wi-Fi:\r\n" );
419376

420-
response -> print( (const char*)"\tMode: " );
377+
response -> print( "\tMode: " );
421378

422379
switch( WiFi.getMode() ){
423380

@@ -461,11 +418,11 @@ void commander_wifiStat_func( char *args, Stream *response ){
461418

462419
}
463420

464-
response -> print( (const char*)"\tRSSI: " );
421+
response -> print( "\tRSSI: " );
465422
response -> print( WiFi.RSSI() );
466423
response -> println( "dBm" );
467424

468-
response -> print( (const char*)"\tMAC : " );
425+
response -> print( "\tMAC : " );
469426
response -> println( WiFi.macAddress() );
470427

471428
}
@@ -476,11 +433,11 @@ void commander_wifiScan_func( char *args, Stream *response ){
476433
int i;
477434
bool hasLocked = false;
478435

479-
response -> print( (const char*)"Scanning for available networks... " );
436+
response -> print( "Scanning for available networks... " );
480437

481438
num = WiFi.scanNetworks();
482439

483-
response -> println( (const char*)"[ OK ]:" );
440+
response -> println( "[ OK ]:" );
484441

485442
for( i = 0; i < num; i++ ){
486443

@@ -519,7 +476,7 @@ void commander_wifiScan_func( char *args, Stream *response ){
519476
}
520477

521478
if( hasLocked ){
522-
response -> print( (const char*)"\r\n * means closed network." );
479+
response -> print( "\r\n * means closed network." );
523480
}
524481

525482
}
@@ -540,7 +497,7 @@ void commander_configTime_func( char *args, Stream *response ){
540497
if( argResult == 3 ){
541498

542499
configTime( gmtOffset_sec, daylightOffset_sec, ntpServer );
543-
response -> print( (const char*)"Time configured." );
500+
response -> print( "Time configured." );
544501
return;
545502

546503
}
@@ -549,15 +506,15 @@ void commander_configTime_func( char *args, Stream *response ){
549506

550507
if( argResult == 2 ){
551508

552-
configTime( gmtOffset_sec, daylightOffset_sec, (const char*)"pool.ntp.org" );
553-
response -> print( (const char*)"Time configured with default NTP server: pool.ntp.org" );
509+
configTime( gmtOffset_sec, daylightOffset_sec, "pool.ntp.org" );
510+
response -> print( "Time configured with default NTP server: pool.ntp.org" );
554511
return;
555512

556513
}
557514

558515
else{
559516

560-
response -> print( (const char*)"Argument error!" );
517+
response -> print( "Argument error!" );
561518
return;
562519

563520
}
@@ -794,11 +751,7 @@ void commander_neofetch_func( char *args, Stream *response ){
794751

795752
void commander_reboot_func( char *args, Stream *response ){
796753

797-
#ifdef __AVR__
798-
response -> println( F( "Rebooting..." ) );
799-
#else
800-
response -> println( (const char*)"Rebooting..." );
801-
#endif
754+
response -> println( FF( "Rebooting..." ) );
802755

803756
#if defined( ESP32 ) || ( ESP8266 )
804757

@@ -813,12 +766,6 @@ void commander_reboot_func( char *args, Stream *response ){
813766

814767
}
815768

816-
#ifdef ARDUINO
817-
818-
819-
820-
#endif
821-
822769

823770
void commander_sin_func( char *args, Stream *response ){
824771

@@ -845,12 +792,7 @@ void commander_not_func( char *args, Stream *response ){
845792

846793
if( argResult != 1 ){
847794

848-
#ifdef __AVR__
849-
response -> print( F( "Argument error!" ) );
850-
#else
851-
response -> print( (const char*)"Argument error!" );
852-
#endif
853-
795+
response -> print( FF( "Argument error!" ) );
854796
return;
855797

856798
}
@@ -869,23 +811,15 @@ void commander_random_func( char *args, Stream *response ){
869811

870812
if( argResult != 2 ){
871813

872-
#ifdef __AVR__
873-
response -> print( F( "Argument error!" ) );
874-
#else
875-
response -> print( (const char*)"Argument error!" );
876-
#endif
814+
response -> print( FF( "Argument error!" ) );
877815

878816
return;
879817

880818
}
881819

882820
if( min >= max ){
883821

884-
#ifdef __AVR__
885-
response -> print( F( "Argument erro! First argument is min, second is max!" ) );
886-
#else
887-
response -> print( (const char*)"Argument erro! First argument is min, second is max!" );
888-
#endif
822+
response -> print( FF( "Argument erro! First argument is min, second is max!" ) );
889823

890824
return;
891825

0 commit comments

Comments
 (0)