APM:Libraries
ring_buffer_pulse.h
Go to the documentation of this file.
1 
15 #ifndef _PULSE_BUFFER_H_
16 #define _PULSE_BUFFER_H_
17 
18 #include "hal_types.h"
19 #include "pwm_in.h"
20 
21 #ifdef __cplusplus
22  extern "C" {
23 #endif
24 
34 typedef struct Pulse_buffer {
36  uint16_t head;
37  volatile uint16_t tail;
38  uint16_t size;
39 } pulse_buffer;
40 
41 
42 
55 static inline void pb_init(volatile pulse_buffer *pb, uint16_t size, Pulse *buf) {
56  pb->head = 0;
57  pb->tail = 0;
58  pb->size = size - 1;
59  pb->buf = buf;
60 }
61 
66 static inline uint16_t pb_full_count(volatile pulse_buffer *pb) {
67  uint16_t h=pb->head;
68  uint16_t t=pb->tail;
69  int32_t size = t - h;
70  if (t < h) {
71  size += pb->size + 1;
72  }
73  return (uint16_t)size;
74 }
75 
80 static inline int pb_is_full(volatile pulse_buffer *pb) {
81  uint16_t t = pb->tail;
82  uint16_t h = pb->head;
83  return (t + 1 == h) || (t == pb->size && h == 0);
84 }
85 
90 static inline int pb_is_empty(volatile pulse_buffer *pb) {
91  bool ret=pb->head == pb->tail;
92  return ret;
93 }
94 
100 static inline void pb_insert(volatile pulse_buffer *pb, Pulse element) {
101  uint16_t t = pb->tail;
102  pb->buf[t] = element;
103  pb->tail = (t == pb->size) ? 0 : t + 1;
104 }
105 
110 static inline Pulse pb_remove(volatile pulse_buffer *pb) {
111  uint16_t h = pb->head;
112  Pulse p = pb->buf[h];
113  pb->head = (h == pb->size) ? 0 : h + 1;
114  return p;
115 }
116 
124 static inline int pb_safe_insert(volatile pulse_buffer *pb, Pulse element) {
125  if (pb_is_full(pb)) {
126  return 0;
127  }
128  pb_insert(pb, element);
129  return 1;
130 }
131 
132 
137 static inline void pb_reset(volatile pulse_buffer *pb) {
138  pb->tail = pb->head;
139 }
140 
141 #ifdef __cplusplus
142  }
143 #endif
144 
145 #endif
146 
static Pulse pb_remove(volatile pulse_buffer *pb)
Remove and return the first item from a ring buffer.
static int pb_is_empty(volatile pulse_buffer *pb)
Returns true if and only if the ring buffer is empty.
static uint16_t pb_full_count(volatile pulse_buffer *pb)
Return the number of elements stored in the ring buffer.
static void pb_init(volatile pulse_buffer *pb, uint16_t size, Pulse *buf)
static int pb_is_full(volatile pulse_buffer *pb)
Returns true if and only if the ring buffer is full.
volatile uint16_t tail
Definition: pwm_in.h:40
static int pb_safe_insert(volatile pulse_buffer *pb, Pulse element)
Attempt to insert an element into a ring buffer.
static void pb_insert(volatile pulse_buffer *pb, Pulse element)
struct Pulse_buffer pulse_buffer
static void pb_reset(volatile pulse_buffer *pb)
Discard all items from a ring buffer.