Events

When using LwRB in the application, it may be useful to get notification on different events, such as info when something has been written or read to/from buffer.

Library has support for events that get called each time there has been a modification in the buffer data, that means on every read or write operation.

Some use cases:

  • Notify application layer that LwRB operation has been executed and send debug message

  • Unlock semaphore when sufficient amount of bytes have been written/read from/to buffer when application uses operating system

  • Write notification to message queue at operating system level to wakeup another task

Note

Every operation that modified read or write internal pointers, is considered as read or write operation. An exception is reset event that sets both internal pointers to 0

Example code for events
 1/**
 2 * \brief           Buffer event function
 3 */
 4void
 5my_buff_evt_fn(lwrb_t* buff, lwrb_evt_type_t type, size_t len) {
 6    switch (type) {
 7        case LWRB_EVT_RESET:
 8            printf("[EVT] Buffer reset event!\r\n");
 9            break;
10        case LWRB_EVT_READ:
11            printf("[EVT] Buffer read event: %d byte(s)!\r\n", (int)len);
12            break;
13        case LWRB_EVT_WRITE:
14            printf("[EVT] Buffer write event: %d byte(s)!\r\n", (int)len);
15            break;
16        default: break;
17    }
18}
19
20/* Later in the code... */
21lwrb_t buff;
22uint8_t buff_data[8];
23
24/* Init buffer and set event function */
25lwrb_init(&buff, buff_data, sizeof(buff_data));
26lwrb_set_evt_fn(&buff, my_buff_evt_fn);