2015-01-06 23:06:28 +01:00
|
|
|
#!/usr/bin/python
|
2014-12-27 14:11:44 +01:00
|
|
|
#
|
2017-01-11 16:24:33 +01:00
|
|
|
# Copyright JS Foundation and other contributors, http://js.foundation
|
2014-12-27 14:11:44 +01:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
|
|
|
# Import library functions we need
|
|
|
|
import RPi.GPIO as GPIO
|
2015-12-21 11:27:58 +01:00
|
|
|
import struct
|
2014-12-27 14:11:44 +01:00
|
|
|
import sys
|
2015-12-21 11:27:58 +01:00
|
|
|
import os
|
|
|
|
import subprocess
|
2016-01-25 10:56:35 +01:00
|
|
|
from time import sleep
|
2014-12-27 14:11:44 +01:00
|
|
|
|
2016-01-29 21:01:01 +01:00
|
|
|
bounce = 25;
|
2014-12-27 14:11:44 +01:00
|
|
|
|
2015-12-12 16:13:15 +01:00
|
|
|
if len(sys.argv) > 2:
|
2014-12-27 14:11:44 +01:00
|
|
|
cmd = sys.argv[1].lower()
|
|
|
|
pin = int(sys.argv[2])
|
2017-07-02 10:59:37 +02:00
|
|
|
GPIO.setmode(GPIO.BOARD)
|
2014-12-27 14:11:44 +01:00
|
|
|
GPIO.setwarnings(False)
|
|
|
|
|
|
|
|
if cmd == "pwm":
|
2018-07-07 20:01:14 +02:00
|
|
|
#print("Initialised pin "+str(pin)+" to PWM")
|
2017-07-01 23:42:13 +02:00
|
|
|
try:
|
2017-03-23 22:02:22 +01:00
|
|
|
freq = int(sys.argv[3])
|
|
|
|
except:
|
|
|
|
freq = 100
|
|
|
|
|
2014-12-27 14:11:44 +01:00
|
|
|
GPIO.setup(pin,GPIO.OUT)
|
2017-03-23 22:02:22 +01:00
|
|
|
p = GPIO.PWM(pin, freq)
|
2014-12-27 14:11:44 +01:00
|
|
|
p.start(0)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data = raw_input()
|
2015-02-25 20:10:44 +01:00
|
|
|
if 'close' in data:
|
2014-12-27 14:11:44 +01:00
|
|
|
sys.exit(0)
|
|
|
|
p.ChangeDutyCycle(float(data))
|
2015-02-25 20:10:44 +01:00
|
|
|
except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program
|
2014-12-27 14:11:44 +01:00
|
|
|
GPIO.cleanup(pin)
|
|
|
|
sys.exit(0)
|
|
|
|
except Exception as ex:
|
2018-07-07 20:01:14 +02:00
|
|
|
print("bad data: "+data)
|
2014-12-27 14:11:44 +01:00
|
|
|
|
2015-01-06 23:06:28 +01:00
|
|
|
elif cmd == "buzz":
|
2018-07-07 20:01:14 +02:00
|
|
|
#print("Initialised pin "+str(pin)+" to Buzz")
|
2014-12-27 14:11:44 +01:00
|
|
|
GPIO.setup(pin,GPIO.OUT)
|
|
|
|
p = GPIO.PWM(pin, 100)
|
|
|
|
p.stop()
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data = raw_input()
|
2015-02-25 20:10:44 +01:00
|
|
|
if 'close' in data:
|
2014-12-27 14:11:44 +01:00
|
|
|
sys.exit(0)
|
|
|
|
elif float(data) == 0:
|
|
|
|
p.stop()
|
|
|
|
else:
|
|
|
|
p.start(50)
|
|
|
|
p.ChangeFrequency(float(data))
|
2015-02-25 20:10:44 +01:00
|
|
|
except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program
|
2014-12-27 14:11:44 +01:00
|
|
|
GPIO.cleanup(pin)
|
|
|
|
sys.exit(0)
|
|
|
|
except Exception as ex:
|
2018-07-07 20:01:14 +02:00
|
|
|
print("bad data: "+data)
|
2014-12-27 14:11:44 +01:00
|
|
|
|
|
|
|
elif cmd == "out":
|
2018-07-07 20:01:14 +02:00
|
|
|
#print("Initialised pin "+str(pin)+" to OUT")
|
2014-12-27 14:11:44 +01:00
|
|
|
GPIO.setup(pin,GPIO.OUT)
|
|
|
|
if len(sys.argv) == 4:
|
|
|
|
GPIO.output(pin,int(sys.argv[3]))
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data = raw_input()
|
2015-02-25 20:10:44 +01:00
|
|
|
if 'close' in data:
|
2014-12-27 14:11:44 +01:00
|
|
|
sys.exit(0)
|
|
|
|
data = int(data)
|
2015-02-25 20:10:44 +01:00
|
|
|
except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program
|
2014-12-27 14:11:44 +01:00
|
|
|
GPIO.cleanup(pin)
|
|
|
|
sys.exit(0)
|
|
|
|
except:
|
2016-10-11 16:37:34 +02:00
|
|
|
if len(sys.argv) == 4:
|
|
|
|
data = int(sys.argv[3])
|
|
|
|
else:
|
|
|
|
data = 0
|
2014-12-27 14:11:44 +01:00
|
|
|
if data != 0:
|
|
|
|
data = 1
|
|
|
|
GPIO.output(pin,data)
|
|
|
|
|
|
|
|
elif cmd == "in":
|
2018-07-07 20:01:14 +02:00
|
|
|
#print("Initialised pin "+str(pin)+" to IN")
|
2017-02-27 20:22:02 +01:00
|
|
|
bounce = float(sys.argv[4])
|
2014-12-27 14:11:44 +01:00
|
|
|
def handle_callback(chan):
|
2017-02-27 20:22:02 +01:00
|
|
|
sleep(bounce/1000.0)
|
2018-07-07 20:01:14 +02:00
|
|
|
print(GPIO.input(chan))
|
2014-12-27 14:11:44 +01:00
|
|
|
|
2016-01-25 10:56:35 +01:00
|
|
|
if sys.argv[3].lower() == "up":
|
|
|
|
GPIO.setup(pin,GPIO.IN,GPIO.PUD_UP)
|
|
|
|
elif sys.argv[3].lower() == "down":
|
|
|
|
GPIO.setup(pin,GPIO.IN,GPIO.PUD_DOWN)
|
2014-12-27 14:11:44 +01:00
|
|
|
else:
|
|
|
|
GPIO.setup(pin,GPIO.IN)
|
2016-01-25 10:56:35 +01:00
|
|
|
|
2018-07-07 20:01:14 +02:00
|
|
|
print(GPIO.input(pin))
|
2017-02-27 20:22:02 +01:00
|
|
|
GPIO.add_event_detect(pin, GPIO.BOTH, callback=handle_callback, bouncetime=int(bounce))
|
2014-12-27 14:11:44 +01:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data = raw_input()
|
2015-02-25 20:10:44 +01:00
|
|
|
if 'close' in data:
|
2014-12-27 14:11:44 +01:00
|
|
|
sys.exit(0)
|
2015-02-25 20:10:44 +01:00
|
|
|
except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program
|
2014-12-27 14:11:44 +01:00
|
|
|
GPIO.cleanup(pin)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2015-01-06 23:06:28 +01:00
|
|
|
elif cmd == "byte":
|
2018-07-07 20:01:14 +02:00
|
|
|
#print("Initialised BYTE mode - "+str(pin)+)
|
2017-07-02 10:59:37 +02:00
|
|
|
list = [7,11,13,12,15,16,18,22]
|
2015-01-06 23:06:28 +01:00
|
|
|
GPIO.setup(list,GPIO.OUT)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data = raw_input()
|
2015-02-25 20:10:44 +01:00
|
|
|
if 'close' in data:
|
2015-01-06 23:06:28 +01:00
|
|
|
sys.exit(0)
|
|
|
|
data = int(data)
|
2015-02-25 20:10:44 +01:00
|
|
|
except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program
|
2015-01-06 23:06:28 +01:00
|
|
|
GPIO.cleanup()
|
|
|
|
sys.exit(0)
|
|
|
|
except:
|
|
|
|
data = 0
|
|
|
|
for bit in range(8):
|
|
|
|
if pin == 1:
|
|
|
|
mask = 1 << (7 - bit)
|
|
|
|
else:
|
|
|
|
mask = 1 << bit
|
|
|
|
GPIO.output(list[bit], data & mask)
|
|
|
|
|
2015-01-29 22:42:33 +01:00
|
|
|
elif cmd == "borg":
|
2018-07-07 20:01:14 +02:00
|
|
|
#print("Initialised BORG mode - "+str(pin)+)
|
2017-07-02 10:59:37 +02:00
|
|
|
GPIO.setup(11,GPIO.OUT)
|
|
|
|
GPIO.setup(13,GPIO.OUT)
|
|
|
|
GPIO.setup(15,GPIO.OUT)
|
|
|
|
r = GPIO.PWM(11, 100)
|
|
|
|
g = GPIO.PWM(13, 100)
|
|
|
|
b = GPIO.PWM(15, 100)
|
2015-01-29 22:42:33 +01:00
|
|
|
r.start(0)
|
|
|
|
g.start(0)
|
|
|
|
b.start(0)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data = raw_input()
|
2015-02-25 20:10:44 +01:00
|
|
|
if 'close' in data:
|
2015-01-29 22:42:33 +01:00
|
|
|
sys.exit(0)
|
|
|
|
c = data.split(",")
|
|
|
|
r.ChangeDutyCycle(float(c[0]))
|
|
|
|
g.ChangeDutyCycle(float(c[1]))
|
|
|
|
b.ChangeDutyCycle(float(c[2]))
|
2015-02-25 20:10:44 +01:00
|
|
|
except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program
|
2015-01-29 22:42:33 +01:00
|
|
|
GPIO.cleanup()
|
|
|
|
sys.exit(0)
|
|
|
|
except:
|
|
|
|
data = 0
|
|
|
|
|
2015-01-06 23:06:28 +01:00
|
|
|
elif cmd == "mouse": # catch mice button events
|
|
|
|
file = open( "/dev/input/mice", "rb" )
|
|
|
|
oldbutt = 0
|
|
|
|
|
|
|
|
def getMouseEvent():
|
|
|
|
global oldbutt
|
|
|
|
global pin
|
|
|
|
buf = file.read(3)
|
|
|
|
pin = pin & 0x07
|
|
|
|
button = ord( buf[0] ) & pin # mask out just the required button(s)
|
|
|
|
if button != oldbutt: # only send if changed
|
|
|
|
oldbutt = button
|
2018-07-07 20:01:14 +02:00
|
|
|
print(button)
|
2015-01-06 23:06:28 +01:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
getMouseEvent()
|
|
|
|
except:
|
|
|
|
file.close()
|
|
|
|
sys.exit(0)
|
|
|
|
|
2015-12-21 11:27:58 +01:00
|
|
|
elif cmd == "kbd": # catch keyboard button events
|
|
|
|
try:
|
|
|
|
while not os.path.isdir("/dev/input/by-path"):
|
|
|
|
time.sleep(10)
|
|
|
|
infile = subprocess.check_output("ls /dev/input/by-path/ | grep -m 1 'kbd'", shell=True).strip()
|
|
|
|
infile_path = "/dev/input/by-path/" + infile
|
|
|
|
EVENT_SIZE = struct.calcsize('llHHI')
|
|
|
|
file = open(infile_path, "rb")
|
|
|
|
event = file.read(EVENT_SIZE)
|
|
|
|
while event:
|
|
|
|
(tv_sec, tv_usec, type, code, value) = struct.unpack('llHHI', event)
|
|
|
|
#if type != 0 or code != 0 or value != 0:
|
|
|
|
if type == 1:
|
|
|
|
# type,code,value
|
|
|
|
print("%u,%u" % (code, value))
|
|
|
|
event = file.read(EVENT_SIZE)
|
2018-07-07 20:01:14 +02:00
|
|
|
print("0,0")
|
2015-12-21 11:27:58 +01:00
|
|
|
file.close()
|
|
|
|
sys.exit(0)
|
|
|
|
except:
|
|
|
|
file.close()
|
|
|
|
sys.exit(0)
|
|
|
|
|
2015-12-12 16:13:15 +01:00
|
|
|
elif len(sys.argv) > 1:
|
|
|
|
cmd = sys.argv[1].lower()
|
|
|
|
if cmd == "rev":
|
2018-07-07 20:01:14 +02:00
|
|
|
print(GPIO.RPI_REVISION)
|
2015-12-12 16:13:15 +01:00
|
|
|
elif cmd == "ver":
|
2018-07-07 20:01:14 +02:00
|
|
|
print(GPIO.VERSION)
|
2016-01-25 10:56:35 +01:00
|
|
|
elif cmd == "info":
|
2018-07-07 20:01:14 +02:00
|
|
|
print(GPIO.RPI_INFO)
|
2015-12-12 16:13:15 +01:00
|
|
|
else:
|
2018-07-07 20:01:14 +02:00
|
|
|
print("Bad parameters - in|out|pwm|buzz|byte|borg|mouse|kbd|ver|info {pin} {value|up|down}")
|
|
|
|
print(" only ver (gpio version) and info (board information) accept no pin parameter.")
|
2015-12-12 16:13:15 +01:00
|
|
|
|
2014-12-27 14:11:44 +01:00
|
|
|
else:
|
2018-07-07 20:01:14 +02:00
|
|
|
print("Bad parameters - in|out|pwm|buzz|byte|borg|mouse|kbd|ver|info {pin} {value|up|down}")
|