Skip to content

Commit

Permalink
Make uart.write return the amount of bytes written. (#2393)
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch authored Jun 11, 2024
1 parent 7d2b9dd commit e9b4081
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lib/uart.toit
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,11 @@ class UartWriter extends io.Writer:
Returns the number of bytes written.
*/
write data/io.Data from/int=0 to/int=data.byte-size --break-length/int=0 --flush/bool=false -> none:
write data/io.Data from/int=0 to/int=data.byte-size --break-length/int=0 --flush/bool=false -> int:
data-size := to - from
while not is-closed_:
from += try-write data from to --break-length=break-length --flush=flush
if from >= to: return
if from >= to: return data-size
yield
assert: is-closed_
throw "WRITER_CLOSED"
Expand All @@ -378,13 +379,13 @@ class UartWriter extends io.Writer:
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.
*/
try-write data/io.Data from/int=0 to/int=data.byte-size --break-length/int=0 --flush/bool=false:
try-write data/io.Data from/int=0 to/int=data.byte-size --break-length/int=0 --flush/bool=false -> int:
if is-closed_: throw "WRITER_CLOSED"
result := port_.try-write_ data from to --break-length=break-length
if flush and (result > 0 or data.byte-size == 0): this.flush
return result

try-write_ data/io.Data from/int to/int --break-length/int=0:
try-write_ data/io.Data from/int to/int --break-length/int=0 -> int:
return port_.try-write_ data from to --break-length=break-length

flush_ -> none:
Expand Down
3 changes: 2 additions & 1 deletion tests/esp32/uart-baud-rate.toit
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ main:
port2.baud-rate = 115200
expect-baud 115200 port2.baud-rate

port1.out.write "like a" --flush
written := port1.out.write "like a" --flush
expect-equals 6 written
bytes = port2.in.read
expect-equals "like a" bytes.to-string

Expand Down

0 comments on commit e9b4081

Please sign in to comment.