Skip to content

Commit

Permalink
Merge pull request #15 from dihm/fast-serial-null-terminate
Browse files Browse the repository at this point in the history
Add null terminator to strings returned from fast_serial_read_until
  • Loading branch information
dihm authored May 21, 2024
2 parents aea505b + e306421 commit 3e4adc6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion prawn_do/fast_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ uint32_t fast_serial_read(const char * buffer, uint32_t buffer_size){
// Read bytes until terminator reached (blocks until terminator or buffer_size is reached)
uint32_t fast_serial_read_until(char * buffer, uint32_t buffer_size, char until){
uint32_t buffer_idx = 0;
while(buffer_idx < buffer_size){
while(buffer_idx < buffer_size - 1){
while(fast_serial_read_available() > 0){
int32_t next_char = tud_cdc_read_char();

Expand All @@ -51,6 +51,7 @@ uint32_t fast_serial_read_until(char * buffer, uint32_t buffer_size, char until)
}
fast_serial_task();
}
buffer[buffer_idx] = '\0'; // Null terminate string
return buffer_idx;
}

Expand Down
1 change: 1 addition & 0 deletions prawn_do/fast_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static inline uint32_t fast_serial_read_atomic(const char * buffer, uint32_t buf
uint32_t fast_serial_read(const char * buffer, uint32_t buffer_size);

// Read bytes until terminator reached (blocks until terminator or buffer_size is reached)
// Adds null terminator to buffer after read completes (reserving one byte in buffer for this)
uint32_t fast_serial_read_until(char * buffer, uint32_t buffer_size, char until);

// Clear read FIFO (without reading it)
Expand Down

0 comments on commit 3e4adc6

Please sign in to comment.