A N 3 7 0
32
Rev. 1.0
7.2.3. Error Cases
It is possible to underflow the PTS. In this case, where the application pops too many elements out of the PTS, a
button vector of zero and a timestamp of zero will be returned. As soon as another button push is qualified it will be
pushed onto the PTS. The application needs to check the number button state change items store in the PTS FIFO
by calling bBsr_GetPtsItemCnt() function before calling the item pop function wBsr_Pop().
It is also possible to overflow the PTS. In this case, where the application doesn’t pop from the PTS fast enough,
two outcomes are possible.
1. If the button vector to be written is all 0’s, indicating that all buttons were released, the PTS is initialized. All
elements are set to zero along with all pointers and counts and the PTS button change FIFO is empty. Then the
“all buttons released” (button vector 0x00) item is pushed to the empty FIFO to notify application about the “all
buttons released” fact, so the application can terminate gracefully.
2. If the button vector to be written shows that at least 1 button is pushed, the button push/release information is
dropped and not written to the PTS. This is assuming that the PTS is already full of unique button vectors and
the application will not be expected to handle more. In order to avoid overflows of the PTS the user should
increase its bPtsSize size accordingly.
7.2.4. Button Sampling and Master Time
Whenever the qualified button change is detected by the vBsr_Service(), the function attaches a real time stamp to
the button status vector, representing a time of the button value change. If the user is not interested in that time
stamp, bTimestamp in the PTS structure below, it is not necessary to implement the master clock.
However, the user might find the timestamp information useful. If the correct time stamp is desired, then the user
must implement a mechanism for keeping the master time. Master time is a system variable implemented as a
LWORD 32-bit unsigned number. It is accessed by 3 API functions:
vSys_SetMasterTime() initializes the master time variable
vSys_IncMasterTime() adds user specified value to it
lSys_GetMasterTime() returns the current value of the master time
The
vBsr_Service()
function calls the lSys_GetMasterTime() function. It is fully up to the user application to setup
the master time.
One suggested way is to enable the real time clock RTC timer with a 1 or 5 ms tick. Then the RTS ISR routine
implements both the master time update and the call to the vBsr_Service() routine. User might find it beneficial if
the actual real time value of the master time is kept in “human” time units, say 1ms increments.
For example, if the RTC timer is interrupting the system every 5 ms then the master time variable can be
incremented by 5 by calling vSys_IncMasterTime(5), keeping the master time in [ms] units. If the vBsr_Service()
routine is then called per each RTC ISR invocation, the user will have effectively 5ms sampling interval of the input
buttons. The button change time stamp added to the PTS structure as bTimestamp is always the master time
divided by 32:
bTimestamp = lSys_GetMasterTime() / 32;
If the master time value is kept in [ms] then the master time stamp maximum time before overflow is 255 x 32 ms =
8.160 seconds. If the user desires longer time stamp the granularity of the master time has to change to 5 ms
“units”.
Obviously, not every invocation of the RTC ISR needs to call the vBsr_Service() routine. Only when the user
desires to sample the buttons the vBsr_Service() has to be called. It is fully up to the application to control the rate
of button sampling.
The following pages show an example of the RTC running at 5 ms with button sampling interval of 5 ms. Those are
just snippets of code to give an idea about the usage of BSR and master time.
As an alternative, the user can use a global variable setup in the main application and updated from the RTC, to
call the vBsr_Service() every other RTC invocation (e.g., implementing a 10 ms button sampling interval). By a
similar mechanism longer sampling intervals are possible.