pisoc.py

This is the highest level module import for the PiSoC API, which the user should import into their scripts for full use of it. It is used to keep records of some globally used variables between all classes.

It is also used to send data to, and receive data from the PiSoC using a user chosen communication protocol.

Note

Most of the classes and functions described in this section have little use to most users. They generally will only be useful when implementing new functionality in the firmware.

At the beginning of each script, insert the following two lines:

>>> from pisoc import * #This will handle dependencies
>>> PiSoC('PC', log_level = 'info') #for PC
>>> PiSoC('PI', log_level = 'info') #for PI

PiSoC parent class

class pisoc.PiSoC
Class:

This class is used to define the register locations of each component on the PiSoC, it defines which communication protocol will be utilized by the API, and it collects compiled data from the PiSoC. The class is not to be assigned to an object; it only needs to be called so that addresses can be defined, compiled data on the PiSoC can be verified, and so that the communication protocol can be initialized.

Optionally, one can construct the object with a logging parameter, as described in the __new__() description, which will print diagnostic information to the terminal.

Example:
>>> from pisoc import *
>>> PiSoC('USB', log_level = 'info')

static __new__(protocol='UART', com_port='/dev/ttyAMA0', baud=9600, log_level=None)
Method:

__new__

Description:

Constructs the PiSoC object. It will decide the communication protocol to be utilized by the API and it will attempt to learn what components are available on the PiSoC

Parameters:
  • protocol (str) – Describes how the PiSoC will communicate with the host device. For simplicity, one can choose “PC” or “PI” so that the generic protocols for those devices will be chosen. The explicit protocol can be specified as “USB” or “UART” as well, for the standard API. With updated firmware, “I2C” and “SPI” will be valid choices.
  • com_port (str) – Optional parameter used to specify a com port for UART, if required. Defaults to “/dev/ttyAMA0”
  • baud (int) – Baud rate to be used for USB UART. Defaults to 9600, but any standard rate is accepted.
  • log_level (str) –

    Determines what messages will be printed to the console when possible.

    debug:All messages will be printed.
    info:All messages except “debug” level messages will be printed.
    warn:Only warnings and error messages will be printed
    error:Only error messages will be printed
Returns:

None

USB communication (USB UART)

class pisoc.USB_UART(baudr=9600)
Class:Provides a serial (USB UART) interface for communicating with the PiSoC
Note:pyserial 2.7 must be available to use this class
Method:__init__
Description:Constructs and initializes a USB_UART object. This is the default protocol for PC use. No serial port should need to be provided, as long as the drivers are properly installed. This function should find the PiSoC automatically.
Parameters:baud (int) – Baud rate to be used. Defaults to 9600, but can be any standard baud rate. (\(9600 * 2^n\)). The Port may need to be configured for higher baud rates on some operating systems (Windows), or the OS may not honor the request.
Returns:None
cleanup()
Method:cleanup
Description:Forces a software reset on the PiSoC and then closes and cleans up the serial port
Returns:None
disconnect()
Method:disconnect
Description:Forces a clean disconnection from the PiSoC, if it is connected.
Returns:None
is_connected()
Method:is_connected
Description:Checks to see if the serial port is open, and that it is connected to a PiSoC
Returns:Boolean value (True/False) indicating whether the PiSoC is connected or disconnected, respectively.
receive_data(*args, **kwargs)
Method:

receive_data

Description:

Called when a returned value is desired from the PiSoC. It will send a command, and then wait for a response. If the response appears valid, it will be unpacked and returned, otherwise debug messages will be logged indicating possible errors.

Parameters:
  • args (unpacked iterable) – a variable length of data to be transferred to the PiSoC (limited to 1 byte per element and a maximum of 55 bytes). This will contain data required for various commands on the PiSoC.
  • Hformat (list) – list of indices of args that should be formatted as unsigned short, independent of their length. Defaults to [2], as this is generally required.
Returns:

Unpacked data from the PiSoC, indicating the returned value.

Raises:
reconnect()
Method:reconnect
Description:Used to reestablish a connection to the PiSoC after it has been lost
Returns:Boolean value (True/False) indicating whether the reconnection was successful or unsuccessful, respectively.
send_data(*args, **kwargs)
Method:

send_data

Description:

Sends data to the PiSoC, without requiring a return value. It will wait to be sure the provided arguments produce a valid command on the PiSoC, and return an error code if not.

Parameters:
  • Hformat (list) – list of indices of args that should be formatted as unsigned short, independent of their length. Defaults to [2], as this is generally required.
  • args (unpacked iterable) – a variable length of data to be transferred to the PiSoC (limited to 1 byte per element and a maximum of 55 bytes). This will contain data required for various commands on the PiSoC.
Returns:

None

Raises:

UART communication

class pisoc.UART(com='/dev/ttyAMA0', baudr=115200)
Method:

__init__

Description:

