StepperII
Dual Axis Stepper Controller

CEepromVar.h

Go to the documentation of this file.
00001 
00005 #pragma once
00006 
00007 #include <avr/eeprom.h>
00008 
00009 //---------------------------------------------------------------------------- //-------------------------------------------------------------------------
00012 template <typename T>
00013 class CEepromVar
00014 {
00015 private:
00016         T* address;                             // EEPROM address
00017         T value;                                // Variable value in RAM
00018 public:         // This is a test
00019         T read(void)
00020         {
00021                 if (sizeof(T) == sizeof(uint8_t))
00022                         return eeprom_read_byte((uint8_t *)address);    // Read and return the value
00023                 else if (sizeof(T) == sizeof(uint16_t))
00024                         return eeprom_read_word((uint16_t *)address);
00025         }
00026 
00027 public:
00028         CEepromVar (T *address, T min, T max, T defaultValue)
00029         {
00030                 this->address = address;                        // Save the eeprom address
00031 
00032                 value = read();                                         // Read stored value in eeprom
00033                 if (value < min || value > max)         // Validate the value
00034                         value = defaultValue;                   // Use default if not valid
00035         }
00036 
00037         void archive(void)
00038         {
00039                 if (value != read())                                    // Is new value different?
00040                 {
00041                         if (sizeof(T) == sizeof(uint8_t))
00042                                 eeprom_write_byte((uint8_t *)address, value);   // Yes, write it
00043                         else if (sizeof(T) == sizeof(uint16_t))
00044                                 eeprom_write_word((uint16_t *)address, value);  // Yes, write it
00045                 }
00046         }
00047 
00048         int  inline operator *  (int x)
00049         {
00050                 return value * x;
00051         }
00052 
00053         void inline operator =  (T x)
00054         {
00055                 value = x;
00056                 archive();
00057         }
00058 
00059         void inline operator += (T x)
00060         {
00061                 value += x;
00062         }
00063 
00064         char inline operator == (T x)
00065         {
00066                 return value == x ? 1: 0;
00067         }
00068 
00069         char inline operator != (T x)
00070         {
00071                 return value != x ? 1: 0;
00072         }
00073 
00074         char inline operator >  (T x)
00075         {
00076                 return value > x ? 1: 0;
00077         }
00078 
00079         T inline operator *  (void)
00080         {
00081                 return value;
00082         }
00083 };
 All Classes Files Functions Variables Defines