* @brief Put a byte into the ring buffer. If ring buffer is full, it will discard an old data and put into a new data. * * @param rb A pointer to the ring buffer object. * @param ch A byte put into the ring buffer. * * @return Return the data size we put into the ring buffer. Always return 1. */
| 303 | * @return Return the data size we put into the ring buffer. Always return 1. |
| 304 | */ |
| 305 | rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch) |
| 306 | { |
| 307 | enum rt_ringbuffer_state old_state; |
| 308 | |
| 309 | RT_ASSERT(rb != RT_NULL); |
| 310 | |
| 311 | old_state = rt_ringbuffer_status(rb); |
| 312 | |
| 313 | rb->buffer_ptr[rb->write_index] = ch; |
| 314 | |
| 315 | /* flip mirror */ |
| 316 | if (rb->write_index == rb->buffer_size - 1) |
| 317 | { |
| 318 | rb->write_mirror = ~rb->write_mirror; |
| 319 | rb->write_index = 0; |
| 320 | if (old_state == RT_RINGBUFFER_FULL) |
| 321 | { |
| 322 | rb->read_mirror = ~rb->read_mirror; |
| 323 | rb->read_index = rb->write_index; |
| 324 | } |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | rb->write_index++; |
| 329 | if (old_state == RT_RINGBUFFER_FULL) |
| 330 | rb->read_index = rb->write_index; |
| 331 | } |
| 332 | |
| 333 | return 1; |
| 334 | } |
| 335 | RTM_EXPORT(rt_ringbuffer_putchar_force); |
| 336 | |
| 337 | /** |
no test coverage detected