Task Scheduling in PiR2 v04 as of 2020 E May 05 by D@CC Schedule of Tasks The actual schedule is an array (named a_t) of future tasks. Each row contains 1. launch-time (earliest time that it can be launched 2. task name to launch 3. parameter1 for the task when launched Callable Tasks ************** Add_TASK() #done -- Next_Launchable_TASK_Number() Remove_TASK() #done -- TASK_Sleep() #done actually runs the task Next_TASK() init_TASK() #done -- launch_TASK() #done -- Add_TASK(a_t,dT, task_name, parameter1) self -explanatory, will launch after dT sec Next_Launchable_TASK_Number(a_t) # returns the number of the next task that can be launched at this time or -1 Launch_TASK(a_t,n) actually runs task n Remove_TASK_Number(a_t, n) remove task number n from the task schedule TASK_Sleep(a_t,dT) # This will run any launchable tasks, if none it will sleep this will 1) time_in= time (actual clock time) 2) time_resume = time + dT # launch any launchable tasks while time not expired 3) while time < time_resume #(time is actual clock time) #run 1 launchable task Next_Task= Next_Launchable_Task_Number if Next_Task == -1 : # no more tasks so break # out of the while else : 3) Remove_Task(a_t,Next_task) 4) Launch_Task(a_t,Next_task) #end if #end while # there is no more time or no more launchable tasks 4) if time_resume > time : #if there is more time sleep (time_resume - time) #end if #end def NexT_TASK(a_t) # this will return the # of the Next Task to run # NB its launch-time might be in the future # if returns 0, there are no tasks in the schedule ********************************************************************************************************** Main PiR2 routine def pir2_main() ############################################################# # ???? ############################################################# array a_t[100,3] #define the schedule array sec1=1 sec2=2 sec15=15 process1() #init init_TASK(a_t,100) add_TASK(a_t,0,"PiR2_Init","") TASK_Sleep(-1) #schedule PiR2_Init for "yesterday" # Initialize PiR2 main #subrB() add_TASK(a_t,0,"PiR2_Sound_Detected","") # at end it will reschedule itself then TASK_Sleep(sec(1)) # keep doing this every 1 sec until ^C add_TASK(a_t,0,"PiR2_Loop","") # at end it will reschedule itself then TASK_Sleep(sec(15)) # keep doing this every 15 sec until ^C while True : try : while True ; # #Launch any launchable tasks TASK_Sleep(sec2) # if none, sleep for 2 seconds #end while escape: break #out of while #end while # exit from program n=Next_TASK(a_t) if n>0 : name=a_t(n, 1) print("^C but task:",name," remains unlaunched") #end if return 0 # end def def PiR2_Sound_Detected(parm1) print("in PiR2_Sound_Detected") add_TASK(a_t,sec1,'PiR2_Sound_Detected',"SD") return #end def def PiR2_Loop(parm1) print("in PiR2_Loop") input("reschedule this Task"), answer if answer != "n" add_TASK(a_t,sec15,'PiR2_Loop',"L") #end if return #end def def PiR2_Init(parm1) print("in PiR2_Init") return #end def ********************************************************************************************************** def init_TASK(a_t,n) # Purpose: to initialize the schedule array ################################### # ################################### a_t=[n,3] for i = 0 to len(a_t) : remove_TASK(a_t,i) #end for #end def def add_TASK(a_t,dT,task_name,parm1_value) is_room = False for i=0 to len(a_t): if a_t[i,0]="" : is_room = True ############################################## # time ############################################## a_t[i,0]=time+dT a_t[i,1]=task_name a_t[i,2]=parm1_value #end if # end for if is_room == False : print("PiR2 warning - No room in schedule array: a_t[]") #end if return #end def def launch_TASK(a_t,n) #Purpose: to launch a task if time > a_t[n,0] : #is it time to launch it name= a_t[n,1] parm1 = a_t[n,2] # # launched tasks don't return any result # parameters are "called by value" # if name="PiR2SoundDetected" :call PiR2_SoundDetected(parm1) if name="PiR2Loop" :call PiR2_Loop(parm1) if name="PiR2Init" :call PiR2_Init(parm1) else : print("PiR2 Warning: Its too early to launch task:",n) #end if return #end def def remove_TASK(a_t,n) #Purpose: to remove task n from the array if n > len(a_t) : a_t[n,0]="" a_t[n,1]="" a_t[n,2]="" else : print ("Pir2 warning:", n, " is too large") #end if # removed it whether it was already romoved or not return # end def def display_TASK_array(a_t) #Purpose : To display all non-null rows of the TASK array is_empty= True ########################################### # syntax of for ########################################### print("row"," time , name parm1") for i=0 to len(a_t) : if (a_t[i,0] != "" ) : is_empty= False print (i,",",a_t[i,0],",",a_t[i,1],",",a_t[i,2]) #end if #end for if is_empty =="" : print"a_t is empty") #end if return #end def /PiR2_Task_Scheduling_04.txt