19
19
*/
20
20
#include "Shell.h"
21
21
22
- /**
23
- * Parses the string and finds all the substrings (arguments)
24
- *
25
- * @param buf The buffer containing the original string
26
- *
27
- * @param argv Pointer to char * array to place the pointers to substrings
28
- *
29
- * @param maxargs The maximum number of pointers that the previous array can hold
30
- *
31
- * @return The total of args parsed
32
- */
33
- static int shell_parse (char * buf , char * * argv , unsigned short maxargs );
34
-
35
- /**
36
- * Prints the command shell prompt
37
- */
38
- static void shell_prompt ();
39
-
40
-
41
- static void shell_format (const char * fmt , va_list va );
42
-
43
- /**
44
- * Default message of the day
45
- */
46
- const char defaultmotd [] = "uShell 1.0 (c) 2015 Jesus Ruben Santa Anna Z.\r\nVisit us: www.geekfactory.mx\r\n" ;
47
-
48
- /**
49
- * String for the shell prompt
50
- */
51
- const char prompt [] = "device>" ;
52
-
53
22
/**
54
23
* This structure array contains the available commands and they associated
55
24
* function entry point, other data required by the commands may be added to
@@ -68,38 +37,72 @@ char * argv_list[CONFIG_SHELL_MAX_COMMAND_ARGS];
68
37
*/
69
38
char shellbuf [CONFIG_SHELL_MAX_INPUT ];
70
39
40
+ #ifdef ARDUINO
41
+ /**
42
+ * This is the buffer for formatted strings
43
+ */
44
+ char shellfmtbuf [CONFIG_SHELL_FMT_BUFFER ];
45
+ #endif
46
+
71
47
shell_reader_t shell_reader = 0 ;
72
48
shell_writer_t shell_writer = 0 ;
73
49
74
- enum _BOOL {
75
- FALSE = 0 ,
76
- TRUE = 1 ,
77
- };
78
50
79
- uint8_t initialized = FALSE;
51
+ bool initialized = false;
52
+
53
+ /**
54
+ * Parses the string and finds all the substrings (arguments)
55
+ *
56
+ * @param buf The buffer containing the original string
57
+ *
58
+ * @param argv Pointer to char * array to place the pointers to substrings
59
+ *
60
+ * @param maxargs The maximum number of pointers that the previous array can hold
61
+ *
62
+ * @return The total of args parsed
63
+ */
64
+ static int shell_parse (char * buf , char * * argv , unsigned short maxargs );
65
+
66
+ /**
67
+ * Prints the command shell prompt
68
+ */
69
+ static void shell_prompt ();
70
+
71
+ /**
72
+ * Helper function for formatting text in shell_printf and shell_printf_pm
73
+ *
74
+ * @param fmt
75
+ *
76
+ * @param va
77
+ */
78
+ static void shell_format (const char * fmt , va_list va );
79
+
80
80
81
- uint8_t shell_init (shell_reader_t reader , shell_writer_t writer , char * msg )
81
+ bool shell_init (shell_reader_t reader , shell_writer_t writer , char * msg )
82
82
{
83
83
if (reader == 0 || writer == 0 )
84
- return FALSE ;
84
+ return false ;
85
85
86
86
shell_unregister_all ();
87
87
88
88
shell_reader = reader ;
89
89
shell_writer = writer ;
90
- initialized = TRUE ;
90
+ initialized = true ;
91
91
92
92
// Print Message and draw command prompt
93
93
if (msg != 0 )
94
94
shell_println (msg );
95
95
else
96
- shell_println (defaultmotd );
96
+ #ifdef ARDUINO
97
+ shell_println_pm (PSTR ("uShell 1.0" ));
98
+ #else
99
+ shell_println ((const char * ) "uShell 1.0" );
100
+ #endif
97
101
shell_prompt ();
98
-
99
- return TRUE;
102
+ return true;
100
103
}
101
104
102
- uint8_t shell_register (shell_program_t program , const char * string )
105
+ bool shell_register (shell_program_t program , const char * string )
103
106
{
104
107
unsigned char i ;
105
108
@@ -108,9 +111,9 @@ uint8_t shell_register(shell_program_t program, const char * string)
108
111
continue ;
109
112
list [i ].shell_program = program ;
110
113
list [i ].shell_command_string = string ;
111
- return TRUE ;
114
+ return true ;
112
115
}
113
- return FALSE ;
116
+ return false ;
114
117
}
115
118
116
119
void shell_unregister_all ()
@@ -136,6 +139,40 @@ void shell_print_commands()
136
139
137
140
void shell_print_error (int error , const char * field )
138
141
{
142
+ #ifdef ARDUINO
143
+ if (field != 0 ) {
144
+ shell_print_pm (PSTR ("#ERROR-FIELD:" ));
145
+ shell_print (field );
146
+ shell_print_pm (PSTR ("\r\n" ));
147
+ }
148
+
149
+ shell_print_pm (PSTR ("#ERROR-TYPE:" ));
150
+ switch (error ) {
151
+ case E_SHELL_ERR_ARGCOUNT :
152
+ shell_println_pm (PSTR ("ARG_COUNT" ));
153
+ break ;
154
+ case E_SHELL_ERR_OUTOFRANGE :
155
+ shell_println_pm (PSTR ("OUT_OF_RANGE" ));
156
+ break ;
157
+ case E_SHELL_ERR_VALUE :
158
+ shell_println_pm (PSTR ("INVALID_VALUE" ));
159
+ break ;
160
+ case E_SHELL_ERR_ACTION :
161
+ shell_println_pm (PSTR ("INVALID_ACTION" ));
162
+ break ;
163
+ case E_SHELL_ERR_PARSE :
164
+ shell_println_pm (PSTR ("PARSING" ));
165
+ break ;
166
+ case E_SHELL_ERR_STORAGE :
167
+ shell_println_pm (PSTR ("STORAGE" ));
168
+ break ;
169
+ case E_SHELL_ERR_IO :
170
+ shell_println_pm (PSTR ("IO" ));
171
+ break ;
172
+ default :
173
+ shell_println_pm (PSTR ("Unknown" ));
174
+ }
175
+ #else
139
176
if (field != 0 ) {
140
177
shell_print ((const char * ) "#ERROR-FIELD:" );
141
178
shell_print (field );
@@ -157,7 +194,7 @@ void shell_print_error(int error, const char * field)
157
194
shell_print ((const char * ) "INVALID_ACTION" );
158
195
break ;
159
196
case E_SHELL_ERR_PARSE :
160
- shell_print ((const char * ) "PARSING" );
197
+ shell_print ((const char * ) "PARSING" );
161
198
break ;
162
199
case E_SHELL_ERR_STORAGE :
163
200
shell_print ((const char * ) "STORAGE" );
@@ -169,6 +206,7 @@ void shell_print_error(int error, const char * field)
169
206
shell_print ("Unknown" );
170
207
}
171
208
shell_print ("\r\n" );
209
+ #endif
172
210
}
173
211
174
212
void shell_print (const char * string )
@@ -177,12 +215,38 @@ void shell_print(const char * string)
177
215
shell_writer (* string ++ );
178
216
}
179
217
218
+ #ifdef ARDUINO
219
+ void shell_print_pm (const char * string )
220
+ {
221
+ uint8_t c ;
222
+ do {
223
+ c = pgm_read_byte (string ++ );
224
+ if (!c )
225
+ break ;
226
+ shell_writer (c );
227
+ } while (1 );
228
+ }
229
+ #endif
230
+
180
231
void shell_println (const char * string )
181
232
{
182
233
shell_print (string );
183
- shell_print ("\r\n" );
234
+ #ifdef ARDUINO
235
+ shell_print_pm (PSTR ("\r\n" ));
236
+ #else
237
+ shell_print ((const char * )"\r\n" );
238
+ #endif
184
239
}
185
240
241
+ #ifdef ARDUINO
242
+
243
+ void shell_println_pm (const char * string )
244
+ {
245
+ shell_print_pm (string );
246
+ shell_print_pm (PSTR ("\r\n" ));
247
+ }
248
+ #endif
249
+
186
250
void shell_printf (const char * fmt , ...)
187
251
{
188
252
va_list argl ;
@@ -191,6 +255,16 @@ void shell_printf(const char * fmt, ...)
191
255
va_end (argl );
192
256
}
193
257
258
+ void shell_printf_pm (const char * fmt , ...)
259
+ {
260
+ // First copy to RAM
261
+ memcpy_P (shellfmtbuf , fmt , strlen_P (fmt )+ 1 );
262
+ va_list argl ;
263
+ va_start (argl , shellfmtbuf );
264
+ shell_format (shellfmtbuf , argl );
265
+ va_end (argl );
266
+ }
267
+
194
268
void shell_task ()
195
269
{
196
270
unsigned int i = 0 , retval = 0 ;
@@ -223,7 +297,7 @@ void shell_task()
223
297
224
298
case SHELL_ASCII_CR : // Enter key pressed
225
299
shellbuf [count ] = '\0' ;
226
- shell_print (( const char * ) "\r\n " );
300
+ shell_println ( " " );
227
301
finished = 1 ;
228
302
break ;
229
303
@@ -259,18 +333,21 @@ void shell_task()
259
333
}
260
334
}
261
335
if (finished != 0 && count != 0 ) // If no command found and buffer not empty
262
- shell_print ((const char * ) "Command NOT found.\r\n" ); // Print not found!!
336
+ #ifdef ARDUINO
337
+ shell_println_pm (PSTR ("Command NOT found." ));
338
+ #else
339
+ shell_println ((const char * ) "Command NOT found." ); // Print not found!!
340
+ #endif
263
341
264
342
count = 0 ;
265
- shell_print (( const char * ) "\r\n " );
343
+ shell_println ( " " );
266
344
shell_prompt ();
267
345
}
268
346
}
269
347
}
270
348
271
349
/*-------------------------------------------------------------*/
272
350
/* Internal functions */
273
-
274
351
/*-------------------------------------------------------------*/
275
352
static int shell_parse (char * buf , char * * argv , unsigned short maxargs )
276
353
{
@@ -318,7 +395,11 @@ static int shell_parse(char * buf, char ** argv, unsigned short maxargs)
318
395
319
396
static void shell_prompt ()
320
397
{
321
- shell_print (prompt );
398
+ #ifdef ARDUINO
399
+ shell_print_pm (PSTR ("device>" ));
400
+ #else
401
+ shell_print ((const char * ) "device>" );
402
+ #endif
322
403
}
323
404
324
405
/*-------------------------------------------------------------*/
0 commit comments