From 1df25129350a20f3503dd3aa3a2b6b92caab08d3 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 22 Feb 2016 18:32:51 +1100 Subject: [PATCH] AP_HAL: added update() method for object ringbuffer to support updating objects for GCS work Tom is doing --- libraries/AP_HAL/utility/RingBuffer.cpp | 22 ++++++++++++++++++++++ libraries/AP_HAL/utility/RingBuffer.h | 14 +++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/libraries/AP_HAL/utility/RingBuffer.cpp b/libraries/AP_HAL/utility/RingBuffer.cpp index 2aae92e5f5..8fdb9b24ab 100644 --- a/libraries/AP_HAL/utility/RingBuffer.cpp +++ b/libraries/AP_HAL/utility/RingBuffer.cpp @@ -66,6 +66,28 @@ uint32_t ByteBuffer::write(const uint8_t *data, uint32_t len) return len; } +/* + update bytes at the read pointer. Used to update an object without + popping it + */ +bool ByteBuffer::update(const uint8_t *data, uint32_t len) +{ + if (len > available()) { + return false; + } + // perform as two memcpy calls + uint32_t n = size - head; + if (n > len) { + n = len; + } + memcpy(&buf[head], data, n); + data += n; + if (len > n) { + memcpy(&buf[0], data, len-n); + } + return true; +} + bool ByteBuffer::advance(uint32_t n) { if (n > available()) { diff --git a/libraries/AP_HAL/utility/RingBuffer.h b/libraries/AP_HAL/utility/RingBuffer.h index 0fe454688f..323059aa74 100644 --- a/libraries/AP_HAL/utility/RingBuffer.h +++ b/libraries/AP_HAL/utility/RingBuffer.h @@ -46,6 +46,12 @@ public: // read bytes from ringbuffer. Returns number of bytes read uint32_t read(uint8_t *data, uint32_t len); + /* + update bytes at the read pointer. Used to update an object without + popping it + */ + bool update(const uint8_t *data, uint32_t len); + // return size of ringbuffer uint32_t get_size(void) const { return size; } @@ -110,7 +116,7 @@ public: } return buffer->write((uint8_t*)&object, sizeof(T)) == sizeof(T); } - + /* throw away an object */ @@ -146,6 +152,12 @@ public: bool peek(T &object) { return peekbytes(&object, sizeof(T)) == sizeof(T); } + + /* update the object at the front of the queue (the one that would + be fetched by pop()) */ + bool update(const T &object) { + return buffer->update((uint8_t*)&object, sizeof(T)); + } private: ByteBuffer *buffer = nullptr;