github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/machine/serial.go (about)

     1  package machine
     2  
     3  import "errors"
     4  
     5  var errNoByte = errors.New("machine: no byte read")
     6  
     7  // UARTConfig is a struct with which a UART (or similar object) can be
     8  // configured. The baud rate is usually respected, but TX and RX may be ignored
     9  // depending on the chip and the type of object.
    10  type UARTConfig struct {
    11  	BaudRate uint32
    12  	TX       Pin
    13  	RX       Pin
    14  	RTS      Pin
    15  	CTS      Pin
    16  }
    17  
    18  // NullSerial is a serial version of /dev/null (or null router): it drops
    19  // everything that is written to it.
    20  type NullSerial struct {
    21  }
    22  
    23  // Configure does nothing: the null serial has no configuration.
    24  func (ns NullSerial) Configure(config UARTConfig) error {
    25  	return nil
    26  }
    27  
    28  // WriteByte is a no-op: the null serial doesn't write bytes.
    29  func (ns NullSerial) WriteByte(b byte) error {
    30  	return nil
    31  }
    32  
    33  // ReadByte always returns an error because there aren't any bytes to read.
    34  func (ns NullSerial) ReadByte() (byte, error) {
    35  	return 0, errNoByte
    36  }
    37  
    38  // Buffered returns how many bytes are buffered in the UART. It always returns 0
    39  // as there are no bytes to read.
    40  func (ns NullSerial) Buffered() int {
    41  	return 0
    42  }
    43  
    44  // Write is a no-op: none of the data is being written and it will not return an
    45  // error.
    46  func (ns NullSerial) Write(p []byte) (n int, err error) {
    47  	return len(p), nil
    48  }