ME405 Library  Version 0.38
Python library in support of ME405 class
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.print_task_function ()
 Task function for the task which prints stuff. More...
 

Variables

 print_task.PT_BUF_SIZE = const (1000)
 The size of the buffer which will hold characters to be printed when the print task has time to print them.
 
 print_task.print_queue
 This queue holds characters to be printed when the print task gets around to it. 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:
print_task = cotask.Task (print_task_function, name = 'Printing',
priority = 0, profile = True)
cotask.task_list.append (print_task)
# In a task which needs to print something:
print_queue.put ("This is a string")
print_queue.put_bytes (bytearray ("A bytearray"))
print_queue.put ("A number: {:d}\r\n".format (number))
Implements multitasking with scheduling and some performance logging.
Definition: cotask.py:66

It is intended for educational use only, but its use is not limited thereto. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Function Documentation

◆ print_task_function()

def print_task.print_task_function ( )

Task 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 function 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.

◆ 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

Variable Documentation

◆ print_queue

print_task.print_queue
Initial value:
1 = task_share.Queue ('B', PT_BUF_SIZE, name = "Print Queue",
2  thread_protect = False, overwrite = False)
A queue which is used to transfer data from one task to another.
Definition: task_share.py:90

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

It is always created when print_task is imported as a module.