1
0
Fork 0
ContactPi/buttond.py

52 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""buttond.py: Watch a Rasberry Pi GPIO button. Exec Hog4 comment macros."""
__author__ = "Kevin Matz"
__copyright__ = "Copyright 2019, Company 235, LLC"
__credits__ = ["Kevin Matz"]
__license__ = "MIT"
__version__ = "3.9"
__maintainer__ = "Kevin Matz"
__email__ = "kevin@company235.com"
__status__ = "Prototype"
import configparser
from baconscript.comment import comment
from gpiozero import Button, GPIODeviceError
from signal import pause
# use mock pins when not working on Pi hardware
# from gpiozero.pins.mock import MockFactory
# from gpiozero import Device
# Device.pin_factory = MockFactory()
# empty button directory
_buttons = {}
# open config file
config = configparser.ConfigParser(allow_no_value=False)
config.read('buttons.cfg')
# set up each buttons
for b in config.get('button', 'names').split(','):
try:
# move to config section
c = config[b]
# read macro values
closed = c.get('close', None)
opened = c.get('open', None)
# set up button on pin
_buttons[b] = Button(c.getint("pin", None))
# connect button callbacks to anonymous functions
_buttons[b].when_pressed = lambda: comment(closed)
_buttons[b].when_released = lambda: comment(opened)
except (KeyError, GPIODeviceError) as e:
print('Error configuring button', b, e)
continue
if __name__ == '__main__':
pause()