Devices

Overview

Pidevices’s main goal is the easy deployment of devices. A device could be anything that get’s connected to the pi(also the pi could be a device, but we don’t see it from that prespective). Pidevices distinct the devices in two categories sensors, which are devices that get any information from the environment and actuators that change the environment.

To make things easier for the maker/developer pidevices use a common interface consisting of three functions start(), stop() and restart(). The distinction between sensors and actuators is a read() function that belongs to sensors and a write() function that belongs only to actuatos.

With that in mind a developer can contribute a new sensor or effector by just implementing these four functions and then any user could use it without having to know the specific implementation.

Device

class pidevices.devices.Device(name='', max_data_length=100)

Base class for sensors and actuators.

The purpose of this class is to provide the tools for the easy implementation of a new device. Every device uses at least one hardware interface to talk with the hardware.

Parameters
  • name (str) – The optional name of the device.

  • max_data_length (int) – The max length of the data deque.

Raises

TypeError – Invalid name type, or invalid max_data_length type.

property hardware_interfaces

A list with the objects of the device’s used hardware interfaces.

property max_data_length

The max length of the data deque.

property id

The name of the device.

init_interface(interface, impl=None, **kwargs)

Choose an implementation for an interface.

Basically using the impl parameter finds the implemented class and creates a new instance of that class.

Parameters
  • interface (str) – String representing the hardware interface type, it should be GPIO/gpio, SPI/spi, UART/uart, I2C/i2c or HPWM/hpwm.

  • impl (str) –

    The specific implementation to be used. If it is none the first that is installed will be used. Currently supported values

    • GPIO: “RPiGPIO”, “Mcp23017GPIO”

    • SPI: “SPIimplementation”

    • UART:

    • I2C: “SMBus2”

    • HPWM: “HPWMPeriphery”

    The above list has the form interface: implementation.

  • **kwargs – Keyword arguments for the constructor of the chosen interface.

Returns

representing the interface’s index in the list.

Return type

int

Raises
  • TypeError – Wrong interface type.

  • NotSupportedInterface – An error occured accessing modules.

  • NotInstalledInterface – An error occured when there isn’t any supported library installed for the current interface.

update_data(value)

Insert an element to the end of the data deque.

get_data(start, end)

Get last abs(end - start) data.

For getting just the last element it is better to use self.data[-1] than this method.

Parameters
  • start (int) – Start index to get elements from data deque.

  • end (int) – End index of data.

Returns

That has the

Return type

list

start()

Empty function for starting devices, which will be overloaded.

stop()

Empty function for stopping devices, which will be overloaded.

restart()

Restart the device.

Sensor

class pidevices.Sensor(name='', max_data_length=100)

Base class of a sensor device. Extends Device.

read()

Empty function for reading from devices, which will be overloaded.

Actuator

class pidevices.Actuator(name='', max_data_length=100)

Base class of an actuator device. Extends Device.

write()

Abstact method for driving an actuator which will be overloaded.