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