Compare commits
No commits in common. "c2e1ac3ae9880ecfee1a5aab80b748e9ddc073a9" and "0b11e06e058f80ab8da5ba24235e6e82e940f23f" have entirely different histories.
c2e1ac3ae9
...
0b11e06e05
2 changed files with 22 additions and 108 deletions
48
main.py
48
main.py
|
@ -3,32 +3,28 @@
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
GPIO.setmode(GPIO.BCM)
|
||||||
|
|
||||||
def buttonEventHandler(value):
|
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||||
print "buton event!"
|
GPIO.setup(24, GPIO.OUT)
|
||||||
if value is 0:
|
|
||||||
|
try:
|
||||||
|
lastState = GPIO.input(23)
|
||||||
|
if lastState == False:
|
||||||
GPIO.output(24, True)
|
GPIO.output(24, True)
|
||||||
print('Button Pressed...')
|
while True:
|
||||||
else:
|
button_state = GPIO.input(23)
|
||||||
GPIO.output(24, False)
|
if button_state == lastState:
|
||||||
print('Button Released...')
|
print('No Change...')
|
||||||
|
else:
|
||||||
|
if button_state == False:
|
||||||
|
GPIO.output(24, True)
|
||||||
|
print('Button Pressed...')
|
||||||
|
else:
|
||||||
|
GPIO.output(24, False)
|
||||||
|
print('Button Released...')
|
||||||
|
lastState = button_state
|
||||||
|
time.sleep(0.02)
|
||||||
|
except:
|
||||||
|
GPIO.cleanup()
|
||||||
|
|
||||||
def main():
|
|
||||||
print "Let's do this!"
|
|
||||||
|
|
||||||
GPIO.setmode(GPIO.BCM)
|
|
||||||
|
|
||||||
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
|
||||||
GPIO.setup(24, GPIO.OUT)
|
|
||||||
#GPIO.add_event_detect(23, GPIO.FALLING)
|
|
||||||
#GPIO.add_event_callback(23, buttonEventHandler)
|
|
||||||
try:
|
|
||||||
while True:
|
|
||||||
GPIO.wait_for_edge(23, GPIO.BOTH)
|
|
||||||
buttonEventHandler(GPIO.input(23))
|
|
||||||
except KeyboardInterrupt as e:
|
|
||||||
GPIO.cleanup()
|
|
||||||
raise
|
|
||||||
|
|
||||||
if __name__== "__main__":
|
|
||||||
main()
|
|
||||||
|
|
|
@ -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()
|
|
Loading…
Reference in a new issue