button config file can be specified
This commit is contained in:
parent
2baf87d7a0
commit
b408015444
64
buttond.py
64
buttond.py
|
@ -46,6 +46,8 @@ def main():
|
||||||
logging.basicConfig(level=log_levels.get(args.verbosity, logging.DEBUG))
|
logging.basicConfig(level=log_levels.get(args.verbosity, logging.DEBUG))
|
||||||
## output header
|
## output header
|
||||||
print(f"Version: {__version__}\t{__copyright__}")
|
print(f"Version: {__version__}\t{__copyright__}")
|
||||||
|
## load config files
|
||||||
|
load_config(args.buttons)
|
||||||
## run the event loop
|
## run the event loop
|
||||||
try:
|
try:
|
||||||
pause()
|
pause()
|
||||||
|
@ -58,6 +60,8 @@ def get_command_arguments():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description='Exec Hog4 comment macros on RasPi GPIO activity.',
|
description='Exec Hog4 comment macros on RasPi GPIO activity.',
|
||||||
epilog=__copyright__)
|
epilog=__copyright__)
|
||||||
|
parser.add_argument('-b', '--buttons', default="buttons.cfg",
|
||||||
|
help="button configuration file")
|
||||||
parser.add_argument('-q', '--quiet', action='store_true',
|
parser.add_argument('-q', '--quiet', action='store_true',
|
||||||
help="sets the default log level to 0, otherwise 3.")
|
help="sets the default log level to 0, otherwise 3.")
|
||||||
parser.add_argument("-v", dest="verbosity",
|
parser.add_argument("-v", dest="verbosity",
|
||||||
|
@ -70,6 +74,33 @@ def get_command_arguments():
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def load_config(file):
|
||||||
|
"""Load a configuration file."""
|
||||||
|
config = configparser.ConfigParser(allow_no_value=True)
|
||||||
|
log.debug("Loading config from %s", file)
|
||||||
|
config.read(file)
|
||||||
|
# set up each buttons
|
||||||
|
for button in config.get('button', 'names').split(','):
|
||||||
|
try:
|
||||||
|
# move to config section
|
||||||
|
cfg = config[button]
|
||||||
|
# read from settings
|
||||||
|
pin = cfg.get('pin', None)
|
||||||
|
closed = cfg.get('close', None)
|
||||||
|
opened = cfg.get('open', None)
|
||||||
|
# set up button on pin
|
||||||
|
log.debug('init %s on GPIO %s', button, pin)
|
||||||
|
_buttons[button] = Button(pin)
|
||||||
|
# connect button callbacks to anonymous functions
|
||||||
|
log.info('connecting %s "closed" to macro %s', button, closed)
|
||||||
|
_buttons[button].when_pressed = lambda macro=closed: activate(macro)
|
||||||
|
log.info('connecting %s "opened" to macro %s', button, opened)
|
||||||
|
_buttons[button].when_released = lambda macro=opened: activate(macro)
|
||||||
|
except (KeyError, GPIODeviceError) as error:
|
||||||
|
log.error('Error configuring button %s: %s', button, error)
|
||||||
|
continue
|
||||||
|
|
||||||
|
|
||||||
def activate(macro):
|
def activate(macro):
|
||||||
"""Sanitize user configured macros."""
|
"""Sanitize user configured macros."""
|
||||||
if macro is None:
|
if macro is None:
|
||||||
|
@ -85,39 +116,6 @@ def activate(macro):
|
||||||
# empty button directory
|
# empty button directory
|
||||||
_buttons = {}
|
_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 from settings
|
|
||||||
pin = c.get('pin', None)
|
|
||||||
closed = c.get('close', None)
|
|
||||||
opened = c.get('open', None)
|
|
||||||
# set up button on pin
|
|
||||||
log.debug('init %s on GPIO %s', b, pin)
|
|
||||||
_buttons[b] = Button(pin)
|
|
||||||
# connect button callbacks to anonymous functions
|
|
||||||
log.info('connecting %s "closed" to macro %s', b, closed)
|
|
||||||
_buttons[b].when_pressed = lambda macro=closed: _activate(macro)
|
|
||||||
log.info('connecting %s "opened" to macro %s', b, opened)
|
|
||||||
_buttons[b].when_released = lambda macro=opened: _activate(macro)
|
|
||||||
|
|
||||||
except (KeyError, GPIODeviceError) as e:
|
|
||||||
log.error('Error configuring button %s: %s', b, e)
|
|
||||||
continue
|
|
||||||
|
|
||||||
|
|
||||||
# clear temp variables off the stack
|
|
||||||
try:
|
|
||||||
del pin, closed, opened
|
|
||||||
del c, config
|
|
||||||
except NameError as e:
|
|
||||||
log.error('failed to release memory: %s', e)
|
|
||||||
|
|
||||||
|
|
||||||
# when run directly, sleep until button signal or KeyboardInterrupt
|
# when run directly, sleep until button signal or KeyboardInterrupt
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue