fusion_hat.pin module

Pin manipulation class

Example

Simple example with auto mode

>>> from fusion_hat.pin import Pin
>>> pin = Pin(17)
>>> pin.value() # read the pin input value
0
>>> pin.value(1) # output pin value to high
1

Output mode

>>> from fusion_hat.pin import Pin
>>> pin = Pin(17, mode=Pin.OUT)
>>> pin.value(1) # output high
1
>>> pin.value(0) # output low
0
>>> pin.value() # read current value, not as input
0
>>> pin.on() # output high
1
>>> pin.off() # output low
0

Input mode

>>> pin = Pin(17, mode=Pin.IN, pull=Pin.PULL_UP)
>>> pin.value() # read the pin input value
0

Active state, If your LED active LOW, you can set active_state make things easier.

>>> led = Pin(17, mode=Pin.OUT, active_state=Pin.ACTIVE_LOW)
>>> led.on() # output low but turn on the LED
0
>>> led.off() # output high but turn off the LED
1
>>> led.value() # read current value, not as input
1
>>> led.raw(1) # You can still set the raw value
1

And also if your button active LOW, you can set active_state to Pin.ACTIVE_LOW. you can also do the same.

>>> button = Pin(17, mode=Pin.IN, pull=Pin.PULL_UP, active_state=Pin.ACTIVE_LOW)
>>> button.value() # read's pin low but return 1 as pressed
1
>>> button.raw() # You can still read the raw value
0

You can also set interrupts on activated or deactivated state.

>>> button = Pin(17, mode=Pin.IN, pull=Pin.PULL_UP, active_state=Pin.ACTIVE_LOW, bouncetime=0.02)
>>> def on_press(pin: Pin) -> None:
...     print("press")
>>> def on_release(pin: Pin) -> None:
...     print("release")
>>> button.when_activated = on_press
>>> button.when_deactivated = on_release
pressed
release
class fusion_hat.pin.Mode(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Pin direction

AUTO = None

Pin direction auto

IN

Pin direction input

OUT

Pin direction output

class fusion_hat.pin.Pull(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Pin pull up/down

UP

Pin internal pull up

DOWN

Pin internal pull down

NONE

Pin internal pull none

class fusion_hat.pin.Active(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Pin active state

HIGH = True

Pin active state high

LOW = False

Pin active state low

class fusion_hat.pin.Trigger(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Pin interrupt

FALLING

Pin interrupt falling

RISING

Pin interrupt rising

BOTH

Pin interrupt both rising and falling

class fusion_hat.pin.Pin(pin: int, *args, mode: Mode = Mode.AUTO, pull: Pull = RPi.GPIO.PUD_OFF, active_state: Active = Active.HIGH, bounce_time: float = 0.02, **kwargs)[source]

Bases: _Base

Pin manipulation class

a pin wraping class of RPi.GPIO. you need to install rpi.lgpio instead of RPi.GPIO on Pi 5

Parameters:
  • pin (int) – pin number of Raspberry Pi

  • mode (Mode, optional) – pin mode(IN/OUT/AUTO). Defaults to Mode.AUTO.

  • pull (Pull, optional) – pin pull (Pull.UP/Pull.DOWN/Pull.NONE). Defaults to Pull.NONE.

  • active_state (Active, optional) – active state of pin, If True, when the hardware pin state is HIGH, the software pin is HIGH. If False, the input polarity is reversed. Defaults to Active.HIGH.

  • bounce_time (float, optional) – bounce time of pin interrupt in seconds. Defaults to 0.02.

  • *args – Additional arguments for fusion_hat._base._Base

  • **kwargs – Additional keyword arguments for fusion_hat._base._Base

OUT

Pin mode output

IN

Pin mode input

AUTO = None

Pin mode auto

PULL_UP

Pin internal pull up

PULL_DOWN

Pin internal pull down

PULL_NONE

Pin internal pull none

ACTIVE_HIGH = True

Pin active state high

ACTIVE_LOW = False

Pin active state low

IRQ_FALLING

Pin interrupt falling

IRQ_RISING

Pin interrupt rising

IRQ_RISING_FALLING

Pin interrupt both rising and falling

close() None[source]

Close the pin

deinit() None[source]

Deinitialize the pin

setup(mode: Mode = Mode.AUTO, pull: Pull = RPi.GPIO.PUD_OFF, active_state: Active = Active.HIGH, bounce_time: float = 0.02) None[source]

Setup the pin

Parameters:
  • mode (Mode, optional) – pin mode(IN/OUT/AUTO). Defaults to Mode.AUTO.

  • pull (Pull, optional) – pin pull (Pull.UP/Pull.DOWN/Pull.NONE). Defaults to Pull.NONE.

  • active_state (Active, optional) – active state of pin, If True, when the hardware pin state is HIGH, the software pin is HIGH. If False, the input polarity is reversed. Defaults to Active.HIGH.

  • bounce_time (float, optional) – bounce time of pin interrupt in seconds. Defaults to 0.02.

__call__(value: [<class 'bool'>, <class 'int'>] = None) int[source]

Set/get the pin value

Parameters:

value (bool/int, optional) – pin value, leave it empty to get the value(0/1). Defaults to None.

Returns:

pin value(0/1)

Return type:

int

raw(value: [<class 'bool'>, <class 'int'>] = None) int[source]

Set/get the pin raw value

Parameters:

value (bool/int, optional) – pin value, leave it empty to get the value(0/1). Defaults to None.

Returns:

pin value(0/1)

Return type:

int

Raises:

ValueError – if pin mode is IN

value(value: [<class 'bool'>, <class 'int'>] = None) int[source]

Set/get the pin value

Parameters:

value (bool/int, optional) – pin value, leave it empty to get the value(0/1). Defaults to None.

Returns:

pin value(0/1)

Return type:

int

Raises:

ValueError – if pin mode is IN

on() int[source]

Set pin on(high)

Returns:

pin value(1)

Return type:

int

off() int[source]

Set pin off(low)

Returns:

pin value(0)

Return type:

int

high() int[source]

Set pin high(1)

Returns:

pin value(1)

Return type:

int

low() int[source]

Set pin low(0)

Returns:

pin value(0)

Return type:

int

irq(handler: Callable[[], None], trigger: Trigger = RPi.GPIO.BOTH) None[source]

Set the pin interrupt

Parameters:
  • handler (Callable[[], None]) – interrupt handler callback function

  • trigger (Trigger, optional) – interrupt trigger(RISING, FALLING, RISING_FALLING). Defaults to Trigger.BOTH.

Raises:

ValueError – if trigger is not valid

init_irq() None[source]

Initialize the pin interrupt

irq_handler(channel: int) None[source]

Handle the pin interrupt

Parameters:

channel (int) – pin number

property when_activated: Callable[[], None]

Get the pressed handler

Returns:

pressed handler

Return type:

Callable[[], None]

property when_deactivated: Callable[[], None]

Get the released handler

Returns:

released handler

Return type:

Callable[[], None]