02-10-2020, 03:23 AM
(03-29-2019, 02:16 AM)Tim Curtis Wrote: Hi,
After noodling it a bit, rather than having a fixed set of actions I've settled on an approach for mapping GPIO pins to commands that allows a pin to perform an arbitrary action limited only by the command or script associated with the pin.
The feature is named "GPIO button handler" and is accessed via System config.
Here is the script that runs in the background and monitors the GPIO pins. It reads the configuration from sql table to determine which pins to monitor and what commands to run.
Code:#!/usr/bin/python
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Based on the script posted in moOde Forum by @Cyanoazimin
# 2019-MM-DD TC moOde 5.0
#
import RPi.GPIO as GPIO
import sys
import time
import datetime
import os
import subprocess
import sqlite3
# Set GPIO pin numbering mode
GPIO.setmode(GPIO.BCM)
# get the configuration
db = sqlite3.connect('/var/local/www/db/moode-sqlite3.db')
db.row_factory = sqlite3.Row
db.text_factory = str
cursor = db.cursor()
# get bounce_time
cursor.execute("SELECT value FROM cfg_gpio where param='bounce_time'")
row = cursor.fetchone()
bounce_time = int(row['value'])
#print str(datetime.datetime.now())[:19] + ' bounce_time=' + str(bounce_time)
# configure the pins
cursor.execute("SELECT * FROM cfg_gpio")
for row in cursor:
#print str(datetime.datetime.now())[:19] + ' row id=' + str(row['id']) + ', enabled=' + row['enabled'] + ', command=' + row['command']
if str(row['id']) == '1' and row['enabled'] == '1':
sw_1_pin = int(row['pin'])
sw_1_cmd = row['command'].split(' ')
GPIO.setup(sw_1_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
def sw_1_event(channel):
subprocess.call(sw_1_cmd)
GPIO.add_event_detect(sw_1_pin, GPIO.RISING, callback = sw_1_event, bouncetime = bounce_time)
print str(datetime.datetime.now())[:19] + ' sw_1: pin=' + str(sw_1_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
elif str(row['id']) == '2' and row['enabled'] == '1':
sw_2_pin = int(row['pin'])
sw_2_cmd = row['command'].split(' ')
GPIO.setup(sw_2_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
def sw_2_event(channel):
subprocess.call(sw_2_cmd)
GPIO.add_event_detect(sw_2_pin, GPIO.RISING, callback = sw_2_event, bouncetime = bounce_time)
print str(datetime.datetime.now())[:19] + ' sw_2: pin=' + str(sw_2_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
elif str(row['id']) == '3' and row['enabled'] == '1':
sw_3_pin = int(row['pin'])
sw_3_cmd = row['command'].split(' ')
GPIO.setup(sw_3_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
def sw_3_event(channel):
subprocess.call(sw_3_cmd)
GPIO.add_event_detect(sw_3_pin, GPIO.RISING, callback = sw_3_event, bouncetime = bounce_time)
print str(datetime.datetime.now())[:19] + ' sw_3: pin=' + str(sw_3_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
elif str(row['id']) == '4' and row['enabled'] == '1':
sw_4_pin = int(row['pin'])
sw_4_cmd = row['command'].split(' ')
GPIO.setup(sw_4_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
def sw_4_event(channel):
subprocess.call(sw_4_cmd)
GPIO.add_event_detect(sw_4_pin, GPIO.RISING, callback = sw_4_event, bouncetime = bounce_time)
print str(datetime.datetime.now())[:19] + ' sw_4: pin=' + str(sw_4_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
elif str(row['id']) == '5' and row['enabled'] == '1':
sw_5_pin = int(row['pin'])
sw_5_cmd = row['command'].split(' ')
GPIO.setup(sw_5_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
def sw_5_event(channel):
subprocess.call(sw_5_cmd)
GPIO.add_event_detect(sw_5_pin, GPIO.RISING, callback = sw_5_event, bouncetime = bounce_time)
print str(datetime.datetime.now())[:19] + ' sw_5: pin=' + str(sw_5_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
elif str(row['id']) == '6' and row['enabled'] == '1':
sw_6_pin = int(row['pin'])
sw_6_cmd = row['command'].split(' ')
GPIO.setup(sw_6_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
def sw_6_event(channel):
subprocess.call(sw_6_cmd)
GPIO.add_event_detect(sw_6_pin, GPIO.RISING, callback = sw_6_event, bouncetime = bounce_time)
print str(datetime.datetime.now())[:19] + ' sw_6: pin=' + str(sw_6_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
elif str(row['id']) == '7' and row['enabled'] == '1':
sw_7_pin = int(row['pin'])
sw_7_cmd = row['command'].split(' ')
GPIO.setup(sw_7_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
def sw_7_event(channel):
subprocess.call(sw_7_cmd)
GPIO.add_event_detect(sw_7_pin, GPIO.RISING, callback = sw_7_event, bouncetime = bounce_time)
print str(datetime.datetime.now())[:19] + ' sw_7: pin=' + str(sw_7_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
elif str(row['id']) == '8' and row['enabled'] == '1':
sw_8_pin = int(row['pin'])
sw_8_cmd = row['command'].split(' ')
GPIO.setup(sw_8_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
def sw_8_event(channel):
subprocess.call(sw_8_cmd)
GPIO.add_event_detect(sw_8_pin, GPIO.RISING, callback = sw_8_event, bouncetime = bounce_time)
print str(datetime.datetime.now())[:19] + ' sw_8: pin=' + str(sw_8_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
# Main
while True:
time.sleep(1)
-Tim
Tim,
I realize this is an older post, however could you provide a list of the possible commands that could be executed from the GPIO handler please or direct me to the list if you have already provided.
I am particularly looking for Random and repeat. Thanks