Skip to content

Commit d7dba9c

Browse files
committed
Add code snippet for UnbufferedSerial API documentation
1 parent e1d5095 commit d7dba9c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "mbed.h"
2+
3+
4+
// Create a DigitalOutput object to toggle an LED whenever data is received.
5+
static DigitalOut led(LED1);
6+
7+
// Create a new UnbufferedSerial object with a default baud rate.
8+
static UnbufferedSerial serial_port(USBTX, USBRX);
9+
10+
void on_rx_interrupt()
11+
{
12+
char c;
13+
14+
// Toggle the LED.
15+
led = !led;
16+
17+
// Read the data to clear the receive interrupt.
18+
if (serial_port.read(&c, 1)) {
19+
// Echo the input back to the terminal.
20+
serial_port.write(&c, 1);
21+
}
22+
}
23+
24+
int main(void) {
25+
// Set desired properties (9600-8-N-1).
26+
serial_port.baud(9600);
27+
serial_port.format(
28+
/* bits */ 8,
29+
/* parity */ SerialBase::None,
30+
/* stop bit */ 1
31+
);
32+
33+
// Register a callback to process a Rx (receive) interrupt.
34+
serial_port.attach(&on_rx_interrupt, SerialBase::RxIrq);
35+
}

0 commit comments

Comments
 (0)