Moved python service file.

This commit is contained in:
John Shaver 2018-04-06 13:07:49 -07:00
parent 8bfc406e85
commit d1ffb76141
2 changed files with 12 additions and 85 deletions

15
main.py
View file

@ -2,15 +2,21 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import time import time
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
CONNECT_TO = ("10.0.0.4", 2000)
def buttonEventHandler(value): def buttonEventHandler(value):
print "buton event!"
if value is 0: if value is 0:
GPIO.output(24, True) GPIO.output(24, True)
sock.sendall("button_press")
print('Button Pressed...') print('Button Pressed...')
else: else:
GPIO.output(24, False) GPIO.output(24, False)
sock.sendall("button_release")
print('Button Released...') print('Button Released...')
def main(): def main():
@ -23,12 +29,15 @@ def main():
#GPIO.add_event_detect(23, GPIO.FALLING) #GPIO.add_event_detect(23, GPIO.FALLING)
#GPIO.add_event_callback(23, buttonEventHandler) #GPIO.add_event_callback(23, buttonEventHandler)
try: try:
sock.connect(CONNECT_TO)
while True: while True:
GPIO.wait_for_edge(23, GPIO.BOTH) GPIO.wait_for_edge(23, GPIO.BOTH, bouncetime=100)
buttonEventHandler(GPIO.input(23)) buttonEventHandler(GPIO.input(23))
except KeyboardInterrupt as e: except:
GPIO.cleanup() GPIO.cleanup()
sock.close()
raise raise
if __name__== "__main__": if __name__== "__main__":
main() main()

View file

@ -1,82 +0,0 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from pulsectl import Pulse
import SocketServer
from threading import Thread
LISTEN_ON = ("10.0.0.4", 2000)
def main():
pulse = Pulse('mic-control')
sources = pulse.source_list()
sourceNumber = getSourceSelection(sources)
server = SocketServer.TCPServer(LISTEN_ON, RequestHandler)
try:
server.serve_forever()
except KeyboardInterrupt:
server.shutdown()
server.server_close()
def getSourceSelection(sources):
sourceCount = len(sources) - 1
error = False
while True:
message = error if error else "Please select a source: "
print(message)
for i, source in enumerate(sources):
print "%i: %s" % (i, source.description)
selection = raw_input("Enter a number[0-%i]:" % sourceCount)
print("")
try :
return getInt(selection, sourceCount)
except InputError as e:
error = e.message
def getInt(value, sourceCount):
number = 0
try:
number = int(value)
except ValueError as e:
raise InputError("Enter only a number...")
if number not in range(0, sourceCount):
raise InputError("Not a valid option...")
return number
class RequestHandler(SocketServer.StreamRequestHandler):
def handle(self):
for line in self.rfile:
message = line.strip()
if message in messageTypes:
messageTypes[message]()
self.wfile.write(message)
print("Closed!")
self.request.close()
def buttonPressed():
print("down!")
def buttonReleased():
print("up!")
messageTypes = {
'button_press': buttonPressed,
'button_release': buttonReleased,
}
class InputError(Exception):
def __init__(self, message):
self.message = message
main()