1
0
Fork 0

unify logging

This commit is contained in:
Kevin Matz 2022-05-12 19:53:40 -04:00
parent 499b20c079
commit 5c32645105
2 changed files with 11 additions and 8 deletions

@ -1 +1 @@
Subproject commit a2146940f7840612ae16df2df58c669c39158642 Subproject commit c392edccf7608d8699e6d1c4d7cd9182ff583a41

View File

@ -15,6 +15,7 @@ __email__ = "kevin@company235.com"
__status__ = "Prototype" __status__ = "Prototype"
import configparser import configparser
import logging
from signal import pause from signal import pause
from baconscript import comment from baconscript import comment
from gpiozero import Button, GPIODeviceError from gpiozero import Button, GPIODeviceError
@ -24,6 +25,8 @@ from gpiozero import Button, GPIODeviceError
# from gpiozero import Device # from gpiozero import Device
# Device.pin_factory = MockFactory() # Device.pin_factory = MockFactory()
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
# callback intermidiary to sanitize user input # callback intermidiary to sanitize user input
def _activate(macro): def _activate(macro):
@ -33,7 +36,7 @@ def _activate(macro):
if macro == "": if macro == "":
return return
# pass macro to baconscript # pass macro to baconscript
print("doing macro ", macro) log.info("doing macro %s", macro)
comment(macro) comment(macro)
@ -53,16 +56,16 @@ for b in config.get('button', 'names').split(','):
closed = c.get('close', None) closed = c.get('close', None)
opened = c.get('open', None) opened = c.get('open', None)
# set up button on pin # set up button on pin
print('init', b, 'on GPIO', pin) log.debug('init %s on GPIO %s', b, pin)
_buttons[b] = Button(pin) _buttons[b] = Button(pin)
# connect button callbacks to anonymous functions # connect button callbacks to anonymous functions
print('connecting', b, '"closed" to macro', closed) log.info('connecting %s "closed" to macro %s', b, closed)
_buttons[b].when_pressed = lambda macro=closed: _activate(macro) _buttons[b].when_pressed = lambda macro=closed: _activate(macro)
print('connecting', b, '"opened" to macro', opened) log.info('connecting %s "opened" to macro %s', b, opened)
_buttons[b].when_released = lambda macro=opened: _activate(macro) _buttons[b].when_released = lambda macro=opened: _activate(macro)
except (KeyError, GPIODeviceError) as e: except (KeyError, GPIODeviceError) as e:
print('Error configuring button', b, e) log.error('Error configuring button %s: %s', b, e)
continue continue
@ -71,7 +74,7 @@ try:
del pin, closed, opened del pin, closed, opened
del c, config del c, config
except NameError as e: except NameError as e:
print('failed to release memory', 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
@ -82,4 +85,4 @@ if __name__ == '__main__':
print() print()
else: else:
# indicate prefered method of sleeping when laoded as a module # indicate prefered method of sleeping when laoded as a module
print('call signal.pause() instead of time.sleep()') log.debug('call signal.pause() instead of time.sleep()')