BayEOS-Arduino  1.8.0_0.0.4
BayEOSBuffer.h
1 
17 #ifndef BayEOSBuffer_h
18 #define BayEOSBuffer_h
19 #define __PROG_TYPES_COMPAT__
20 #include <Arduino.h>
21 
22 
23 #define SERIAL_DEBUG 0
24 //RTC-Stuff...
25 #define SECONDS_PER_DAY 86400L
26 
27 
28 class DateTime {
29 public:
30  DateTime (long t =0);
31  DateTime (uint16_t year, uint8_t month, uint8_t day,
32  uint8_t hour =0, uint8_t min =0, uint8_t sec =0);
33  DateTime (const char* date, const char* time);
34 
35  uint16_t year() const;
36  uint8_t month() const;
37  uint8_t day() const;
38  uint8_t hour() const;
39  uint8_t minute() const;
40  uint8_t second() const;
41  uint8_t dayOfWeek() const;
42 
43  // 32-bit times as seconds since 1/1/2000
44  long get() const;
45 
46 protected:
47  uint8_t yOff, m, d, hh, mm, ss;
48 };
49 
50 //Abstract RTC
51 class RTC {
52 public:
53  virtual void begin()=0;
54  virtual void adjust(const DateTime& dt)=0;
55  virtual DateTime now()=0;
56 };
57 
58 
59 
60 
61 
62 class BayEOSBuffer {
63 public:
64  BayEOSBuffer(void);
65 
66 
67 
72  unsigned long available(void);
73 
78  uint8_t freeSpace(uint8_t length);
79 
80 
84  int readPacket(uint8_t *dest);
85 
89  uint8_t readBinary(unsigned long pos,uint8_t length, uint8_t *dest);
90 
94  uint8_t readBinary(unsigned long pos,unsigned long end,uint8_t length, uint8_t *dest);
95 
99  void next(void);
100 
104  void set(unsigned long pos);
108  void seekReadPointer(unsigned long pos);
112  void reset(void);
117  uint8_t addPacket(const uint8_t *payload,uint8_t length);
118 
122  uint8_t initPacket(unsigned long pos);
123 
127  uint8_t initNextPacket(void);
131  uint8_t packetLength(void);
135  unsigned long packetMillis(void);
136 
137 
143  void setRTC(RTC& rtc,boolean absolute_time=true);
144 
149  uint8_t rtc(void);
150 
151 
152  unsigned long getTime(void);
153 
154  unsigned long writePos(void);
155  unsigned long readPos(void);
156  unsigned long endPos(void);
157  unsigned long length(void);
158 
159 
160  boolean _absoluteTime;
161  boolean _framesDiscarded;
162 
163 protected:
164  unsigned long _max_length;
165  RTC* _rtc;
166  unsigned long _read_pos;
167  unsigned long _write_pos;
168  unsigned long _pos;
169  unsigned long _end;
170  uint8_t b_write(const uint8_t b);
171  uint8_t b_write(const uint8_t *b, uint8_t length);
172  int b_read();
173  int b_read(uint8_t *dest, int length);
174  uint8_t b_seek(unsigned long pos);
175 
176 
177 private:
178  /*
179  * reset storage to inital state
180  */
181  virtual void resetStorage(void)=0;
182 
183  /*
184  * write one byte to the buffer at the current _write_pos
185  */
186  virtual uint8_t write(const uint8_t b)=0;
187 
188  /*
189  * write length bytes to the buffer at the current _write_pos
190  */
191  virtual uint8_t write(const uint8_t *b,uint8_t length)=0;
192 
193  /*
194  * seek the write/read pointer of the buffer
195  */
196  virtual uint8_t seek(unsigned long pos)=0;
197 
198  /*
199  * read one byte from the buffer
200  * returns -1 on failure
201  */
202  virtual int read(void)=0;
203 
204  /*
205  * read length bytes from the buffer into destination
206  * returns number of bytes read
207  * -1 on failure
208  */
209  virtual int read(uint8_t *dest,int length)=0;
210 
211 
212  /*
213  * Flush the buffer
214  */
215  virtual void flush(void)=0;
216 
217  unsigned long _millis;
218  uint8_t _packet_length;
219  int _res;
220 
221 };
222 
223 
224 
225 
226 
227 
228 #endif