Last active
October 22, 2015 15:25
-
-
Save misterhay/e06109777b85115dbb62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# some code/ideas borrowed from http://youtu.be/oaf_zQcrg7g | |
import time, threading | |
delayTime = 0.25 | |
import RPi.GPIO as GPIO | |
GPIO.setmode(GPIO.BCM) | |
pins = [2, 3, 4, 17, 27, 22, 10, 9, 11, 14, 15, 18, 23, 24, 25, 8, 7] | |
#pins = [2, 3, 4, 17, 27, 22, 10, 9, 14, 15, 18, 23, 24, 25, 8, 7] | |
#pins = [2, 3, 4, 17, 27, 22, 10, 9, 11] | |
#pins = [14, 15, 18, 23, 24, 25] | |
# iterate through pins to set them up and flash them | |
for pin in pins: | |
print 'Initializing', pin | |
GPIO.setup(pin, GPIO.OUT) | |
GPIO.output(pin, GPIO.LOW) | |
time.sleep(delayTime) | |
GPIO.output(pin, GPIO.HIGH) | |
import OSC | |
import threading | |
receiveAddress = ('0.0.0.0', 9000) # allow it to receive from any IP on port 9000 | |
server = OSC.OSCServer(receiveAddress) # set up the OSC server | |
#server.addDefaultHandlers() # not really necessary, but won't hurt | |
print 'To turn on a pin send /output/str(pin) [1]' | |
# e.g. | |
#OSC.OSCMessage('/output/'+str(pin))[1]' | |
#client.send(message) | |
def outputHandler(address, typetag, value, source): | |
# typetag should be i for integer (1 for output on, 0 for off) | |
output = int(address.split('/')[2]) # get the output number from the OSC address | |
pin = pins[output-1] # translate the output number back to a pin number from the outputs list | |
if value == [0]: | |
print 'turning off pin', pin, 'from', output | |
GPIO.output(pin, GPIO.HIGH) # turn on the pin which turns off the relay | |
if value == [1]: | |
print 'turning on pin', pin, 'from', output | |
GPIO.output(pin, GPIO.LOW) # turn off the pin which turns on the relay | |
for i in range(0, len(pins)): | |
server.addMsgHandler('/output/'+str(i), outputHandler) | |
for addr in sorted(server.getOSCAddressSpace()): | |
print addr | |
threadingServer = threading.Thread(target = server.serve_forever) | |
threadingServer.start() # start the server thread we just defined | |
print 'OSC receiving server is running, press ctrl-c to quit' | |
try: | |
while 1: # almost the same as while True: | |
time.sleep(5) # do nothing here, just wait for ctrl-c | |
except KeyboardInterrupt: # what to do if there's a ctrl-c | |
print 'Stopping the OSC receiving server...' | |
server.close() | |
print 'Waiting...' | |
threadingServer.join() # stop the thread | |
GPIO.cleanup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment