-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eecae63
commit 8281eee
Showing
2 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
}; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
|
||
} | ||
|
||
} |
8281eee
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
スパゲッティになりそうなのでパケットの分割はやめるかも