Reference

Core

class pifacecommon.core.DigitalInputItem(pin_num, port, board_num=0)

An digital input connected to a pin a PiFace product.

Note

This hides the fact that inputs are active low.

>>> input_port = pifacecommon.core.GPIOB
>>> pifacecommon.core.read_bit(0, input_port)
1
>>> hex(pifacecommon.core.DigitalInputItem(0, input_port).value)
0
value

The inverse value of the digital input item (inputs are active low).

class pifacecommon.core.DigitalInputPort(port, board_num=0)

An digital input port on a PiFace product.

Note

This hides the fact that inputs are active low.

>>> input_port = pifacecommon.core.GPIOB
>>> hex(pifacecommon.core.read(input_port))
'0xFF'
>>> hex(pifacecommon.core.DigitalInputPort(input_port).value)
'0x00'
value

The value of the digital input port.

class pifacecommon.core.DigitalItem(pin_num, port, board_num=0)

A digital item connected to a pin on a PiFace product. Has most of the same properties of a Digital Port.

value

The value of the digital item.

class pifacecommon.core.DigitalOutputItem(pin_num, port, board_num=0)

An output connected to a pin a PiFace Digital product.

toggle()

Toggles the digital output item’s value.

turn_off()

Sets the digital output item’s value to 0.

turn_on()

Sets the digital output item’s value to 1.

class pifacecommon.core.DigitalOutputPort(port, board_num=0)

An digital output port on a PiFace product

all_off()

Turns all outputs off.

all_on()

Turns all outputs on.

toggle()

Toggles all outputs.

class pifacecommon.core.DigitalPort(port, board_num=0)

A digital port on a PiFace product.

handler

The module that handles this port (can be useful for emulator/testing).

value

The value of the digital port.

pifacecommon.core.deinit()

Closes the SPI device file descriptor.

pifacecommon.core.get_bit_mask(bit_num)

Translates a bit num to bit mask.

Parameters:bit_num (int) – The bit number.
Returns:int – the bit mask
Raises :RangeError
>>> pifacecommon.core.get_bit_mask(0)
1
>>> pifacecommon.core.get_bit_mask(1)
2
>>> bin(pifacecommon.core.get_bit_mask(3))
'0b1000'
pifacecommon.core.get_bit_num(bit_pattern)

Returns the lowest bit num from a given bit pattern. Returns None if no bits set.

Parameters:bit_pattern (int) – The bit pattern.
Returns:int – the bit number
Returns:None – no bits set
>>> pifacecommon.core.get_bit_num(0)
None
>>> pifacecommon.core.get_bit_num(0b1)
0
>>> pifacecommon.core.get_bit_num(0b11000)
3
pifacecommon.core.init(bus=0, chip_select=0)

Initialises the SPI device file descriptor.

Parameters:
  • bus (int) – The SPI device bus number
  • chip_select – The SPI device chip_select number
  • chip_select – int
Raises :

InitError

pifacecommon.core.read(address, board_num=0)

Returns the value of the address specified.

Parameters:
  • address (int) – The address to read from.
  • board_num (int) – The board number to read from.
pifacecommon.core.read_bit(bit_num, address, board_num=0)

Returns the bit specified from the address.

Parameters:
  • bit_num (int) – The bit number to read from.
  • address (int) – The address to read from.
  • board_num (int) – The board number to read from.
Returns:

int – the bit value from the address

pifacecommon.core.sleep_microseconds(microseconds)

Sleeps for the given number of microseconds.

Parameters:microseconds (int) – Number of microseconds to sleep for.
pifacecommon.core.spisend(bytes_to_send)

Sends bytes via the SPI bus.

Parameters:bytes_to_send (bytes) – The bytes to send on the SPI device.
Returns:bytes – returned bytes from SPI device
Raises :InitError
pifacecommon.core.write(data, address, board_num=0)

Writes data to the address specified.

Parameters:
  • data (int) – The data to write.
  • address (int) – The address to write to.
  • board_num (int) – The board number to write to.
pifacecommon.core.write_bit(value, bit_num, address, board_num=0)

Writes the value given to the bit in the address specified.

Parameters:
  • value (int) – The value to write.
  • bit_num (int) – The bit number to write to.
  • address (int) – The address to write to.
  • board_num (int) – The board number to write to.

Interrupts

class pifacecommon.interrupts.EventQueue(pin_function_maps)

Stores events in a queue.

add_event(event)

Adds events to the queue. Will ignore events that occur before the settle time for that pin/direction. Such events are assumed to be bouncing.

class pifacecommon.interrupts.FunctionMap(callback, settle_time=None)

Maps something to a callback function. (This is an abstract class, you must implement a SomethingFunctionMap).

class pifacecommon.interrupts.InterruptEvent(interrupt_flag, interrupt_capture, board_num, timestamp)

An interrupt event.

class pifacecommon.interrupts.PinFunctionMap(pin_num, direction, callback, settle_time)

Maps an IO pin and a direction to callback function.

class pifacecommon.interrupts.PortEventListener(port, board_num=0)

Listens for port events and calls the registered functions.

>>> def print_flag(event):
...     print(event.interrupt_flag)
...
>>> port = pifacecommon.core.GPIOA
>>> listener = pifacecommon.interrupts.PortEventListener(port)
>>> listener.register(0, pifacecommon.interrupts.IODIR_ON, print_flag)
>>> listener.activate()
activate()

When activated the PortEventListener will run callbacks associated with pins/directions.

deactivate()

When deactivated the PortEventListener will not run anything.

register(pin_num, direction, callback, settle_time=0.02)

Registers a pin number and direction to a callback function.

Parameters:
  • pin_num (int) – The pin pin number.
  • direction (int) – The event direction (use: IODIR_ON/IODIR_OFF/IODIR_BOTH)
  • callback (function) – The function to run when event is detected.
  • settle_time (int) – Time within which subsequent events are ignored.
pifacecommon.interrupts.clear_interrupts(port)

Clears the interrupt flags by ‘read’ing the capture register on all boards.

pifacecommon.interrupts.disable_interrupts(port)

Disables interrupts on the port specified.

Parameters:port (int) – The port to enable interrupts on (pifacecommon.core.GPIOA, pifacecommon.core.GPIOB)
pifacecommon.interrupts.enable_interrupts(port)

Enables interrupts on the port specified.

Parameters:port (int) – The port to enable interrupts on (pifacecommon.core.GPIOA, pifacecommon.core.GPIOB)
pifacecommon.interrupts.handle_events(function_maps, event_queue, event_matches_function_map, terminate_signal)

Waits for events on the event queue and calls the registered functions.

Parameters:
  • function_maps (list) – A list of classes that have inheritted from FunctionMaps describing what to do with events.
  • event_queue (multiprocessing.Queue) – A queue to put events on.
  • event_matches_function_map (function) – A function that determines if the given event and FunctionMap match.
  • terminate_signal – The signal that, when placed on the event queue, causes this function to exit.
pifacecommon.interrupts.watch_port_events(port, board_num, pin_function_maps, event_queue)

Waits for a port event. When a port event occurs it is placed onto the event queue.

Parameters:
  • port (int) – The port we are waiting for interrupts on (GPIOA/GPIOB).
  • board_num (int) – The board we are waiting for interrupts on.
  • pin_function_maps (list) – A list of classes that have inheritted from FunctionMaps describing what to do with events.
  • event_queue (multiprocessing.Queue) – A queue to put events on.

Table Of Contents

Previous topic

Example

This Page