misc: refactored plugin system to use classes

This commit is contained in:
Simone Margaritelli
2019-11-01 13:51:45 +01:00
parent ae330dc0b5
commit 2f948306eb
20 changed files with 943 additions and 1049 deletions

View File

@@ -1,38 +1,40 @@
__author__ = 'ratmandu@gmail.com'
__version__ = '1.0.0'
__name__ = 'gpio_buttons'
__license__ = 'GPL3'
__description__ = 'GPIO Button support plugin'
import logging
import RPi.GPIO as GPIO
import subprocess
running = False
OPTIONS = dict()
GPIOs = {}
COMMANDs = None
def runCommand(channel):
command = GPIOs[channel]
logging.info(f"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()
import pwnagotchi.plugins as plugins
def on_loaded():
logging.info("GPIO Button plugin loaded.")
#get list of GPIOs
gpios = OPTIONS['gpios']
class GPIOButtons(plugins.Plugin):
__author__ = 'ratmandu@gmail.com'
__version__ = '1.0.0'
__license__ = 'GPL3'
__description__ = 'GPIO Button support plugin'
#set gpio numbering
GPIO.setmode(GPIO.BCM)
def __init__(self):
self.running = False
self.ports = {}
self.commands = None
for i in gpios:
gpio = list(i)[0]
command = i[gpio]
GPIOs[gpio] = command
GPIO.setup(gpio, GPIO.IN, GPIO.PUD_UP)
GPIO.add_event_detect(gpio, GPIO.FALLING, callback=runCommand, bouncetime=250)
logging.info("Added command: %s to GPIO #%d", command, gpio)
def runCommand(self, channel):
command = self.ports[channel]
logging.info(f"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("GPIO Button plugin loaded.")
# get list of GPIOs
gpios = self.options['gpios']
# set gpio numbering
GPIO.setmode(GPIO.BCM)
for i in gpios:
gpio = list(i)[0]
command = i[gpio]
self.ports[gpio] = command
GPIO.setup(gpio, GPIO.IN, GPIO.PUD_UP)
GPIO.add_event_detect(gpio, GPIO.FALLING, callback=self.runCommand, bouncetime=250)
logging.info("Added command: %s to GPIO #%d", command, gpio)