|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Custom commands |
| 4 | +nav_order: 6 |
| 5 | +permalink: /commander_custom |
| 6 | +parent: Commander Interface |
| 7 | +grand_parent: Communication |
| 8 | +grand_grand_parent: Writing the Code |
| 9 | +grand_grand_grand_parent: Arduino <span class="simple">Simple<span class="foc">FOC</span>library</span> |
| 10 | +--- |
| 11 | + |
| 12 | +# Extending commander with custom functionality |
| 13 | + |
| 14 | +To extend the commander interface with your own functionality that has not been implemented by the <span class="simple">Simple<span class="foc">FOC</span>library</span> you only really need to: |
| 15 | +1. implement your callback function `void myFunc(char*){}` |
| 16 | +2. add it to the commander `commander.add('.',myFunc,"..")` |
| 17 | + |
| 18 | +```cpp |
| 19 | +void myFunc(char*){ |
| 20 | + // do something useful |
| 21 | +} |
| 22 | + |
| 23 | +Commander commander = Commander(...) |
| 24 | +void setup(){ |
| 25 | +... |
| 26 | + commander.add('A',myFunc,"my functionality"); |
| 27 | +... |
| 28 | +} |
| 29 | +void loop(){ |
| 30 | + ... |
| 31 | + commander.run() |
| 32 | +} |
| 33 | +``` |
| 34 | +
|
| 35 | +## Example |
| 36 | +
|
| 37 | +This is an example code of extending the commander interface with two new functionalities, turning on and off a led light and reading 5 analog pins. |
| 38 | +```cpp |
| 39 | +
|
| 40 | +#include <SimpleFOC.h> |
| 41 | +
|
| 42 | +// instantiate the commander |
| 43 | +Commander command = Commander(Serial); |
| 44 | +
|
| 45 | +// led control function |
| 46 | +void doLed(char* cmd){ |
| 47 | + if(atoi(cmd)) digitalWrite(LED_BUILTIN, HIGH); |
| 48 | + else digitalWrite(LED_BUILTIN, LOW); |
| 49 | +}; |
| 50 | +// get analog input |
| 51 | +void doAnalog(char* cmd){ |
| 52 | + if (cmd[0] == '0') Serial.println(analogRead(A0)); |
| 53 | + else if (cmd[0] == '1') Serial.println(analogRead(A1)); |
| 54 | + else if (cmd[0] == '2') Serial.println(analogRead(A2)); |
| 55 | + else if (cmd[0] == '3') Serial.println(analogRead(A3)); |
| 56 | + else if (cmd[0] == '4') Serial.println(analogRead(A4)); |
| 57 | +}; |
| 58 | +
|
| 59 | +void setup() { |
| 60 | + // define pins |
| 61 | + pinMode(LED_BUILTIN, OUTPUT); |
| 62 | + pinMode(A0, INPUT); |
| 63 | + pinMode(A1, INPUT); |
| 64 | + pinMode(A2, INPUT); |
| 65 | + pinMode(A3, INPUT); |
| 66 | + pinMode(A4, INPUT); |
| 67 | +
|
| 68 | + // Serial port to be used |
| 69 | + Serial.begin(115200); |
| 70 | +
|
| 71 | + // add new commands |
| 72 | + command.add('L', doLed, "led on/off"); |
| 73 | + command.add('A', doAnalog, "analog read A0-A4"); |
| 74 | +
|
| 75 | + Serial.println(F("Commander listening")); |
| 76 | + Serial.println(F(" - Send ? to see the node list...")); |
| 77 | + Serial.println(F(" - Send L0 to turn the led off and L1 to turn it off")); |
| 78 | + Serial.println(F(" - Send A0-A4 to read the analog pins")); |
| 79 | + _delay(1000); |
| 80 | +} |
| 81 | +
|
| 82 | +
|
| 83 | +void loop() { |
| 84 | +
|
| 85 | + // user communication |
| 86 | + command.run(); |
| 87 | + _delay(10); |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +Then in your serial terminal you can just |
| 92 | +```sh |
| 93 | +$ ? # list the commands |
| 94 | +L: led on/off |
| 95 | +A: analog read A0-A4 |
| 96 | +$ L0 # led off |
| 97 | +$ A1 # read A1 |
| 98 | +321 |
| 99 | +$ A3 # read A3 |
| 100 | +1023 |
| 101 | +$ L1 # led on |
| 102 | +$ L0 # led off |
| 103 | +``` |
0 commit comments