Arduino-Storage
External Memory Storage library for Arduino
Storage.h
Go to the documentation of this file.
1 
19 #ifndef STORAGE_H
20 #define STORAGE_H
21 
27 class Storage {
28 public:
30  const uint32_t SIZE;
31 
36  Storage(uint32_t size) : SIZE(size), m_addr(0) {}
37 
42  uint32_t room()
43  {
44  return (SIZE - m_addr);
45  }
46 
53  uint32_t alloc(size_t count)
54  {
55  if (count > room()) return (UINT32_MAX);
56  uint32_t res = m_addr;
57  m_addr += count;
58  return (res);
59  }
60 
65  void free(uint32_t addr)
66  {
67  if (addr < m_addr) m_addr = addr;
68  }
69 
78  virtual int read(void* dest, uint32_t src, size_t count) = 0;
79 
88  virtual int write(uint32_t dest, const void* src, size_t count) = 0;
89 
93  class Block {
94  public:
100  Block(Storage &mem, uint32_t size) :
101  SIZE(size),
102  m_mem(mem),
103  m_addr(m_mem.alloc(size))
104  {
105  }
106 
111  {
112  m_mem.free(m_addr);
113  }
114 
119  uint32_t addr()
120  {
121  return (m_addr);
122  }
123 
132  int read(void* buf, uint32_t offset, size_t size)
133  {
134  if (offset + size <= SIZE)
135  return (m_mem.read(buf, m_addr + offset, size));
136  return (-1);
137  }
138 
148  int write(uint32_t offset, const void* buf, size_t size)
149  {
150  if (offset + size <= SIZE)
151  return (m_mem.write(m_addr + offset, buf, size));
152  return (-1);
153  }
154 
156  const uint32_t SIZE;
157 
158  protected:
161 
163  const uint32_t m_addr;
164  };
165 
171  class Cache : public Block {
172  public:
183  Cache(Storage &mem, void* buf, size_t size, size_t nmemb = 1) :
184  Block(mem, size * nmemb),
185  MSIZE(size),
186  NMEMB(nmemb),
187  m_buf(buf)
188  {
189  }
190 
196  uint32_t addr(size_t ix = 0)
197  {
198  if (ix == 0)
199  return (m_addr);
200  if (ix < NMEMB)
201  return (m_addr + (ix * MSIZE));
202  return (UINT32_MAX);
203  }
204 
212  int read(size_t ix = 0)
213  {
214  if (ix == 0)
215  return (m_mem.read(m_buf, m_addr, MSIZE));
216  if (ix < NMEMB)
217  return (m_mem.read(m_buf, m_addr + (ix * MSIZE), MSIZE));
218  return (-1);
219  }
220 
228  int write(size_t ix = 0)
229  {
230  if (ix == 0)
231  return (m_mem.write(m_addr, m_buf, MSIZE));
232  if (ix < NMEMB)
233  return (m_mem.write(m_addr + (ix * MSIZE), m_buf, MSIZE));
234  return (-1);
235  }
236 
238  const size_t MSIZE;
239 
241  const size_t NMEMB;
242 
243  protected:
245  void* m_buf;
246  };
247 
253  class Stream : public ::Stream {
254  public:
260  Stream(Storage &mem, size_t size) :
261  SIZE(size),
262  m_mem(mem),
263  m_addr(m_mem.alloc(size)),
264  m_put(0),
265  m_get(0),
266  m_count(0)
267  {
268  }
269 
274  uint32_t addr()
275  {
276  return (m_addr);
277  }
278 
286  virtual size_t write(uint8_t byte)
287  {
288  if (m_count == SIZE) return (0);
289  m_mem.write(m_addr + m_put, &byte, sizeof(byte));
290  m_count += 1;
291  m_put += 1;
292  if (m_put == SIZE) m_put = 0;
293  return (sizeof(byte));
294  }
295 
304  virtual size_t write(const uint8_t *buffer, size_t size)
305  {
306  uint16_t room = SIZE - m_count;
307  if (room == 0) return (0);
308  if (size > room) size = room;
309  size_t res = size;
310  room = SIZE - m_put;
311  if (size > room) {
312  m_mem.write(m_addr + m_put, buffer, room);
313  buffer += room;
314  size -= room;
315  m_count += room;
316  m_put = 0;
317  }
318  m_mem.write(m_addr + m_put, buffer, size);
319  m_count += size;
320  m_put += size;
321  return (res);
322  }
323 
329  virtual int available()
330  {
331  return (m_count);
332  }
333 
340  virtual int peek()
341  {
342  if (m_count == 0) return (-1);
343  uint8_t res = 0;
344  m_mem.read(&res, m_addr + m_get, sizeof(res));
345  return (res);
346  }
347 
354  virtual int read()
355  {
356  if (m_count == 0) return (-1);
357  uint8_t res = 0;
358  m_mem.read(&res, m_addr + m_get, sizeof(res));
359  m_count -= 1;
360  m_get += 1;
361  if (m_get == SIZE) m_get = 0;
362  return (res);
363  }
364 
369  virtual void flush()
370  {
371  m_put = 0;
372  m_get = 0;
373  m_count = 0;
374  }
375 
377  size_t SIZE;
378 
379  protected:
382 
384  const uint32_t m_addr;
385 
387  uint16_t m_put;
388 
390  uint16_t m_get;
391 
393  uint16_t m_count;
394  };
395 
396 protected:
398  uint32_t m_addr;
399 };
400 #endif
uint32_t addr(size_t ix=0)
Definition: Storage.h:196
const uint32_t SIZE
Definition: Storage.h:156
uint32_t alloc(size_t count)
Definition: Storage.h:53
int read(void *buf, uint32_t offset, size_t size)
Definition: Storage.h:132
virtual int peek()
Definition: Storage.h:340
const uint32_t SIZE
Definition: Storage.h:30
uint8_t buf[BUF_MAX]
Definition: AT24CXX.ino:75
const uint32_t m_addr
Definition: Storage.h:163
int write(uint32_t offset, const void *buf, size_t size)
Definition: Storage.h:148
uint16_t m_put
Definition: Storage.h:387
Storage & m_mem
Definition: Storage.h:381
const size_t MSIZE
Definition: Storage.h:238
virtual int read()
Definition: Storage.h:354
uint32_t m_addr
Definition: Storage.h:398
virtual int read(void *dest, uint32_t src, size_t count)=0
virtual int available()
Definition: Storage.h:329
int read(size_t ix=0)
Definition: Storage.h:212
const size_t NMEMB
Definition: Storage.h:241
Block(Storage &mem, uint32_t size)
Definition: Storage.h:100
Storage(uint32_t size)
Definition: Storage.h:36
const size_t NMEMB
Definition: Vector.ino:45
int write(size_t ix=0)
Definition: Storage.h:228
uint32_t addr()
Definition: Storage.h:119
Cache(Storage &mem, void *buf, size_t size, size_t nmemb=1)
Definition: Storage.h:183
virtual size_t write(uint8_t byte)
Definition: Storage.h:286
uint32_t room()
Definition: Storage.h:42
uint16_t m_get
Definition: Storage.h:390
uint16_t m_count
Definition: Storage.h:393
const uint32_t m_addr
Definition: Storage.h:384
virtual size_t write(const uint8_t *buffer, size_t size)
Definition: Storage.h:304
Storage & m_mem
Definition: Storage.h:160
virtual void flush()
Definition: Storage.h:369
Stream(Storage &mem, size_t size)
Definition: Storage.h:260
size_t SIZE
Definition: Storage.h:377
uint32_t addr()
Definition: Storage.h:274
void * m_buf
Definition: Storage.h:245
virtual int write(uint32_t dest, const void *src, size_t count)=0
void free(uint32_t addr)
Definition: Storage.h:65
int count
Definition: Stream.ino:26