Skip to content

Commit

Permalink
とりあえずパケット生成
Browse files Browse the repository at this point in the history
  • Loading branch information
RyosukeSasaki committed Sep 21, 2019
1 parent eecae63 commit 8281eee
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
69 changes: 69 additions & 0 deletions raspberry_pi/bb_UART/include/UART.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#pragma once

#include <cstdint>
#include <queue>
#include <memory>

//
// 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<uint8_t> transmit_queue;

public:
UART(uart_conf&);
virtual uart_err_t initialize();
// パケット送信
virtual int thrower(const uint8_t*);
// パケット作成→キュー追加
template <class T> uart_err_t transmit(const T&);
virtual int receive();
uart_err_t get_data();

};

}

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

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

template <class T>
uart_err_t UART::transmit(const T &buf_)
{
if (transmit_queue.size() + (int)sizeof(T) > 1024) {
return(UART_BUFFER_OVER);

} else {
std::unique_ptr<uint8_t> 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<conf.MAX_PAYLOAD_SIZE*(i+1); j++){
if (j < (int)sizeof(T)){
transmit_queue.push(*buf[j]);
} else {
break;
}

}

}

}

}

uart_err_t UART::get_data()
{

}

}

1 comment on commit 8281eee

@RyosukeSasaki
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

スパゲッティになりそうなのでパケットの分割はやめるかも

Please sign in to comment.