Advertisement
Guest User

progmem wrapper

a guest
Jun 5th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. template<typename Type>
  2. class InProgmem {
  3.   friend class Font;
  4.   Type const val; // InProgmem<Type> should be as big as Type
  5.   InProgmem(Type const &val) : val(val) {} // Keep the compiler quiet
  6.   // Prevent copying, makes problems otherwise
  7.   InProgmem(const InProgmem<Type>& other) : val(other.val) {};
  8.   InProgmem<Type>& operator = (const InProgmem<Type>& other)
  9.     { return *this; }
  10. public:
  11. #if defined AVR
  12.   operator Type() const {
  13.     Type out;
  14.     memcpy_P(&out, this, sizeof(Type));
  15.     return out;
  16.   }
  17. #else
  18.   // If running on a computer just return the value
  19.   operator Type() const {
  20.     return val;
  21.   }
  22. #endif
  23. } __attribute__((packed));
  24.  
  25. struct MyData {
  26.   InProgmem<int> a, b;
  27. };
  28.  
  29. PROGMEM int const dataInProgmem[] = {23, 42};
  30.  
  31. void setup() {
  32.   Serial.begin(9600);
  33.   MyData *data = (MyData*)&dataInProgmem;
  34.   Serial.println(data->a);
  35.   Serial.println(data->b);
  36. }
  37.  
  38. void loop() {
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement