Mechatronics Portfolio - Nick Greco
print_task.py File Reference

This file contains code for a task which prints things from a queue. More...

Functions

def print_task.put (a_string)
 Put a string into the print queue so it can be printed by the printing task whenever that task gets a chance. More...
 
def print_task.put_bytes (b_arr)
 Put bytes from a bytearray or bytes into the print queue. More...
 
def print_task.run ()
 Run function for the task which prints stuff. More...
 

Variables

 print_task.BUF_SIZE = const (100)
 The size of the buffer which will hold characters to be printed when the print task has time to print them.
 
bool print_task.THREAD_PROTECT = True
 A flag which is passed to the queue constructor to control whether the queue will protect puts and gets from being corrupted by interrupts.
 
bool print_task.PROFILE = True
 A flag which controls if the printing task is to be profiled.
 
 print_task.print_queue
 This queue holds characters to be printed when the print task gets around to it. More...
 
 print_task.print_task
 This is the task which schedules printing. More...
 

Detailed Description

This file contains code for a task which prints things from a queue.

It helps to reduce latency in a system having tasks which print because it sends things to be printed out the serial port one character at a time, even when other tasks put whole strings into the queue at once. When run as a low-priority task, this allows higher priority tasks to interrupt the printing between characters, even when all the tasks are being cooperatively scheduled with a priority-based scheduler.

Example code:

# In each module which needs to print something:
import print_task
# In the main module or wherever tasks are created:
shares.print_task = print_task.PrintTask (name = 'Printing',
buf_size = 100, thread_protect = True, priority = 0)
cotask.task_list.append (shares.print_task)
# In a task which needs to print something:
shares.print_task.put ('This is a string')
shares.print_task.put_bytes (bytearray ('A bytearray'))

Function Documentation

◆ put()

def print_task.put (   a_string)

Put a string into the print queue so it can be printed by the printing task whenever that task gets a chance.

If the print queue is full, characters are lost; this is better than blocking to wait for space in the queue, as we'd block the printing task and space would never open up. When a character has been put into the queue, the go() method of the print task is called so that the run method will be called as soon as the print task is run by the task scheduler.

Parameters
a_stringA string to be put into the queue

◆ put_bytes()

def print_task.put_bytes (   b_arr)

Put bytes from a bytearray or bytes into the print queue.

When characters have been put into the queue, the go() method of the print task is called so that the run method will be called as soon as the print task is run by the task scheduler.

Parameters
b_arrThe bytearray whose contents go into the queue

◆ run()

def print_task.run ( )

Run function for the task which prints stuff.

This function checks for any characters to be printed in the queue; if any characters are found then one character is printed, after which the print task yields so other tasks can run. This functino must be called periodically; the normal way is to make it the run function of a low priority task in a cooperatively multitasked system so that the task scheduler calls this function when the higher priority tasks don't need to run.

Variable Documentation

◆ print_queue

print_task.print_queue
Initial value:
1 = task_share.Queue ('B', BUF_SIZE, name = "Print_Queue",
2  thread_protect = THREAD_PROTECT, overwrite = False)
This class implements a queue which is used to transfer data from one task to another.
Definition: task_share.py:37

This queue holds characters to be printed when the print task gets around to it.

◆ print_task

print_task.print_task
Initial value:
1 = cotask.Task (run, name = 'Printing', priority = 0,
2  profile = PROFILE)
This class implements behavior common to tasks in a cooperative multitasking system which runs in Mic...
Definition: cotask.py:49

This is the task which schedules printing.