mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Let nrgpio code work with python 3
(just in case that becomes default)
This commit is contained in:
parent
946a6d6041
commit
4bcf13cb58
@ -23,10 +23,6 @@ from time import sleep
|
|||||||
|
|
||||||
bounce = 25;
|
bounce = 25;
|
||||||
|
|
||||||
if sys.version_info >= (3,0):
|
|
||||||
print("Sorry - currently only configured to work with python 2.x")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
if len(sys.argv) > 2:
|
if len(sys.argv) > 2:
|
||||||
cmd = sys.argv[1].lower()
|
cmd = sys.argv[1].lower()
|
||||||
pin = int(sys.argv[2])
|
pin = int(sys.argv[2])
|
||||||
@ -34,7 +30,7 @@ if len(sys.argv) > 2:
|
|||||||
GPIO.setwarnings(False)
|
GPIO.setwarnings(False)
|
||||||
|
|
||||||
if cmd == "pwm":
|
if cmd == "pwm":
|
||||||
#print "Initialised pin "+str(pin)+" to PWM"
|
#print("Initialised pin "+str(pin)+" to PWM")
|
||||||
try:
|
try:
|
||||||
freq = int(sys.argv[3])
|
freq = int(sys.argv[3])
|
||||||
except:
|
except:
|
||||||
@ -54,10 +50,10 @@ if len(sys.argv) > 2:
|
|||||||
GPIO.cleanup(pin)
|
GPIO.cleanup(pin)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print "bad data: "+data
|
print("bad data: "+data)
|
||||||
|
|
||||||
elif cmd == "buzz":
|
elif cmd == "buzz":
|
||||||
#print "Initialised pin "+str(pin)+" to Buzz"
|
#print("Initialised pin "+str(pin)+" to Buzz")
|
||||||
GPIO.setup(pin,GPIO.OUT)
|
GPIO.setup(pin,GPIO.OUT)
|
||||||
p = GPIO.PWM(pin, 100)
|
p = GPIO.PWM(pin, 100)
|
||||||
p.stop()
|
p.stop()
|
||||||
@ -76,10 +72,10 @@ if len(sys.argv) > 2:
|
|||||||
GPIO.cleanup(pin)
|
GPIO.cleanup(pin)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print "bad data: "+data
|
print("bad data: "+data)
|
||||||
|
|
||||||
elif cmd == "out":
|
elif cmd == "out":
|
||||||
#print "Initialised pin "+str(pin)+" to OUT"
|
#print("Initialised pin "+str(pin)+" to OUT")
|
||||||
GPIO.setup(pin,GPIO.OUT)
|
GPIO.setup(pin,GPIO.OUT)
|
||||||
if len(sys.argv) == 4:
|
if len(sys.argv) == 4:
|
||||||
GPIO.output(pin,int(sys.argv[3]))
|
GPIO.output(pin,int(sys.argv[3]))
|
||||||
@ -103,11 +99,11 @@ if len(sys.argv) > 2:
|
|||||||
GPIO.output(pin,data)
|
GPIO.output(pin,data)
|
||||||
|
|
||||||
elif cmd == "in":
|
elif cmd == "in":
|
||||||
#print "Initialised pin "+str(pin)+" to IN"
|
#print("Initialised pin "+str(pin)+" to IN")
|
||||||
bounce = float(sys.argv[4])
|
bounce = float(sys.argv[4])
|
||||||
def handle_callback(chan):
|
def handle_callback(chan):
|
||||||
sleep(bounce/1000.0)
|
sleep(bounce/1000.0)
|
||||||
print GPIO.input(chan)
|
print(GPIO.input(chan))
|
||||||
|
|
||||||
if sys.argv[3].lower() == "up":
|
if sys.argv[3].lower() == "up":
|
||||||
GPIO.setup(pin,GPIO.IN,GPIO.PUD_UP)
|
GPIO.setup(pin,GPIO.IN,GPIO.PUD_UP)
|
||||||
@ -116,7 +112,7 @@ if len(sys.argv) > 2:
|
|||||||
else:
|
else:
|
||||||
GPIO.setup(pin,GPIO.IN)
|
GPIO.setup(pin,GPIO.IN)
|
||||||
|
|
||||||
print GPIO.input(pin)
|
print(GPIO.input(pin))
|
||||||
GPIO.add_event_detect(pin, GPIO.BOTH, callback=handle_callback, bouncetime=int(bounce))
|
GPIO.add_event_detect(pin, GPIO.BOTH, callback=handle_callback, bouncetime=int(bounce))
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
@ -129,7 +125,7 @@ if len(sys.argv) > 2:
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
elif cmd == "byte":
|
elif cmd == "byte":
|
||||||
#print "Initialised BYTE mode - "+str(pin)+
|
#print("Initialised BYTE mode - "+str(pin)+)
|
||||||
list = [7,11,13,12,15,16,18,22]
|
list = [7,11,13,12,15,16,18,22]
|
||||||
GPIO.setup(list,GPIO.OUT)
|
GPIO.setup(list,GPIO.OUT)
|
||||||
|
|
||||||
@ -152,7 +148,7 @@ if len(sys.argv) > 2:
|
|||||||
GPIO.output(list[bit], data & mask)
|
GPIO.output(list[bit], data & mask)
|
||||||
|
|
||||||
elif cmd == "borg":
|
elif cmd == "borg":
|
||||||
#print "Initialised BORG mode - "+str(pin)+
|
#print("Initialised BORG mode - "+str(pin)+)
|
||||||
GPIO.setup(11,GPIO.OUT)
|
GPIO.setup(11,GPIO.OUT)
|
||||||
GPIO.setup(13,GPIO.OUT)
|
GPIO.setup(13,GPIO.OUT)
|
||||||
GPIO.setup(15,GPIO.OUT)
|
GPIO.setup(15,GPIO.OUT)
|
||||||
@ -190,7 +186,7 @@ if len(sys.argv) > 2:
|
|||||||
button = ord( buf[0] ) & pin # mask out just the required button(s)
|
button = ord( buf[0] ) & pin # mask out just the required button(s)
|
||||||
if button != oldbutt: # only send if changed
|
if button != oldbutt: # only send if changed
|
||||||
oldbutt = button
|
oldbutt = button
|
||||||
print button
|
print(button)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
@ -215,7 +211,7 @@ if len(sys.argv) > 2:
|
|||||||
# type,code,value
|
# type,code,value
|
||||||
print("%u,%u" % (code, value))
|
print("%u,%u" % (code, value))
|
||||||
event = file.read(EVENT_SIZE)
|
event = file.read(EVENT_SIZE)
|
||||||
print "0,0"
|
print("0,0")
|
||||||
file.close()
|
file.close()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
except:
|
except:
|
||||||
@ -225,14 +221,14 @@ if len(sys.argv) > 2:
|
|||||||
elif len(sys.argv) > 1:
|
elif len(sys.argv) > 1:
|
||||||
cmd = sys.argv[1].lower()
|
cmd = sys.argv[1].lower()
|
||||||
if cmd == "rev":
|
if cmd == "rev":
|
||||||
print GPIO.RPI_REVISION
|
print(GPIO.RPI_REVISION)
|
||||||
elif cmd == "ver":
|
elif cmd == "ver":
|
||||||
print GPIO.VERSION
|
print(GPIO.VERSION)
|
||||||
elif cmd == "info":
|
elif cmd == "info":
|
||||||
print GPIO.RPI_INFO
|
print(GPIO.RPI_INFO)
|
||||||
else:
|
else:
|
||||||
print "Bad parameters - in|out|pwm|buzz|byte|borg|mouse|kbd|ver|info {pin} {value|up|down}"
|
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."
|
print(" only ver (gpio version) and info (board information) accept no pin parameter.")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print "Bad parameters - in|out|pwm|buzz|byte|borg|mouse|kbd|ver|info {pin} {value|up|down}"
|
print("Bad parameters - in|out|pwm|buzz|byte|borg|mouse|kbd|ver|info {pin} {value|up|down}")
|
||||||
|
Loading…
Reference in New Issue
Block a user