Created
October 5, 2021 21:16
-
-
Save sago35/5edcdc6f38156048e1550f48dbb8405f to your computer and use it in GitHub Desktop.
TinyGo + XIAO + USBCDC + UART (D6/D7)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"machine" | |
"time" | |
) | |
var ( | |
usbcdc = machine.USB | |
uart = machine.UART1 | |
tx = machine.UART_TX_PIN | |
rx = 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 usbcdc.Buffered() > 0 { | |
data, _ := usbcdc.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++ | |
} | |
} | |
if uart.Buffered() > 0 { | |
data, _ := uart.ReadByte() | |
usbcdc.Write([]byte{data}) | |
} | |
time.Sleep(10 * time.Millisecond) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By connecting D6 and D7, this code will work.