Skip to content

Commit 8e0222d

Browse files
geekfactorygeekfactory
authored andcommitted
Added arduino example and improved documentation
1 parent 0bb4b67 commit 8e0222d

File tree

5 files changed

+568
-116
lines changed

5 files changed

+568
-116
lines changed

Shell.h

Lines changed: 132 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@
2929
/*-------------------------------------------------------------*/
3030
/* Macros and definitions */
3131
/*-------------------------------------------------------------*/
32+
#if !defined(CONFIG_SHELL_MAX_COMMANDS)
33+
/**
34+
* Defines the maximum number of commands that can be registered
35+
*/
36+
#define CONFIG_SHELL_MAX_COMMANDS 5
37+
#endif
38+
#if !defined(CONFIG_SHELL_MAX_INPUT)
39+
/**
40+
* Defines the maximum characters that the input buffer can accept
41+
*/
42+
#define CONFIG_SHELL_MAX_INPUT 100
43+
#endif
44+
#if !defined(CONFIG_SHELL_MAX_COMMAND_ARGS)
45+
/**
46+
* Configures the maximum number of arguments per command tha can be accepted
47+
*/
48+
#define CONFIG_SHELL_MAX_COMMAND_ARGS 10
49+
#endif
3250

3351
#define SHELL_ASCII_NUL 0x00
3452
#define SHELL_ASCII_BEL 0x07
@@ -49,25 +67,6 @@
4967
#define SHELL_RET_FAILURE 1
5068
#define SHELL_RET_IOPENDING -1
5169

52-
#if !defined(CONFIG_SHELL_MAX_COMMANDS)
53-
/**
54-
* Defines the maximum number of commands that can be registered
55-
*/
56-
#define CONFIG_SHELL_MAX_COMMANDS 5
57-
#endif
58-
#if !defined(CONFIG_SHELL_MAX_INPUT)
59-
/**
60-
* Defines the maximum characters that the input buffer can accept
61-
*/
62-
#define CONFIG_SHELL_MAX_INPUT 100
63-
#endif
64-
#if !defined(CONFIG_SHELL_MAX_COMMAND_ARGS)
65-
/**
66-
* Configures the maximum number of arguments per command tha can be accepted
67-
*/
68-
#define CONFIG_SHELL_MAX_COMMAND_ARGS 10
69-
#endif
70-
7170
/*-------------------------------------------------------------*/
7271
/* Typedefs enums & structs */
7372
/*-------------------------------------------------------------*/
@@ -94,13 +93,13 @@ typedef int (*shell_reader_t) (char *);
9493
* shell
9594
*/
9695
enum shell_errors {
97-
E_SHELL_ERR_ARGCOUNT = 0,
98-
E_SHELL_ERR_OUTOFRANGE,
99-
E_SHELL_ERR_VALUE,
100-
E_SHELL_ERR_ACTION,
101-
E_SHELL_ERR_PARSE,
102-
E_SHELL_ERR_STORAGE,
103-
E_SHELL_ERR_IO,
96+
E_SHELL_ERR_ARGCOUNT = 0, //!< There are missing arguments for the command
97+
E_SHELL_ERR_OUTOFRANGE, //!< The program received an argument that is out of range
98+
E_SHELL_ERR_VALUE, //!< The program received an argument with a value different than expected
99+
E_SHELL_ERR_ACTION, //!< Invalid action requested for the current state
100+
E_SHELL_ERR_PARSE, //!< Cannot parse the user input
101+
E_SHELL_ERR_STORAGE, //!< Cannot access storage device or memory device
102+
E_SHELL_ERR_IO, //!< IO device error caused program interruption
104103
};
105104

106105
/**
@@ -118,97 +117,114 @@ struct shell_command_entry {
118117
#ifdef __cplusplus
119118
extern "C" {
120119
#endif
120+
/**
121+
* @brief Prepares the command line interface
122+
*
123+
* Initializes the resources used by the command line interface. This function
124+
* also tries to start the command line task if the option is enabled. The main
125+
* program for the command line interface where all the data is handled is
126+
* shell_task().
127+
*
128+
* @param reader The callback function used to get a character from the stream.
129+
*
130+
* @param writer The callback function used to write a character to the stream.
131+
*
132+
* @param msg The message to display upon startup of the program
133+
*
134+
* @return Returns TRUE if the shell was succesfully initialized, returns FALSE
135+
* otherwise.
136+
*/
137+
uint8_t shell_init(shell_reader_t reader, shell_writer_t writer, char * msg);
121138

