Guest User

Untitled

a guest
Jul 12th, 2011
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. /*
  2. Flash.h - An Arduino library for flash-based (ROM) data collections.
  3. Copyright (C) 2009 Mikal Hart
  4. All rights reserved.
  5.  
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10.  
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with this library; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20.  
  21. #ifndef FLASH_H
  22. #define FLASH_H
  23.  
  24. #include <avr/pgmspace.h>
  25. #include <WProgram.h>
  26.  
  27. #define FLASH_LIBRARY_VERSION 3
  28.  
  29. // Use these macros to define your flash-based data structures
  30. // Example: FLASH_STRING(str, "Four score and seven years ago");
  31. #define FLASH_STRING(name, value) \
  32. static const char name##_flash[] PROGMEM = value; \
  33. _FLASH_STRING name(name##_flash);
  34.  
  35. // Example: FLASH_ARRAY(float, temperatures, 98.1, 98.5, 99.1, 102.1);
  36. #define FLASH_ARRAY(type, name, values...) \
  37. static const type name##_flash[] PROGMEM = { values }; \
  38. _FLASH_ARRAY<type> name(name##_flash, sizeof(name##_flash) / sizeof(type));
  39.  
  40. // Example: FLASH_TABLE(uint8_t, fonts, 7, {ON, OFF, ON, ON, OFF, ON, OFF}, {OFF, ON, OFF, ON, OFF, ON, OFF});
  41. #define FLASH_TABLE(type, name, cols, values...) \
  42. static const type name##_flash[][cols] PROGMEM = { values }; \
  43. _FLASH_TABLE<type> name((const PROGMEM type *)name##_flash, sizeof(name##_flash) / sizeof(name##_flash[0]), cols);
  44.  
  45. // Example: FLASH_STRING_ARRAY(nums, PSTR("One"), PSTR("Two"), PSTR("Three"), PSTR("Four"), PSTR("Five"));
  46. #define FLASH_STRING_ARRAY(name, values...) \
  47. const PROGMEM char *name##_arr[] = { values }; \
  48. _FLASH_STRING_ARRAY name(name##_arr, sizeof(name##_arr) / sizeof(name##_arr[0]));
  49.  
  50. #ifndef ARDUINO_CORE_PRINTABLE_SUPPORT
  51. class _Printable
  52. {
  53. public:
  54. virtual void print(Print &stream) const = 0;
  55. };
  56. #endif
  57.  
  58. // Inline ROM strings. Example: Serial << F("Hello, big world!");
  59. #define F(str) (_FLASH_STRING(PSTR(str)).Printable())
  60.  
  61. /* _FLASH_STRING class. Use the FLASH_STRING() macro to create these, or use inline F() macro. */
  62. class _FLASH_STRING : public _Printable
  63. {
  64. public:
  65. _FLASH_STRING(const prog_char *arr);
  66.  
  67. size_t length() const
  68. { return strlen_P(_arr); }
  69.  
  70. char *copy(char *to, size_t size = -1, size_t offset = 0) const
  71. {
  72. return size == -1 ?
  73. strcpy_P(to, _arr + offset) : strncpy_P(to, _arr + offset, size);
  74. }
  75.  
  76. const prog_char *access() const
  77. { return _arr; }
  78.  
  79. const _Printable &Printable() const
  80. { return *this; }
  81.  
  82. char operator[](int index) const
  83. { return static_cast<char>(pgm_read_byte(_arr + index)); }
  84.  
  85. void print(Print &stream) const;
  86.  
  87. private:
  88. const prog_char *_arr;
  89. };
  90.  
  91. /* _FLASH_ARRAY template class. Use the FLASH_ARRAY() macro to create these. */
  92. template<class T>
  93. class _FLASH_ARRAY : public _Printable
  94. {
  95. typedef T PROGMEM _DataType;
  96.  
  97. public:
  98. _FLASH_ARRAY(const _DataType *arr, size_t count) : _arr(arr), _size(count)
  99. { }
  100.  
  101. size_t count() const
  102. { return _size; }
  103.  
  104. const _DataType *access() const
  105. { return _arr; }
  106.  
  107. T operator[](int index) const
  108. {
  109. uint32_t val = 0;
  110. if (sizeof(T) == 1)
  111. val = pgm_read_byte(_arr + index);
  112. else if (sizeof(T) == 2)
  113. val = pgm_read_word(_arr + index);
  114. else if (sizeof(T) == 4)
  115. val = pgm_read_dword(_arr + index);
  116. return *reinterpret_cast<T *>(&val);
  117. }
  118.  
  119. void print(Print &stream) const
  120. {
  121. for (size_t i=0; i<_size; ++i)
  122. {
  123. stream.print((*this)[i]);
  124. if (i < _size - 1)
  125. stream.print(",");
  126. }
  127. }
  128.  
  129. private:
  130. const _DataType *_arr;
  131. size_t _size;
  132. };
  133.  
  134. /* _FLASH_TABLE template class. Use the FLASH_TABLE() macro to create these. */
  135. template<class T>
  136. class _FLASH_TABLE : public _Printable
  137. {
  138. typedef T PROGMEM _DataType;
  139.  
  140. public:
  141. _FLASH_TABLE(const _DataType *arr, size_t rows, size_t cols) : _arr(arr), _rows(rows), _cols(cols)
  142. { }
  143.  
  144. size_t rows() const
  145. { return _rows; }
  146.  
  147. size_t cols() const
  148. { return _cols; }
  149.  
  150. const _DataType *access()
  151. { return _arr; }
  152.  
  153. _FLASH_ARRAY<T> operator[](int index) const
  154. {
  155. _FLASH_ARRAY<T> row(_arr + index * _cols, _cols);
  156. return row;
  157. }
  158.  
  159. void print(Print &stream) const
  160. {
  161. for (int i=0; i<_rows; ++i)
  162. {
  163. _FLASH_ARRAY<T> row(_arr + i * _cols, _cols);
  164. row.print(stream);
  165. stream.println();
  166. }
  167. }
  168.  
  169. private:
  170. const _DataType *_arr;
  171. size_t _rows, _cols;
  172. };
  173.  
  174. /* _FLASH_STRING_ARRAY class. Use the FLASH_STRING_ARRAY() macro to create these. */
  175. class _FLASH_STRING_ARRAY : public _Printable
  176. {
  177. public:
  178. _FLASH_STRING_ARRAY(const prog_char **arr, size_t count) : _arr(arr), _size(count)
  179. { }
  180.  
  181. size_t count() const
  182. { return _size; }
  183.  
  184. _FLASH_STRING operator[](int index) const
  185. { return _FLASH_STRING(_arr[index]); }
  186.  
  187. void print(Print &stream) const
  188. {
  189. for (size_t i=0; i<_size; ++i)
  190. {
  191. _FLASH_STRING str(_arr[i]);
  192. str.print(stream);
  193. stream.println();
  194. }
  195. }
  196.  
  197. private:
  198. const prog_char **_arr;
  199. size_t _size;
  200. };
  201.  
  202. #ifndef ARDUINO_STREAMING
  203. #define ARDUINO_STREAMING
  204.  
  205. template<class T>
  206. inline Print &operator <<(Print &stream, T arg)
  207. { stream.print(arg); return stream; }
  208.  
  209. #endif
  210.  
  211. inline Print &operator <<(Print &stream, const _Printable &printable)
  212. { printable.print(stream); return stream; }
  213.  
  214. inline Print &operator <<(Print &stream, const _FLASH_STRING &printable)
  215. { printable.print(stream); return stream; }
  216.  
  217. template<class T>
  218. inline Print &operator <<(Print &stream, const _FLASH_ARRAY<T> &printable)
  219. { printable.print(stream); return stream; }
  220.  
  221. template<class T>
  222. inline Print &operator <<(Print &stream, const _FLASH_TABLE<T> &printable)
  223. { printable.print(stream); return stream; }
  224.  
  225. inline Print &operator <<(Print &stream, const _FLASH_STRING_ARRAY &printable)
  226. { printable.print(stream); return stream; }
  227.  
  228. #endif
Advertisement
Add Comment
Please, Sign In to add comment