BayEOSArduino Library
BayEOSBuffer.h
1 
22 #ifndef BayEOSBuffer_h
23 #define BayEOSBuffer_h
24 #define __PROG_TYPES_COMPAT__
25 #include <Arduino.h>
26 
27 
28 #define SERIAL_DEBUG 0
29 //RTC-Stuff...
30 #define SECONDS_PER_DAY 86400L
31 
32 
33 class DateTime {
34 public:
35  DateTime (long t =0);
36  DateTime (uint16_t year, uint8_t month, uint8_t day,
37  uint8_t hour =0, uint8_t min =0, uint8_t sec =0);
38  DateTime (const char* date, const char* time);
39 
40  uint16_t year() const;
41  uint8_t month() const;
42  uint8_t day() const;
43  uint8_t hour() const;
44  uint8_t minute() const;
45  uint8_t second() const;
46  uint8_t dayOfWeek() const;
47 
48  // 32-bit times as seconds since 1/1/2000
49  long get() const;
50 
51 protected:
52  uint8_t yOff, m, d, hh, mm, ss;
53 };
54 
55 //Abstract RTC
56 class RTC {
57 public:
58  virtual void begin()=0;
59  virtual void adjust(const DateTime& dt)=0;
60  virtual DateTime now()=0;
61 };
62 
63 
64 
65 #define RTC_RELATIVE_MILLIS 0
66 #define RTC_RELATIVE_SECONDS 2
67 #define RTC_ABSOLUTE_SECONDS 1
68 
69 
70 class BayEOSBuffer {
71 public:
72  BayEOSBuffer(void);
73 
74 
75 
80  unsigned long available(void);
81 
86  uint8_t freeSpace(uint16_t length);
87 
91  void makeFreeSpace(uint16_t length);
92 
96  unsigned long freeSpace(void);
97 
98 
102  int readPacket(uint8_t *dest);
103 
107  uint8_t readBinary(unsigned long pos,uint8_t length, uint8_t *dest);
108 
112  uint8_t readBinary(unsigned long pos,unsigned long end,uint8_t length, uint8_t *dest);
113 
117  void next(void);
118 
122  void set(unsigned long pos);
126  void seekReadPointer(unsigned long pos);
130  void reset(void);
134  void skip(void);
139  uint8_t addPacket(const uint8_t *payload,uint8_t length);
140 
144  uint8_t initPacket(unsigned long pos);
145 
149  uint8_t initNextPacket(void);
153  uint8_t packetLength(void);
157  unsigned long packetMillis(void);
158 
159 
165  void setRTC(RTC& rtc,uint8_t timeType=RTC_ABSOLUTE_SECONDS);
166 
171  uint8_t rtc(void);
172 
173 
174  unsigned long getTime(void);
175 
176  unsigned long writePos(void);
177  unsigned long readPos(void);
178  unsigned long endPos(void);
179  unsigned long length(void);
180 
181 
182  uint8_t _timeType;
183  boolean _framesDiscarded;
184 
185 protected:
186  unsigned long _max_length;
187  RTC* _rtc;
188  unsigned long _read_pos;
189  unsigned long _write_pos;
190  unsigned long _pos;
191  unsigned long _end;
192  uint8_t b_write(const uint8_t b);
193  uint8_t b_write(const uint8_t *b, uint8_t length);
194  int b_read();
195  int b_read(uint8_t *dest, int length);
196  uint8_t b_seek(unsigned long pos);
197 
198 
199 private:
200  /*
201  * reset storage to inital state
202  */
203  virtual void resetStorage(void)=0;
204 
205  /*
206  * write one byte to the buffer at the current _write_pos
207  */
208  virtual uint8_t write(const uint8_t b)=0;
209 
210  /*
211  * write length bytes to the buffer at the current _write_pos
212  */
213  virtual uint8_t write(const uint8_t *b,uint8_t length)=0;
214 
215  /*
216  * seek the write/read pointer of the buffer
217  */
218  virtual uint8_t seek(unsigned long pos)=0;
219 
220  /*
221  * read one byte from the buffer
222  * returns -1 on failure
223  */
224  virtual int read(void)=0;
225 
226  /*
227  * read length bytes from the buffer into destination
228  * returns number of bytes read
229  * -1 on failure
230  */
231  virtual int read(uint8_t *dest,int length)=0;
232 
233 
234  /*
235  * Flush the buffer
236  */
237  virtual void flush(void)=0;
238 
239  unsigned long _millis;
240  uint8_t _packet_length;
241  int _res;
242 
243 };
244 
245 
246 
247 
248 
249 
250 #endif
Definition: BayEOSBuffer.h:70
unsigned long packetMillis(void)
Definition: BayEOSBuffer.cpp:254
void seekReadPointer(unsigned long pos)
Definition: BayEOSBuffer.cpp:164
uint8_t rtc(void)
Definition: BayEOSBuffer.cpp:263
unsigned long available(void)
Definition: BayEOSBuffer.cpp:78
uint8_t initNextPacket(void)
Definition: BayEOSBuffer.cpp:177
void next(void)
Definition: BayEOSBuffer.cpp:200
void makeFreeSpace(uint16_t length)
Definition: BayEOSBuffer.cpp:90
void reset(void)
Definition: BayEOSBuffer.cpp:168
uint8_t addPacket(const uint8_t *payload, uint8_t length)
Definition: BayEOSBuffer.cpp:230
uint8_t readBinary(unsigned long pos, uint8_t length, uint8_t *dest)
Definition: BayEOSBuffer.cpp:129
uint8_t initPacket(unsigned long pos)
Definition: BayEOSBuffer.cpp:181
void setRTC(RTC &rtc, uint8_t timeType=RTC_ABSOLUTE_SECONDS)
Definition: BayEOSBuffer.cpp:258
void skip(void)
Definition: BayEOSBuffer.cpp:173
void set(unsigned long pos)
Definition: BayEOSBuffer.cpp:145
uint8_t packetLength(void)
Definition: BayEOSBuffer.cpp:250
int readPacket(uint8_t *dest)
Definition: BayEOSBuffer.cpp:125
unsigned long freeSpace(void)
Definition: BayEOSBuffer.cpp:83
Definition: BayEOSBuffer.h:33
Definition: BayEOSBuffer.h:56