Tetsuya Yamamoto / Mbed 2 deprecated MD-Tutorial

Dependencies:   mbed

Committer:
tetsu_0207
Date:
Sat Nov 28 07:48:16 2020 +0000
Revision:
6:17e3a28338dc
Child:
7:0bc2bc07f2fe
[Debug]

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tetsu_0207 6:17e3a28338dc 1 #include "BufferSerial.h"
tetsu_0207 6:17e3a28338dc 2
tetsu_0207 6:17e3a28338dc 3 Serial pc(USBTX, USBRX);
tetsu_0207 6:17e3a28338dc 4
tetsu_0207 6:17e3a28338dc 5 BufferSerial::BufferSerial(PinName tx, PinName rx,int baud,int timeout)
tetsu_0207 6:17e3a28338dc 6 :RawSerial(tx,rx,baud)
tetsu_0207 6:17e3a28338dc 7 {
tetsu_0207 6:17e3a28338dc 8 // initialize functions
tetsu_0207 6:17e3a28338dc 9 _rx_buffer_head = 0;
tetsu_0207 6:17e3a28338dc 10 _rx_buffer_tail = 0;
tetsu_0207 6:17e3a28338dc 11 // time out
tetsu_0207 6:17e3a28338dc 12 timer_ms = timeout;
tetsu_0207 6:17e3a28338dc 13 timer.start();
tetsu_0207 6:17e3a28338dc 14 // attach
tetsu_0207 6:17e3a28338dc 15 RawSerial::attach(callback(this,&BufferSerial::serial_rx), Serial::RxIrq);
tetsu_0207 6:17e3a28338dc 16 }
tetsu_0207 6:17e3a28338dc 17
tetsu_0207 6:17e3a28338dc 18 int BufferSerial::available(void)
tetsu_0207 6:17e3a28338dc 19 {
tetsu_0207 6:17e3a28338dc 20 return ((unsigned int)(SERIAL_RX_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail)) % SERIAL_RX_BUFFER_SIZE;
tetsu_0207 6:17e3a28338dc 21 }
tetsu_0207 6:17e3a28338dc 22
tetsu_0207 6:17e3a28338dc 23 int BufferSerial::read(void)
tetsu_0207 6:17e3a28338dc 24 {
tetsu_0207 6:17e3a28338dc 25 if (_rx_buffer_head == _rx_buffer_tail) {
tetsu_0207 6:17e3a28338dc 26 return -1;
tetsu_0207 6:17e3a28338dc 27 } else {
tetsu_0207 6:17e3a28338dc 28 unsigned char c = _rx_buffer[_rx_buffer_tail];
tetsu_0207 6:17e3a28338dc 29 _rx_buffer_tail = (rx_buffer_index_t)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE;
tetsu_0207 6:17e3a28338dc 30 return c;
tetsu_0207 6:17e3a28338dc 31 }
tetsu_0207 6:17e3a28338dc 32 }
tetsu_0207 6:17e3a28338dc 33
tetsu_0207 6:17e3a28338dc 34 size_t BufferSerial::readBytesUntil(char charactor,char* buffer,int length)
tetsu_0207 6:17e3a28338dc 35 {
tetsu_0207 6:17e3a28338dc 36 size_t index = 0;
tetsu_0207 6:17e3a28338dc 37 while (index < length) {
tetsu_0207 6:17e3a28338dc 38 int c = timedRead();
tetsu_0207 6:17e3a28338dc 39 //pc.printf("debug_c: %x\n\r",c);
tetsu_0207 6:17e3a28338dc 40 if (c < 0 || c == charactor){
tetsu_0207 6:17e3a28338dc 41 //pc.printf("break index:%d \n\r",index);
tetsu_0207 6:17e3a28338dc 42 break;
tetsu_0207 6:17e3a28338dc 43 }
tetsu_0207 6:17e3a28338dc 44 *buffer++ = (char)c;
tetsu_0207 6:17e3a28338dc 45 index++;
tetsu_0207 6:17e3a28338dc 46 }
tetsu_0207 6:17e3a28338dc 47 return index;
tetsu_0207 6:17e3a28338dc 48 }
tetsu_0207 6:17e3a28338dc 49
tetsu_0207 6:17e3a28338dc 50 void BufferSerial::serial_rx(void)
tetsu_0207 6:17e3a28338dc 51 {
tetsu_0207 6:17e3a28338dc 52 while(RawSerial::readable()) {
tetsu_0207 6:17e3a28338dc 53 unsigned char c = RawSerial::getc();
tetsu_0207 6:17e3a28338dc 54 rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE;
tetsu_0207 6:17e3a28338dc 55 if (i != _rx_buffer_tail) {
tetsu_0207 6:17e3a28338dc 56 _rx_buffer[_rx_buffer_head] = c;
tetsu_0207 6:17e3a28338dc 57 _rx_buffer_head = i;
tetsu_0207 6:17e3a28338dc 58 //pc.printf("%x\n\r",c);
tetsu_0207 6:17e3a28338dc 59 }
tetsu_0207 6:17e3a28338dc 60 }
tetsu_0207 6:17e3a28338dc 61 }
tetsu_0207 6:17e3a28338dc 62
tetsu_0207 6:17e3a28338dc 63 int BufferSerial::timedRead()
tetsu_0207 6:17e3a28338dc 64 {
tetsu_0207 6:17e3a28338dc 65 int c;
tetsu_0207 6:17e3a28338dc 66 timer.reset();
tetsu_0207 6:17e3a28338dc 67 _startMillis = timer.read_ms();
tetsu_0207 6:17e3a28338dc 68 do {
tetsu_0207 6:17e3a28338dc 69 c = read();
tetsu_0207 6:17e3a28338dc 70 pc.printf("tr: %x \n\r",c);
tetsu_0207 6:17e3a28338dc 71 if (c >= 0) return c;
tetsu_0207 6:17e3a28338dc 72 } while(timer.read_ms() - _startMillis < _timeout);
tetsu_0207 6:17e3a28338dc 73 return -1; // -1 indicates timeout
tetsu_0207 6:17e3a28338dc 74 }