The 4d Systems module

Picaso Common

The 4d Systems module at present contains the Picaso Common driver, which at a minimum works for the Picaso uLCD-28PTU LCD display, but likely will also work for other displays in the same series.

digraph inheritance4372758e43 { rankdir=LR; size="8.0, 12.0"; "PyExpLabSys.drivers.four_d_systems.PicasoCommon" [URL="#PyExpLabSys.drivers.four_d_systems.PicasoCommon",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5)",target="_top",tooltip="Implementation of the common parts of the serial communication to the"]; "PyExpLabSys.drivers.four_d_systems.PicasouLCD28PTU" [URL="#PyExpLabSys.drivers.four_d_systems.PicasouLCD28PTU",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5)",target="_top",tooltip="Driver for the Picaso 28 PTU Pi LCD display"]; "PyExpLabSys.drivers.four_d_systems.PicasoCommon" -> "PyExpLabSys.drivers.four_d_systems.PicasouLCD28PTU" [arrowsize=0.5,style="setlinewidth(0.5)"]; }

Usage Example

import time
from PyExpLabSys.drivers.four_d_systems import PicasouLCD28PTU

# Text example
picaso = PicasouLCD28PTU(serial_device='/dev/ttyUSB0', baudrate=9600)
picaso.clear_screen()
for index in range(5):
    picaso.move_cursor(index, index)
    picaso.put_string('CINF')

# Touch example
picaso.move_cursor(7, 0)
picaso.put_string('Try and touch me!')
picaso.touch_set('enable')
for _ in range(25):
    time.sleep(0.2)
    print picaso.touch_get_status()
    print picaso.touch_get_coordinates()

picaso.close()

four_d_systems module

Drivers for the 4d systems displays

For usage examples see the file PyExpLabSys/test/integration_tests/test_four_d_systems.py

Note

Only a small sub-set of the specification is plemented, but with the available examples it should be real easy to add more commands.

Note

An internal method ‘_to_16_bit_rgb’ exists to convert a HTML hex color code or an RGB tuple of floats to the irregular 16 bit RGB color scale this device use. It should make working with colors a lot easier.

Note

The displays must be activated for serial communication. At present the only way we know how to do that, is to follow the procedure described in the serial specification, which involves taking it past a Windows program.

Note

At present only communication via the USB connection has been tested. For communication directly via the internal connection on the Raspberry Pi it may be necessary to do some preparation in order to free the pins up for serial communication.

PyExpLabSys.drivers.four_d_systems.to_ascii(string)[source]

Convert non-ascii character in a unicode string to ascii

PyExpLabSys.drivers.four_d_systems.to_ascii_utf8(string)[source]

Convert non-ascii characters in a utf-8 encoded string to ascii

PyExpLabSys.drivers.four_d_systems.to_word()

pack(format, v1, v2, …) -> bytes

Return a bytes object containing the values v1, v2, … packed according to the format string. See help(struct) for more on format strings.

PyExpLabSys.drivers.four_d_systems.to_words(*args)[source]

Convert integers or tuples of integers to 2 byte words

This will convert e.g args:

1, 200 -> b'\x00\x01\x00\xc8'
(1, 200) -> b'\x00\x01\x00\xc8'
Parameters:args – integers or tuple of lists of integers
Returns:The corresponding bytes string
Return type:bytes
PyExpLabSys.drivers.four_d_systems.to_gci(image, resize=None)[source]

Convert an imame to PICASO GCI format bytestring

Parameters:
  • image (str or PIL.Image) – Path of image or PIL.Image object
  • resize (tuple) – A 2 element tuple (x, y) for resizing
class PyExpLabSys.drivers.four_d_systems.PicasoCommon(serial_device='/dev/ttyUSB0', baudrate=9600, debug=False)[source]

Bases: object

Implementation of the common parts of the serial communication to the Picaso devices

Raises::py:class:`serial.serialutil.SerialException` - All public methods – in this class may raise this exception if there are problems with the serial communication
__init__(serial_device='/dev/ttyUSB0', baudrate=9600, debug=False)[source]

Initialize the driver

The serial device and the baudrate are configurable, as described below. The rest of the serial communication parameters are; bytesize: 8 bits, parity: None, stopbits: one (as per the manual) and a timeout of 3 seconds.

Parameters:
  • serial_device (str) – The serial device to open communication on
  • baud_rate (int) – The baudrate for the communication. This should be the baud rate the display is set for per default. The baud can be changed after instantiation with the set_baud_rate() method
  • debug (bool) – Enable a check of whether there are bytes left in waiting after a reply has been fetched.
close()[source]

Close the serial communication

_send_command(command, reply_length=0, output_as_bytes=False, reply_is_string=False)[source]

Send a command and return status and reply

Parameters:
  • command (bytes) – The command to send e.g. b’ÿÍ’ to clear the screen
  • reply_length (int) – The length of the expected reply i.e. WITHOUT an acknowledge.
  • output_as_bytes (bool) – Return bytes instead of int
  • reply_is_string (bool) – Overrides the reply_length and read the reply length from the reply itself. Applicaple only to variable length strings.
Returns:

If a return value is requested (with reply_length) a int will be returned formed from the bytes. If output_as_bytes is set, then the raw bytes are returned

Return type:

bytes or int

Raises:

PicasoException – If the command fails or if the reply does not have the requested length

static _to_16_bit_rgb(color)[source]

Convert a color to the non regular 16 bit RGB.

Parameters:color (str or tuple) – 24 bit RGB HTML hex string e.g. '#ffffff' or RGB tuple or floats e.g. (1.0, 1.0, 1.0)
Returns:An integer that represents the 2 bytes
Return type:str
static _from_16_bit_rgb(color)[source]

Convert a non regular 16 bit RGB to tuple of float e.g (1.0, 0.0, 1.0)

Parameters:color (int) – Integer representing the two bytes that form the color
Returns:Color as tuple of floats e.g. (1.0, 0.0, 1.0)
Return type:tuple
move_cursor(line, column)[source]

Move the cursor to line, column

The actual position in which the cursor is placed is based on the current text parameters such as width and height

Parameters:
  • line (int) – The line number to move the cursor to
  • column (int) – The column to move the cursor to
Raises:

PicasoException – If the command fails

put_string(string)[source]

Write a string on the display

Note

It has not been investigated whether characters outside of ASCII can be used. If that becomes necessary, try, and possibly consult the manual

Parameters:string (str) – Ascii string to write, max length 511 chars
Returns:The number of bytes written
Return type:int
Raises:PicasoException – If the command fails or if the reply does not have the expected length
character_width(character)[source]

Get the width of a character in pixels with the current font

Parameters:

character (str) – Character to get the width of

Returns:

The width in pixels

Return type:

int

Raises:
  • PicasoException – If the command fails or if the reply does not have the expected length
  • ValueError – If character does not have length 1
character_height(character)[source]

Get the height of a character in pixels with the current font

Parameters:

character (str) – Character to get the height of

Returns:

The height in pixels

Return type:

int

Raises:
  • PicasoException – If the command fails or if the reply does not have the expected length
  • ValueError – If character does not have length 1
text_foreground_color(color)[source]

Sets the foreground color of the text

Parameters:color (tuple or string) – 24 bit RGB HTML hex string e.g. ‘#ffffff’ or RGB tuple or floats e.g. (1.0, 1.0, 1.0)
Returns:Previous color as tuple of floats e.g. (1.0, 1.0, 1.0)
Return type:tuple
Raises:PicasoException – If the command fails or if the reply does not have the expected length
text_background_color(color)[source]

Sets the background color of the text

Parameters:color (tuple or string) – 24 bit RGB HTML hex string e.g. ‘#ffffff’ or RGB tuple or floats e.g. (1.0, 1.0, 1.0)
Returns:Previous color as tuple of floats e.g. (1.0, 1.0, 1.0)
Return type:tuple
Raises:PicasoException – If the command fails or if the reply does not have the expected length
text_width(factor)[source]

Sets the text width

Parameters:factor (int) – Width multiplier (1-16) relative to default width
Returns:Previous width multiplier
Return type:int
Raises:PicasoException – If the command fails or if the reply does not have the expected length
text_height(factor)[source]

Sets the text height

Parameters:factor (int) – Height multiplier (1-16) relative to default height
Returns:Previous height multiplier
Return type:int
Raises:PicasoException – If the command fails or if the reply does not have the expected length
text_factor(factor)[source]

Sets the text width and height

Parameters:factor (int) – Width and height multiplier (1-16) relative to default width and height
Returns:Previous width and height multipliers
Return type:tuple
Raises:PicasoException – If the command fails or if the reply does not have the expected length
text_x_gap(pixels)[source]

Sets the horizontal gap between chars in pixels

Parameters:pixels (int) – The requested horizontal gap in pixels
Returns:The previous horizontal gap in pixels
Return type:int
Raises:PicasoException – If the command fails or if the reply does not have the expected length
text_y_gap(pixels)[source]

Sets the vertical gap between chars in pixels

Parameters:pixels (int) – The requested vertical gap in pixels
Returns:The previous vertical gap in pixels
Return type:int
Raises:PicasoException – If the command fails or if the reply does not have the expected length
text_attribute(attribute, status=True)[source]

Sets the text attribute status

Parameters:
  • attribute (str) – The attribute to set, can be one of ‘bold’, ‘inverse’, ‘italic’, ‘opacity’ and ‘underline’ where ‘inverse’ inter changes the back and foreground colors of the text
  • status (bool) – Boolean for the text attribute status, where True means ‘on’ or ‘opaque’ in the case of opacity
Returns:

The previous status

Return type:

bool

Raises:
  • PicasoException – If the command fails or if the reply does not have the expected length
  • ValueError – If attribute is unknown
clear_screen()[source]

Clear the screen

Raises:PicasoException – If the command fails
draw_line(start, end, color)[source]

Draw a line from x1, y1 to x2, y2 and return boolean for success

Parameters:
  • start (tuple) – Start point (x, y), where x and y are ints
  • end (tuple) – End point (x, y), where x and y are ints
  • color (tuple or string) – 24 bit RGB HTML hex string e.g. ‘#ffffff’ or RGB tuple or floats e.g. (1.0, 1.0, 1.0)
Raises:

PicasoException – If the command fails

draw_rectangle(top_left, bottom_right, color)[source]

Draw a rectangle

Parameters:
  • top_left (tuple) – Coordinates of top left corner (x, y)
  • bottom_right (tuple) – Coordinates of bottom right corner (x, y)
  • color (tuple or string) – 24 bit RGB HTML hex string e.g. ‘#ffffff’ or RGB tuple or floats e.g. (1.0, 1.0, 1.0)
Raises:

PicasoException – If the command fails

draw_filled_rectangle(top_left, bottom_right, color)[source]

Draw a filled rectangle

Parameters:
  • top_left (tuple) – Coordinates of top left corner (x, y)
  • bottom_right (tuple) – Coordinates of bottom right corner (x, y)
  • color (tuple or string) – 24 bit RGB HTML hex string e.g. ‘#ffffff’ or RGB tuple or floats e.g. (1.0, 1.0, 1.0)
Raises:

PicasoException – If the command fails

put_pixel(x, y, color)[source]

Set a pixel

move_origin(x, y)[source]

Move the origin to a point, forming the basis for the next graphics or text command

Parameters:
  • x (int) – X-coordinate for the new origin
  • y (int) – Y-coordinate for the new origin
Raises:

PicasoException – If the command fails

screen_mode(mode)[source]

Sets the screen mode

Parameters:mode (str) – The mode for the screen. Can be either 'landscape', 'landscape reverse', 'portrait' or 'portrait reverse'
Returns:Returns previous screen mode on success or None on failure
Return type:str
Raises:PicasoException – If the command fails or if the reply does not have the expected length
get_graphics_parameters(parameter)[source]

Gets graphics parameters

Note

The meaning of the results from the 'last_object_*' parameters is not known. It was expected to be coordinates, but they are much to large

Parameters:parameter (str) – The parameter to fetch, can be 'x_max' for the x resolution under the current orientation, 'y_max' for the y resolution under the current orientation, 'last_object_left', 'last_object_top', 'last_object_right', 'last_object_bottom' for the relevant parameter for the last object.
Returns:The requested parameter
Return type:int
Raises:PicasoException – If the command fails or if the reply does not have the expected length
media_init()[source]

Initialize a uSD/SD/SDHC memory card for media operations

Returns:True is the memory card was present and successfully initialized and False otherwise
Return type:bool
set_sector_address(high_word, low_word)[source]

Set the internal adress pointer for a sector

The two argumenst are the two upper and lower bytes of a 4 byte sector address respectively. I.e. the total sector address will be formed as: high_word * 65535 + low_word

Parameters:
  • high_word (int) – An integer between 0 and 65535. See above.
  • low_word (int) – An integer between 0 and 65535. See above.
read_sector()[source]

Read the block (512 bytes) at the sector address pointer

Raises:PicasoException – If the sector read fails

The address pointer is set with set_sector_address(). After the read, the sector address pointer will be incremented by 1.

write_sector(block)[source]

Write a block (512 bytes) to the sector at the sector address pointer

Note

The size of block must be 512 bytes

Note

Rememer to call flush() after writes to ensure that they are written out to the SD-card

Returns:Write successful
Return type:bool

The address pointer is set with set_sector_address(). After the write, the sector address pointer will be incremented by 1.

write_sectors(blocks)[source]

Write multiple blocks to consequtive sectors starting at the sector adddress pointer

This is a convenience method around several calls to write_sector().

Note

The size of blocks must be a multiple of 512 bytes

Note

Rememer to call flush() after writes to ensure that they are written out to the SD-card

Parameters:blocks (bytes) – The data to write
Returns:Whether all writes were successful
Return type:bool
flush_media()[source]

Ensure that data written is actually written out to the SD card

Returns:Whether flush operation was successful
Return type:bool
display_image(x, y)[source]

Display the image at the sector pointer or byte pointer at x, y coordinates

Parameters:
  • x (int) – X-coordinate of the upper left corner
  • y (int) – Y-coordinate of the upper left corner
set_baud_rate(baud_rate)[source]

Set the communication baud rate

Note

This change will affect only the current session

Parameters:baud_rate (int) – The baud rate to set. Valid values are keys in BAUD_RATES
touch_detect_region(upper_left, bottom_right)[source]

Specify a touch detection region

Parameters:
  • upper_left (tuple) – (x, y) for the upper left corner, where x and y are ints
  • bottom_right (tuple) – (x, y) for the lower right corner, where x and y are ints
Raises:

PicasoException – If the command fails

touch_set(mode)[source]

Set touch screen related parameters

Parameters:
  • mode (string) – The mode to set. It can be either 'enable';
  • enables and initializes the touch screen, 'disable' (which) –
  • disables the touch screen or 'default' which will reset (which) –
  • current active region to the default which is the full screen (the) –
  • area.
Raises:

PicasoException – If the command fails

touch_get_status()[source]

Return the state of the touch screen

Returns:The state of the touch screen, can be either 'invalid/notouch', 'press', 'release', 'moving' or None on error
Return type:str
Raises:PicasoException – If the command fails or if the reply does not have the expected length
touch_get_coordinates()[source]

Return the coordinates of the LAST touch event

Returns:(x, y) where x and y are ints
Return type:tuple
Raises:PicasoException – If the command fails or if the reply does not have the expected length
get_display_model()[source]

Get the display model

Returns:The display model
Return type:str
Raises:PicasoException – If the command fails or if the reply does not have the expected length
get_spe_version()[source]

Get the version of the Serial Platform Environment

Returns:The version or None on failure
Return type:str
Raises:PicasoException – If the command fails or if the reply does not have the expected length
class PyExpLabSys.drivers.four_d_systems.PicasouLCD28PTU(serial_device='/dev/ttyUSB0', baudrate=9600, debug=False)[source]

Bases: PyExpLabSys.drivers.four_d_systems.PicasoCommon

Driver for the Picaso 28 PTU Pi LCD display

For details on the methods that can be called on this class see the documentation for PicasoCommon

__init__(serial_device='/dev/ttyUSB0', baudrate=9600, debug=False)[source]

Initialize the driver

The serial device and the baudrate are configurable, as described below. The rest of the serial communication parameters are; bytesize: 8 bits, parity: None, stopbits: one (as per the manual) and a timeout of 3 seconds.

Parameters:
  • serial_device (str) – The serial device to open communication on
  • baud_rate (int) – The baudrate for the communication. This should be the baud rate the display is set for per default. The baud can be changed after instantiation with the set_baud_rate() method
  • debug (bool) – Enable a check of whether there are bytes left in waiting after a reply has been fetched.
exception PyExpLabSys.drivers.four_d_systems.PicasoException(message, exception_type)[source]

Bases: Exception

Exception for Picaso communication

The exception_type parameter can be either, 'failed' or 'unexpected_reply'

__init__(message, exception_type)[source]

Initialize self. See help(type(self)) for accurate signature.

class PyExpLabSys.drivers.four_d_systems.Button(picaso, top_left, bottom_right, text, text_justify='center', left_justify_indent=None, text_color='#000000', inactive_color='#B2B2B2', active_color='#979797')[source]

Bases: object

Class that represents a button to use in the interface

__init__(picaso, top_left, bottom_right, text, text_justify='center', left_justify_indent=None, text_color='#000000', inactive_color='#B2B2B2', active_color='#979797')[source]

Initialize self. See help(type(self)) for accurate signature.

set_position(top_left, bottom_right)[source]

Set position of the button

draw_button(active=False)[source]

Draw button with either its active or inactive background color