-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbutton_test.py
More file actions
executable file
·90 lines (73 loc) · 3.61 KB
/
button_test.py
File metadata and controls
executable file
·90 lines (73 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2020-2021 by Murray Altheim. All rights reserved. This file is part
# of the Robot Operating System project, released under the MIT License. Please
# see the LICENSE file included as part of this package.
#
# This tests the Button task, which reads the state of the push button.
#
import pigpio
import os, sys, signal, time
from colorama import init, Fore, Style
init()
#import RPi.GPIO as GPIO
from core.logger import Logger, Level
from core.component import Component
_log = Logger('button-test', Level.INFO)
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
class Button(Component):
'''
Sets up a falling-edge callback on a GPIO pin.
'''
def __init__(self, pin, callback, level=Level.INFO):
self._log = Logger('button', level)
Component.__init__(self, self._log, suppressed=False, enabled=True)
self._pi = pigpio.pi()
if not self._pi.connected:
raise Exception('unable to establish connection to Pi.')
self._log.info('establishing callback on pin {:d}.'.format(pin))
self._pi.set_mode(gpio=pin, mode=pigpio.INPUT) # GPIO 12 as input
_cb1 = self._pi.callback(pin, pigpio.EITHER_EDGE, callback)
self._log.info('ready.')
# ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
@property
def name(self):
return 'button'
# ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
def close(self):
if self._pi:
self._pi.stop()
Component.close(self)
self._log.info('closed.')
# ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
def callback_method(gpio, level, tick):
global activated
activated = True
print(Fore.YELLOW + 'callback method fired; gpio: {}; level: {}; tick: {}'.format(gpio, level, tick) + Style.RESET_ALL)
# _log.info(Fore.YELLOW + 'callback method fired; gpio: {}; level: {}; tick: {}'.format(gpio, level, tick))
# main ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
_button = None
def main():
global activated
activated = False
_log.info('starting test...')
# GPIO.setmode(GPIO.BCM)
# GPIO.setup(_pin, GPIO.IN)
# GPIO.add_event_detect(_pin, GPIO.FALLING, callback=callback_method, bouncetime=200)
try:
_pin = 12
_button = Button(_pin, callback_method)
_button.enable()
while True:
_log.info(Fore.BLACK + 'waiting for button press on pin {:d};\t'.format(_pin) + Fore.GREEN + ' activated: {}'.format(activated))
time.sleep(1.0)
except KeyboardInterrupt:
_log.info(Fore.RED + "caught Ctrl-C.")
finally:
if _button:
_log.info("closing button...")
if _button:
_button.close()
if __name__== "__main__":
main()