Wirepas SDK
doublebuffer.h
Go to the documentation of this file.
1 /* Copyright 2019 Wirepas Ltd. All Rights Reserved.
2  *
3  * See file LICENSE.txt for full license details.
4  *
5  */
6 
7 #ifndef DOUBLEBUFFER_H_
8 #define DOUBLEBUFFER_H_
9 
10 /*
11  * Simple helper to manage a double buffering
12  */
13 #if !defined BUFFER_SIZE
14 #error Please define BUFFER_SIZE before including doublebuffer.h!
15 #endif
16 
17 /* Generic structure to manage double buffering with a current active one */
18 typedef struct
19 {
20  uint8_t * active_buffer_p; //< Address of active buffer
21  uint8_t current_writing_index; //< Current writing index in active buffer
22  uint8_t buffer_1[BUFFER_SIZE]; //< First buffer
23  uint8_t buffer_2[BUFFER_SIZE]; //< Second buffer
25 
27 #define DoubleBuffer_init(buffers) do { \
28  (buffers).active_buffer_p = (buffers).buffer_1; \
29  (buffers).current_writing_index = 0; \
30  } while(0)
31 
33 #define DoubleBuffer_getActive(buffers) ((buffers).active_buffer_p)
34 
36 #define DoubleBuffer_swipe(buffers) do { \
37  if ((buffers).active_buffer_p == (buffers).buffer_1) \
38  { \
39  (buffers).active_buffer_p = (buffers).buffer_2; \
40  } \
41  else \
42  { \
43  (buffers).active_buffer_p = (buffers).buffer_1; \
44  } \
45  (buffers).current_writing_index = 0; \
46  } while(0)
47 
49 #define DoubleBuffer_getIndex(buffers) ((buffers).current_writing_index)
50 
52 #define DoubleBuffer_incrIndex(buffers, inc) ((buffers).current_writing_index += inc)
53 
54 
55 #endif /* DOUBLEBUFFER_H_ */
double_buffer_t
Definition: doublebuffer.h:18
double_buffer_t::current_writing_index
uint8_t current_writing_index
Definition: doublebuffer.h:21
double_buffer_t::active_buffer_p
uint8_t * active_buffer_p
Definition: doublebuffer.h:20