Class UartWriter

extends Writer

Class summary


write data/Data from/int= to/int= --break-length/int= --flush/bool= -> int
Writes data to the Port.
try-write data/Data from/int= to/int= --break-length/int= --flush/bool= -> int
Tries to write the given data to this writer.
flush -> none
operator == other/any -> bool
Whether this object is equal to the other.
stringify -> string
Stringifies this object.
processed -> int
The amount of bytes that have been written to this writer so far.
write-byte value/int --flush/bool= -> none
Writes a single byte.
write-from reader/Reader --flush/bool= -> none
Writes all data that is provided by the given reader.
little-endian -> EndianWriter
Provides endian-aware functions to write to this instance.
big-endian -> EndianWriter
Provides endian-aware functions to write to this instance.

Methods

Provides endian-aware functions to write to this instance.
The big-endian byte order writes higher-order (big) bytes first. For example, if the target of the write operation is a byte array, then the first byte written (at position 0) is the most significant byte of the number that is written.
Examples

import io

main:
  buffer := io.Buffer
  buffer.big-endian.write-int32 0x12345678
  // The most significant byte 0x12 is at index 0.
  print buffer.bytes  // => #[0x12, 0x34, 0x56, 0x78]

flush -> none

Provides endian-aware functions to write to this instance.
The little-endian byte order writes lower-order ("little") bytes first. For example, if the target of the write operation is a byte array, then the first byte written (at position 0) is the least significant byte of the number that is written.
Examples

import io

main:
  buffer := io.Buffer  // A writer.
  buffer.little-endian.write-int32 0x12345678
  // The least significant byte 0x78 is at index 0.
  print buffer.bytes  // => #[0x78, 0x56, 0x34, 0x12]

operator == other/any -> bool
Whether this object is equal to the other.
By default, identical is used for equality.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:

class Pin:
  number/int

  constructor .number:

  operator == other:
    if other is not Pin: return false
    return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).

The amount of bytes that have been written to this writer so far.

Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.

try-write data/Data from/int=0 to/int=data.byte-size --break-length/int=0 --flush/bool=false -> int
Tries to write the given data to this writer.
If the writer can't write all the data at once, it writes as much as possible.
If the writer is closed while writing, throws, or returns the number of bytes written.
Otherwise always returns the number of bytes written.
If break-length is greater than 0, an additional break signal is added after the data is written. The duration of the break signal is bit-duration * break-length, where bit-duration is the duration it takes to write one bit at the current baud rate.
If flush is true and the complete requested slice is accepted, waits until those bytes have left the physical pins. A partial write does not flush: the caller still has bytes to enqueue, and draining between chunks would introduce an on-wire gap.

write data/Data from/int=0 to/int=data.byte-size --break-length/int=0 --flush/bool=false -> int
Writes data to the Port.
If break-length is greater than 0, an additional break signal is added after the data is written. The duration of the break signal is bit-duration * break-length, where bit-duration is the duration it takes to write one bit at the current baud rate.
If not all bytes could be written without blocking, this will be indicated by the return value. In this case the break is not written even if requested. The easiest way to handle this by using the writer.Writer class. Alternatively, something like the following could be used.

for position := 0; position < data.byte-size; null:
  position += my-uart.write (data.byte-slice position data.byte-size)
If flush is true, the method blocks until all bytes that were written have been emitted to the physical pins. This is equivalent to calling flush. Otherwise, returns as soon as the data is buffered.
Returns the number of bytes written.

write-byte value/int --flush/bool=false -> none
Writes a single byte.

write-from reader/Reader --flush/bool=false -> none
Writes all data that is provided by the given reader.