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 #if BUFFER_SIZE == 256
22  uint8_t current_writing_index; //< Current writing index in active buffer
23 #elif BUFFER_SIZE == 512
24  uint16_t current_writing_index; //< Current writing index in active buffer
25 #else
26 #error Unsupported BUFFER_SIZE
27 #endif
28  uint8_t buffer_1[BUFFER_SIZE]; //< First buffer
29  uint8_t buffer_2[BUFFER_SIZE]; //< Second buffer
31 
33 #define DoubleBuffer_init(buffers) do { \
34  (buffers).active_buffer_p = (buffers).buffer_1; \
35  (buffers).current_writing_index = 0; \
36  } while(0)
37 
39 #define DoubleBuffer_getActive(buffers) ((buffers).active_buffer_p)
40 
42 #define DoubleBuffer_swipe(buffers) do { \
43  if ((buffers).active_buffer_p == (buffers).buffer_1) \
44  { \
45  (buffers).active_buffer_p = (buffers).buffer_2; \
46  } \
47  else \
48  { \
49  (buffers).active_buffer_p = (buffers).buffer_1; \
50  } \
51  (buffers).current_writing_index = 0; \
52  } while(0)
53 
55 #define DoubleBuffer_getIndex(buffers) ((buffers).current_writing_index)
56 
58 #define DoubleBuffer_incrIndex(buffers, inc) ((buffers).current_writing_index += inc)
59 
60 
61 #endif /* DOUBLEBUFFER_H_ */
double_buffer_t
Definition: doublebuffer.h:18
double_buffer_t::active_buffer_p
uint8_t * active_buffer_p
Definition: doublebuffer.h:20