270 views
in Getting started by
I have a GUI application with some dialogs and many switches on the dialogs.  

I would like to save the configuration of those switches, ie which are on and which are off,  when the application exits, and then when the application starts again restore the switches to the same configuration.

I realize I can do this by saving the settings to a file, in native code, on shut down, and then reading the file, in native code, during init when the app starts the next time.

But is there a more direct way to do this using some Chora code or object ?  Some sort of config file ?

1 Answer

+1 vote
by
 
Best answer

Hello Robert,

exactly, save the settings on shutdown and during init when the application starts the next time, read the stored settings again. This is generally the correct approach. How you do this, depends on your application case and the location where the data is stored. Chora does not provide any dedicated function to save/load data.

How difficult this task is, depends on the structure of your application. Concrete, it depends on the location where the GUI application stores all settings data. If the data is spread over the whole implementation, the respective Save/Load methods will need to access all these locations.

Another approach is, you manage all settings data within a single, global object. The GUI dialogs access thereupon this object to query and modify the setting values. This concepts follows the Model-View-Controller programming paradigm and is supported natively by Embedded Wizard. To store and restore the settings you only need to save and load the values from this unique object.

Maybe, you are already following this approach? If not, here a short indication how to implement a GUI application following the MVC paradigm:

1. Implement a new class intended to store all settings. See the section Implementing a Device Interface. It explains this step on an example of an interface to the HW/Device. In your case, if the settings are not affecting the HW/Device, you can ignore the tasks to communicate with the HW/Device. Please note, if you follow the steps from this section, the class is accompanied by an autoobject (a global object).

2. Within the class, add properties for each setting you want to control in the UI dialogs. Configure the property with the corresponding data type. For example, for a On/Off setting use the bool data type.

3. In your dialogs or wherever you need the widgets, you connect the widgets with the appropriate properties (step 2) from the global object (step 1). For example, the Toggle Button can be connected to a bool property.

Now the widgets reflect automatically the current value of the associated properties. When the user changes the state of the widget, the associated property is changed implicitly. If there are multiple widgets connected to the same property, all widgets are updates automatically when the property changes.

4. You can implement two methods in the class (step 1): Load() and Save(). The methods contain native code to transfer the property values to/from flash memory/file/... or whatever location you prefer to retain the settings.

5. Shortly before you enter the main-loop load the previously stored state by invoking the Load() method in context of the global autoobject (step 1). This would look like:

/* Obtain access to the global instance containing the settings */
ApplicationDeviceClass device = EwGetAutoObject( &ApplicationDevice,
                                                  ApplicationDeviceClass );

/* Invoke the function to load the settings */
ApplicationDeviceClass__Load( device );

5. Shortly before you leave the main-loop save the last state by invoking the Save() method in context of the global autoobject (step 1). This would look like:

/* Obtain access to the global instance containing the settings */
ApplicationDeviceClass device = EwGetAutoObject( &ApplicationDevice,
                                                  ApplicationDeviceClass );

/* Invoke the function to load the settings */
ApplicationDeviceClass__Save( device );

Does it help you further?

Best regards

Paul Banach

by
Paul

I suspected this was the general approach.

But thank you for providing all the detail.  I will take some time to digest this but I am sure it will save me some time in the long run.

Thank you

Robert.

Embedded Wizard Website | Privacy Policy | Imprint

...