-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjks-ring-buffer.h
68 lines (57 loc) · 1.26 KB
/
jks-ring-buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
/**
* @file jks-ring-buffer.h
* @author https://github.com/jks-liu/cbed
* @brief
* @date 2021-06-22
*
* @copyright Copyright (c) 2021
*
*/
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
struct jks_ring_buffer {
uint8_t index;
uint8_t element_count;
};
/**
* @brief Init ring buffer size
*
* @param ring
* @param element_count Buffer length
*/
inline
void jks_ring_buffer_init(struct jks_ring_buffer *ring, uint8_t element_count)
{
ring->index = 0;
ring->element_count = element_count;
}
/**
* @brief After you stored value to current index,
* call this function to let index point to next position
*
* @param ring
* index: Index where you have stored the current value to
* element_count: How many elements in the ring buffer
* Allowed max of element_count is 255
*/
inline
void jks_ring_buffer_update_index(struct jks_ring_buffer *ring)
{
ring->index = (ring->index + 1) % ring->element_count;
}
inline
uint8_t jks_ring_buffer_previous_index(struct jks_ring_buffer ring)
{
return ring.index == 0 ? (ring.element_count - 1) : (ring.index - 1);
}
inline
uint8_t jks_ring_buffer_next_index(struct jks_ring_buffer ring)
{
return (ring.index + 1) % ring.element_count;
}
#ifdef __cplusplus
}
#endif