Last active
August 1, 2016 17:03
-
-
Save Adirockzz95/3571d23692ff51925443cac984909fd1 to your computer and use it in GitHub Desktop.
Selfie bluetooth controller
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
import subprocess | |
import logging | |
import time | |
# CHIP IO | |
import CHIP_IO.GPIO as GPIO | |
# import evedev module | |
import evdev | |
def connect(): | |
cmd = ['./Blue.sh'] | |
subprocess.call(cmd) | |
time.sleep(5) | |
try: | |
dev = evdev.InputDevice('/dev/input/event1') | |
print("connected..") | |
return dev | |
except OSError: | |
logging.error("No bluetooth device found. Exiting the program") | |
raise | |
if __name__ == "__main__": | |
# use constants for better readability | |
LOW = 0 | |
HIGH = 1 | |
connected = False | |
# The logic used here is Active low. That means, when the | |
# GPIO is pulled HIGH then LED goes off and when the GPIO | |
# is pulled LOW then LED goes on. | |
# default state is HIGH | |
RED_CURRENT_STATE = HIGH | |
GREEN_CURRENT_STATE = HIGH | |
# GPIO4 is XIO-P5 on the CHIP | |
GPIO.setup("GPIO4", GPIO.OUT) | |
# GPIO3 is XIO-P4 on the CHIP | |
GPIO.setup("GPIO3", GPIO.OUT) | |
GPIO.output("GPIO4", GPIO.HIGH) | |
GPIO.output("GPIO3", GPIO.HIGH) | |
while True: | |
try: | |
# first connection try | |
if not connected: | |
device = connect() | |
connected = True | |
if connected: | |
for event in device.read_loop(): | |
# 28 is the event code for Android button | |
if event.code == 28 and event.value == 1: | |
if GREEN_CURRENT_STATE == HIGH: | |
GPIO.output("GPIO4", GPIO.LOW) | |
print("GREEN LED ON") | |
GREEN_CURRENT_STATE = LOW | |
elif GREEN_CURRENT_STATE == LOW: | |
GPIO.output("GPIO4", GPIO.HIGH) | |
print("GREEN LED OFF") | |
GREEN_CURRENT_STATE = HIGH | |
# 115 is the event code for IOS button. | |
elif event.code == 115 and event.value == 1: | |
if RED_CURRENT_STATE == HIGH: | |
GPIO.output("GPIO3", GPIO.LOW) | |
print("RED LED ON") | |
RED_CURRENT_STATE = LOW | |
elif RED_CURRENT_STATE == LOW: | |
GPIO.output("GPIO3", GPIO.HIGH) | |
print("RED LED OFF") | |
RED_CURRENT_STATE = HIGH | |
except (OSError, IOError): | |
# try connecting last time | |
# if failed then exit the program | |
# if connected then continue the inner for loop | |
device = connect() | |
connected = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment