Skip to content

Commit

Permalink
Merge branch 'master' into pclink
Browse files Browse the repository at this point in the history
  • Loading branch information
a8jan committed Jul 10, 2023
2 parents 1e3f124 + 15270e9 commit 9ac36e8
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 122 deletions.
57 changes: 2 additions & 55 deletions lib/bus/rc2014bus/rc2014bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,65 +35,11 @@ uint8_t rc2014_checksum(uint8_t *buf, unsigned short len)
return checksum;
}

rc2014Buffer::rc2014Buffer(unsigned size) :
size_(size),
dirty_(true)
{
buffer_.reserve(size + 1);
buffer_.push_back(0); // set the first checksum
}

bool rc2014Buffer::validate()
{
if (buffer_.size() == 0)
return true;

if (buffer_.size() == 1) {
if (buffer_.back() == 0) {
return true;
}
return false;
}

uint8_t ck = rc2014_checksum(buffer_.data(), buffer_.size() - 1);

return ck == buffer_.back();
}

uint8_t* rc2014Buffer::data()
{
return buffer_.data();
}

size_t rc2014Buffer::max_size()
{
return size_;
}

size_t rc2014Buffer::data_size()
{
return buffer_.size();
}

void rc2014Buffer::append(uint8_t val)
{
buffer_.back() = val;
buffer_.push_back(rc2014_checksum(buffer_.data(), buffer_.size() - 1));
}

void virtualDevice::rc2014_send(uint8_t b)
{
rc2014Bus.busTxByte(b);
}

void virtualDevice::rc2014_send(rc2014Buffer& buffer)
{
for (int i = 0; i < buffer.data_size(); i++) {
rc2014Bus.busTxByte(buffer.data()[i]);
}
}


void virtualDevice::rc2014_send_string(const std::string& str)
{
for (auto& c: str) {
Expand Down Expand Up @@ -239,7 +185,8 @@ void virtualDevice::rc2014_response_status()

void virtualDevice::rc2014_handle_stream()
{
//fnUartBUS.flush_input();
// flush fifo
streamFifoRx.clear();
}

bool virtualDevice::rc2014_poll_interrupt()
Expand Down
106 changes: 93 additions & 13 deletions lib/bus/rc2014bus/rc2014bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#include <vector>
#include <deque>
#include <mutex>
#include <string>
#include <vector>

#include <map>

Expand Down Expand Up @@ -55,20 +57,95 @@ union cmdFrame_t

// buffer with inbuilt checksum
// -- instantiate with desired buffer size + 1 for checksum
class rc2014Buffer {
template <size_t BUFFER_SIZE>
class rc2014Fifo {
public:
explicit rc2014Buffer(size_t size);
rc2014Fifo() { clear(); };

bool validate();
uint8_t* data();
size_t max_size();
size_t data_size();
void append(uint8_t val);

protected:
std::vector<uint8_t> buffer_;
size_t size_;
bool dirty_;
bool is_empty() {
std::scoped_lock<std::mutex> lock(_m);
return (_i_head == _i_tail);
};

bool is_full() {
std::scoped_lock<std::mutex> lock(_m);
return ((_i_head + 1) % BUFFER_SIZE) == _i_tail;
};

bool is_overrun() {
std::scoped_lock<std::mutex> lock(_m);
return _overrun;
};

bool is_underrun() {
std::scoped_lock<std::mutex> lock(_m);
return _underrun;
};

void push(uint8_t val) {
if (is_full()) {
_overrun = true;
return;
}

std::scoped_lock<std::mutex> lock(_m);
_fifo[_i_head] = val;
_i_head = (_i_head + 1) % BUFFER_SIZE;
};

void push(uint8_t* buffer, size_t len) {
for (size_t i = 0; i < len; i++)
push(buffer[i]);
}

void push(const char* str) {
push(std::string(str));
}

void push(const std::string& str) {
for (auto &ch : str)
push(ch);
}

uint8_t pop() {
if (is_empty()) {
_overrun = true;
return 0xff;
}

std::scoped_lock<std::mutex> lock(_m);
uint8_t val = _fifo[_i_tail];
_i_tail = (_i_tail + 1) % BUFFER_SIZE;
return val;
};

void pop(uint8_t* buffer, size_t len) {
for (size_t i = 0; i < len; i++)
buffer[i] = pop();
}

void clear() {
std::scoped_lock<std::mutex> lock(_m);
_i_tail = _i_head = 0;
_overrun = _underrun = false;
};

size_t avail() {
std::scoped_lock<std::mutex> lock(_m);
if (_i_head == _i_tail)
return 0; // Empty buffer

return (_i_head - _i_tail + BUFFER_SIZE) % BUFFER_SIZE;
}

private:
std::mutex _m;
std::array<uint8_t, BUFFER_SIZE> _fifo;
unsigned _i_tail;
unsigned _i_head;
bool _overrun;
bool _underrun;
};

class systemBus;
Expand Down Expand Up @@ -99,7 +176,6 @@ class virtualDevice
* @return none
*/
void rc2014_send(uint8_t b);
void rc2014_send(rc2014Buffer& buffer);

/**
* @brief Send string buffer to rc2014
Expand Down Expand Up @@ -277,6 +353,10 @@ class virtualDevice
* @return the device # (0-15) of this device
*/
uint8_t id() { return _devnum; }

protected:
rc2014Fifo<1024> streamFifoTx; // streamed data from rc2014
rc2014Fifo<1024> streamFifoRx; // streamed data destined for rc2014
};

/**
Expand Down
Loading

0 comments on commit 9ac36e8

Please sign in to comment.