How are you coding for your devices settings that are user controlled

I am looking for some feedback regarding how to manage a large amount of code I have that is for the settings of the device and how it’s used. For example, The devices itself is modular, so the user must change settings or preferences to make it work for their application. Some examples are the users preferred temperature unit(C or F) or a “High Temp” warning value, etc. Some setting are related to the LCD display(contrast, or invert) So then I need to pass a delegate to the Contrast and invert Methods of the LCD.

After some trial and error and research I finally arrived at and implemented “Type-Safe Enum Class” pattern to handle all the settings. I’m somewhat happy with the solution, but it’s a lot of code to manage. I’ve tried to use as much abstraction and DI as possible.

The settings must persist in EEPROM and are loaded on boot through the settings class. This is a little fragile as there are wear-leveling and circular buffers tightly connected to the settings which is stored in EEPROM. After writing this out, I may need to do some refactoring and some more “single responsibility principle”

TLDR;
Anyway, are there any other patterns or ways to accomplish this? What are you doing for these situations?

All of my functions which are configurable use data. I’ve exposed commands which can be sent wirelessly to change the data values. For instance if someone wants to change the volume level of the noise at startup there’s an opcode followed by the new preferred value.

For larger amaounts of data to configure I use memory streams and binary reader for deserializing long continuous data.

Most of this stuff is not persistent, but some goes into config sector if it is.

For the long serialization mine is fragile too.

So to alleviate pain the first value of the serialized data is always the same size and is always the version number of the deserializer it is meant for.

1 Like

Not sure I understand, but and external SPI chip can help with data storage. SD cards also work well. As for serialisation, I recently wrote my own XmlDocument with Load and Save methods to do just that. It makes it nice and human readable, not to mention that visual studio has a native xml viewer when debugging xml.

Before that I used byte arrays, and had every method trained on the specific index it’s byte data was stored in. For example, if I had a property “public double LocationX” I would ensure that the 3 bytes after index 9 is where that data is stored. This method made the data small, and easy to persist, however it was a lot of code and difficult to debug.

that’s what I’m doing now. Byte arrays. Yes, difficult to debug. The data I’m storing is simple(ints, doubles, bytes/bools). I’m using an SPI EEPROM chip for low cost and small footprint. I will look into micro SD for next version of my hardware…

using (var stream = new MemoryStream(RawData))
{
    using (var reader = new BinaryReader(stream))
    {      
          DeserializedDataClass = Deserializ(reader);
    }
}

This makes it pretty easy to read from large byte arrays ( RawData is the array ). I’m not sure where Binary Reader came from but I’m sure it’s out there.

and then you just read the stream in order inside the deserialize function:

int FirstDatum = reader.ReadInt32();
bool SecondDatum = reader.ReadBool();

// skip two bytes
reader.ReadByte();
reader.ReadByte();

ushort ThirdDatum = reader.ReadUInt16();

etc.

I’m really not sure if this is what you are looking to solve but it is easier then pure arrays.

I’m looking for a more abstract way to handle and organize device settings with some type of coding pattern/style.

My “low-level” EEPROM functions are ok. I can read byte arrays and convert them into their appropriate type and conversely convert them to byte arrays and write, etc…