Hardware Interfaces Implementations¶
GPIO Implementations¶
RPi.GPIO implementation¶
-
class
pidevices.
RPiGPIO
(**kwargs)¶ GPIO hardware interface implementation using RPi.GPIO library extends
GPIO
.- Raises
ImportError – If the rpigpio library is not installed.
-
property
pwm_pins
¶ A dictionary that contains instances of RPi.GPIO’s pwm class.
-
initialize
()¶ Initialize RPi.GPIO mode. By default it inializes to RPi.GPIO.BCM
-
read
(pin)¶ Read the value of an input pin.
- Parameters
pin (str) – The pin’s name.
- Returns
that is 1 for high voltage, 0 for low voltage.
- Return type
int
- Raises
NotInputPin – Try to read from non input pin.
-
write
(pin, value)¶ Write a value to an output pin.
- Parameters
pin (str) – The pin’s name.
value (int) – The output value. The value should be 0 or 1 for a simple output pin and if it is pwm should be between [0, 1].
- Raises
TypeError – Try to write non arithmetic value or use value greater than 1 or use negative value.
NotOutputPin – Try to write to an input pin.
-
remove_pins
(*args)¶ Remove a pin/pins from the dictionary and free the resources.
- Parameters
*args – String with the pin’s name, it could be more than one.
-
set_pin_function
(pin, function)¶ Set a pin’s function.
- Parameters
pin (str) – The pin name.
function (str) – The function value.
- Raises
TypeError – Error occured using invalid function name.
-
set_pin_pull
(pin, pull)¶ Set a pin’s pull up.
- Parameters
pin (str) – The pin name.
pull (str) – The pull up value.
- Raises
TypeError – Error occured using invalid pull name.
NotInputPin – Try to set pull up resistor to a non input pin.
-
set_pin_pwm
(pin, pwm)¶ Set a pwm pin.
- Parameters
pin (str) – The pin’s name.
pwm (int) – The pwm value.
- Raises
TypeError – Invalid pwm type.
NotOutputPin – Error occured trying to drive a non output pin.
-
set_pin_frequency
(pin, frequency)¶ Set a pwm pin’s frequency.
- Parameters
pin (str) – The pin’s name.
frequency (int) – The frequency value.
- Raises
NotPwmPin – Error occured trying to set pwm frequency to a non pwm pin.
-
set_pin_edge
(pin, edge)¶ Set a pin’s edge value.
- Parameters
pin (str) – The pin’s name.
edge (str) – The edge value.
- Raises
NotInputPin – Try to set edge to a non input pin.
-
set_pin_bounce
(pin, bounce)¶ Set a pin’s bounce time.
- Parameters
pin (str) – The pin’s name.
bounce (int) – The bounce value.
-
set_pin_event
(pin, event, *args)¶ Set the function that will be called with a new edge.
- Parameters
pin (str) – Pin name
event (function) – Function pointer to the target function.
*args – The arguments of the target function.
- Raises
NotInputPin – Try to set event to a non input pin.
-
wait_pin_for_edge
(pin, timeout=None)¶ Wait pin for an edge detection.
- Parameters
pin (str) – Pin name.
timeout (int) – The time until it stops waiting for an edge signal.
-
close
()¶ Close interface.input
mcp23x17 chip family¶
Mcp23x17GPIO¶
-
class
pidevices.
Mcp23x17GPIO
(**kwargs)¶ GPIO class implementation using mcp23x17 family chips. Extends
GPIO
- Parameters
**kwargs – Could be multiple keyword arguments in the form of pin_name = pin_number(pin number is A_x or B_x, because the implementation use the mcp23x17 devices.) For example for the hc-sr04 sonar, it would be echo=”A_1”, trigger=”B_2”.
-
initialize
()¶ Initialize hardware and os resources.
-
add_pins
(**kwargs)¶ Add new pins to the pins dictionary.
- Parameters
**kwargs – Keyword arguments pin_name=pin_number. For example echo=”A_1”, trigger=”B_2”.
-
read
(pin)¶ Read the value of an input pin.
- Parameters
pin (str) – The pin’s name.
- Returns
that is 1 for high voltage, 0 for low voltage.
- Return type
int
- Raises
NotInputPin – Try to read from non input pin.
-
write
(pin, value)¶ Write a value to an output pin.
- Parameters
pin (str) – The pin’s name.
value (int) – The output value. The value should be 0 or 1 for a simple output pin and if it is pwm should be between [0, 1].
- Raises
TypeError – Try to write non arithmetic value or use value greater than 1 or use negative value.
NotOutputPin – Try to write to an input pin.
-
remove_pins
(*args)¶ Remove a pin/pins from the dictionary and free the resources.
- Parameters
*args – String with the pin’s name, it could be more than one.
-
set_pin_function
(pin, function)¶ Set a pin’s function.
- Parameters
pin (str) – The pin name.
function (str) – The function value.
- Raises
TypeError – Error occured using invalid function name.
-
set_pin_pull
(pin, pull)¶ Set a pin’s pull up.
- Parameters
pin (str) – The pin name.
pull (str) – The pull up value.
- Raises
TypeError – Error occured using invalid pull name.
NotInputPin – Try to set pull up resistor to a non input pin.
-
set_pin_edge
(pin, edge)¶ Set a pin’s edge value.
- Parameters
pin (str) – The pin’s name.
edge (str) – The edge value.
- Raises
NotInputPin – Try to set edge to a non input pin.
-
set_pin_bounce
(pin, bounce)¶ Set a pin’s bounce time.
- Parameters
pin (str) – The pin’s name.
bounce (int) – The bounce value.
-
set_pin_event
(pin, event, *args)¶ Set the function that will be called with a new edge.
In RPi.GPIO calling this function enables interrupt handling. In this implementation the
start_polling()
function need to be called in order to catch interrupts.- Parameters
pin (str) – Pin name
event (function) – Function pointer to the target function.
*args – The arguments of the target function.
- Raises
NotInputPin – Try to set event to a non input pin.
-
start_polling
(pins)¶ Start polling for interrupts the specified pins.
- Parameters
pins (list) – List with the names of the target pins to be polled.
-
stop_polling
()¶ Stop polling for interrupts
-
wait_pin_for_edge
(pin, timeout=None)¶ Wait pin for an edge detection.
- Parameters
pin (str) – Pin name.
timeout (int) – The time until it stops waiting for an edge signal.
- Returns
Indicating if the interrupt occured.
- Return type
int
- Raise:
NotInputPin: Can’t wait for edge to a non input pin.
-
close
()¶ Close interface.input
Mcp23017GPIO¶
-
class
pidevices.
Mcp23017GPIO
(bus=1, address=32, **kwargs)¶ GPIO class implementation using mcp23017 chip. Extends
Mcp23x17GPIO
- Parameters
bus (int) – Optional argument for specifying the i2c bus of the mcp23017 module. Defaults to
1
.address (int) – Optional argument for specifying the i2c address of the mcp23017 module. Defaults to
0x20
.**kwargs – Could be multiple keyword arguments in the form of pin_name = pin_number(pin number is A_x or B_x, because the implementation use the mcp23x17 devices.) For example for the hc-sr04 sonar, it would be echo=”A_1”, trigger=”B_2”.
-
initialize
()¶ Initialize hardware and os resources.
SPI Implementations¶
Spidev implementation¶
-
class
pidevices.
SPIimplementation
(port, device)¶ SPI imlementation wrapping spidev library. Extends
SPI
.- Parameters
port (int) – SPI port on raspberry pi.
devive (int) – SPI device of raspberry.
- Raises
ImportError – If spidev is not installed.
-
read
(n)¶ Read n words from spi
- Parameters
n (int) – The number of bytes to read from spi.
-
write
(data)¶ Write data to spi
- Parameters
data (list) – A list with integers to be writter to the device.
-
read_write
(data)¶ Writes data (a list of integer words where each word is assumed to have
bits_per_word
bits or less) to the SPI interface, and reads an equivalent number of words, returning them as a list of integers.
-
close
()¶ Free hardware resources.
HWPM Implementations¶
Python periphery HPWM¶
-
class
pidevices.
HPWMPeriphery
(pin)¶ Wrapper around python periphery pwm implementation extends
HPWM
.This class need the periphery module to be installed. Also some configuration files need to be added to the system. More info in the repo’s README.
- Parameters
pin (int) – The pin number in bcm mode.
- Raises
ImportError – Error if the periphery library is not installed.
-
read
()¶ Read from hardware pwm pin.
- Returns
The duty cycle of the pwm pin.
- Return type
float
-
write
(value)¶ Write to the pwm pin.
- Parameters
value (float) – The new duty cycle value.
-
close
()¶ Free hardware resources.
I2C Implementations¶
SMBus2 implementation¶
-
class
pidevices.
SMBus2
(bus)¶ Wrapper for smbus2 library extends
I2C
- Parameters
bus (int) – The i2c bus of the raspberry pi. The pi has two buses.
-
read
(address, register, byte_num=1)¶ Read using the smbus protocol.
- Parameters
address – The address of the spi slave
register – The register’s address.
byte_num – How many bytes to read from the device. Max 32
- Returns
A list with byte_num elements.
-
write
(address, register, data)¶ Write using the smbus protocol.
- Parameters
address – The address of the spi slave
register – The address of the register inside the slave.
data – A list or a single byte, if it is a list max length 32 bytes. It is error prone so write less and make consecutive calls.
-
write_i2c
(address, register, data)¶ Write using the i2c protocol
- Parameters
address – The address of the spi slave
register – The address of the register inside the slave.
data – A list or a single byte, if it is a list max length 32 bytes.
-
read_i2c
(address, byte_num)¶ Read using the i2c protocol.
- Parameters
address – The address of the spi slave
byte_num – How many bytes to read from the device. Max 32
- Returns
A list with byte_num elements.
-
read_write
(address, register, data, byte_num)¶ Combined read and write command using the i2c protocol.
- Parameters
address – The address of the spi slave
register – The address of the register inside the slave.
data – A list or a single byte, if it is a list max length 32 bytes.
byte_num – How many bytes to read from the device. Max 32
- Returns
A list with byte_num elements.
-
close
()¶ Free hardware resources.