|
| 1 | + |
| 2 | +/* |
| 3 | + * Created on 2023.Jun.25 |
| 4 | + * |
| 5 | + * Copyright (c) 2023 - Daniel Hajnal |
| 6 | + |
| 7 | + * This file is part of the Commander-API project. |
| 8 | + * Modified 2023.Aug.07 |
| 9 | + * |
| 10 | + * This is a simple example, that demonstrates how |
| 11 | + * to use the base functionality of th Commander-API. |
| 12 | +*/ |
| 13 | + |
| 14 | + |
| 15 | +// Necessary includes |
| 16 | +#include "Commander-API.hpp" |
| 17 | +#include "Commander-IO.hpp" |
| 18 | +#include "Commander-API-Commands.hpp" |
| 19 | +#include <math.h> |
| 20 | + |
| 21 | + |
| 22 | +// We have to create an object from Commander class. |
| 23 | +Commander commander; |
| 24 | + |
| 25 | +// Add echo and env commands to the API tree. |
| 26 | +Commander::API_t API_tree[] = { |
| 27 | + API_ELEMENT_ECHO, |
| 28 | + API_ELEMENT_ENV |
| 29 | +}; |
| 30 | + |
| 31 | +// Global system variables. |
| 32 | +float constPI = M_PI; |
| 33 | +int adcValue = 532; |
| 34 | +char boardID[] = "This is a very special board."; |
| 35 | +char swVersion[] = "V0.0.1"; |
| 36 | + |
| 37 | +// System Variable array. This array will store the |
| 38 | +// name and the instance of the system variables. |
| 39 | +Commander::SystemVariable_t systemVariables[] = { |
| 40 | + systemVariableFloat( constPI ), |
| 41 | + systemVariableInt( adcValue ), |
| 42 | + systemVariableString( boardID ), |
| 43 | + systemVariableString( swVersion ) |
| 44 | +}; |
| 45 | + |
| 46 | +// This is a buffer to hold the incoming command. |
| 47 | +char commandFromSerial[ 30 ]; |
| 48 | + |
| 49 | +// This variable tracks the location of the next free |
| 50 | +// space in the commandFromSerial buffer. |
| 51 | +uint8_t commandIndex = 0; |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +// System init section. |
| 57 | +void setup(){ |
| 58 | + |
| 59 | + Serial.begin(115200); |
| 60 | + |
| 61 | + // There is an option to attach a debug channel to Commander. |
| 62 | + // It can be handy to find any problems during the initialization |
| 63 | + // phase. In this example, we will use Serial for this. |
| 64 | + commander.attachDebugChannel( &Serial ); |
| 65 | + |
| 66 | + // At start, Commander does not know anything about our commands. |
| 67 | + // We have to attach the API_tree array from the previous steps |
| 68 | + // to Commander to work properly. |
| 69 | + commander.attachTree( API_tree ); |
| 70 | + |
| 71 | + // Attach the system variable array to the command parser. |
| 72 | + Commander::attachVariables( systemVariables ); |
| 73 | + |
| 74 | + // After we attached the API_tree, Commander has to initialize |
| 75 | + // itself for the fastest runtime possible. It creates a balanced |
| 76 | + // binary tree from the API_tree to boost the search speed. |
| 77 | + // This part uses some recursion, to make the code space small. |
| 78 | + // But recursion is a bit stack hungry, so please initialize |
| 79 | + // Commander at the beginning of your code to prevent stack-overlow. |
| 80 | + commander.init(); |
| 81 | + |
| 82 | + Serial.println(); |
| 83 | + Serial.println( "---- Init Finished ----" ); |
| 84 | + Serial.println(); |
| 85 | + |
| 86 | + Serial.println( "Type something" ); |
| 87 | + Serial.print( "$: " ); |
| 88 | + |
| 89 | + |
| 90 | +} |
| 91 | + |
| 92 | +// Infinite loop. |
| 93 | +void loop(){ |
| 94 | + |
| 95 | + // Check if there is any data incoming. |
| 96 | + while( Serial.available() ){ |
| 97 | + |
| 98 | + // Read the next incoming character. |
| 99 | + char c = Serial.read(); |
| 100 | + |
| 101 | + // Every command from Serial is terminated with a new-line |
| 102 | + // character. If a new-line character arrives, we have to |
| 103 | + // terminate the string in the commandFromSerial buffer, |
| 104 | + // and execute it. After execution, we have to reset the |
| 105 | + // commandIndex counter to zero. |
| 106 | + if( c == '\r' ){ |
| 107 | + commandFromSerial[ commandIndex ] = '\0'; |
| 108 | + Serial.println(); |
| 109 | + commander.execute( commandFromSerial, &Serial ); |
| 110 | + commandIndex = 0; |
| 111 | + Serial.print( "$: " ); |
| 112 | + } |
| 113 | + |
| 114 | + // If we have a carriage-return character we simply |
| 115 | + // ignore it. |
| 116 | + else if( c == '\n' ){ |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + // Handle backspace events. |
| 121 | + else if( ( c == '\b' ) || ( c == 127 ) ){ |
| 122 | + if( commandIndex > 0 ){ |
| 123 | + commandIndex--; |
| 124 | + Serial.print( "\b \b" ); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Every other case we just put the data to the next |
| 129 | + // free space in the commandFromSerial buffer, increment |
| 130 | + // the commandIndex, and check if it wants to overflow. |
| 131 | + else{ |
| 132 | + commandFromSerial[ commandIndex ] = c; |
| 133 | + commandIndex++; |
| 134 | + if( commandIndex >= sizeof( commandFromSerial ) ){ |
| 135 | + commandIndex = sizeof( commandFromSerial ) - 1; |
| 136 | + } |
| 137 | + else{ |
| 138 | + Serial.print( c ); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | +} |
0 commit comments