Using Pre-gets and Post-sets
|Top Previous Next|
Like the Windows registry, STG library may be used to provide a well-organized, persistent storage for your device's operational parameters. One less obvious and very powerful concept of setting usage is that settings can serve as transit points for controlling and/or monitoring the device. That is, writing to a setting can cause some related code to be executed after the new value is set (post-write), and this code will put the new value "into action". Reading a setting can cause the value of the setting to be updated first (pre-read), and only then returned to the caller.
Imagine, for instance, that there is an "LS" (Lamp State) setting that defined if the "lamp" is on or off. Once we change the setting value through, say, a telnet command, or some kind of setup screen, the state of the lamp should change, too.
The elegant way of achieving this is by using callback_stg_post_set(). It is invoked every time stg_set() or stg_sg(,,,EN_STG_SET) is used. This callback procedure offers a catch-all place where you can respond to changing setting values.
Here is the code template for handling the lamp. The beauty of this approach is that the setting may be modified in several different places in your application, but you only need to respond to the setting value change in a single place — callback_stg_post_set() procedure:
Another callback procedure — callback_stg_pre_get() — is called every time your application reads a setting through stg_get() or stg_sg(,,,EN_STG_GET). This allows to update the setting value before returning it to the caller.
Example: Let's say you have a setting called "CT" (Current Temperature). Here is a code template for automatically updating the setting each time its value is requested:
The use of pre-gets and post-sets is further illustrated in our sample project.