Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


moOde 5.0 Release Candidate RC1
#31
(04-03-2019, 08:43 PM)imi11 Wrote: Could you explain the "debug print" (after entering second sudo command)...

Sure. See the actual script below. When its run from the cmd line it prints the configuration for each button thats ON.

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)
Enjoy the Music!
moodeaudio.org | Mastodon Feed | GitHub
Reply


Messages In This Thread
moOde 5.0 Release Candidate RC1 - by Tim Curtis - 03-29-2019, 07:06 PM
RE: moOde 5.0 Release Candidate RC1 - by rylfas - 03-31-2019, 09:00 AM
RE: moOde 5.0 Release Candidate RC1 - by rylfas - 03-31-2019, 01:26 PM
RE: moOde 5.0 Release Candidate RC1 - by rylfas - 03-31-2019, 08:32 PM
RE: moOde 5.0 Release Candidate RC1 - by Edward - 03-31-2019, 07:17 PM
RE: moOde 5.0 Release Candidate RC1 - by jonners - 03-31-2019, 10:02 PM
RE: moOde 5.0 Release Candidate RC1 - by JST1963 - 03-31-2019, 10:43 PM
RE: moOde 5.0 Release Candidate RC1 - by JST1963 - 04-01-2019, 11:51 PM
RE: moOde 5.0 Release Candidate RC1 - by imi11 - 04-03-2019, 05:41 PM
RE: moOde 5.0 Release Candidate RC1 - by imi11 - 04-03-2019, 06:11 PM
RE: moOde 5.0 Release Candidate RC1 - by miklau - 04-03-2019, 06:24 PM
RE: moOde 5.0 Release Candidate RC1 - by miklau - 04-03-2019, 08:28 PM
RE: moOde 5.0 Release Candidate RC1 - by imi11 - 04-03-2019, 07:41 PM
RE: moOde 5.0 Release Candidate RC1 - by Edward - 04-03-2019, 09:29 PM
RE: moOde 5.0 Release Candidate RC1 - by imi11 - 04-04-2019, 06:13 PM
RE: moOde 5.0 Release Candidate RC1 - by miklau - 04-03-2019, 08:47 PM
RE: moOde 5.0 Release Candidate RC1 - by miklau - 04-04-2019, 08:50 PM
RE: moOde 5.0 Release Candidate RC1 - by miklau - 04-06-2019, 08:59 AM
RE: moOde 5.0 Release Candidate RC1 - by imi11 - 04-03-2019, 08:43 PM
RE: moOde 5.0 Release Candidate RC1 - by Tim Curtis - 04-03-2019, 08:58 PM
RE: moOde 5.0 Release Candidate RC1 - by HemiRick - 04-04-2019, 09:41 PM
RE: moOde 5.0 Release Candidate RC1 - by Zigzag - 04-05-2019, 12:14 PM
RE: moOde 5.0 Release Candidate RC1 - by Macdelf - 04-09-2019, 07:37 PM
RE: moOde 5.0 Release Candidate RC1 - by Macdelf - 04-10-2019, 12:30 PM

Forum Jump: