|
1 |
| -##Librería de linea de comandos para Arduino y otros microcontroladores. |
| 1 | +# GeekFactory Shell Library # |
| 2 | + |
| 3 | +This library allows us to **control our devices with simple text commands**. The experience is similar to the terminal in Mac OS or linux and the Windows command line. The commands can be transmitted to the microcontroller using a serial port, a TCP/IP socket or a USB (CDC) connection. |
| 4 | +**This library is written in pure C in order to allow it to work in other microcontrollers/compilers that do not support C++**. |
| 5 | + |
| 6 | +## Basic library usage ## |
| 7 | + |
| 8 | +In order to use the library we need to call the initialization function that will tell the library the functions that it shall use to read / write the communication media (serial, TCP/IP, etc.). The following code will prepare the library and instruct it to use shell_reader() function to read bytes from the serial port and shell_writter() function to write bytes on the serial port. |
| 9 | + |
| 10 | +```c |
| 11 | +// Initialize command line interface (CLI) |
| 12 | +// We pass the function pointers to the read and write functions that we implement below |
| 13 | +// We can also pass a char pointer to display a custom start message |
| 14 | +shell_init(shell_reader, shell_writer, 0); |
| 15 | +``` |
| 16 | +
|
| 17 | +The next step is to register the commands. This will associate the functions that will run when the command name (string) is found in the user input. |
| 18 | +
|
| 19 | +```c |
| 20 | + // Add commands to the shell |
| 21 | +shell_register(command_mycommand, "mycommand"); |
| 22 | +shell_register(command_othercommand, "othercommand"); |
| 23 | +``` |
| 24 | + |
| 25 | +Finally, inside the main loop of the application, the programmer should call the **shell_task()** function. Inside this function the library takes care of the character reception, string matching and also this function will call the appropriate command when needed. |
| 26 | + |
| 27 | +```c |
| 28 | + // This should always be called to process user input |
| 29 | +shell_task(); |
| 30 | +``` |
| 31 | + |
| 32 | +The programs (functions) that implement the functionality of each command should be written using the standard C prototype and receive their parameters in the exact same way it happens on a desktop PC when we write a command line tool. |
| 33 | + |
| 34 | +```c |
| 35 | +int command_func(int argc, char ** argv); |
| 36 | +``` |
| 37 | +
|
| 38 | +## Basic library example using arduino |
| 39 | +
|
| 40 | +```cpp |
| 41 | +/** |
| 42 | + * GeekFactory - "Construye tu propia tecnologia" |
| 43 | + * Distribucion de materiales para el desarrollo e innovacion tecnologica |
| 44 | + * www.geekfactory.mx |
| 45 | + * |
| 46 | + * Ejemplo de libreria Shell. Este ejemplo representa la configuracion minima |
| 47 | + * requerida para implementar la funcionalidad de interfaz de comandos. En esta |
| 48 | + * ocasion registramos 2 comandos y enviamos texto como respuesta a cada uno. |
| 49 | + * |
| 50 | + * Example for Shell library. This example shows the minimum setup needed to |
| 51 | + * implement a command line interface. This time we register 2 commands and we |
| 52 | + * send text as response for each command. |
| 53 | + */ |
| 54 | +
|
| 55 | +#include <Shell.h> |
| 56 | +
|
| 57 | +void setup() |
| 58 | +{ |
| 59 | + // Prepare serial communication |
| 60 | + Serial.begin(9600); |
| 61 | + // Wait after reset or power on... |
| 62 | + delay(1000); |
| 63 | +
|
| 64 | + // Initialize command line interface (CLI) |
| 65 | + // We pass the function pointers to the read and write functions that we implement below |
| 66 | + // We can also pass a char pointer to display a custom start message |
| 67 | + shell_init(shell_reader, shell_writer, 0); |
| 68 | +
|
| 69 | + // Add commands to the shell |
| 70 | + shell_register(command_mycommand, "mycommand"); |
| 71 | + shell_register(command_othercommand, "othercommand"); |
| 72 | +} |
| 73 | +
|
| 74 | +void loop() |
| 75 | +{ |
| 76 | + // This should always be called to process user input |
| 77 | + shell_task(); |
| 78 | +} |
| 79 | +
|
| 80 | +/** |
| 81 | + * Test commands: The commands should always use this function prototype. |
| 82 | + * They receive 2 parameters: The total count of arguments (argc) and a pointer |
| 83 | + * to the begining of each one of the null-terminated argument strings. |
| 84 | + * |
| 85 | + * In this example we ignore the parameters passed to the functions |
| 86 | + */ |
| 87 | +int command_mycommand(int argc, char** argv) |
| 88 | +{ |
| 89 | + shell_println("Running \"mycommand\" now"); |
| 90 | + shell_println("Exit..."); |
| 91 | + return SHELL_RET_SUCCESS; |
| 92 | +} |
| 93 | +
|
| 94 | +int command_othercommand(int argc, char** argv) |
| 95 | +{ |
| 96 | + shell_println("Running \"othercommand\" now"); |
| 97 | + shell_println("Exit..."); |
| 98 | + return SHELL_RET_SUCCESS; |
| 99 | +} |
| 100 | +
|
| 101 | +/** |
| 102 | + * Function to read data from serial port |
| 103 | + * Functions to read from physical media should use this prototype: |
| 104 | + * int my_reader_function(char * data) |
| 105 | + */ |
| 106 | +int shell_reader(char * data) |
| 107 | +{ |
| 108 | + // Wrapper for Serial.read() method |
| 109 | + if (Serial.available()) { |
| 110 | + *data = Serial.read(); |
| 111 | + return 1; |
| 112 | + } |
| 113 | + return 0; |
| 114 | +} |
| 115 | +
|
| 116 | +/** |
| 117 | + * Function to write data to serial port |
| 118 | + * Functions to write to physical media should use this prototype: |
| 119 | + * void my_writer_function(char data) |
| 120 | + */ |
| 121 | +void shell_writer(char data) |
| 122 | +{ |
| 123 | + // Wrapper for Serial.write() method |
| 124 | + Serial.write(data); |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +## Project Objectives ## |
| 129 | + |
| 130 | +Our library should fulfill the following goals: |
| 131 | + |
| 132 | +* Code should be portable to other platforms |
| 133 | +* Library should be compact |
| 134 | +* It should be easy to add new commands |
| 135 | + |
| 136 | + |
| 137 | +## Supported devices ## |
| 138 | + |
| 139 | +This library was developed/tested on the following boards: |
| 140 | + |
| 141 | +* Arduino UNO R3 |
| 142 | +* Arduino Mega 2560 R3 |
| 143 | +* Basic testing on ESP8266 boards |
| 144 | + |
| 145 | +The library was also tested / compiled on the following PIC microcontrollers using MPLAB X: |
| 146 | + |
| 147 | +* PIC24FJ64GA002 |
| 148 | +* PIC24HJ128GA504 |
| 149 | + |
| 150 | +The library is meant to work in other CPU architectures where a C compiler is available, please tell us about your experience if you try it in other platforms. |
| 151 | + |
| 152 | +## Contact me ## |
| 153 | + |
| 154 | +* Feel free to write for any inquiry: ruben at geekfactory.mx |
| 155 | +* Check our website: https://www.geekfactory.mx |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +#Librería de linea de comandos para Arduino y otros microcontroladores.# |
2 | 160 |
|
3 | 161 | Esta librería permite **controlar nuestros programas y dispositivos con simples comandos de texto** de manera similar a la terminal en Mac OSX o linux y la linea de comandos de Windows. Los comandos pueden transmitirse al microcontrolador mediante un puerto serie, un socket TCP/IP o una conexión USB CDC.
|
4 | 162 |
|
|
0 commit comments