Skip to content

Commit d2630d4

Browse files
committed
Add FileHandle_sigio example
1 parent 123fd62 commit d2630d4

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## FileHandle sigio example
2+
3+
The example monitors a serial console device, and every time it outputs a character, send it to the console back and toggle LED2.
4+
5+
1. Flash the board, and ensure the target's USB is plugged into the PC.
6+
2. Open serial console (select associated COM port, transmission speed: 19200 bps).
7+
3. Reset the target.
8+
4. Every time that device outputs a character, the character should appear in the console and LED2 should be toggled LED2.
9+
10+
**Note:** You can check the associated serial port using `mbedls` command.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Main thread
2+
#include "mbed.h"
3+
4+
static DigitalOut led1(LED1);
5+
static DigitalOut led2(LED2);
6+
7+
static BufferedSerial device(STDIO_UART_TX, STDIO_UART_RX);
8+
9+
static void callback_ex()
10+
{
11+
// always read until data is exhausted - we may not get another
12+
// sigio otherwise
13+
while (1) {
14+
char c;
15+
if (device.read(&c, 1) != 1) {
16+
break;
17+
}
18+
putchar(c);
19+
led2 = !led2;
20+
}
21+
}
22+
23+
int main()
24+
{
25+
// BufferedSerial-specific method - all others are from FileHandle base class
26+
device.set_baud(19200);
27+
28+
// Ensure that device.read() returns -EAGAIN when out of data
29+
device.set_blocking(false);
30+
31+
// sigio callback is deferred to event queue, as we cannot in general
32+
// perform read() calls directly from the sigio() callback.
33+
device.sigio(mbed_event_queue()->event(callback_ex));
34+
35+
while (1) {
36+
led1 = !led1;
37+
ThisThread::sleep_for(500);
38+
}
39+
}

0 commit comments

Comments
 (0)