Saturday, April 9, 2011

Saving Configuration Items

One important goal of my odometer was the following: Maintain user configuration between power cycles.

We potentially need to track many different configuration items - some are functional, like the 'factor', the number of ticks per mile/kilometer.  Others are purely user preference - how quickly text should blink.
Regardless, having an easy and extensible way of storing this data is key.  The Arduino Playground shows the use of EEPROMWriteAnything with structs - a simple and lightweight solution, but with a few things I'd like to avoid.  The primary problem is the fact that, if you put all your configuration data into a single struct, the simple solution writes the entire struct out every time.  Yes, you could compare and only write the changed bytes - but that still leaves my other major complaint - you still have to remember to write it back out after changing it!

My solution - a library.  The primary part of my configuration management solution is configItem - a templated class that's effectively a container for a single piece of configuration data.  However, it doesn't just hold the data, but actually manages reading and writing from memory.

That's all fine and dandy, but using that class by itself would probably involve lots of hard-coded addresses, or wasteful runtime math.  The configItem(s) are meant to be used in a class derived from 'config' - a simple base class that provides some utilities for setting up the addresses.

So, how exactly does it all get hooked up?  We put our configItem(s) as public members of our derived class, and then simply call setAddress() on each in the constructor:

Elegant?  Not terribly.  Simple, effective, and easily extensible?  I think so.  The only thing I feel like I'm missing from this solution is on the configItem level - a mechanism for defining a min and max value.  I experimented a bit with non-class template parameters, but was unable to find a solution that really worked.

No comments:

Post a Comment