122-
/**
123-
* @brief Prepares the command line interface
124-
*
125-
* Initializes the resources used by the command line interface. This function
126-
* also tries to start the command line task if the option is enabled. The main
127-
* program for the command line interface where all the data is handled is
128-
* shell_task().
129-
*
130-
* @param reader The callback function used to get a character from the stream.
131-
*
132-
* @param writer The callback function used to write a character to the stream.
133-
*
134-
* @param msg The message to display upon startup of the program
135-
*
136-
* @return Returns TRUE if the shell was succesfully initialized, returns FALSE
137-
* otherwise.
138-
*/
139-
uint8_t shell_init(shell_reader_t reader, shell_writer_t writer, char * msg);
140-
141-
/**
142-
* @brief Register a command with the command interpreter
143-
*
144-
* Registers a command on the available command list. The name of the command
145-
* and the function that implements it´s functionality should be provided.
146-
*
147-
* @param program The type shell_program_t is a pointer to a function
148-
* that is executed when the written command matches the associated name
149-
*
150-
* @param string A string containing the name of the command to be registered.
151-
*
152-
* @return Returns TRUE if command was successfully added to the command list,
153-
* or returns FALSE if something fails (no more commands can be registered).
154-
*/
155-
uint8_t shell_register(shell_program_t program, const char * string);
156-
157-
/**
158-
* @brief Unregister all commands
159-
*
160-
* Errases all entries on the command list, returning it to it´s default status.
161-
*/
162-
void shell_unregister_all();
163-
164-
/**
165-
* @brief Prints the list of registered commands
166-
*
167-
* Prints a list of available commands to the terminal using the callback
168-
* functions provided on initialization.
169-
*/
170-
void shell_print_commands();
171-
172-
/**
173-
* @brief Prints error messages to the terminal screen
174-
*
175-
* @param error The code (ID) of the error to print
176-
*
177-
* @param field The name of the parameter or variable where the error was
178-
* detected.
179-
*/
180-
void shell_print_error(int error, const char * field);
181-
182-
/**
183-
* @brief Prints a null terminated string to the terminal
184-
*
185-
* Displays a string on the terminal. The string should be null terminated.
186-
*
187-
* @param string The string to send to the terminal
188-
*/
189-
void shell_print(const char * string);
190-
191-
/**
192-
* @brief Prints a string and moves the cursor to the next line
193-
*
194-
* @param string The string to send to the terminal
195-
*/
196-
void shell_println(const char * string);
197-
198-
/**
199-
*
200-
*/
201-
void shell_printf(char * fmt, ...);
202-
203-
/**
204-
*
205-
* @brief Main Shell processing loop
206-
*
207-
* This function implements the main functionality of the command line interface
208-
* this function should be called frequently so it can handle the input from the
209-
* data stream.
210-
*/
211-
void shell_task();
139+
/**
140+
* @brief Register a command with the command interpreter
141+
*
142+
* Registers a command on the available command list. The name of the command
143+
* and the function that implements it´s functionality should be provided.
144+
*
145+
* @param program The type shell_program_t is a pointer to a function
146+
* that is executed when the written command matches the associated name
147+
*
148+
* @param string A string containing the name of the command to be registered.
149+
*
150+
* @return Returns TRUE if command was successfully added to the command list,
151+
* or returns FALSE if something fails (no more commands can be registered).
152+
*/
153+
uint8_t shell_register(shell_program_t program, const char * string);
154+
155+
/**
156+
* @brief Unregister all commands
157+
*
158+
* Errases all entries on the command list, returning it to it´s default status.
159+
*/
160+
void shell_unregister_all();
161+
162+
/**
163+
* @brief Prints the list of registered commands
164+
*
165+
* Prints a list of available commands to the terminal using the callback
166+
* functions provided on initialization.
167+
*/
168+
void shell_print_commands();
169+
170+
/**
171+
* @brief Prints error messages to the terminal screen
172+
*
173+
* This function presents an alternative for displaying program errors in a
174+
* uniform format.
175+
*
176+
* @param error The code (ID) of the error to print
177+
*
178+
* @param field The name of the parameter or variable where the error was
179+
* detected.
180+
*/
181+
void shell_print_error(int error, const char * field);
182+
183+
/**
184+
* @brief Prints a null terminated string to the terminal
185+
*
186+
* Displays a string on the terminal. The string should be null terminated.
187+
*
188+
* @param string The string to send to the terminal
189+
*/
190+
void shell_print(const char * string);
191+
192+
/**
193+
* @brief Prints a string and moves the cursor to the next line
194+
*
195+
* Displays a string on the terminal and moves the cursor to the next line. The
196+
* string should be null terminated.
197+
*
198+
* @param string The string to send to the terminal
199+
*/
200+
void shell_println(const char * string);
201+
202+
/**
203+
* @brief Prints formatted text to the terminal
204+
*
205+
* Displays a string (fmt) on the terminal. If the string includes format
206+
* specifiers (subsequences beginning with '%'), the additional arguments
207+
* following format are formatted and inserted in the resulting string
208+
* replacing their respective specifiers.
209+
*
210+
* This function implements it´s own mechanism for text formatting. It doesn´t
211+
* rely on the native print functions.
212+
*
213+
* @param fmt The string to send to the terminal, the string can include format
214+
* specifiers in a similar fashion to printf standard function.
215+
*
216+
* @param ... Aditional arguments that are inserted on the string as text
217+
*/
218+
void shell_printf(char * fmt, ...);
219+
220+
/**
221+
* @brief Main Shell processing loop
222+
*
223+
* This function implements the main functionality of the command line interface
224+
* this function should be called frequently so it can handle the input from the
225+
* data stream.
226+
*/
227+
void shell_task();
212228

