From 8fe2478a0b42ceea0f0503e01217590feecf845e Mon Sep 17 00:00:00 2001 From: cclauss Date: Sun, 12 Aug 2018 19:58:11 +0200 Subject: [PATCH] Define raw_input() in Python 3 (#473) Many thanks --- hardware/LEDborg/nrgpio.py | 5 +++++ hardware/PiLcd/nrlcd.py | 8 +++++++- hardware/PiLiter/nrgpio.py | 5 +++++ hardware/Pibrella/nrgpio.py | 7 ++++++- hardware/neopixel/neopix.py | 5 +++++ hardware/sensehat/sensehat.py | 6 ++++++ hardware/unicorn/unihat.py | 3 ++- 7 files changed, 36 insertions(+), 3 deletions(-) diff --git a/hardware/LEDborg/nrgpio.py b/hardware/LEDborg/nrgpio.py index d9c301cd..e284d1e6 100755 --- a/hardware/LEDborg/nrgpio.py +++ b/hardware/LEDborg/nrgpio.py @@ -4,6 +4,11 @@ import RPi.GPIO as GPIO import sys +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + bounce = 20 # bounce time in mS to apply if len(sys.argv) > 1: diff --git a/hardware/PiLcd/nrlcd.py b/hardware/PiLcd/nrlcd.py index 622a8c3f..0ab76eb8 100755 --- a/hardware/PiLcd/nrlcd.py +++ b/hardware/PiLcd/nrlcd.py @@ -11,7 +11,13 @@ import RPi.GPIO as GPIO import time import sys -import os, select +import os +import select + +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 # Turn off warnings if you run it a second time... GPIO.setwarnings(False) diff --git a/hardware/PiLiter/nrgpio.py b/hardware/PiLiter/nrgpio.py index d9c301cd..e284d1e6 100755 --- a/hardware/PiLiter/nrgpio.py +++ b/hardware/PiLiter/nrgpio.py @@ -4,6 +4,11 @@ import RPi.GPIO as GPIO import sys +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + bounce = 20 # bounce time in mS to apply if len(sys.argv) > 1: diff --git a/hardware/Pibrella/nrgpio.py b/hardware/Pibrella/nrgpio.py index 42a8d9b8..e5c6bf25 100755 --- a/hardware/Pibrella/nrgpio.py +++ b/hardware/Pibrella/nrgpio.py @@ -8,6 +8,11 @@ import os import subprocess from time import sleep +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + bounce = 25; if len(sys.argv) > 2: @@ -177,7 +182,7 @@ if len(sys.argv) > 2: elif cmd == "kbd": # catch keyboard button events try: while not os.path.isdir("/dev/input/by-path"): - time.sleep(10) + 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') diff --git a/hardware/neopixel/neopix.py b/hardware/neopixel/neopix.py index 36ea2ae3..288002b2 100755 --- a/hardware/neopixel/neopix.py +++ b/hardware/neopixel/neopix.py @@ -9,6 +9,11 @@ except ImportError: from neopixel import Adafruit_NeoPixel as PixelStrip, Color __version__ = "legacy" +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + # LED strip configuration: LED_COUNT = 8 # Number of LED pixels. LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!). diff --git a/hardware/sensehat/sensehat.py b/hardware/sensehat/sensehat.py index 76c7e05e..2b53248b 100644 --- a/hardware/sensehat/sensehat.py +++ b/hardware/sensehat/sensehat.py @@ -30,6 +30,12 @@ import threading from sense_hat import SenseHat +try: + StandardError # Python 2 +except NameError: + StandardError = Exception # Python 3 + + EVENT_FORMAT = 'llHHI' EVENT_SIZE = struct.calcsize(EVENT_FORMAT) EVENT_NAMES = {103:'U',105:'L',106:'R',108:'D',28:'E'} diff --git a/hardware/unicorn/unihat.py b/hardware/unicorn/unihat.py index e75d5203..43a6648e 100755 --- a/hardware/unicorn/unihat.py +++ b/hardware/unicorn/unihat.py @@ -1,6 +1,7 @@ #!/usr/bin/python # Import library functions we need +from __future__ import print_function import sys import os import unicornhat as UH @@ -64,4 +65,4 @@ while True: except (EOFError, SystemExit): # hopefully always caused by us sigint'ing the program sys.exit(0) except Exception as ex: - print "bad data: "+data + print("bad data: "+data)