Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Serial_Writer.h
- Committer:
- e2011220
- Date:
- 2021-04-29
- Revision:
- 0:1779b0a23b09
- Child:
- 1:a9cd302df0c5
File content as of revision 0:1779b0a23b09:
/*---------------------------
Author:Issaimaru
----------------------------*/
#ifndef SERIAL_WRITER
#define SERIAL_WRITER
#include <mbed.h>
class Serial_Writer
{
public:
Serial_Writer(PinName TxPin,PinName RxPin,int baudrate);
template <typename T>
inline void write(T &send,int delay){
int num=sizeof(send);
char buffer[num];
for (int i=0,k=0;i<num;k++){
for(int _bitNum=sizeof(send[0])-1;_bitNum>=0;i++,_bitNum--)buffer[i]=(send[k]>>(8*_bitNum))&0xFF;
}
_Serial.putc('[');
_Serial.putc(uint8_t(num)&0xFF);
_Serial.putc(uint8_t(sizeof(send[0]))&0xFF);
for (int p=0;p<num;p++)_Serial.putc(buffer[p]);
_Serial.putc(']');
wait_ms(delay);
}
template <typename R>
inline int receive(R &get){//getの各要素に0x0を必ず代入してください。
if (_Serial.readable()&&_Serial.getc()=='['){
uint8_t num=_Serial.getc();
uint8_t bits=_Serial.getc();
if !(num%bits)return;
char buffer[num];
for (int i=0;i<num;i++)buffer[i]=_Serial.getc();
if (_Serial.getc()=']'){
for (int p=0,k=0;p<num/bits;p++){
for(uint8_t _bits=bits-1;_bits>=0;k++,_bits--)get[p]|=buffer[k]<<(8*_bits);
}
return 1;
}
return -1;
}
return 0;
}
private:
Serial _Serial;
};
#endif