mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-03 02:27:48 +00:00
4f4d7976c6
* machine/uart: add core support for multiple UARTs by allowing for multiple RingBuffers * machine/uart: complete core support for multiple UARTs * machine/uart: no need to store pointer to UART, better to treat like I2C and SPI * machine/uart: increase ring buffer size to 128 bytes * machine/uart: improve godocs comments and use comma-ok idiom for buffer Put/Get methods
45 lines
931 B
Go
45 lines
931 B
Go
// This is a echo console running on the device UART.
|
|
// Connect using default baudrate for this hardware, 8-N-1 with your terminal program.
|
|
package main
|
|
|
|
import (
|
|
"machine"
|
|
"time"
|
|
)
|
|
|
|
// change these to test a different UART or pins if available
|
|
var (
|
|
uart = machine.UART0
|
|
tx uint8 = machine.UART_TX_PIN
|
|
rx uint8 = machine.UART_RX_PIN
|
|
)
|
|
|
|
func main() {
|
|
uart.Configure(machine.UARTConfig{TX: tx, RX: rx})
|
|
uart.Write([]byte("Echo console enabled. Type something then press enter:\r\n"))
|
|
|
|
input := make([]byte, 64)
|
|
i := 0
|
|
for {
|
|
if uart.Buffered() > 0 {
|
|
data, _ := uart.ReadByte()
|
|
|
|
switch data {
|
|
case 13:
|
|
// return key
|
|
uart.Write([]byte("\r\n"))
|
|
uart.Write([]byte("You typed: "))
|
|
uart.Write(input[:i])
|
|
uart.Write([]byte("\r\n"))
|
|
i = 0
|
|
default:
|
|
// just echo the character
|
|
uart.WriteByte(data)
|
|
input[i] = data
|
|
i++
|
|
}
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
}
|