213229
#ifdef __cplusplus
214230
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* SIMPLE EXAMPLE OF COMMAND SHELL LIBRARY
3+
*
4+
* This library implements a command line interface. The experience is similar to the windows
5+
* command prompt. This library can invoke functions when a specific string is received, in this
6+
* example we register 2 commands that can be executed by typing their names on the arduino serial
7+
* monitor (using the hardware UART).
8+
*
9+
* The "shell" library is designed to be independent of the communication channel, this means
10+
* that it can be used with serial, softSerial and even TCP connections. For this reason the
11+
* shell library requires 2 pointers to functions to read and write characters to and from the
12+
* physical transmission media.
13+
*
14+
* Arguments can be passed to the programs that are invoked by the command line and parsed by
15+
* standard string parsing functions, or you can write your own functions. The arguments
16+
* are passed as pointers to characters (null terminated strings) in a similar way that you
17+
* would receive them on any command line tool written in C. Each parameter is separated by
18+
* a "space" character after the command name.
19+
*/
20+
21+
#include <Shell.h>
22+
23+
void setup()
24+
{
25+
// Prepare serial communication
26+
Serial.begin(9600);
27+
// Wait after reset or power on...
28+
delay(1000);
29+
30+
// Initialize command line interface (CLI)
31+
// We pass the function pointers to the read and write functions that we implement below
32+
// We can also pass a char pointer to display a custom start message
33+
shell_init(shell_reader, shell_writer, 0);
34+
35+
// Add commands to the shell
36+
shell_register(command_mycommand, "mycommand");
37+
shell_register(command_othercommand, "othercommand");
38+
}
39+
40+
void loop()
41+
{
42+
// This should always be called to process user input
43+
shell_task();
44+
}
45+
46+
/**
47+
* Test commands: The commands should always use this function prototype.
48+
* They receive 2 parameters: The total count of arguments (argc) and a pointer
49+
* to the begining of each one of the null-terminated argument strings.
50+
*
51+
+ In this example we ignore the parameters passed to the functions
52+
*/
53+
int command_mycommand(int argc, char** argv)
54+
{
55+
shell_println("Running \"mycommand\" now");
56+
shell_println("Exit...");
57+
return SHELL_RET_SUCCESS;
58+
}
59+
60+
int command_othercommand(int argc, char** argv)
61+
{
62+
shell_println("Running \"othercommand\" now");
63+
shell_println("Exit...");
64+
return SHELL_RET_SUCCESS;
65+
}
66+
67+
/**
68+
* Function to read data from serial port
69+
* Functions to read from physical media should use this prototype:
70+
* int my_reader_function(char * data)
71+
*/
72+
int shell_reader(char * data)
73+
{
74+
// Wrapper for Serial.read() method
75+
if (Serial.available()) {
76+
*data = Serial.read();
77+
return 1;
78+
}
79+
return 0;
80+
}
81+
82+
/**
83+
* Function to write data to serial port
84+
* Functions to write to physical media should use this prototype
85+
* void my_writer_function(char data)
86+
*/
87+
void shell_writer(char data)
88+
{
89+
// Wrapper for Serial.write() method
90+
Serial.write(data);
91+
}

0 commit comments

Comments
 (0)