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:

 

 

sub callback_stg_post_set(byref stg_name_or_num as string, index as byte,byref stg_value as string)

 if stg_name_or_num="LS" then

         if stg_value=0 then

                 'turn the lamp off

         else

                 'turn the lamp on

         end if

 end if

end sub

 

 

Note that stg_restore_multiple() and stg_restore_member() also write to settings and callback_stg_post_set() will be called for them too — once for each setting affected.

 

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:

 

 

sub callback_stg_pre_get(byref stg_name_or_num as string,index as byte,byref stg_value as string)

 if stg_name_or_num="CPTN" then

         stg_value=get_temperature() 'some function in your project that returns current T

 end if

end sub

 

 

The use of  pre-gets and post-sets is further illustrated in our sample project.

 

Because TIDE compiler does not allow recursions, some STG library's own procedures can't be called from within callback_stg_post_set() and callback_stg_pre_get(). These procedures are:

Stg_start()
Stg_check_all()
Stg_restore_multiple()
Stg_restore_member()
Stg_get()
Stg_set()
Stg_sg()