Serial I/O program using rtos and ttreads for NMNU Ambient Computing
Dependencies: SLCD mbed-rtos mbed
Fork of Serial_IO_test_v2 by
serialO_v2.cpp@0:60b70ac7ed38, 2015-03-05 (annotated)
- Committer:
- scohennm
- Date:
- Thu Mar 05 03:33:01 2015 +0000
- Revision:
- 0:60b70ac7ed38
Use ISR instead of checking char buffer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:60b70ac7ed38 | 1 | #include "mbed.h" |
scohennm | 0:60b70ac7ed38 | 2 | #include "SLCD.h" |
scohennm | 0:60b70ac7ed38 | 3 | |
scohennm | 0:60b70ac7ed38 | 4 | #define LCDLEN 10 |
scohennm | 0:60b70ac7ed38 | 5 | #define MAXCHAR 4 |
scohennm | 0:60b70ac7ed38 | 6 | #define LEDBLINKTIME 0.2f |
scohennm | 0:60b70ac7ed38 | 7 | |
scohennm | 0:60b70ac7ed38 | 8 | |
scohennm | 0:60b70ac7ed38 | 9 | DigitalOut rLed(LED_RED); |
scohennm | 0:60b70ac7ed38 | 10 | |
scohennm | 0:60b70ac7ed38 | 11 | |
scohennm | 0:60b70ac7ed38 | 12 | Serial pc(USBTX, USBRX); // tx, rx |
scohennm | 0:60b70ac7ed38 | 13 | SLCD slcd; //define LCD display |
scohennm | 0:60b70ac7ed38 | 14 | char rxChar; |
scohennm | 0:60b70ac7ed38 | 15 | int charIndex = 0; |
scohennm | 0:60b70ac7ed38 | 16 | char rxString[LCDLEN]; |
scohennm | 0:60b70ac7ed38 | 17 | bool charReady = false; |
scohennm | 0:60b70ac7ed38 | 18 | |
scohennm | 0:60b70ac7ed38 | 19 | // set up intterupt for serial port |
scohennm | 0:60b70ac7ed38 | 20 | |
scohennm | 0:60b70ac7ed38 | 21 | void serialISR(){ |
scohennm | 0:60b70ac7ed38 | 22 | rxChar = pc.getc(); // reading clears the buffer |
scohennm | 0:60b70ac7ed38 | 23 | pc.printf("%c\n\r", rxChar); |
scohennm | 0:60b70ac7ed38 | 24 | rxString[charIndex] = rxChar; |
scohennm | 0:60b70ac7ed38 | 25 | charIndex = (charIndex + 1 )% MAXCHAR; |
scohennm | 0:60b70ac7ed38 | 26 | charReady = true; |
scohennm | 0:60b70ac7ed38 | 27 | return; |
scohennm | 0:60b70ac7ed38 | 28 | } |
scohennm | 0:60b70ac7ed38 | 29 | |
scohennm | 0:60b70ac7ed38 | 30 | void LCDMessNoDwell(char *lMess){ |
scohennm | 0:60b70ac7ed38 | 31 | slcd.Home(); |
scohennm | 0:60b70ac7ed38 | 32 | slcd.clear(); |
scohennm | 0:60b70ac7ed38 | 33 | slcd.printf(lMess); |
scohennm | 0:60b70ac7ed38 | 34 | } |
scohennm | 0:60b70ac7ed38 | 35 | |
scohennm | 0:60b70ac7ed38 | 36 | |
scohennm | 0:60b70ac7ed38 | 37 | int main() |
scohennm | 0:60b70ac7ed38 | 38 | { |
scohennm | 0:60b70ac7ed38 | 39 | // set up interrupt |
scohennm | 0:60b70ac7ed38 | 40 | pc.attach(&serialISR); |
scohennm | 0:60b70ac7ed38 | 41 | |
scohennm | 0:60b70ac7ed38 | 42 | while (true) { |
scohennm | 0:60b70ac7ed38 | 43 | rLed = !rLed; // toggle led |
scohennm | 0:60b70ac7ed38 | 44 | if (charReady) { |
scohennm | 0:60b70ac7ed38 | 45 | LCDMessNoDwell(rxString); |
scohennm | 0:60b70ac7ed38 | 46 | charReady = false; |
scohennm | 0:60b70ac7ed38 | 47 | } |
scohennm | 0:60b70ac7ed38 | 48 | wait(LEDBLINKTIME); |
scohennm | 0:60b70ac7ed38 | 49 | } |
scohennm | 0:60b70ac7ed38 | 50 | } |