When I started my latest project I just stuffed a bunch data into structs and then nest structs but this has turned into a DRY fail and if-then soup that isn’t very adaptable so I reworking a lot of that and am hoping to get some pointers on the best approach. In my mobile app I have (4) “cards” where each represents a schedule/mode for the device. The user can pick a RGB color value, Start Time and enable or disable that particular mode (this is where I struggle). When they press save in the app it will trigger a Particle.function and pass in the relevant data. Where I could use a little guidance is the firmware side of things. Since a mode/schedule could be on or off depending on the settings what is the best way to store this data then use it for scheduling?
I was thinking a class object to store all of the user settings as well as some computed values on the device then std:vector or list so I can iterate through them and break out/skip it if enabled=false.
In our my system I have 3 devices which need to be on between certain times during each day and depending on the weekday. Each device also has some specialities, like one device does not have an off time but rather speed (talking about fan speed). By utilizing something like below you can use different functions to check the timers and different ones to actually enforce the timers. Basically I have something like this:
//0 = lights, 1 = device xx 2 = device xx 3 = device xx , 4 = device xx
//15 timers
//for seven days. 0 = sunday
//0= off time, 1 = on time.
//value = hhmm
retained uint16_t deviceTimers[5][15][7][2];
Now it’s relatively simple to attach function to particle subscribe which receives a timer and then “check timers” function which is run inside loop which just checks if device should be on like this
if (deviceTimers[device][i][Time.day()-1][1] <= (Time.hour()*100+Time.minute()) && deviceTimers[device][i][Time.day()-1][0] > (Time.hour()*100+Time.minute())) {
return 1; //device should be on
}
Lastly if you have RGB value you need to save you can use similar kind of multidimensional array for each device. Yep this might not be the fanciest way to use structs etc but very simple and usually effective way to handle multiple scenarios. This also enables you to utilize for loops etc. effectively and avoid as much of if/else loops as possible.
Also it would be great to hear if someone has any other ideas to similar case.