luma.core.interface.serial

Encapsulates sending commands and data over a serial interface, whether that is I²C, SPI or bit-banging GPIO.

class luma.core.interface.serial.i2c(bus=None, port=1, address=60)[source]

Bases: object

Wrap an I²C (Inter-Integrated Circuit) interface to provide data() and command() methods.

Parameters:
  • bus – A smbus implementation, if None is supplied (default), smbus2 is used. Typically this is overridden in tests, or if there is a specific reason why pysmbus must be used over smbus2.
  • port (int) – I²C port number, usually 0 or 1 (default).
  • address (int) – I²C address, default: 0x3C.
Raises:

Note

  1. Only one of bus OR port arguments should be supplied; if both are, then bus takes precedence.
  2. If bus is provided, there is an implicit expectation that it has already been opened.
cleanup()[source]

Clean up I²C resources

command(*cmd)[source]

Sends a command or sequence of commands through to the I²C address - maximum allowed is 32 bytes in one go.

Parameters:cmd (int) – A spread of commands.
Raises:luma.core.error.DeviceNotFoundError – I2C device could not be found.
data(data)[source]

Sends a data byte or sequence of data bytes through to the I²C address - maximum allowed in one transaction is 32 bytes, so if data is larger than this, it is sent in chunks.

Parameters:data (list, bytearray) – A data sequence.
class luma.core.interface.serial.spi(spi=None, gpio=None, port=0, device=0, bus_speed_hz=8000000, cs_high=False, transfer_size=4096, gpio_DC=24, gpio_RST=25)[source]

Bases: luma.core.interface.serial.bitbang

Wraps an SPI (Serial Peripheral Interface) bus to provide data() and command() methods.

Parameters:
  • spi – SPI implementation (must be compatible with spidev)
  • gpio – GPIO interface (must be compatible with RPi.GPIO). For slaves that don’t need reset or D/C functionality, supply a noop implementation instead.
  • port (int) – SPI port, usually 0 (default) or 1.
  • device (int) – SPI device, usually 0 (default) or 1.
  • bus_speed_hz (int) – SPI bus speed, defaults to 8MHz.
  • cs_high (bool) – Whether SPI chip select is high, defaults to False.
  • transfer_size (int) – Maximum amount of bytes to transfer in one go. Some implementations only support a maximum of 64 or 128 bytes, whereas RPi/py-spidev supports 4096 (default).
  • gpio_DC (int) – The GPIO pin to connect data/command select (DC) to (defaults to 24).
  • gpio_RST (int) – The GPIO pin to connect reset (RES / RST) to (defaults to 25).
Raises:
cleanup()[source]

Clean up SPI & GPIO resources.

class luma.core.interface.serial.bitbang(gpio=None, transfer_size=4096, **kwargs)[source]

Bases: object

Wraps an SPI (Serial Peripheral Interface) bus to provide data() and command() methods. This is a software implementation and is thus a lot slower than the default SPI interface. Don’t use this class directly unless there is a good reason!

Parameters:
  • gpio

    GPIO interface (must be compatible with RPi.GPIO). For slaves that don’t need reset or D/C functionality, supply a noop implementation instead.

  • transfer_size (int) – Max bytes to transfer in one go. Some implementations only support maximum of 64 or 128 bytes, whereas RPi/py-spidev supports 4096 (default).
  • SCLK (int) – The GPIO pin to connect the SPI clock to.
  • SDA (int) – The GPIO pin to connect the SPI data (MOSI) line to.
  • CE (int) – The GPIO pin to connect the SPI chip enable (CE) line to.
  • DC (int) – The GPIO pin to connect data/command select (DC) to.
  • RST (int) – The GPIO pin to connect reset (RES / RST) to.
cleanup()[source]

Clean up GPIO resources if managed.

command(*cmd)[source]

Sends a command or sequence of commands through to the SPI device.

Parameters:cmd (int) – A spread of commands.
data(data)[source]

Sends a data byte or sequence of data bytes through to the SPI device. If the data is more than transfer_size bytes, it is sent in chunks.

Parameters:data (list, bytearray) – A data sequence.