From b408015444df716832d1eacc953c59a2125deed8 Mon Sep 17 00:00:00 2001 From: Kevin Matz Date: Thu, 12 May 2022 23:30:24 -0400 Subject: [PATCH] button config file can be specified --- buttond.py | 64 ++++++++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/buttond.py b/buttond.py index ae31a7f..804d9d5 100755 --- a/buttond.py +++ b/buttond.py @@ -46,6 +46,8 @@ def main(): logging.basicConfig(level=log_levels.get(args.verbosity, logging.DEBUG)) ## output header print(f"Version: {__version__}\t{__copyright__}") + ## load config files + load_config(args.buttons) ## run the event loop try: pause() @@ -58,6 +60,8 @@ def get_command_arguments(): parser = argparse.ArgumentParser( description='Exec Hog4 comment macros on RasPi GPIO activity.', epilog=__copyright__) + parser.add_argument('-b', '--buttons', default="buttons.cfg", + help="button configuration file") parser.add_argument('-q', '--quiet', action='store_true', help="sets the default log level to 0, otherwise 3.") parser.add_argument("-v", dest="verbosity", @@ -70,6 +74,33 @@ def get_command_arguments(): 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): """Sanitize user configured macros.""" if macro is None: @@ -85,39 +116,6 @@ def activate(macro): # 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 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 if __name__ == '__main__':