Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bb uart #20

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
13 changes: 13 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pigpio = "*"

[dev-packages]
pylint = "*"

[requires]
python_version = "3.7"
116 changes: 116 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions share/bb_UART/bb_uart_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pigpio

pi = pigpio.pi()
try:
pi.bb_serial_read_close(17)
status = pi.bb_serial_read_open(17, 9600)
while True:
count, data = pi.bb_serial_read(17)
while count:
print(data)

finally:
pi.bb_serial_read_close(17)
71 changes: 71 additions & 0 deletions share/bb_UART/include/UART.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <cstdint>
#include <queue>
#include <memory>
#include <mutex>
#include <numeric>

namespace uart
{
enum uart_err_t
{
UART_SUCCEEDED = 0,
UART_ERROR_UNKNOWN = -1,
UART_INIT_FAILED = -2,
UART_TRANSMIT_FAILED = -3,
UART_RECEIVE_FAILED = -4,
UART_HASH_NOT_MATCH = -5,
UART_BUFFER_OVER = -6
};

struct pdu
{
struct header
{
uint8_t seq_num;
uint8_t payload_size;
uint8_t checksum;
};
struct data
{
uint8_t type;
uint8_t* data;
uint8_t checksum;
};
};

struct uart_conf
{
int TX = -1; // TX pin
int RX = -1; // RX pin
int BAUD = 9600;
int DATA_BIT_LEN = 8; // bits
int STOP_BIT = 2;
int OFFSET = 0;
bool ENABLE_RESEND = true; // 再送
bool ENABLE_ECC = false;
};

class UART
{
private:
const uart_conf conf;
uint8_t sequence_num;
std::queue<uint8_t> transmit_queue;
std::mutex lock;

public:
UART(uart_conf&);
virtual uart_err_t initialize();
virtual int thrower(const char*);
// Generate Protocol Data Unit
template <class T> uart_err_t create_pdu(const char&, const T&);
template <class T> void calc_parity(char*, const T&, size_t&);
virtual int receive();
uart_err_t get_data();

};

}

50 changes: 50 additions & 0 deletions share/bb_UART/src/UART.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "UART.hpp"

namespace uart
{
UART::UART(uart_conf& conf_) : conf(conf_), sequence_num(0) {}

template <class T>
uart_err_t UART::create_pdu(const char &type, const T &data_)
{
size_t size = sizeof(T)
std::unique_ptr<pdu> buf(new pdu);

(*buf).data.type = type;

if (conf.ENABLE_ECC == false){
(*buf).data.data = (char*)&data_;
} else {
calc_parity((*buf).data.data, data_, size);
}

(*buf).data.checksum = std::accumulate((*buf).data.data,
(*buf).data.data+(uint8_t)size, 0) / (uint8_t)size;

(*buf).header.payload_size = (char)size;

lock.lock();
(*buf).header.seq_num = sequence_num;
if (sequence_num == 255) {
sequence_num = 0;
} else {
sequence_num++;
}
lock.unlock();

(*buf).header.checksum = ((*buf).header.seq_num + (*buf).header.payload_size) / 2;

}

template<class T>
void UART::calc_parity(char* buf, const T &data_, size_t &size)
{

}

uart_err_t UART::get_data()
{

}

}