SerialLibrary for arrc
Revision 0:4801795a9073, committed 2021-12-11
- Comitter:
- hamohamo
- Date:
- Sat Dec 11 02:37:36 2021 +0000
- Commit message:
- SerialLibrary;
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Buffer52.hpp Sat Dec 11 02:37:36 2021 +0000 @@ -0,0 +1,32 @@ +#ifndef _52BUFFER +#define _52BUFFER + +#include <vector> + +namespace ARRC{ +template <class type> class Buffer{ +public: + Buffer(unsigned size):buf(size,0),size(size),read_ref(0),write_ref(0){} + type read(){ + type ret = 0; + if(read_ref < write_ref){ + ret = buf[read_ref % size]; + read_ref++; + } + return ret; + } + void write(type elem){ + buf[write_ref % size] = elem; + write_ref++; + } + bool readable(){return (read_ref < write_ref);} + int datacount(){return write_ref - read_ref;} +private: +std::vector<type> buf; +long unsigned read_ref,write_ref; +long unsigned size; +}; + +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/goto_serial.cpp Sat Dec 11 02:37:36 2021 +0000 @@ -0,0 +1,104 @@ +#include "goto_serial.hpp" + +namespace ARRC{ + +gotoSerial::gotoSerial(PinName tx,PinName rx,int baudrate):ser(tx,rx,baudrate),sub_vars_size(0),sub_vars(0),buf(256){ + ser.attach(callback(this,&gotoSerial::interrupt_read),Serial::RxIrq); +} +bool gotoSerial::publish(unsigned id,int num,unsigned type){ + Pack byte; + byte.integer = num; + int8_t _1_byte = (byte.integer >> 24) & 0xFF; + uint8_t _2_byte = (byte.integer >> 16) & 0xFF; + uint8_t _3_byte = (byte.integer >> 8) & 0xFF; + uint8_t _4_byte = (byte.integer >> 0) & 0xFF; + int8_t sum = id+type+_1_byte+_2_byte+_3_byte+_4_byte; + int32_t numsum = (_1_byte << 24) + (_2_byte<<16) + (_3_byte << 8) + _4_byte; + ser.putc(STARTDATA); + ser.putc(id); + ser.putc(type); + ser.putc(_1_byte); + ser.putc(_2_byte); + ser.putc(_3_byte); + ser.putc(_4_byte); + ser.putc(sum); + ser.putc(ENDDATA); + while(!ser.writeable()); + return true; +} +bool gotoSerial::publish(unsigned id,float num,unsigned type){ + Pack byte; + byte.decimal = num; + uint8_t _1_byte = (byte.integer >> 24) & 0xFF; + uint8_t _2_byte = (byte.integer >> 16) & 0xFF; + uint8_t _3_byte = (byte.integer >> 8) & 0xFF; + uint8_t _4_byte = (byte.integer >> 0) & 0xFF; + uint8_t sum = id+type+_1_byte+_2_byte+_3_byte+_4_byte; + int32_t numsum = (_1_byte << 24) + (_2_byte<<16) + (_3_byte << 8) + _4_byte; + ser.putc(STARTDATA); + ser.putc(id); + ser.putc(type); + ser.putc(_1_byte); + ser.putc(_2_byte); + ser.putc(_3_byte); + ser.putc(_4_byte); + ser.putc(sum); + ser.putc(ENDDATA); + while(!ser.writeable()); + return true; +} +bool gotoSerial::subscribe(unsigned id,int* var){ + if(sub_vars_size < id) sub_vars_size = id; + sub_vars.resize(sub_vars_size+1); + sub_vars.at(id).integer = var; + return true; +} +bool gotoSerial::subscribe(unsigned id,float* var){ + if(sub_vars_size < id) sub_vars_size = id; + sub_vars.resize(sub_vars_size+1); + sub_vars.at(id).decimal = var; + return true; +} +bool gotoSerial::subscribe(unsigned id,Func func){ + if(sub_vars_size < id) sub_vars_size = id; + sub_funcs.resize(sub_vars_size+1); + sub_funcs.at(id) = func; + return true; +} +void gotoSerial::interrupt_read(){ + int8_t byte; + while(ser.readable()){ + byte = ser.getc(); + buf.write(byte); + if(byte == ENDDATA) while(buf.readable()) update(); + } +} +bool gotoSerial::update(){ + if(buf.readable()){ + int id = 0; + int type = 0; + if(buf.read() == STARTDATA){ + id = buf.read(); + type = buf.read(); + if(0 <= id and id <= sub_vars_size){ + uint8_t _1_byte = buf.read(); + uint8_t _2_byte = buf.read(); + uint8_t _3_byte = buf.read(); + uint8_t _4_byte = buf.read(); + uint8_t sum = id+type+_1_byte+_2_byte+_3_byte+_4_byte; + if(buf.read() == sum) { + Pack byte; + byte.integer = ((int32_t)_1_byte << 24) + ((int32_t)_2_byte<<16) + ((int32_t)_3_byte << 8) + (int32_t)_4_byte; + if(type == INT) *sub_vars.at(id).integer = byte.integer; + else if(type == FLOAT) *sub_vars.at(id).decimal = byte.decimal; + else if(type == FUNC) (*(sub_funcs.at(id)))(byte); + } + } + else for(int i = 0;i < 5;i++) buf.read(); + } + } + return true; +} + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/goto_serial.hpp Sat Dec 11 02:37:36 2021 +0000 @@ -0,0 +1,70 @@ +#ifndef _52SERIAL +#define _52SERIAL + +#include "mbed.h" +#include "Buffer52.hpp" +/* +使用方法 +ARRC::gotoSerial ser(PC_12,PD_2,9600); +DigitalOut rede(PH_1); +送信側 rede = 1; +受信側 rede = 0; + +publish(id,送信する値,INT or FLOAT or FUNC(送信する型の指定)); +INT,FLOAT 送信と受信でなるべく合わせること。 +FUNC 受信側で設定した関数に値を引数として送信する。 +subscribe(id,変数または関数のポインタ(例 &value , func)); + +受信側の関数(scrp_slaveでいうところのコマンド)の宣言方法 + +void func(Pack data){ + printf("subscribed_int:%d\n",data.integer); + printf("subscribed_float:%d\n",data.decimal); + led = !led; +} +ToDo +redeを組み込む +送受信可能にする +*/ + +namespace ARRC{ + +const unsigned STARTDATA = 0x01; +const unsigned ENDDATA = 0x09; +const unsigned INT = 1; +const unsigned FLOAT = 2; +const unsigned FUNC = 3; + +typedef union { + float decimal; + int integer; +} Pack; + +typedef void (*Func)(Pack data); + +typedef union { + float* decimal; + int* integer; +} DATA; + +class gotoSerial{ +public: + gotoSerial(PinName tx,PinName rx,int baudrate); + bool publish(unsigned id,int num,unsigned type); + bool publish(unsigned id,float num,unsigned type); + bool subscribe(unsigned id,int* var); + bool subscribe(unsigned id,float* var); + bool subscribe(unsigned id,Func func); +private: + void interrupt_read(); + bool update(); + int sub_vars_size; + std::vector<DATA> sub_vars; + std::vector<Func> sub_funcs; + Buffer<int8_t> buf; + Serial ser; +}; + +} + +#endif