Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the ring buffer size.
Embedded C
volatile char uart_buffer[[1]]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing too large a buffer wastes memory.
Choosing too small a buffer may cause overflow.
✗ Incorrect
The ring buffer size is set to 64 bytes to balance memory and performance.
2fill in blank
mediumComplete the code to increment the head index in the ring buffer.
Embedded C
head = (head + [1]) % BUFFER_SIZE; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 does not move the head.
Using -1 causes underflow.
✗ Incorrect
Incrementing by 1 moves the head to the next position in the buffer.
3fill in blank
hardFix the error in the condition to check if the buffer is full.
Embedded C
if (((head + 1) % BUFFER_SIZE) == [1]) { /* buffer full */ }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing to head causes wrong condition.
Comparing to BUFFER_SIZE is invalid index.
✗ Incorrect
The buffer is full when the next head position equals the tail.
4fill in blank
hardFill both blanks to read a byte from the buffer and update the tail index.
Embedded C
char data = uart_buffer[[1]]; tail = (tail [2] 1) % BUFFER_SIZE;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using head instead of tail to read data.
Decrementing tail causes errors.
✗ Incorrect
Read data at tail, then increment tail by 1 wrapping around.
5fill in blank
hardFill all three blanks to write a byte to the buffer and update the head index safely.
Embedded C
if (((head + 1) % BUFFER_SIZE) != [1]) { uart_buffer[[2]] = data; head = (head [3] 1) % BUFFER_SIZE; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Writing data at tail instead of head.
Using '-' instead of '+' to increment head.
✗ Incorrect
Check buffer not full by comparing next head to tail, write data at head, then increment head.