Skip to content

Commit 193ec38

Browse files
Examples: Update serial to also use printf.
1 parent 72357f1 commit 193ec38

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

examples/serial.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Micro:bit Educational Foundation and contributors
2+
* Copyright 2020-2022 Micro:bit Educational Foundation and contributors
33
* SPDX-License-Identifier: Apache-2.0
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,19 +16,29 @@
1616
*/
1717
#include "mbed.h"
1818

19-
static UnbufferedSerial pc_serial(USBTX, USBRX, 115200);
19+
UnbufferedSerial pc_serial(USBTX, USBRX, 115200);
2020

21-
void rx_irq() {
21+
// Wire up printf to serial
22+
FileHandle *mbed::mbed_override_console(int fd) {
23+
return &pc_serial;
24+
}
25+
26+
void rx_irq_echo() {
2227
char single_byte;
23-
// Echo each byte received
2428
if (pc_serial.read(&single_byte, 1)) {
2529
pc_serial.write(&single_byte, 1);
2630
}
2731
}
2832

2933
int main(void) {
34+
printf("printf test! (MbedOS %d.%d.%d)\n",
35+
MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
36+
37+
// Send a string directly (size calculation removes the null terminator)
3038
char hello[] = "hello world!\n";
31-
size_t hello_size = sizeof(hello) / sizeof(hello[0]);
39+
size_t hello_size = (sizeof(hello) / sizeof(hello[0])) - 1;
3240
pc_serial.write(hello, hello_size);
33-
pc_serial.attach(&rx_irq, SerialBase::RxIrq);
41+
42+
// Configure RX IRQ to a function to echos back the data
43+
pc_serial.attach(&rx_irq_echo, SerialBase::RxIrq);
3444
}

0 commit comments

Comments
 (0)