Skip to content

Commit aaea283

Browse files
committed
Add comments to serial readline loop.
1 parent 5b74829 commit aaea283

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

gps_uart.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,39 +122,51 @@ static int32_t gps_uart_worker(void* context)
122122
size_t len = 0;
123123
do
124124
{
125+
// receive serial bytes into rx_buf, starting at rx_offset from the start of the buffer
126+
// the maximum we can receive is RX_BUF_SIZE - 1 - rx_offset
125127
len = furi_stream_buffer_receive(gps_uart->rx_stream, gps_uart->rx_buf + rx_offset, RX_BUF_SIZE - 1 - rx_offset,
126128
0);
127129
if (len > 0)
128130
{
131+
// increase rx_offset by the number of bytes received, and null-terminate rx_buf
129132
rx_offset += len;
130133
gps_uart->rx_buf[rx_offset] = '\0';
131134

135+
// look for strings ending in newlines, starting at the start of rx_buf
132136
char * line_current = (char *)gps_uart->rx_buf;
133137
while (1)
134138
{
139+
// skip null characters
135140
while (*line_current == '\0' && line_current < (char *)gps_uart->rx_buf + rx_offset - 1)
136141
{
137142
line_current++;
138143
}
139144

145+
// find the next newline
140146
char * newline = strchr(line_current, '\n');
141-
if (newline)
147+
if (newline) // newline found
142148
{
149+
// put a null terminator in place of the newline, to delimit the line string
143150
*newline = '\0';
151+
152+
// attempt to parse the line as a NMEA sentence
144153
gps_uart_parse_nmea(gps_uart, line_current);
154+
155+
// move the cursor to the character after the newline
145156
line_current = newline + 1;
146157
}
147-
else
158+
else // no more newlines found
148159
{
149-
if (line_current > (char *)gps_uart->rx_buf)
160+
if (line_current > (char *)gps_uart->rx_buf) // at least one line was found
150161
{
162+
// clear parsed lines, and move any leftover bytes to the start of rx_buf
151163
rx_offset = 0;
152-
while (*line_current)
164+
while (*line_current) // stop when the original rx_offset terminator is reached
153165
{
154166
gps_uart->rx_buf[rx_offset++] = *(line_current++);
155167
}
156168
}
157-
break;
169+
break; // go back to receiving bytes from the serial stream
158170
}
159171
}
160172
}

0 commit comments

Comments
 (0)