Scheduling & Background Tasks
| process
pskill |
ps
pschange pspause |
Some subroutines are more useful if they can be made to monitor events or be made to run every so often. This is the purpose of scheduling, it is to place a function in a queue and then run it at set intervals, the main word for placing a function in the queue is ‘process’.
The intention of the scheduler is to monitor or process in relatively slow time ( 20+ mS ) and not to be used as an event capture or a timing system. The reason for this, is that the scheduler is called every 1mS by a system interrupt and ALL of the scheduled functions are checked in turn. This works well for when there is a relatively long time interval or few scheduled tasks. The times requested using the 'process' statement are also just approximate as these can be pushed out depending on how many processes are scheduled and how long it takes to complete a processed function.
To illustrate this, two functions have been scheduled, all they do is toggle a port pin each time they are called. The code for this is here:
setport "e1" as output
setport "e2" as output
process p1 every 5
process p2 every 10
endf
function p1
port "e1" T
endf
function p2
dim j
port "e2" 1
for j = 1 to 15
port "e2" T
next
port "e2" 0
endf

The function p1 has been scheduled every 5ms, p1 takes just less than 1ms to complete. P2 has been scheduled for 10ms but P2 takes about 10 ms to complete. The effect of this can be seen on the scope trace. Take the centre vertical line as the starting point, P1 gets called and the line is toggled low. When P1 completes P2 gets called and takes about 10ms. This prevents P1 from being called and also delays the start of the next call to P2.
It is important therefore to understand that the process timings depend heavily on what the actual processes are and how long they take. Ideally the functions should last less than the process call, i.e a function that takes 10ms as in p1 above should be called every, say, 20ms. This will help assure that P2 gets called on time but will still block P1 whilst it is running.
| process |
| process <function>
every <time> process keypad every 35 process slow every 1000 |
This statement places a function in a queue (which must exists) and then runs it at set intervals. The time interval is in milliseconds. Please see the notes above about the tiimg.
Notes:
- There is a limit to the number of subroutines that can be placed in a queue, currently 20.
- The New statement will clear and initialise the queue, as will other statements such as reload and fclear.
- There will be a limit to just how many subroutines can be practically serviced, this will depend on the frequency and complexity of the subroutines and so cannot be determined in advance.
- Do not use the wait statement inside a scheduled routine.
| ps |
| ps |
This simply lists the current processes in the queue. Each process is given an ID and this ID value is used for the other statements. The output from this statement is:
ID
Name
Address Pause Interval
1 kp_proc_scan 0x9d038159
0
35
ID: is the process identifier, this number can be used in subsequent process keywords. Name: is the name of the function that is associated with the ID. Address: is the subroutines address. Pause: if set to 1 means that the process has been paused, 0 means that it hasn't. Interval: this is the requested time interval that the function should run and will be the same number as entered when setting up the process.
| pschange |
| pschange <ID>
<interval> pschange 3 500 |
This is used to change the interval of a running process. The ID number is used to identify the process and can be found with the ‘ps’ statement.
| pspause |
| pspause <ID> pspause 3 1 pspause 3 0 |
Pauses a running task without it being removed from the process queue. A ‘1’ following the process ID will cause that task to pause, a 0 will cause it to resume.
| pskill |
| pskill <ID> pskill 2 |
Removes a process form the queue. The process ID will identify the process to remove.