mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-09 05:23:40 +00:00
machine: make UART objects pointer receivers
This means that machine.UART0, machine.UART1, etc are of type *machine.UART, not machine.UART. This makes them easier to pass around and avoids surprises when they are passed around by value while they should be passed around by reference. There is a small code size impact in some cases, but it is relatively minor.
This commit is contained in:
committed by
Ron Evans
parent
7c949ad386
commit
aa5b8d0df7
+5
-5
@@ -27,7 +27,7 @@ type UARTConfig struct {
|
||||
//
|
||||
|
||||
// Read from the RX buffer.
|
||||
func (uart UART) Read(data []byte) (n int, err error) {
|
||||
func (uart *UART) Read(data []byte) (n int, err error) {
|
||||
// check if RX buffer is empty
|
||||
size := uart.Buffered()
|
||||
if size == 0 {
|
||||
@@ -49,7 +49,7 @@ func (uart UART) Read(data []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
// Write data to the UART.
|
||||
func (uart UART) Write(data []byte) (n int, err error) {
|
||||
func (uart *UART) Write(data []byte) (n int, err error) {
|
||||
for _, v := range data {
|
||||
uart.WriteByte(v)
|
||||
}
|
||||
@@ -58,7 +58,7 @@ func (uart UART) Write(data []byte) (n int, err error) {
|
||||
|
||||
// ReadByte reads a single byte from the RX buffer.
|
||||
// If there is no data in the buffer, returns an error.
|
||||
func (uart UART) ReadByte() (byte, error) {
|
||||
func (uart *UART) ReadByte() (byte, error) {
|
||||
// check if RX buffer is empty
|
||||
buf, ok := uart.Buffer.Get()
|
||||
if !ok {
|
||||
@@ -68,12 +68,12 @@ func (uart UART) ReadByte() (byte, error) {
|
||||
}
|
||||
|
||||
// Buffered returns the number of bytes currently stored in the RX buffer.
|
||||
func (uart UART) Buffered() int {
|
||||
func (uart *UART) Buffered() int {
|
||||
return int(uart.Buffer.Used())
|
||||
}
|
||||
|
||||
// Receive handles adding data to the UART's data buffer.
|
||||
// Usually called by the IRQ handler for a machine.
|
||||
func (uart UART) Receive(data byte) {
|
||||
func (uart *UART) Receive(data byte) {
|
||||
uart.Buffer.Put(data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user