Fernando Cosentino
/
uTerminal_Example
Test program to demonstrate usage of uTerminal library
main.cpp@1:a2c302339cda, 2019-02-18 (annotated)
- Committer:
- fbcosentino
- Date:
- Mon Feb 18 18:24:49 2019 +0000
- Revision:
- 1:a2c302339cda
- Parent:
- 0:e9d152d29a2e
Examples for new USB feature
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fbcosentino | 0:e9d152d29a2e | 1 | /** |
fbcosentino | 1:a2c302339cda | 2 | * @file main.cpp |
fbcosentino | 0:e9d152d29a2e | 3 | * uTerminal_Example is a simple example demonstrating how to |
fbcosentino | 0:e9d152d29a2e | 4 | * use the uTerminal library. |
fbcosentino | 0:e9d152d29a2e | 5 | * |
fbcosentino | 0:e9d152d29a2e | 6 | * @author Fernando Cosentino |
fbcosentino | 0:e9d152d29a2e | 7 | */ |
fbcosentino | 0:e9d152d29a2e | 8 | |
fbcosentino | 0:e9d152d29a2e | 9 | #include "mbed.h" |
fbcosentino | 0:e9d152d29a2e | 10 | #include "uTerminal.h" |
fbcosentino | 0:e9d152d29a2e | 11 | |
fbcosentino | 1:a2c302339cda | 12 | //================================================================ |
fbcosentino | 1:a2c302339cda | 13 | // INSTANCING THE TERMINAL: |
fbcosentino | 1:a2c302339cda | 14 | |
fbcosentino | 1:a2c302339cda | 15 | /* |
fbcosentino | 1:a2c302339cda | 16 | // Create a Serial object yourself |
fbcosentino | 1:a2c302339cda | 17 | Serial pc(P0_19, P0_18); // TX, RX pins for board n-DAP |
fbcosentino | 1:a2c302339cda | 18 | uTerminal terminal(&pc); // argument is the address of a Serial object |
fbcosentino | 1:a2c302339cda | 19 | */ |
fbcosentino | 1:a2c302339cda | 20 | |
fbcosentino | 1:a2c302339cda | 21 | // or... |
fbcosentino | 1:a2c302339cda | 22 | // This will automatically create an internal Serial object |
fbcosentino | 1:a2c302339cda | 23 | //uTerminal terminal(P0_19, P0_18); // TX, RX pins for board n-DAP |
fbcosentino | 1:a2c302339cda | 24 | |
fbcosentino | 1:a2c302339cda | 25 | // or... |
fbcosentino | 1:a2c302339cda | 26 | // Create a USBSerial object yourself (works with blocking and non-blocking) |
fbcosentino | 1:a2c302339cda | 27 | //USBSerial pc; // blocking |
fbcosentino | 1:a2c302339cda | 28 | USBSerial pc(0x1f00, 0x2012, 0x0001, false); // non-blocking |
fbcosentino | 1:a2c302339cda | 29 | uTerminal terminal(&pc); // argument is the address of a USBSerial object |
fbcosentino | 1:a2c302339cda | 30 | |
fbcosentino | 1:a2c302339cda | 31 | //================================================================ |
fbcosentino | 1:a2c302339cda | 32 | |
fbcosentino | 0:e9d152d29a2e | 33 | |
fbcosentino | 0:e9d152d29a2e | 34 | char sbuf[1024]; // useb by sprintf |
fbcosentino | 0:e9d152d29a2e | 35 | |
fbcosentino | 0:e9d152d29a2e | 36 | /** |
fbcosentino | 0:e9d152d29a2e | 37 | * Callback called to process a message received by uTerminal |
fbcosentino | 0:e9d152d29a2e | 38 | * If Auto-mode is used, this is called whenever a message arrives. |
fbcosentino | 0:e9d152d29a2e | 39 | * Otherwise, called whenever termina.Process() is polled. |
fbcosentino | 0:e9d152d29a2e | 40 | */ |
fbcosentino | 1:a2c302339cda | 41 | |
fbcosentino | 0:e9d152d29a2e | 42 | void msg_received() { |
fbcosentino | 0:e9d152d29a2e | 43 | int i; |
fbcosentino | 0:e9d152d29a2e | 44 | |
fbcosentino | 0:e9d152d29a2e | 45 | terminal.print("Received:\n"); |
fbcosentino | 0:e9d152d29a2e | 46 | terminal.print(terminal.Command); |
fbcosentino | 0:e9d152d29a2e | 47 | terminal.print(" = "); |
fbcosentino | 0:e9d152d29a2e | 48 | terminal.print(terminal.Value); |
fbcosentino | 0:e9d152d29a2e | 49 | terminal.print("\n"); |
fbcosentino | 0:e9d152d29a2e | 50 | |
fbcosentino | 0:e9d152d29a2e | 51 | sprintf(sbuf, "NumParams: %d\n", terminal.NumParams); |
fbcosentino | 0:e9d152d29a2e | 52 | terminal.print(sbuf); |
fbcosentino | 0:e9d152d29a2e | 53 | |
fbcosentino | 0:e9d152d29a2e | 54 | for (i=0; i<terminal.NumParams; i++) { |
fbcosentino | 0:e9d152d29a2e | 55 | sprintf(sbuf, "Param %d = ", i); |
fbcosentino | 0:e9d152d29a2e | 56 | terminal.print(sbuf); |
fbcosentino | 0:e9d152d29a2e | 57 | terminal.GetParam(); |
fbcosentino | 0:e9d152d29a2e | 58 | terminal.print(terminal.Param); |
fbcosentino | 0:e9d152d29a2e | 59 | terminal.print("\n"); |
fbcosentino | 0:e9d152d29a2e | 60 | } |
fbcosentino | 0:e9d152d29a2e | 61 | } |
fbcosentino | 0:e9d152d29a2e | 62 | |
fbcosentino | 1:a2c302339cda | 63 | |
fbcosentino | 0:e9d152d29a2e | 64 | /** |
fbcosentino | 0:e9d152d29a2e | 65 | * Main loop |
fbcosentino | 0:e9d152d29a2e | 66 | */ |
fbcosentino | 0:e9d152d29a2e | 67 | int main() { |
fbcosentino | 0:e9d152d29a2e | 68 | |
fbcosentino | 0:e9d152d29a2e | 69 | // Attach the callback. This is optional, you can inspect the |
fbcosentino | 0:e9d152d29a2e | 70 | // member objects Command, Value and Param from anywhere. |
fbcosentino | 0:e9d152d29a2e | 71 | // But this is convenient |
fbcosentino | 0:e9d152d29a2e | 72 | terminal.attach(&msg_received); |
fbcosentino | 0:e9d152d29a2e | 73 | |
fbcosentino | 0:e9d152d29a2e | 74 | // Uncomment below for auto-mode (fires interrupt whenever there is a message) |
fbcosentino | 0:e9d152d29a2e | 75 | terminal.ModeAuto(); |
fbcosentino | 0:e9d152d29a2e | 76 | |
fbcosentino | 0:e9d152d29a2e | 77 | // Uncomment below for manual mode (polled) |
fbcosentino | 0:e9d152d29a2e | 78 | //terminal.ModeManual(); // optional since default is manual mode |
fbcosentino | 0:e9d152d29a2e | 79 | |
fbcosentino | 0:e9d152d29a2e | 80 | // Warn user the program is running |
fbcosentino | 0:e9d152d29a2e | 81 | terminal.print("\nInitialised!\n\n"); |
fbcosentino | 0:e9d152d29a2e | 82 | |
fbcosentino | 0:e9d152d29a2e | 83 | while(1) { |
fbcosentino | 0:e9d152d29a2e | 84 | /* |
fbcosentino | 0:e9d152d29a2e | 85 | // Polled (manual) version: |
fbcosentino | 0:e9d152d29a2e | 86 | if (terminal.Process()) { |
fbcosentino | 0:e9d152d29a2e | 87 | // you can actually call msg_received() from here yourself, |
fbcosentino | 0:e9d152d29a2e | 88 | // if the callback feature is not used. |
fbcosentino | 0:e9d152d29a2e | 89 | terminal.print("Processed!\n"); |
fbcosentino | 0:e9d152d29a2e | 90 | } |
fbcosentino | 0:e9d152d29a2e | 91 | */ |
fbcosentino | 0:e9d152d29a2e | 92 | } |
fbcosentino | 0:e9d152d29a2e | 93 | } |