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@0:1779b0a23b09, 2021-04-29 (annotated)
- Committer:
- e2011220
- Date:
- Thu Apr 29 03:16:39 2021 +0000
- Revision:
- 0:1779b0a23b09
- Child:
- 1:a9cd302df0c5
f
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| e2011220 | 0:1779b0a23b09 | 1 | /*--------------------------- |
| e2011220 | 0:1779b0a23b09 | 2 | |
| e2011220 | 0:1779b0a23b09 | 3 | Author:Issaimaru |
| e2011220 | 0:1779b0a23b09 | 4 | |
| e2011220 | 0:1779b0a23b09 | 5 | ----------------------------*/ |
| e2011220 | 0:1779b0a23b09 | 6 | #ifndef SERIAL_WRITER |
| e2011220 | 0:1779b0a23b09 | 7 | #define SERIAL_WRITER |
| e2011220 | 0:1779b0a23b09 | 8 | #include <mbed.h> |
| e2011220 | 0:1779b0a23b09 | 9 | class Serial_Writer |
| e2011220 | 0:1779b0a23b09 | 10 | { |
| e2011220 | 0:1779b0a23b09 | 11 | public: |
| e2011220 | 0:1779b0a23b09 | 12 | Serial_Writer(PinName TxPin,PinName RxPin,int baudrate); |
| e2011220 | 0:1779b0a23b09 | 13 | |
| e2011220 | 0:1779b0a23b09 | 14 | template <typename T> |
| e2011220 | 0:1779b0a23b09 | 15 | |
| e2011220 | 0:1779b0a23b09 | 16 | inline void write(T &send,int delay){ |
| e2011220 | 0:1779b0a23b09 | 17 | int num=sizeof(send); |
| e2011220 | 0:1779b0a23b09 | 18 | char buffer[num]; |
| e2011220 | 0:1779b0a23b09 | 19 | for (int i=0,k=0;i<num;k++){ |
| e2011220 | 0:1779b0a23b09 | 20 | for(int _bitNum=sizeof(send[0])-1;_bitNum>=0;i++,_bitNum--)buffer[i]=(send[k]>>(8*_bitNum))&0xFF; |
| e2011220 | 0:1779b0a23b09 | 21 | } |
| e2011220 | 0:1779b0a23b09 | 22 | _Serial.putc('['); |
| e2011220 | 0:1779b0a23b09 | 23 | _Serial.putc(uint8_t(num)&0xFF); |
| e2011220 | 0:1779b0a23b09 | 24 | _Serial.putc(uint8_t(sizeof(send[0]))&0xFF); |
| e2011220 | 0:1779b0a23b09 | 25 | for (int p=0;p<num;p++)_Serial.putc(buffer[p]); |
| e2011220 | 0:1779b0a23b09 | 26 | _Serial.putc(']'); |
| e2011220 | 0:1779b0a23b09 | 27 | wait_ms(delay); |
| e2011220 | 0:1779b0a23b09 | 28 | } |
| e2011220 | 0:1779b0a23b09 | 29 | |
| e2011220 | 0:1779b0a23b09 | 30 | template <typename R> |
| e2011220 | 0:1779b0a23b09 | 31 | inline int receive(R &get){//getの各要素に0x0を必ず代入してください。 |
| e2011220 | 0:1779b0a23b09 | 32 | if (_Serial.readable()&&_Serial.getc()=='['){ |
| e2011220 | 0:1779b0a23b09 | 33 | uint8_t num=_Serial.getc(); |
| e2011220 | 0:1779b0a23b09 | 34 | uint8_t bits=_Serial.getc(); |
| e2011220 | 0:1779b0a23b09 | 35 | if !(num%bits)return; |
| e2011220 | 0:1779b0a23b09 | 36 | char buffer[num]; |
| e2011220 | 0:1779b0a23b09 | 37 | for (int i=0;i<num;i++)buffer[i]=_Serial.getc(); |
| e2011220 | 0:1779b0a23b09 | 38 | if (_Serial.getc()=']'){ |
| e2011220 | 0:1779b0a23b09 | 39 | for (int p=0,k=0;p<num/bits;p++){ |
| e2011220 | 0:1779b0a23b09 | 40 | for(uint8_t _bits=bits-1;_bits>=0;k++,_bits--)get[p]|=buffer[k]<<(8*_bits); |
| e2011220 | 0:1779b0a23b09 | 41 | } |
| e2011220 | 0:1779b0a23b09 | 42 | return 1; |
| e2011220 | 0:1779b0a23b09 | 43 | } |
| e2011220 | 0:1779b0a23b09 | 44 | return -1; |
| e2011220 | 0:1779b0a23b09 | 45 | } |
| e2011220 | 0:1779b0a23b09 | 46 | return 0; |
| e2011220 | 0:1779b0a23b09 | 47 | } |
| e2011220 | 0:1779b0a23b09 | 48 | |
| e2011220 | 0:1779b0a23b09 | 49 | private: |
| e2011220 | 0:1779b0a23b09 | 50 | Serial _Serial; |
| e2011220 | 0:1779b0a23b09 | 51 | }; |
| e2011220 | 0:1779b0a23b09 | 52 | |
| e2011220 | 0:1779b0a23b09 | 53 | #endif |