1919 */
2020#include "Shell.h"
2121
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-
5322/**
5423 * This structure array contains the available commands and they associated
5524 * 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];
6837 */
6938char shellbuf [CONFIG_SHELL_MAX_INPUT ];
7039
40+ #ifdef ARDUINO
41+ /**
42+ * This is the buffer for formatted strings
43+ */
44+ char shellfmtbuf [CONFIG_SHELL_FMT_BUFFER ];
45+ #endif
46+
7147shell_reader_t shell_reader = 0 ;
7248shell_writer_t shell_writer = 0 ;
7349
74- enum _BOOL {
75- FALSE = 0 ,
76- TRUE = 1 ,
77- };
7850
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+
8080
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 )
8282{
8383 if (reader == 0 || writer == 0 )
84- return FALSE ;
84+ return false ;
8585
8686 shell_unregister_all ();
8787
8888 shell_reader = reader ;
8989 shell_writer = writer ;
90- initialized = TRUE ;
90+ initialized = true ;
9191
9292 // Print Message and draw command prompt
9393 if (msg != 0 )
9494 shell_println (msg );
9595 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
97101 shell_prompt ();
98-
99- return TRUE;
102+ return true;
100103}
101104
102- uint8_t shell_register (shell_program_t program , const char * string )
105+ bool shell_register (shell_program_t program , const char * string )
103106{
104107 unsigned char i ;
105108
@@ -108,9 +111,9 @@ uint8_t shell_register(shell_program_t program, const char * string)
108111 continue ;
109112 list [i ].shell_program = program ;
110113 list [i ].shell_command_string = string ;
111- return TRUE ;
114+ return true ;
112115 }
113- return FALSE ;
116+ return false ;
114117}
115118
116119void shell_unregister_all ()
@@ -136,6 +139,40 @@ void shell_print_commands()
136139
137140void shell_print_error (int error , const char * field )
138141{
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
139176 if (field != 0 ) {
140177 shell_print ((const char * ) "#ERROR-FIELD:" );
141178 shell_print (field );
@@ -157,7 +194,7 @@ void shell_print_error(int error, const char * field)
157194 shell_print ((const char * ) "INVALID_ACTION" );
158195 break ;
159196 case E_SHELL_ERR_PARSE :
160- shell_print ((const char * ) "PARSING" );
197+ shell_print ((const char * ) "PARSING" );
161198 break ;
162199 case E_SHELL_ERR_STORAGE :
163200 shell_print ((const char * ) "STORAGE" );
@@ -169,6 +206,7 @@ void shell_print_error(int error, const char * field)
169206 shell_print ("Unknown" );
170207 }
171208 shell_print ("\r\n" );
209+ #endif
172210}
173211
174212void shell_print (const char * string )
@@ -177,12 +215,38 @@ void shell_print(const char * string)
177215 shell_writer (* string ++ );
178216}
179217
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+
180231void shell_println (const char * string )
181232{
182233 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
184239}
185240
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+
186250void shell_printf (const char * fmt , ...)
187251{
188252 va_list argl ;
@@ -191,6 +255,16 @@ void shell_printf(const char * fmt, ...)
191255 va_end (argl );
192256}
193257
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+
194268void shell_task ()
195269{
196270 unsigned int i = 0 , retval = 0 ;
@@ -223,7 +297,7 @@ void shell_task()
223297
224298 case SHELL_ASCII_CR : // Enter key pressed
225299 shellbuf [count ] = '\0' ;
226- shell_print (( const char * ) "\r\n " );
300+ shell_println ( " " );
227301 finished = 1 ;
228302 break ;
229303
@@ -259,18 +333,21 @@ void shell_task()
259333 }
260334 }
261335 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
263341
264342 count = 0 ;
265- shell_print (( const char * ) "\r\n " );
343+ shell_println ( " " );
266344 shell_prompt ();
267345 }
268346 }
269347}
270348
271349/*-------------------------------------------------------------*/
272350/* Internal functions */
273-
274351/*-------------------------------------------------------------*/
275352static int shell_parse (char * buf , char * * argv , unsigned short maxargs )
276353{
@@ -318,7 +395,11 @@ static int shell_parse(char * buf, char ** argv, unsigned short maxargs)
318395
319396static void shell_prompt ()
320397{
321- shell_print (prompt );
398+ #ifdef ARDUINO
399+ shell_print_pm (PSTR ("device>" ));
400+ #else
401+ shell_print ((const char * ) "device>" );
402+ #endif
322403}
323404
324405/*-------------------------------------------------------------*/
0 commit comments