Skip to content

Commit 9130161

Browse files
committed
Refactor F() macro to remove unnecessary #ifdefs
1 parent 9d185dc commit 9130161

File tree

1 file changed

+22
-100
lines changed

1 file changed

+22
-100
lines changed

src/Commander-API.cpp

Lines changed: 22 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ SOFTWARE.
3434

3535
#include "Commander-API.hpp"
3636

37+
#if defined(ARDUINO) && defined(__AVR__)
38+
#define FF(s) F(s)
39+
#else
40+
#define FF(s) (s)
41+
#endif
42+
3743
const char *Commander::version = COMMANDER_API_VERSION;
3844

3945
void Commander::attachTreeFunction( API_t *API_tree_p, uint32_t API_tree_size_p ){
@@ -42,27 +48,16 @@ void Commander::attachTreeFunction( API_t *API_tree_p, uint32_t API_tree_size_p
4248
API_tree = API_tree_p;
4349
API_tree_size = API_tree_size_p;
4450

45-
#if defined( ARDUINO ) && defined( __AVR__ )
46-
47-
dbgResponse -> print( F( "API tree attached with " ) );
48-
dbgResponse -> print( API_tree_size );
49-
dbgResponse -> println( F( " commands." ) );
50-
51-
#else
52-
53-
dbgResponse -> print( (const char*)"API tree attached with " );
51+
dbgResponse -> print( FF( "API tree attached with " ) );
5452
dbgResponse -> print( API_tree_size );
55-
dbgResponse -> println( " commands." );
56-
57-
#endif
58-
53+
dbgResponse -> println( FF( " commands." ) );
5954
}
6055

6156
void Commander::init(){
6257

6358
// Generic conter variables.
6459
uint32_t i;
65-
uint32_t j;
60+
uint32_t j;
6661

6762
// Temporary variable, used to flip elements.
6863
API_t temp;
@@ -82,26 +77,10 @@ void Commander::init(){
8277

8378
#endif
8479

85-
#if defined( ARDUINO ) && defined( __AVR__ )
86-
87-
dbgResponse -> println( F( "Commander init start" ) );
88-
89-
#else
90-
91-
dbgResponse -> println( (const char*)"Commander init start" );
92-
93-
#endif
80+
dbgResponse -> println( FF( "Commander init start" ) );
9481

9582
// Make the tree ordered by alphabet.
96-
#if defined( ARDUINO ) && defined( __AVR__ )
97-
98-
dbgResponse -> print( F( " Creating alphabetical order... " ) );
99-
100-
#else
101-
102-
dbgResponse -> print( (const char*)" Creating alphabetical order... " );
103-
104-
#endif
83+
dbgResponse -> print( FF( " Creating alphabetical order... " ) );
10584

10685
// Bubble sort. We need to sort the commands into an alphabetical order.
10786
for( i = 0; i < API_tree_size; i++ ){
@@ -128,42 +107,16 @@ void Commander::init(){
128107

129108
}
130109

131-
#if defined( ARDUINO ) && defined( __AVR__ )
132-
133-
dbgResponse -> println( F( "[ OK ]" ) );
134-
135-
#else
136-
137-
dbgResponse -> println( (const char*)"[ OK ]" );
138-
139-
#endif
140-
110+
dbgResponse -> println( FF( "[ OK ]" ) );
141111

142112
// Optimize the tree to make it balanced.
143113
// It is necessary to speed up the command
144114
// search phase.
145-
#if defined( ARDUINO ) && defined( __AVR__ )
146-
147-
dbgResponse -> print( F( " Create balanced binary structure... " ) );
148-
149-
#else
150-
151-
dbgResponse -> print( (const char*)" Create balanced binary structure... " );
152-
153-
#endif
115+
dbgResponse -> print( FF( " Create balanced binary structure... " ) );
154116
optimize_api_tree();
155117

156-
#if defined( ARDUINO ) && defined( __AVR__ )
157-
158-
dbgResponse -> println( F( "[ OK ]" ) );
159-
dbgResponse -> println( F( "Commander init finished!" ) );
160-
161-
#else
162-
163-
dbgResponse -> println( (const char*)"[ OK ]" );
164-
dbgResponse -> println( (const char*)"Commander init finished!" );
165-
166-
#endif
118+
dbgResponse -> println( FF( "[ OK ]" ) );
119+
dbgResponse -> println( FF( "Commander init finished!" ) );
167120

168121
}
169122

@@ -492,19 +445,9 @@ void Commander::executeCommand( char *cmd, void* parent ){
492445

493446
// If we went through the whole tree and we did not found the command in it,
494447
// we have to notice the user abut the problem. Maybe a Type-O
495-
#if defined( ARDUINO ) && defined( __AVR__ )
496-
497-
response -> print( F( "Command \'" ) );
448+
response -> print( FF( "Command \'" ) );
498449
response -> print( tempBuff );
499-
response -> println( F( "\' not found!" ) );
500-
501-
#else
502-
503-
response -> print( (const char*)"Command \'" );
504-
response -> print( tempBuff );
505-
response -> println( (const char*)"\' not found!" );
506-
507-
#endif
450+
response -> println( FF( "\' not found!" ) );
508451

509452
#ifdef COMMANDER_ENABLE_PIPE_MODULE
510453

@@ -668,31 +611,10 @@ void Commander::printHelp( Stream* out, bool description, bool style ){
668611
uint32_t i;
669612

670613
if( style ){
671-
672-
#if defined( ARDUINO ) && defined( __AVR__ )
673-
674-
out -> println( F( "\033[1;31m----\033[1;32m Available commands \033[1;31m----\033[0;37m\r\n" ) );
675-
676-
#else
677-
678-
out -> println( (const char*)"\033[1;31m----\033[1;32m Available commands \033[1;31m----\033[0;37m\r\n" );
679-
680-
#endif
681-
614+
out -> println( FF( "\033[1;31m----\033[1;32m Available commands \033[1;31m----\033[0;37m\r\n" ) );
682615
}
683-
684616
else{
685-
686-
#if defined( ARDUINO ) && defined( __AVR__ )
687-
688-
out -> println( F( "---- Available commands ----\r\n" ) );
689-
690-
#else
691-
692-
out -> println( (const char*)"---- Available commands ----\r\n" );
693-
694-
#endif
695-
617+
out -> println( FF( "---- Available commands ----\r\n" ) );
696618
}
697619

698620
for( i = 0; i < API_tree_size; i++ ){
@@ -719,9 +641,9 @@ void Commander::printHelp( Stream* out, bool description, bool style ){
719641

720642
else if( memoryType == MEMORY_PROGMEM ){
721643

722-
out -> print( F( "\033[1;32m" ) );
644+
out -> print( FF( "\033[1;32m" ) );
723645
out -> print( API_tree[ find_api_index_by_place( i ) ].name_P );
724-
out -> print( F( "\033[0;37m" ) );
646+
out -> print( FF( "\033[0;37m" ) );
725647
out -> print( ':' );
726648
out -> print( ' ' );
727649
out -> print( API_tree[ find_api_index_by_place( i ) ].desc_P );
@@ -844,4 +766,4 @@ void Commander::enableFormatting(){
844766
}
845767
void Commander::disableFormatting(){
846768
formatting = false;
847-
}
769+
}

0 commit comments

Comments
 (0)