1
0
Fork 0
ContactPi/buttond.py

86 lines
2.3 KiB
Python
Raw Normal View History

2019-11-01 10:34:57 -04:00
#!/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"
2019-11-17 11:12:53 -05:00
__version__ = "1.0"
2019-11-01 10:34:57 -04:00
__maintainer__ = "Kevin Matz"
__email__ = "kevin@company235.com"
__status__ = "Prototype"
import configparser
from signal import pause
from baconscript import comment
2022-05-12 18:46:33 -04:00
from gpiozero import Button, GPIODeviceError
2019-11-01 10:34:57 -04:00
# use mock pins when not working on Pi hardware
# from gpiozero.pins.mock import MockFactory
# from gpiozero import Device
# Device.pin_factory = MockFactory()
2019-11-03 20:41:50 -05:00
2019-11-17 11:10:48 -05:00
# callback intermidiary to sanitize user input
2019-11-17 10:58:21 -05:00
def _activate(macro):
if macro is None:
2019-11-03 20:42:23 -05:00
return
2019-11-17 10:58:21 -05:00
macro = macro.strip('\"').upper()
2022-05-12 18:46:53 -04:00
if macro == "":
2019-11-03 20:42:23 -05:00
return
2019-11-17 11:10:48 -05:00
# pass macro to baconscript
2019-11-17 10:58:21 -05:00
print("doing macro ", macro)
comment(macro)
2019-11-03 20:41:50 -05:00
2019-11-01 10:34:57 -04:00
# empty button directory
_buttons = {}
# open config file
2019-11-03 20:42:23 -05:00
config = configparser.ConfigParser(allow_no_value=True)
2019-11-01 10:34:57 -04:00
config.read('buttons.cfg')
# set up each buttons
for b in config.get('button', 'names').split(','):
try:
# move to config section
c = config[b]
2019-11-17 10:54:43 -05:00
# read from settings
pin = c.get('pin', None)
2019-11-01 10:34:57 -04:00
closed = c.get('close', None)
opened = c.get('open', None)
# set up button on pin
2019-11-17 11:30:36 -05:00
print('init', b, 'on GPIO', pin)
2019-11-17 10:54:43 -05:00
_buttons[b] = Button(pin)
2019-11-01 10:34:57 -04:00
# connect button callbacks to anonymous functions
2019-11-17 11:30:36 -05:00
print('connecting', b, '"closed" to macro', closed)
2019-11-17 10:58:21 -05:00
_buttons[b].when_pressed = lambda macro=closed: _activate(macro)
2019-11-17 11:30:36 -05:00
print('connecting', b, '"opened" to macro', opened)
2019-11-17 10:58:21 -05:00
_buttons[b].when_released = lambda macro=opened: _activate(macro)
2019-11-01 10:34:57 -04:00
except (KeyError, GPIODeviceError) as e:
print('Error configuring button', b, e)
continue
2019-11-17 11:10:48 -05:00
# clear temp variables off the stack
2019-11-17 10:55:26 -05:00
try:
del pin, closed, opened
2019-11-17 11:27:08 -05:00
del c, config
2019-11-17 11:11:52 -05:00
except NameError as e:
print('failed to release memory', e)
2019-11-17 10:55:26 -05:00
2019-11-17 11:10:48 -05:00
# when run directly, sleep until button signal or KeyboardInterrupt
2019-11-01 10:34:57 -04:00
if __name__ == '__main__':
2019-11-03 20:29:28 -05:00
try:
pause()
except KeyboardInterrupt:
2019-11-17 11:11:13 -05:00
print()
2019-11-17 11:11:37 -05:00
else:
# indicate prefered method of sleeping when laoded as a module
print('call signal.pause() instead of time.sleep()')