diff --git a/raspberry_pi/bb_UART/include/UART.hpp b/raspberry_pi/bb_UART/include/UART.hpp new file mode 100644 index 0000000..e8ac039 --- /dev/null +++ b/raspberry_pi/bb_UART/include/UART.hpp @@ -0,0 +1,69 @@ +#pragma once + +#include +#include +#include + +// +// packet +// +// name : bit +// header ACK : 11xxxxxx (x: sequence_num) +// data : 00xxxxxx (x: sequence_num) +// +// payload_size : xxxxxxxx +// +// hash : xxxxyyyy (x: header's hash, y: payload's hash) +// +// payload : xxxxxxxx +// : xxxxxxxx +// ~~~~~~~~ +// : xxxxxxxx +// + +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 uart_conf + { + int TX = -1; // TX pin + int RX = -1; // RX pin + int BAUD = 9600; + int DATA_BIT_LEN = 8; // bits + int MAX_PAYLOAD_SIZE= 32; // ペイロードサイズ(1-255bytes) + int STOP_BIT = 2; + int OFFSET = 0; + bool ENABLE_RESEND = true; // 再送 + }; + + class UART + { + private: + const uart_conf conf; + uint8_t sequence_num; + std::queue transmit_queue; + + public: + UART(uart_conf&); + virtual uart_err_t initialize(); + // パケット送信 + virtual int thrower(const uint8_t*); + // パケット作成→キュー追加 + template uart_err_t transmit(const T&); + virtual int receive(); + uart_err_t get_data(); + + }; + +} + diff --git a/raspberry_pi/bb_UART/src/UART.cpp b/raspberry_pi/bb_UART/src/UART.cpp new file mode 100644 index 0000000..86a724f --- /dev/null +++ b/raspberry_pi/bb_UART/src/UART.cpp @@ -0,0 +1,47 @@ +#include "UART.hpp" + +namespace uart +{ + UART::UART(uart_conf& conf_) : conf(conf_), sequence_num(0) {} + + template + uart_err_t UART::transmit(const T &buf_) + { + if (transmit_queue.size() + (int)sizeof(T) > 1024) { + return(UART_BUFFER_OVER); + + } else { + std::unique_ptr buf(new T(1)); + buf = (uint8_t*)&buf_; + + // キュー追加、dataサイズがペイロード最大サイズを超える場合は分割 + int j = 0; + for (int i=0; i<(float)sizeof(T)/conf.MAX_PAYLOAD_SIZE; i++) { + const uint8_t header = sequence_num & 0b00111111; + transmit_queue.push(header); + if (sequence_num == 63) { + sequence_num = 0; + } else { + sequence_num++; + } + for (j; j