/// © MiroZ 2024 #include #include static uint8_t mem[30000]; // template class CircBuffer { protected: uint16_t * m_buffer = (uint16_t *)mem; size_t head_ = 0; size_t tail_ = 0; bool full_ = false; uint32_t m_size = 0; public: CircBuffer() { // assert(m_buffer = (T*)calloc(size, sizeof(T))); m_size = 30000/2; } ~CircBuffer() { // free(m_buffer); } bool put(uint16_t item) { //std::lock_guard lock(mutex_); if (full_) return false; m_buffer[head_] = item; head_ = (head_ + 1) % m_size; full_ = head_ == tail_; return true; } bool get(uint16_t & val) { //std::lock_guard lock(mutex_); if (empty()) return false; // Read data and advance the tail (we now have a free space) val = m_buffer[tail_]; full_ = false; tail_ = (tail_ + 1) % m_size; return true; } void reset() { //std::lock_guard lock(mutex_); head_ = tail_ = 0; full_ = false; } bool empty() { //std::lock_guard lock(mutex_); // if head and tail are equal, we are empty return (!full_ && (head_ == tail_)); } bool full() { return full_; } size_t capacity() { return m_size; } size_t size() { //std::lock_guard lock(mutex_); size_t size = m_size; if (!full_) { if (head_ >= tail_) size = head_ - tail_; else size = m_size + head_ - tail_; } return size; } };