SFBByteBuffer.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00051 #ifndef SFBBYTEBUFFER_H
00052 #define SFBBYTEBUFFER_H
00053
00054 #include "SFBTypes.h"
00055 #include "SFBConstants.h"
00056
00061 class SFBByteBuffer {
00062
00063 protected:
00064 u8 (&buf)[BYTE_BUFFER_BYTES];
00065 u16 oldIndex;
00066 u16 newIndex;
00067
00068
00069 SFBByteBuffer(u8 (&buffer)[BYTE_BUFFER_BYTES])
00070 : buf(buffer),
00071 oldIndex(0),
00072 newIndex(0)
00073 { }
00074
00075 u16 spaceAvailable() {
00076 return ((BYTE_BUFFER_BYTES-1)-(newIndex-oldIndex))&BYTE_BUFFER_MASK;
00077 }
00078
00079 u16 bytesAvailable() {
00080 return (newIndex-oldIndex)&BYTE_BUFFER_MASK;
00081 }
00082
00083 u16 wrap(u16 index) {
00084 return index&BYTE_BUFFER_MASK;
00085 }
00086 void uncheckedStoreByte(u8 data) {
00087 buf[newIndex] = data;
00088 newIndex = wrap(newIndex+1);
00089 }
00090 u8 uncheckedFetchByte() {
00091 u8 val = buf[oldIndex];
00092 oldIndex = wrap(oldIndex+1);
00093 return val;
00094 }
00095
00096 enum {
00097 BB_ESC = 0xA5,
00098 BB_EESC = 0x00
00099
00100 };
00101
00102 };
00103
00114 class SFBRxByteBuffer : public SFBByteBuffer {
00115 public:
00116
00117 enum {
00118 BBFLAG_ERRORBITS= PK_OVERRUN|PK_PARITY|PK_FRAMING|PK_BREAK,
00119 BBFLAG_OVERFLOW= PK_BUFFER,
00120 BBFLAG_ZERO= PK_DELETED|PK_BAD_ESCAPE
00121 };
00122
00123 SFBRxByteBuffer(u8 (&buffer)[BYTE_BUFFER_BYTES]) : SFBByteBuffer(buffer), flags(0) { }
00124
00128 void resetBG() ;
00129
00133 int fetchByteBG() ;
00134
00138 void storeErrorStatusIL(u8 status) {
00139 flags |= status;
00140 }
00141
00145 void storeByteIL(u8 data) ;
00146
00147 private:
00148 u8 flags;
00149
00150 };
00151
00162 class SFBTxByteBuffer : public SFBByteBuffer {
00163 public:
00164 SFBTxByteBuffer(u8 (&buffer)[BYTE_BUFFER_BYTES]) : SFBByteBuffer(buffer), pendingCount(0) { }
00165
00169 void resetBG() ;
00170
00175 void storeByteBG(u8 data) ;
00176
00181 bool canStoreBytesBG(u32 count) ;
00182
00192 bool shipBytesBG() ;
00193
00194
00199 int fetchByteIL() ;
00200
00201 private:
00202 int pendingCount;
00203
00204 };
00205
00206 #endif
00207