I²C Module

This module contains definitions for managing and controlling I²C devices.

class berrymon.i2c.I2CDevice(addr: int, bus: int)[source]

Bases: object

This class represents an I²C device and implements it’s bare-metal functions like reading and writing bytes from and to the device.

The sleep at the end of every writing sequence method is necessary because of the clock speed of every standard I²C device, 100kHz. By making the Pi sleep for 0.0001 seconds after each writing sequence we make sure that we are not sending two bytes in one clock cycle.

Variables:
  • addr (int) – The address of the device on it’s bus.
  • bus (int) – The number of the bus the device is connected to.
read_block_register(register: int, length: int) → int[source]

Reads a block of bytes from a specific register.

Parameters:
  • register (int) – The address of the register to read from.
  • length (int) – The length of the block to read.
Returns:

The read block of bytes.

Return type:

int

read_byte() → int[source]

Reads a single byte from the device.

Returns:The read byte.
Return type:int
read_byte_register(register: int) → int[source]

Reads a single byte from a specific register.

Parameters:register (int) – The address of the register to read from.
Returns:The read byte.
Return type:int
write_block_register(register: int, block: int)[source]

Writes a block of bytes to a specific register.

Parameters:
  • register (int) – The address of the register to write to.
  • block (int) – The block of bytes to write.
write_byte(byte: int)[source]

Writes a byte to the device.

Parameters:byte (int) – The byte to write.
write_byte_register(register: int, byte: int)[source]

Writes a byte to a specific register.

Parameters:
  • register (int) – The address of the register to write to.
  • byte (int) – The byte to write to the register.
class berrymon.i2c.I2CDisplay(addr: int, bus: int, lines: int, line_length: int)[source]

Bases: berrymon.i2c.I2CDevice

This class represents an I²C enabled LCD display. It implements the I2CDevice and adds functions for printing text and toggling backlight. Displays with up to four lines are supported.

Variables:
  • lines (int) – The amount of lines on the display.
  • line_length (int) – The line length of the display.
  • backlight (bool) – The backlight status of the display, True equals on, False equals off. Do not set this manually, use the functions for toggling backlight!
ENABLE = 4

The enable bit.

READWRITE = 2

The read/write bit.

REGISTER = 1

The register selection bit.

clear()[source]

Clears the display.

disable_backlight()[source]

Disables the display backlight.

enable_backlight()[source]

Enables the display backlight.

print(string: str, line: int)[source]

Prints a string to the given line. This method will not clear the remaining space on the line. If line is greater than 4 or less than 1, it will print to the first line.

Parameters:
  • string (str) – The string to print.
  • line (int) – The line to print to.
print_center(string: str, line: int)[source]

Prints a string centered in the given line. This function will, as it fills empty spaces with a space, overwrite all previous text on the line. If line is greater than 4 or less than 1, it will print to the first line.

Parameters:
  • string (str) – The string to print.
  • line (int) – The line to print to.
print_clear(string: str, line: int)[source]

Prints a string to the given line, filling all remaining space with spaces. This way all previous text will be cleared.

Parameters:
  • string (str) – The string to print.
  • line (int) – The line to print to.
set_backlight(on: bool)[source]

Sets the display backlight to the desired status, either on or off.

Parameters:on (bool) – The desired backlight status, True means on, False means off.
setup()[source]

Initializes the display by putting it into 4-Bit mode, enabling backlight and clearing it. The sleep at the end is necessary to make sure the display is fully initialized before executing any other order.

You don’t have to call this manually, this function will automatically be called by the constructor.

strobe(byte: int)[source]

Strobes a byte to the display.

Parameters:byte (int) – The byte to strobe.
write(byte, bit=0)[source]

Writes a byte and control bit to the display.

Parameters:
  • byte (int) – The byte to write.
  • bit (int) – The control bit.
write4(byte: int)[source]

Writes a byte to the display in 4-Bit mode.

Parameters:byte (int) – The byte to write.