From 6fb00f4fc3aebb9fb06ac177a4a639ea1dadf306 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 7 Jan 2015 08:04:08 +1100 Subject: [PATCH] AP_HAL: create a common utility/RingBuffer.h header --- libraries/AP_HAL/UARTDriver.h | 9 --------- libraries/AP_HAL/utility/RingBuffer.h | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 libraries/AP_HAL/utility/RingBuffer.h diff --git a/libraries/AP_HAL/UARTDriver.h b/libraries/AP_HAL/UARTDriver.h index cf11b5c273..c5ea81ae37 100644 --- a/libraries/AP_HAL/UARTDriver.h +++ b/libraries/AP_HAL/UARTDriver.h @@ -7,15 +7,6 @@ #include "AP_HAL_Namespace.h" #include "utility/BetterStream.h" -/* - buffer handling macros - */ -#define BUF_AVAILABLE(buf) ((buf##_head > (_tail=buf##_tail))? (buf##_size - buf##_head) + _tail: _tail - buf##_head) -#define BUF_SPACE(buf) (((_head=buf##_head) > buf##_tail)?(_head - buf##_tail) - 1:((buf##_size - buf##_tail) + _head) - 1) -#define BUF_EMPTY(buf) (buf##_head == buf##_tail) -#define BUF_ADVANCETAIL(buf, n) buf##_tail = (buf##_tail + n) % buf##_size -#define BUF_ADVANCEHEAD(buf, n) buf##_head = (buf##_head + n) % buf##_size - /* Pure virtual UARTDriver class */ class AP_HAL::UARTDriver : public AP_HAL::BetterStream { public: diff --git a/libraries/AP_HAL/utility/RingBuffer.h b/libraries/AP_HAL/utility/RingBuffer.h new file mode 100644 index 0000000000..56a77a9314 --- /dev/null +++ b/libraries/AP_HAL/utility/RingBuffer.h @@ -0,0 +1,22 @@ +#ifndef __AP_HAL_UTILITY_RINGBUFFER_H__ +#define __AP_HAL_UTILITY_RINGBUFFER_H__ + +/* + common ring buffer handling macros + + These macros assume a ring buffer like this: + + uint8_t *_buf; + uint16_t _buf_size; + volatile uint16_t _buf_head; + volatile uint16_t _buf_tail; + + These should be converted to a class in future + */ +#define BUF_AVAILABLE(buf) ((buf##_head > (_tail=buf##_tail))? (buf##_size - buf##_head) + _tail: _tail - buf##_head) +#define BUF_SPACE(buf) (((_head=buf##_head) > buf##_tail)?(_head - buf##_tail) - 1:((buf##_size - buf##_tail) + _head) - 1) +#define BUF_EMPTY(buf) (buf##_head == buf##_tail) +#define BUF_ADVANCETAIL(buf, n) buf##_tail = (buf##_tail + n) % buf##_size +#define BUF_ADVANCEHEAD(buf, n) buf##_head = (buf##_head + n) % buf##_size + +#endif // __AP_HAL_UTILITY_RINGBUFFER_H__