Constructs and initializes a UART object. This is the default protocol for Raspberry Pi use.

Parameters:
  • com (str) – Serial port to be used for communication. Defaults to /dev/ttyAMA0
  • baud (int) – Baud rate to be used. Defaults to 115200, which is required for use with the default firmware. This can be changed, but will only work desireably with adjusted firmware.
Returns:

None

cleanup()
Method:cleanup
Description:Forces a software reset on the PiSoC and then closes and cleans up the serial port
Returns:None
receive_data(*args, **kwargs)
Method:

receive_data

Description:

Called when a returned value is desired from the PiSoC. It will send a command, and then wait for a response. If the response appears valid, it will be unpacked and returned, otherwise debug messages will be logged indicating possible errors.

Parameters:
  • args (unpacked iterable) – a variable length of data to be transferred to the PiSoC (limited to 1 byte per element and a maximum of 55 bytes). This will contain data required for various commands on the PiSoC.
  • Hformat (list) – list of indices of args that should be formatted as unsigned short, independent of their length. Defaults to [2], as this is generally required.
Returns:

Unpacked data from the PiSoC, indicating the returned value.

send_data(*args, **kwargs)
Method:

send_data

Description:

Sends data to the PiSoC, without requiring a return value. It will wait to be sure the provided arguments produce a valid command on the PiSoC, and return an error code if not.

Parameters:
  • args (unpacked iterable) – a variable length of data to be transferred to the PiSoC (limited to 1 byte per element and a maximum of 55 bytes). This will contain data required for various commands on the PiSoC.
  • Hformat (list) – list of indices of args that should be formatted as unsigned short, independent of their length. Defaults to [2], as this is generally required.
Returns:

None

I2C communication

class pisoc.I2C(addr=7, bus=1)
Method:

__init__

Description:

Constructs and initializes an I2C object. This is the default protocol for the Raspberry Pi 3 to use.

Parameters:
  • addr (int) – 7-bit I2C address for the PiSoC.
  • bus (int) – I2C bus to communicate over. Defaults to 1 for the Raspberry Pi.
Returns:

None

receive_data(*args, **kwargs)
Method:

receive_data

Description:

Called when a returned value is desired from the PiSoC. It will send a command, and then wait for a response. If the response appears valid, it will be unpacked and returned, otherwise debug messages will be logged indicating possible errors.

Parameters:
  • args (unpacked iterable) – a variable length of data to be transferred to the PiSoC (limited to 1 byte per element and a maximum of 55 bytes). This will contain data required for various commands on the PiSoC.
  • Hformat (list) – list of indices of args that should be formatted as unsigned short, independent of their length. Defaults to [2], as this is generally required.
Returns:

Unpacked data from the PiSoC, indicating the returned value.

send_data(*args, **kwargs)
Method:

send_data

Description:

Sends data to the PiSoC, without requiring a return value. It will wait to be sure the provided arguments produce a valid command on the PiSoC, and return an error code if not.

Parameters:
  • args (unpacked iterable) – a variable length of data to be transferred to the PiSoC (limited to 1 byte per element and a maximum of 55 bytes). This will contain data required for various commands on the PiSoC.
  • Hformat (list) – list of indices of args that should be formatted as unsigned short, independent of their length. Defaults to [2], as this is generally required.
Returns:

None

Exceptions

class pisoc.LostConnection
Exception:LostConnection
Description:Raised when connection to the PiSoC is lost
class pisoc.ClosedPortException
Exception:ClosedPortException
Description:Raised when a write or read attempt is being made on a closed port

Special Functions

pisoc.Test_Read(*args, **kwargs)
Function:

Test_Read

Description:

Used to test data transfer between devices. It will take a generic list of data, and echo back a piece of that data at the specified location.

Parameters:
  • args (unpacked iterable) – a variable length of data to be transferred to the PiSoC (limited to 1 byte per element and a maximum of 55 bytes).
  • index (int) – the index of args containing the element that should be echoed back. Defaults to 0.
Returns:

The data returned from the PiSoC; if transmission of args was successful, the return value should be args[index].

Example:
>>> from pisoc import *
>>> PiSoC('PC', log_level = 'info')
>>> Test_Read(1, 2, 3, 4, 5)
1
>>> Test_Read(2, 4, 6, 8, 10, 12, 22, index = 3)
8
pisoc.PrepareData(*args, **kwargs)
Function:

PrepareData

Description:

Used internally to format unpacked arguments into a character buffer for transfer to the PiSoC in the expected format.

Parameters:
  • args (unpacked iterable) – Ordered list of data to be sent to the pisoc.
  • Hformat (list) – list of indices of args that should be formatted as unsigned short, independent of their length. Defaults to [2], as this is generally required.
Returns:

character buffer to be sent to the PiSoC sequentially.

Table Of Contents

Previous topic

analog.py

Next topic

Example Projects!

This Page