-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathgpio_buttons_ng.py
More file actions
67 lines (59 loc) · 1.98 KB
/
gpio_buttons_ng.py
File metadata and controls
67 lines (59 loc) · 1.98 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
import logging
import RPi.GPIO as GPIO
import subprocess
import pwnagotchi.plugins as plugins
class GPIOButtons_ng(plugins.Plugin):
__GitHub__ = ""
__author__ = "(edited by: itsdarklikehell bauke.molenaar@gmail.com), ratmandu@gmail.com"
__version__ = "1.0.0"
__license__ = "GPL3"
__description__ = "GPIO Button support plugin"
__name__ = "GPIOButtons_ng"
__help__ = "GPIO Button support plugin"
__dependencies__ = {
"apt": ["none"],
"pip": ["scapy"],
}
__defaults__ = {
"enabled": False,
}
def __init__(self):
self.running = False
self.ports = {}
self.commands = None
def runCommand(self, channel):
command = self.ports[channel]
logging.info(
f"[{self.__class__.__name__}] Button Pressed! Running command: {command}"
)
process = subprocess.Popen(
command,
shell=True,
stdin=None,
stdout=open("/dev/null", "w"),
stderr=None,
executable="/bin/bash",
)
process.wait()
def on_loaded(self):
logging.info(f"[{self.__class__.__name__}] plugin loaded")
# get list of GPIOs
gpios = self.options["gpios"]
# set gpio numbering
GPIO.setmode(GPIO.BCM)
for gpio, command in gpios.items():
gpio = int(gpio)
self.ports[gpio] = command
GPIO.setup(gpio, GPIO.IN, GPIO.PUD_UP)
GPIO.add_event_detect(
gpio, GPIO.FALLING, callback=self.runCommand, bouncetime=600
)
# set pimoroni display hat mini LED off/dim
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
logging.info(
f"[{self.__class__.__name__}] Added command: %s to GPIO #%d",
command,
gpio,
)