|
| 1 | +/* |
| 2 | +* Created on June 18 2020 |
| 3 | +* |
| 4 | +* Copyright (c) 2020 - Daniel Hajnal |
| 5 | + |
| 6 | +* This file is part of the Commander-API project. |
| 7 | +*/ |
| 8 | + |
| 9 | +/// This demo requires Shellminator library. |
| 10 | +/// You can find it here: https://github.com/dani007200964/Shellminator |
| 11 | + |
| 12 | +#include <stdio.h> |
| 13 | +#include <stdarg.h> |
| 14 | + |
| 15 | +/// Include Commander-API |
| 16 | +#include "interpreter.hpp" |
| 17 | + |
| 18 | +/// Include Shellminator library |
| 19 | +#include "Shellminator.hpp" |
| 20 | + |
| 21 | +/// Declaration of the execution function. |
| 22 | +/// This function will be called every time when |
| 23 | +/// a command arrives. |
| 24 | +void execution_function( char *cmd ); |
| 25 | + |
| 26 | +/// Create a Shellminator object, and initialise it to use Serial |
| 27 | +Shellminator shell( &Serial, execution_function ); |
| 28 | + |
| 29 | +/// Setup function |
| 30 | +void setup() { |
| 31 | + |
| 32 | + /// Initialise Serial with 115200 baudrate |
| 33 | + Serial.begin( 115200 ); |
| 34 | + |
| 35 | + /// Wait for connection |
| 36 | + while( !Serial ); |
| 37 | + |
| 38 | + /// Clear the terminal |
| 39 | + shell.clear(); |
| 40 | + |
| 41 | + /// Wait 2 seconds before initialising the program. |
| 42 | + /// It is optional, just simply give you some time to open |
| 43 | + /// a terminal application after sketch upload. |
| 44 | + delay( 2000 ); |
| 45 | + |
| 46 | + /// Print start message |
| 47 | + Serial.println( "Program start...\r\n" ); |
| 48 | + |
| 49 | + /// Initialise the interpreter. |
| 50 | + /// This function is better to called in your program |
| 51 | + /// as early as possible, because ot will make a binary |
| 52 | + /// tree of your commands, and this process uses some |
| 53 | + /// recursive functions. The recursive function calls |
| 54 | + /// can eat up some stack memory. After the initialisation |
| 55 | + /// this memory will be freed up. |
| 56 | + init_interpreter(); |
| 57 | + |
| 58 | + /// Initialise shell object. |
| 59 | + shell.begin( "arnold" ); |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +/// Infinite loop |
| 64 | +void loop() { |
| 65 | + |
| 66 | + /// Feed Shellminator's update function. |
| 67 | + shell.update(); |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +/// You need a wrapper function to connect |
| 72 | +/// Shellminator to Commander API. |
| 73 | +/// This wrapper function is very simple. |
| 74 | +/// Shellminator will call execution_function |
| 75 | +/// every time when a command arrives to Serial. |
| 76 | +/// This function will pass that command to |
| 77 | +/// Commander API and configures Commander to |
| 78 | +/// genberate the response back to Serial. |
| 79 | +void execution_function( char *cmd ){ |
| 80 | + |
| 81 | + execute( cmd, arduino_printf ); |
| 82 | + |
| 83 | +} |
0 commit comments