Basic I/O components

This module provides classes to manage common input/output components, such as buttons and LEDs, with support for PWM-based brightness control for LEDs. The classes allow for checking button states, toggling LEDs, blinking LEDs, and controlling brightness and fading effects using PWM.

Example

from hw_config import Led

led = Led(2)

print("LED blinking...")
led.blink(times=3)

print("Toggling LED...")
led.toggle()

# Example of using the PwmLed class
led = PwmLed(2)

print("Fading in...")
led.fade_in(duration=2)

Author

Tomas Fryza

Modification history

  • 2024-11-11 : Added Sphinx-style comments for documentation.

  • 2024-10-26 : Added demo method to demonstrate usage of the classes.

  • 2024-09-28 : File created, initial release.

class hw_config.Button(pin_number)[source]

Bases: object

A class to manage a button connected to a GPIO pin with a pull-up resistor.

is_pressed()[source]

Check if the button is currently pressed using active-low logic.

Returns:

True if the button is pressed; False otherwise.

class hw_config.Led(*args: Any, **kwargs: Any)[source]

Bases: Pin

A class to control an LED connected to a specified GPIO pin.

Blink the LED a specified number of times.

Parameters:
  • duration – Duration in seconds for each on/off cycle. Default is 0.5 seconds.

  • times – Number of times the LED should blink. Default is 5.

toggle()[source]

Toggle the LED state between on and off.

class hw_config.PwmLed(*args: Any, **kwargs: Any)[source]

Bases: PWM

A class to control an LED using PWM, allowing for brightness adjustment, fading, and on/off control.

fade_in(duration=1)[source]

Gradually increase the brightness to create a fade-in effect.

Parameters:

duration – Total duration of the fade-in effect, in seconds. Default is 1 second.

fade_out(duration=1)[source]

Gradually decrease the brightness to create a fade-out effect.

Parameters:

duration – Total duration of the fade-out effect, in seconds. Default is 1 second.

off()[source]

Turn the LED off by setting the brightness to 0.

on(brightness=100)[source]

Turn the LED on by setting it to a specified brightness level.

Parameters:

brightness – Brightness level as a percentage (0 to 100). Default is 100%.

set_brightness(brightness)[source]

Set the LED brightness using PWM.

Parameters:

brightness – Brightness level as a percentage (0 to 100).

hw_config.demo()[source]

Demonstrates usage of the Button, Led, and PwmLed classes.