![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This is the code we showed at Uncraftivism
SerialBuffered.cpp@0:57f4fdadc97f, 2009-11-20 (annotated)
- Committer:
- jarkman
- Date:
- Fri Nov 20 14:40:48 2009 +0000
- Revision:
- 0:57f4fdadc97f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jarkman | 0:57f4fdadc97f | 1 | #include "stdafx.h" |
jarkman | 0:57f4fdadc97f | 2 | #include "mbed.h" |
jarkman | 0:57f4fdadc97f | 3 | #include "SerialBuffered.h" |
jarkman | 0:57f4fdadc97f | 4 | |
jarkman | 0:57f4fdadc97f | 5 | |
jarkman | 0:57f4fdadc97f | 6 | extern Logger pcSerial; |
jarkman | 0:57f4fdadc97f | 7 | |
jarkman | 0:57f4fdadc97f | 8 | SerialBuffered::SerialBuffered( size_t bufferSize, PinName tx, PinName rx ) : Serial( tx, rx ) |
jarkman | 0:57f4fdadc97f | 9 | { |
jarkman | 0:57f4fdadc97f | 10 | m_buffSize = 0; |
jarkman | 0:57f4fdadc97f | 11 | m_contentStart = 0; |
jarkman | 0:57f4fdadc97f | 12 | m_contentEnd = 0; |
jarkman | 0:57f4fdadc97f | 13 | m_timeout = 1.0; |
jarkman | 0:57f4fdadc97f | 14 | |
jarkman | 0:57f4fdadc97f | 15 | |
jarkman | 0:57f4fdadc97f | 16 | attach( this, &SerialBuffered::handleInterrupt ); |
jarkman | 0:57f4fdadc97f | 17 | |
jarkman | 0:57f4fdadc97f | 18 | m_buff = (uint8_t *) malloc( bufferSize ); |
jarkman | 0:57f4fdadc97f | 19 | if( m_buff == NULL ) |
jarkman | 0:57f4fdadc97f | 20 | { |
jarkman | 0:57f4fdadc97f | 21 | pcSerial.printf("SerialBuffered - failed to alloc buffer size %d\r\n", (int) bufferSize ); |
jarkman | 0:57f4fdadc97f | 22 | } |
jarkman | 0:57f4fdadc97f | 23 | else |
jarkman | 0:57f4fdadc97f | 24 | { |
jarkman | 0:57f4fdadc97f | 25 | m_buffSize = bufferSize; |
jarkman | 0:57f4fdadc97f | 26 | } |
jarkman | 0:57f4fdadc97f | 27 | } |
jarkman | 0:57f4fdadc97f | 28 | |
jarkman | 0:57f4fdadc97f | 29 | |
jarkman | 0:57f4fdadc97f | 30 | SerialBuffered::~SerialBuffered() |
jarkman | 0:57f4fdadc97f | 31 | { |
jarkman | 0:57f4fdadc97f | 32 | if( m_buff ) |
jarkman | 0:57f4fdadc97f | 33 | free( m_buff ); |
jarkman | 0:57f4fdadc97f | 34 | } |
jarkman | 0:57f4fdadc97f | 35 | |
jarkman | 0:57f4fdadc97f | 36 | void SerialBuffered::setTimeout( float seconds ) |
jarkman | 0:57f4fdadc97f | 37 | { |
jarkman | 0:57f4fdadc97f | 38 | m_timeout = seconds; |
jarkman | 0:57f4fdadc97f | 39 | } |
jarkman | 0:57f4fdadc97f | 40 | |
jarkman | 0:57f4fdadc97f | 41 | size_t SerialBuffered::readBytes( uint8_t *bytes, size_t requested ) |
jarkman | 0:57f4fdadc97f | 42 | { |
jarkman | 0:57f4fdadc97f | 43 | int i = 0; |
jarkman | 0:57f4fdadc97f | 44 | |
jarkman | 0:57f4fdadc97f | 45 | for( ; i < requested; ) |
jarkman | 0:57f4fdadc97f | 46 | { |
jarkman | 0:57f4fdadc97f | 47 | int c = getc(); |
jarkman | 0:57f4fdadc97f | 48 | if( c < 0 ) |
jarkman | 0:57f4fdadc97f | 49 | break; |
jarkman | 0:57f4fdadc97f | 50 | bytes[i] = c; |
jarkman | 0:57f4fdadc97f | 51 | i++; |
jarkman | 0:57f4fdadc97f | 52 | } |
jarkman | 0:57f4fdadc97f | 53 | |
jarkman | 0:57f4fdadc97f | 54 | return i; |
jarkman | 0:57f4fdadc97f | 55 | |
jarkman | 0:57f4fdadc97f | 56 | } |
jarkman | 0:57f4fdadc97f | 57 | |
jarkman | 0:57f4fdadc97f | 58 | |
jarkman | 0:57f4fdadc97f | 59 | int SerialBuffered::getc() |
jarkman | 0:57f4fdadc97f | 60 | { |
jarkman | 0:57f4fdadc97f | 61 | m_timer.reset(); |
jarkman | 0:57f4fdadc97f | 62 | m_timer.start(); |
jarkman | 0:57f4fdadc97f | 63 | |
jarkman | 0:57f4fdadc97f | 64 | #ifdef ON_DESKTOP |
jarkman | 0:57f4fdadc97f | 65 | handleInterrupt(); |
jarkman | 0:57f4fdadc97f | 66 | #endif |
jarkman | 0:57f4fdadc97f | 67 | |
jarkman | 0:57f4fdadc97f | 68 | while( m_contentStart == m_contentEnd ) |
jarkman | 0:57f4fdadc97f | 69 | { |
jarkman | 0:57f4fdadc97f | 70 | |
jarkman | 0:57f4fdadc97f | 71 | wait_ms( 1 ); |
jarkman | 0:57f4fdadc97f | 72 | if( m_timeout > 0 && m_timer.read() > m_timeout ) |
jarkman | 0:57f4fdadc97f | 73 | { |
jarkman | 0:57f4fdadc97f | 74 | pcSerial.printf("SerialBuffered - timeout\r\n"); |
jarkman | 0:57f4fdadc97f | 75 | |
jarkman | 0:57f4fdadc97f | 76 | return EOF; |
jarkman | 0:57f4fdadc97f | 77 | } |
jarkman | 0:57f4fdadc97f | 78 | |
jarkman | 0:57f4fdadc97f | 79 | #ifdef ON_DESKTOP |
jarkman | 0:57f4fdadc97f | 80 | handleInterrupt(); |
jarkman | 0:57f4fdadc97f | 81 | #endif |
jarkman | 0:57f4fdadc97f | 82 | } |
jarkman | 0:57f4fdadc97f | 83 | |
jarkman | 0:57f4fdadc97f | 84 | m_timer.stop(); |
jarkman | 0:57f4fdadc97f | 85 | |
jarkman | 0:57f4fdadc97f | 86 | uint8_t result = m_buff[m_contentStart++]; |
jarkman | 0:57f4fdadc97f | 87 | |
jarkman | 0:57f4fdadc97f | 88 | m_contentStart = m_contentStart % m_buffSize; |
jarkman | 0:57f4fdadc97f | 89 | |
jarkman | 0:57f4fdadc97f | 90 | |
jarkman | 0:57f4fdadc97f | 91 | return result; |
jarkman | 0:57f4fdadc97f | 92 | } |
jarkman | 0:57f4fdadc97f | 93 | |
jarkman | 0:57f4fdadc97f | 94 | |
jarkman | 0:57f4fdadc97f | 95 | int SerialBuffered::readable() |
jarkman | 0:57f4fdadc97f | 96 | { |
jarkman | 0:57f4fdadc97f | 97 | #ifdef ON_DESKTOP |
jarkman | 0:57f4fdadc97f | 98 | handleInterrupt(); |
jarkman | 0:57f4fdadc97f | 99 | #endif |
jarkman | 0:57f4fdadc97f | 100 | |
jarkman | 0:57f4fdadc97f | 101 | return m_contentStart != m_contentEnd ; |
jarkman | 0:57f4fdadc97f | 102 | } |
jarkman | 0:57f4fdadc97f | 103 | |
jarkman | 0:57f4fdadc97f | 104 | void SerialBuffered::handleInterrupt() |
jarkman | 0:57f4fdadc97f | 105 | { |
jarkman | 0:57f4fdadc97f | 106 | |
jarkman | 0:57f4fdadc97f | 107 | while( Serial::readable()) |
jarkman | 0:57f4fdadc97f | 108 | { |
jarkman | 0:57f4fdadc97f | 109 | if( m_contentStart == (m_contentEnd +1) % m_buffSize) |
jarkman | 0:57f4fdadc97f | 110 | { |
jarkman | 0:57f4fdadc97f | 111 | pcSerial.printf("SerialBuffered - buffer overrun, data lost!\r\n" ); |
jarkman | 0:57f4fdadc97f | 112 | Serial::getc(); |
jarkman | 0:57f4fdadc97f | 113 | |
jarkman | 0:57f4fdadc97f | 114 | } |
jarkman | 0:57f4fdadc97f | 115 | else |
jarkman | 0:57f4fdadc97f | 116 | { |
jarkman | 0:57f4fdadc97f | 117 | |
jarkman | 0:57f4fdadc97f | 118 | m_buff[ m_contentEnd ++ ] = Serial::getc(); |
jarkman | 0:57f4fdadc97f | 119 | |
jarkman | 0:57f4fdadc97f | 120 | m_contentEnd = m_contentEnd % m_buffSize; |
jarkman | 0:57f4fdadc97f | 121 | |
jarkman | 0:57f4fdadc97f | 122 | |
jarkman | 0:57f4fdadc97f | 123 | |
jarkman | 0:57f4fdadc97f | 124 | } |
jarkman | 0:57f4fdadc97f | 125 | } |
jarkman | 0:57f4fdadc97f | 126 | } |
jarkman | 0:57f4fdadc97f | 127 |