SerialLibrary for arrc

Committer:
hamohamo
Date:
Sat Dec 11 02:37:36 2021 +0000
Revision:
0:4801795a9073
SerialLibrary;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hamohamo 0:4801795a9073 1 #include "goto_serial.hpp"
hamohamo 0:4801795a9073 2
hamohamo 0:4801795a9073 3 namespace ARRC{
hamohamo 0:4801795a9073 4
hamohamo 0:4801795a9073 5 gotoSerial::gotoSerial(PinName tx,PinName rx,int baudrate):ser(tx,rx,baudrate),sub_vars_size(0),sub_vars(0),buf(256){
hamohamo 0:4801795a9073 6 ser.attach(callback(this,&gotoSerial::interrupt_read),Serial::RxIrq);
hamohamo 0:4801795a9073 7 }
hamohamo 0:4801795a9073 8 bool gotoSerial::publish(unsigned id,int num,unsigned type){
hamohamo 0:4801795a9073 9 Pack byte;
hamohamo 0:4801795a9073 10 byte.integer = num;
hamohamo 0:4801795a9073 11 int8_t _1_byte = (byte.integer >> 24) & 0xFF;
hamohamo 0:4801795a9073 12 uint8_t _2_byte = (byte.integer >> 16) & 0xFF;
hamohamo 0:4801795a9073 13 uint8_t _3_byte = (byte.integer >> 8) & 0xFF;
hamohamo 0:4801795a9073 14 uint8_t _4_byte = (byte.integer >> 0) & 0xFF;
hamohamo 0:4801795a9073 15 int8_t sum = id+type+_1_byte+_2_byte+_3_byte+_4_byte;
hamohamo 0:4801795a9073 16 int32_t numsum = (_1_byte << 24) + (_2_byte<<16) + (_3_byte << 8) + _4_byte;
hamohamo 0:4801795a9073 17 ser.putc(STARTDATA);
hamohamo 0:4801795a9073 18 ser.putc(id);
hamohamo 0:4801795a9073 19 ser.putc(type);
hamohamo 0:4801795a9073 20 ser.putc(_1_byte);
hamohamo 0:4801795a9073 21 ser.putc(_2_byte);
hamohamo 0:4801795a9073 22 ser.putc(_3_byte);
hamohamo 0:4801795a9073 23 ser.putc(_4_byte);
hamohamo 0:4801795a9073 24 ser.putc(sum);
hamohamo 0:4801795a9073 25 ser.putc(ENDDATA);
hamohamo 0:4801795a9073 26 while(!ser.writeable());
hamohamo 0:4801795a9073 27 return true;
hamohamo 0:4801795a9073 28 }
hamohamo 0:4801795a9073 29 bool gotoSerial::publish(unsigned id,float num,unsigned type){
hamohamo 0:4801795a9073 30 Pack byte;
hamohamo 0:4801795a9073 31 byte.decimal = num;
hamohamo 0:4801795a9073 32 uint8_t _1_byte = (byte.integer >> 24) & 0xFF;
hamohamo 0:4801795a9073 33 uint8_t _2_byte = (byte.integer >> 16) & 0xFF;
hamohamo 0:4801795a9073 34 uint8_t _3_byte = (byte.integer >> 8) & 0xFF;
hamohamo 0:4801795a9073 35 uint8_t _4_byte = (byte.integer >> 0) & 0xFF;
hamohamo 0:4801795a9073 36 uint8_t sum = id+type+_1_byte+_2_byte+_3_byte+_4_byte;
hamohamo 0:4801795a9073 37 int32_t numsum = (_1_byte << 24) + (_2_byte<<16) + (_3_byte << 8) + _4_byte;
hamohamo 0:4801795a9073 38 ser.putc(STARTDATA);
hamohamo 0:4801795a9073 39 ser.putc(id);
hamohamo 0:4801795a9073 40 ser.putc(type);
hamohamo 0:4801795a9073 41 ser.putc(_1_byte);
hamohamo 0:4801795a9073 42 ser.putc(_2_byte);
hamohamo 0:4801795a9073 43 ser.putc(_3_byte);
hamohamo 0:4801795a9073 44 ser.putc(_4_byte);
hamohamo 0:4801795a9073 45 ser.putc(sum);
hamohamo 0:4801795a9073 46 ser.putc(ENDDATA);
hamohamo 0:4801795a9073 47 while(!ser.writeable());
hamohamo 0:4801795a9073 48 return true;
hamohamo 0:4801795a9073 49 }
hamohamo 0:4801795a9073 50 bool gotoSerial::subscribe(unsigned id,int* var){
hamohamo 0:4801795a9073 51 if(sub_vars_size < id) sub_vars_size = id;
hamohamo 0:4801795a9073 52 sub_vars.resize(sub_vars_size+1);
hamohamo 0:4801795a9073 53 sub_vars.at(id).integer = var;
hamohamo 0:4801795a9073 54 return true;
hamohamo 0:4801795a9073 55 }
hamohamo 0:4801795a9073 56 bool gotoSerial::subscribe(unsigned id,float* var){
hamohamo 0:4801795a9073 57 if(sub_vars_size < id) sub_vars_size = id;
hamohamo 0:4801795a9073 58 sub_vars.resize(sub_vars_size+1);
hamohamo 0:4801795a9073 59 sub_vars.at(id).decimal = var;
hamohamo 0:4801795a9073 60 return true;
hamohamo 0:4801795a9073 61 }
hamohamo 0:4801795a9073 62 bool gotoSerial::subscribe(unsigned id,Func func){
hamohamo 0:4801795a9073 63 if(sub_vars_size < id) sub_vars_size = id;
hamohamo 0:4801795a9073 64 sub_funcs.resize(sub_vars_size+1);
hamohamo 0:4801795a9073 65 sub_funcs.at(id) = func;
hamohamo 0:4801795a9073 66 return true;
hamohamo 0:4801795a9073 67 }
hamohamo 0:4801795a9073 68 void gotoSerial::interrupt_read(){
hamohamo 0:4801795a9073 69 int8_t byte;
hamohamo 0:4801795a9073 70 while(ser.readable()){
hamohamo 0:4801795a9073 71 byte = ser.getc();
hamohamo 0:4801795a9073 72 buf.write(byte);
hamohamo 0:4801795a9073 73 if(byte == ENDDATA) while(buf.readable()) update();
hamohamo 0:4801795a9073 74 }
hamohamo 0:4801795a9073 75 }
hamohamo 0:4801795a9073 76 bool gotoSerial::update(){
hamohamo 0:4801795a9073 77 if(buf.readable()){
hamohamo 0:4801795a9073 78 int id = 0;
hamohamo 0:4801795a9073 79 int type = 0;
hamohamo 0:4801795a9073 80 if(buf.read() == STARTDATA){
hamohamo 0:4801795a9073 81 id = buf.read();
hamohamo 0:4801795a9073 82 type = buf.read();
hamohamo 0:4801795a9073 83 if(0 <= id and id <= sub_vars_size){
hamohamo 0:4801795a9073 84 uint8_t _1_byte = buf.read();
hamohamo 0:4801795a9073 85 uint8_t _2_byte = buf.read();
hamohamo 0:4801795a9073 86 uint8_t _3_byte = buf.read();
hamohamo 0:4801795a9073 87 uint8_t _4_byte = buf.read();
hamohamo 0:4801795a9073 88 uint8_t sum = id+type+_1_byte+_2_byte+_3_byte+_4_byte;
hamohamo 0:4801795a9073 89 if(buf.read() == sum) {
hamohamo 0:4801795a9073 90 Pack byte;
hamohamo 0:4801795a9073 91 byte.integer = ((int32_t)_1_byte << 24) + ((int32_t)_2_byte<<16) + ((int32_t)_3_byte << 8) + (int32_t)_4_byte;
hamohamo 0:4801795a9073 92 if(type == INT) *sub_vars.at(id).integer = byte.integer;
hamohamo 0:4801795a9073 93 else if(type == FLOAT) *sub_vars.at(id).decimal = byte.decimal;
hamohamo 0:4801795a9073 94 else if(type == FUNC) (*(sub_funcs.at(id)))(byte);
hamohamo 0:4801795a9073 95 }
hamohamo 0:4801795a9073 96 }
hamohamo 0:4801795a9073 97 else for(int i = 0;i < 5;i++) buf.read();
hamohamo 0:4801795a9073 98 }
hamohamo 0:4801795a9073 99 }
hamohamo 0:4801795a9073 100 return true;
hamohamo 0:4801795a9073 101 }
hamohamo 0:4801795a9073 102
hamohamo 0:4801795a9073 103 }
hamohamo 0:4801795a9073 104