1
0
Fork 0
ContactPi/buttond.py

68 lines
1.7 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()
def activate(path):
if path is None:
return
path = path.strip('\"').upper()
if path is "":
return
print("doing macro ", path)
comment(path)
# empty button directory
_buttons = {}
# open config file
config = configparser.ConfigParser(allow_no_value=True)
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
p = c.get('pin', None)
print('init button on GPIO', p)
_buttons[b] = Button(p)
# connect button callbacks to anonymous functions
_buttons[b].when_pressed = lambda path=closed: activate(path)
_buttons[b].when_released = lambda path=opened: activate(path)
except (KeyError, GPIODeviceError) as e:
print('Error configuring button', b, e)
continue
if __name__ == '__main__':
try:
pause()
except KeyboardInterrupt:
pass