Step 4: Completing the Project
This step corresponds to test_stg_lib_4.
OK, settings defined, comms protocol is their to read and write them, now let's put all this to good use. Rules of engagement:
•Writing new value into the "CPTN" setting should play a corresponding pattern stored in the "PTN" setting.
•Briefly pressing the MD button should play the next pattern.
Our sample project nicely illustrates what was said in Using Pre-gets and Post-sets. When the "CPTN" setting is being written to, callback_stg_post_set() is called and this gives us a chance to "load" another pattern. Notice how this is done! We can't use stg_get() or stg_sg() inside callback_stg_post_set() — this limitation is explained in Using Pre-gets and Post-sets. We, however, need to read the "PTN" setting in order to play the new pattern!
Luckily, there is almost always a workaround. In our case, we load a very short blank pattern "-" instead. When this pattern is done playing, the on_pat() event is generated and then we read one of the "PTN" members according to the pattern_num variable.
Callback_stg_pre_get() is also useful in our project. When we send a command to read the current value of the "CPTN" setting, the setting is first updated with the current value of the pattern_num variable!
Notice also how stg_get_num_members() is used in play_next_pattern(). Each time the button is pressed, pattern_num is incremented by one, until it reaches the number of members in the "PTN" setting, after which it is reset back to 0.
Here is the I/O Ninja (ninja.tibbo.com) session where we switched between different patterns:
** Tibbo Basic **
Established TCP connection with 192.168.1.93:1000 from 1723 SCPTN,0,1<CR> A<CR> ASCPTN,0,2<CR> A<CR>
Try this and observe how different patterns start playing on the LEDs.