Rick McConney
/
StarterKit
GSMA version
Fork of AvnetATT_shape_hackathon by
SerialBuffered/SerialBuffered.cpp@46:733edf15f9e8, 2017-01-09 (annotated)
- Committer:
- elmkom
- Date:
- Mon Jan 09 18:23:37 2017 +0000
- Revision:
- 46:733edf15f9e8
- Parent:
- 0:9d5134074d84
tcp test code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:9d5134074d84 | 1 | |
JMF | 0:9d5134074d84 | 2 | #include "mbed.h" |
JMF | 0:9d5134074d84 | 3 | #include "SerialBuffered.h" |
JMF | 0:9d5134074d84 | 4 | |
JMF | 0:9d5134074d84 | 5 | SerialBuffered::SerialBuffered( PinName tx, PinName rx, size_t bufferSize ) : Serial( tx, rx ) |
JMF | 0:9d5134074d84 | 6 | { |
JMF | 0:9d5134074d84 | 7 | m_buffSize = 0; |
JMF | 0:9d5134074d84 | 8 | m_contentStart = 0; |
JMF | 0:9d5134074d84 | 9 | m_contentEnd = 0; |
JMF | 0:9d5134074d84 | 10 | m_timeout = -1.0; |
JMF | 0:9d5134074d84 | 11 | _txpin = tx; |
JMF | 0:9d5134074d84 | 12 | _rxpin = rx; |
JMF | 0:9d5134074d84 | 13 | |
JMF | 0:9d5134074d84 | 14 | attach( this, &SerialBuffered::handleInterrupt ); |
JMF | 0:9d5134074d84 | 15 | |
JMF | 0:9d5134074d84 | 16 | m_buff = (uint8_t *) malloc( bufferSize ); |
JMF | 0:9d5134074d84 | 17 | if( m_buff ) |
JMF | 0:9d5134074d84 | 18 | m_buffSize = bufferSize; |
JMF | 0:9d5134074d84 | 19 | } |
JMF | 0:9d5134074d84 | 20 | |
JMF | 0:9d5134074d84 | 21 | |
JMF | 0:9d5134074d84 | 22 | SerialBuffered::~SerialBuffered() |
JMF | 0:9d5134074d84 | 23 | { |
JMF | 0:9d5134074d84 | 24 | if( m_buff ) |
JMF | 0:9d5134074d84 | 25 | free( m_buff ); |
JMF | 0:9d5134074d84 | 26 | } |
JMF | 0:9d5134074d84 | 27 | |
JMF | 0:9d5134074d84 | 28 | void SerialBuffered::setTimeout( float seconds ) |
JMF | 0:9d5134074d84 | 29 | { |
JMF | 0:9d5134074d84 | 30 | m_timeout = seconds; |
JMF | 0:9d5134074d84 | 31 | } |
JMF | 0:9d5134074d84 | 32 | |
JMF | 0:9d5134074d84 | 33 | size_t SerialBuffered::readBytes( uint8_t *bytes, size_t requested ) |
JMF | 0:9d5134074d84 | 34 | { |
JMF | 0:9d5134074d84 | 35 | int i = 0; |
JMF | 0:9d5134074d84 | 36 | |
JMF | 0:9d5134074d84 | 37 | for( ; i < requested; ) |
JMF | 0:9d5134074d84 | 38 | { |
JMF | 0:9d5134074d84 | 39 | int c = getc(); |
JMF | 0:9d5134074d84 | 40 | if( c < 0 ) |
JMF | 0:9d5134074d84 | 41 | break; |
JMF | 0:9d5134074d84 | 42 | bytes[i] = c; |
JMF | 0:9d5134074d84 | 43 | i++; |
JMF | 0:9d5134074d84 | 44 | } |
JMF | 0:9d5134074d84 | 45 | |
JMF | 0:9d5134074d84 | 46 | return i; |
JMF | 0:9d5134074d84 | 47 | |
JMF | 0:9d5134074d84 | 48 | } |
JMF | 0:9d5134074d84 | 49 | |
JMF | 0:9d5134074d84 | 50 | |
JMF | 0:9d5134074d84 | 51 | int SerialBuffered::_getc() |
JMF | 0:9d5134074d84 | 52 | { |
JMF | 0:9d5134074d84 | 53 | m_timer.reset(); |
JMF | 0:9d5134074d84 | 54 | m_timer.start(); |
JMF | 0:9d5134074d84 | 55 | while (m_contentStart == m_contentEnd) { |
JMF | 0:9d5134074d84 | 56 | if (m_timeout >= 0 && m_timer.read() >= m_timeout ) |
JMF | 0:9d5134074d84 | 57 | return EOF; |
JMF | 0:9d5134074d84 | 58 | wait_ms( 1 ); |
JMF | 0:9d5134074d84 | 59 | } |
JMF | 0:9d5134074d84 | 60 | |
JMF | 0:9d5134074d84 | 61 | m_timer.stop(); |
JMF | 0:9d5134074d84 | 62 | |
JMF | 0:9d5134074d84 | 63 | int nbp = (m_contentStart + 1) % m_buffSize; |
JMF | 0:9d5134074d84 | 64 | uint8_t result = m_buff[m_contentStart]; |
JMF | 0:9d5134074d84 | 65 | m_contentStart = nbp; |
JMF | 0:9d5134074d84 | 66 | return result; |
JMF | 0:9d5134074d84 | 67 | } |
JMF | 0:9d5134074d84 | 68 | |
JMF | 0:9d5134074d84 | 69 | //int Serial::_getc() { |
JMF | 0:9d5134074d84 | 70 | // return _base_getc(); |
JMF | 0:9d5134074d84 | 71 | //} |
JMF | 0:9d5134074d84 | 72 | |
JMF | 0:9d5134074d84 | 73 | int SerialBuffered::_putc(int c) { |
JMF | 0:9d5134074d84 | 74 | return _base_putc(c); |
JMF | 0:9d5134074d84 | 75 | } |
JMF | 0:9d5134074d84 | 76 | |
JMF | 0:9d5134074d84 | 77 | int SerialBuffered::readable() |
JMF | 0:9d5134074d84 | 78 | { |
JMF | 0:9d5134074d84 | 79 | return m_contentStart != m_contentEnd ; |
JMF | 0:9d5134074d84 | 80 | } |
JMF | 0:9d5134074d84 | 81 | |
JMF | 0:9d5134074d84 | 82 | void SerialBuffered::handleInterrupt() |
JMF | 0:9d5134074d84 | 83 | { |
JMF | 0:9d5134074d84 | 84 | while (serial_readable(&_serial)) { |
JMF | 0:9d5134074d84 | 85 | char c_in = _base_getc(); |
JMF | 0:9d5134074d84 | 86 | int nbp = (m_contentEnd + 1) % m_buffSize; |
JMF | 0:9d5134074d84 | 87 | |
JMF | 0:9d5134074d84 | 88 | if (nbp != m_contentStart) { |
JMF | 0:9d5134074d84 | 89 | m_buff[m_contentEnd] = c_in; |
JMF | 0:9d5134074d84 | 90 | m_contentEnd = nbp; |
JMF | 0:9d5134074d84 | 91 | } |
JMF | 0:9d5134074d84 | 92 | } |
JMF | 0:9d5134074d84 | 93 | } |