diff --git a/.travis.yml b/.travis.yml index 0f87fad..b315707 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ language: python python: 2.7 -install: pip install -qq flake8 pytest pytest-cov tox +install: + - pip install -qq flake8 pytest pytest-cov tox + - python setup.py install script: - flake8 --version - flake8 --ignore=E501 pingo diff --git a/README.rst b/README.rst index 33f391e..552d6f1 100644 --- a/README.rst +++ b/README.rst @@ -46,11 +46,12 @@ Once you have a board instance, it's possible to access its pins through the ``b .. code-block:: python import pingo + from pingo import State, Mode from time import sleep board = pingo.detect.get_board() led_pin = board.pins[13] - led_pin.mode = pingo.OUT + led_pin.mode = Mode.OUT while True: led_pin.hi() diff --git a/pingo/__init__.py b/pingo/__init__.py index 2ad3e29..e6405ac 100644 --- a/pingo/__init__.py +++ b/pingo/__init__.py @@ -1,32 +1,32 @@ +from __future__ import absolute_import + # api -from board import ANALOG # noqa -from board import IN # noqa -from board import OUT # noqa -from board import PWM # noqa -from board import HIGH # noqa -from board import LOW # noqa -from board import ModeNotSuported # noqa -from board import WrongPinMode # noqa -from board import PwmOutputCapable # noqa -from board import AnalogInputCapable # noqa -from board import Board # noqa -from board import PwmPin # noqa -from board import AnalogPin # noqa -from board import DigitalPin # noqa -from board import GroundPin # noqa -from board import Pin # noqa -from board import VccPin # noqa -import parts # noqa +from .board import State # noqa +from .board import Mode # noqa +from .board import ModeNotSuported # noqa +from .board import WrongPinMode # noqa +from .board import PwmOutputCapable # noqa +from .board import AnalogInputCapable # noqa +from .board import Board # noqa +from .board import PwmPin # noqa +from .board import AnalogPin # noqa +from .board import DigitalPin # noqa +from .board import GroundPin # noqa +from .board import Pin # noqa +from .board import VccPin # noqa +import pingo.parts # import * # noqa + # boards -import rpi # noqa -import ghost # noqa -import intel # noqa -import udoo # noqa -import pcduino # noqa -import arduino # noqa -import bbb # noqa +import pingo.rpi # noqa +import pingo.ghost # noqa +import pingo.intel # noqa +import pingo.udoo # noqa +import pingo.pcduino # noqa +import pingo.arduino # noqa +import pingo.bbb # noqa + # resources -import detect # noqa -import test # noqa +import pingo.detect # noqa +import pingo.test # noqa diff --git a/pingo/arduino/__init__.py b/pingo/arduino/__init__.py index 6e45239..89d4f20 100644 --- a/pingo/arduino/__init__.py +++ b/pingo/arduino/__init__.py @@ -1,4 +1,4 @@ -from firmata import ArduinoFirmata # noqa -from firmata import get_arduino # noqa -from pyun import YunBridge # noqa -from pyun import ArduinoYun # noqa +from .firmata import ArduinoFirmata # noqa +from .firmata import get_arduino # noqa +from .pyun import YunBridge # noqa +from .pyun import ArduinoYun # noqa diff --git a/pingo/arduino/firmata.py b/pingo/arduino/firmata.py index eb1e5ea..37984ea 100644 --- a/pingo/arduino/firmata.py +++ b/pingo/arduino/firmata.py @@ -9,9 +9,10 @@ import pingo from pingo.board import Board, DigitalPin, AnalogPin, PwmPin +from pingo.board import State, Mode from pingo.board import AnalogInputCapable, PwmOutputCapable from pingo.detect import detect -from util_firmata import pin_list_to_board_dict +from .util_firmata import pin_list_to_board_dict PyMata = None @@ -20,14 +21,14 @@ True: 1, 0: 0, 1: 1, - pingo.LOW: 0, - pingo.HIGH: 1, + State.LOW: 0, + State.HIGH: 1, } # TODO: PyMata suports Input, Output, PWM, Servo, Encoder and Tone PIN_MODES = { - pingo.IN: 0, - pingo.OUT: 1, + Mode.IN: 0, + Mode.OUT: 1, } VERBOSE = False diff --git a/pingo/arduino/pyun.py b/pingo/arduino/pyun.py index 240beba..4e29fb3 100644 --- a/pingo/arduino/pyun.py +++ b/pingo/arduino/pyun.py @@ -47,7 +47,7 @@ def makeURL(self, command, pin, *args): else: url = self.base_url + '%s/%d' % (command, pin) if self.verbose: - print '[YunBridge] url: ', url + print('[YunBridge] url: %s' % url) return url def get(self, command, pin, *args): diff --git a/pingo/arduino/test_util_firmata.py b/pingo/arduino/test_util_firmata.py index 47ba150..3951b2d 100644 --- a/pingo/arduino/test_util_firmata.py +++ b/pingo/arduino/test_util_firmata.py @@ -1,6 +1,6 @@ import unittest -from util_firmata import pin_list_to_board_dict +from .util_firmata import pin_list_to_board_dict class FirmataCapabilityDetect(unittest.TestCase): @@ -43,12 +43,8 @@ def test_capability_response(self): 0x7F, # END_SYSEX (Pin delimiter) ] - data_arduino = list( - # [0x6C] # CAPABILITY_RESPONSE - unavailible_pin - + digital_pin - + analog_pin - ) + data_arduino = list(unavailible_pin + digital_pin + analog_pin) + # [0x6C] # CAPABILITY_RESPONSE pinmap = pin_list_to_board_dict(data_arduino) for key in test_layout.keys(): diff --git a/pingo/bbb/__init__.py b/pingo/bbb/__init__.py index 637b257..d4c8632 100644 --- a/pingo/bbb/__init__.py +++ b/pingo/bbb/__init__.py @@ -1 +1 @@ -from bbb import BeagleBoneBlack # noqa +from .bbb import BeagleBoneBlack # noqa diff --git a/pingo/board.py b/pingo/board.py index 4a296cc..58dba5e 100644 --- a/pingo/board.py +++ b/pingo/board.py @@ -5,15 +5,11 @@ from abc import ABCMeta, abstractmethod from .util import StrKeyDict +from enum import Enum -HIGH = 'HIGH' -LOW = 'LOW' +State = Enum('State', 'LOW HIGH') -# TODO: 4 states implementation: IN, OUT, ANALOG, PWM -IN = 'IN' -OUT = 'OUT' -ANALOG = 'ANALOG' -PWM = 'PWM' +Mode = Enum('Mode', 'IN OUT ANALOG PWM') class WrongPinMode(Exception): @@ -249,8 +245,8 @@ def __repr__(self): @property def mode(self): - """[property] Get/set pin mode to ``pingo.IN``, ``pingo.OUT`` - ``pingo.ANALOG`` or ``pingo.PWM``""" + """[property] Get/set pin mode to ``Mode.IN``, ``Mode.OUT`` + ``Mode.ANALOG`` or ``Mode.PWM``""" return self._mode @mode.setter @@ -258,15 +254,19 @@ def mode(self, value): if value not in self.suported_modes: raise ModeNotSuported() - if value in [IN, OUT]: + if value in [Mode.IN, Mode.OUT]: self.board._set_digital_mode(self, value) - elif value == ANALOG: + elif value == Mode.ANALOG: self.board._set_analog_mode(self, value) - elif value == PWM: + elif value == Mode.PWM: self.board._set_pwm_mode(self, value) self._mode = value + @property + def is_analog(self): + return issubclass(type(self), AnalogPin) + class DigitalPin(Pin): """Defines common interface for all digital pins. @@ -279,7 +279,7 @@ class DigitalPin(Pin): because pins delegate all board-dependent behavior to the board. """ - suported_modes = [IN, OUT] + suported_modes = [Mode.IN, Mode.OUT] def __init__(self, board, location, gpio_id=None): Pin.__init__(self, board, location, gpio_id) @@ -287,52 +287,52 @@ def __init__(self, board, location, gpio_id=None): @property def state(self): - """[property] Get/set pin state to ``pingo.HIGH`` or ``pingo.LOW``""" - if self.mode not in [IN, OUT]: + """[property] Get/set pin state to ``State.HIGH`` or ``State.LOW``""" + if self.mode not in [Mode.IN, Mode.OUT]: raise WrongPinMode() - if self.mode == IN: + if self.mode == Mode.IN: self._state = self.board._get_pin_state(self) return self._state @state.setter def state(self, value): - if self.mode != OUT: + if self.mode != Mode.OUT: raise WrongPinMode() self.board._set_pin_state(self, value) self._state = value def low(self): - """Set voltage of pin to ``pingo.LOW`` (GND).""" - self.state = LOW + """Set voltage of pin to ``State.LOW`` (GND).""" + self.state = State.LOW lo = low # shortcut for interactive use def high(self): """Set state of the pin to ``pingo.HIGH`` (Vcc).""" - self.state = HIGH + self.state = State.HIGH hi = high # shortcut for interactive use def toggle(self): """Change state of the pin.""" - self.state = HIGH if self.state == LOW else LOW + self.state = State.HIGH if self.state == State.LOW else State.LOW def pulse(self): """Generate a pulse in state of the pin.""" - if self.state == LOW: - self.state = HIGH - self.state = LOW + if self.state == State.LOW: + self.state = State.HIGH + self.state = State.LOW else: - self.state = LOW - self.state = HIGH + self.state = State.LOW + self.state = State.HIGH class PwmPin(DigitalPin): - suported_modes = [IN, OUT, PWM] + suported_modes = [Mode.IN, Mode.OUT, Mode.PWM] def __init__(self, board, location, gpio_id=None, frequency=None): DigitalPin.__init__(self, board, location, gpio_id) @@ -344,13 +344,13 @@ def __init__(self, board, location, gpio_id=None, frequency=None): @property def value(self): - if self.mode != PWM: + if self.mode != Mode.PWM: raise WrongPinMode() return self.board._get_pwm_duty_cycle(self) @value.setter def value(self, value): - if self.mode != PWM: + if self.mode != Mode.PWM: raise WrongPinMode() if not 0.0 <= value <= 100.0: raise ArgumentOutOfRange() @@ -359,13 +359,13 @@ def value(self, value): @property def frequency(self): - if self.mode != PWM: + if self.mode != Mode.PWM: raise WrongPinMode() return self.board._get_pwm_frequency(self) @frequency.setter def frequency(self, new_frequency): - if self.mode != PWM: + if self.mode != Mode.PWM: raise WrongPinMode() if new_frequency <= 0.0: raise ArgumentOutOfRange() @@ -382,7 +382,7 @@ class AnalogPin(Pin): This pin type supports read operations only. """ - suported_modes = [IN, ANALOG] + suported_modes = [Mode.IN, Mode.ANALOG] def __init__(self, board, location, resolution, gpio_id=None): """ diff --git a/pingo/compat.py b/pingo/compat.py new file mode 100644 index 0000000..1fda172 --- /dev/null +++ b/pingo/compat.py @@ -0,0 +1,28 @@ +import sys + +PY2 = sys.version_info[0] == 2 +PY3 = (sys.version_info[0] >= 3) + + +def iteritems(obj, **kwargs): + """replacement for six's iteritems for Python2/3 compat + uses 'iteritems' if available and otherwise uses 'items'. + Passes kwargs to method. + """ + func = getattr(obj, "iteritems", None) + if not func: + func = obj.items + return func(**kwargs) + + +def iterkeys(obj, **kwargs): + func = getattr(obj, "iterkeys", None) + if not func: + func = obj.keys + return func(**kwargs) + + +if PY2: + from UserDict import UserDict # noqa +else: + from collections import UserDict # noqa diff --git a/pingo/detect/__init__.py b/pingo/detect/__init__.py index 912d0a5..abd0b71 100644 --- a/pingo/detect/__init__.py +++ b/pingo/detect/__init__.py @@ -1,4 +1,4 @@ -from detect import get_board +from .detect import get_board def has_module(module_name): diff --git a/pingo/detect/detect.py b/pingo/detect/detect.py index 55e9e05..c59530f 100644 --- a/pingo/detect/detect.py +++ b/pingo/detect/detect.py @@ -36,8 +36,7 @@ def _find_arduino_dev(system): return os.path.join(os.path.sep, 'dev', devices[0]) elif system == 'Darwin': - devices = (glob.glob('/dev/tty.usbmodem*') - + glob.glob('/dev/tty.usbserial*')) + devices = (glob.glob('/dev/tty.usbmodem*') + glob.glob('/dev/tty.usbserial*')) if len(devices) == 1: return os.path.join(os.path.sep, 'dev', devices[0]) return False diff --git a/pingo/detect/test_detect.py b/pingo/detect/test_detect.py index ffc9481..659086f 100644 --- a/pingo/detect/test_detect.py +++ b/pingo/detect/test_detect.py @@ -2,7 +2,6 @@ import platform import pingo -import detect class DetectBasics(unittest.TestCase): @@ -13,7 +12,7 @@ def test_board(self): @unittest.skipIf(not platform.system() == 'Linux', 'Not Linux') def test_read_cpu_info(self): - info = detect._read_cpu_info() + info = pingo.detect.detect._read_cpu_info() assert isinstance(info, dict) if __name__ == '__main__': diff --git a/pingo/examples/analog_bars.py b/pingo/examples/analog_bars.py index ccf9179..f733e77 100644 --- a/pingo/examples/analog_bars.py +++ b/pingo/examples/analog_bars.py @@ -1,11 +1,12 @@ import pingo +from pingo import Mode import time board = pingo.detect.get_board() # board = pingo.arduino.get_arduino() pot = board.pins['A0'] -pot.mode = pingo.ANALOG +pot.mode = Mode.ANALOG def bar(pin): diff --git a/pingo/examples/blink.py b/pingo/examples/blink.py index f6d8b26..48e4c48 100644 --- a/pingo/examples/blink.py +++ b/pingo/examples/blink.py @@ -9,10 +9,11 @@ import time import pingo +from pingo import Mode board = pingo.detect.get_board() led = board.pins[13] -led.mode = pingo.OUT +led.mode = Mode.OUT while True: led.toggle() diff --git a/pingo/examples/blink_firmata.py b/pingo/examples/blink_firmata.py index 8565541..3bbbc4c 100644 --- a/pingo/examples/blink_firmata.py +++ b/pingo/examples/blink_firmata.py @@ -9,11 +9,12 @@ import time import pingo +from pingo import Mode ard = pingo.arduino.ArduinoFirmata('/dev/tty.usbmodemfa1341') print('Connected to: %s' % ard) led = ard.pins[13] -led.mode = pingo.OUT +led.mode = Mode.OUT while True: led.toggle() diff --git a/pingo/examples/blink_firmata_auto.py b/pingo/examples/blink_firmata_auto.py index 4a08c52..e56fc5b 100644 --- a/pingo/examples/blink_firmata_auto.py +++ b/pingo/examples/blink_firmata_auto.py @@ -9,11 +9,12 @@ import time import pingo +from pingo import Mode ard = pingo.arduino.get_arduino() print('Connected to: %s' % ard) led = ard.pins[13] -led.mode = pingo.OUT +led.mode = Mode.OUT while True: led.toggle() diff --git a/pingo/examples/dimmer.py b/pingo/examples/dimmer.py index b86db10..4ce4b68 100644 --- a/pingo/examples/dimmer.py +++ b/pingo/examples/dimmer.py @@ -8,20 +8,21 @@ from pprint import pprint import pingo +from pingo import Mode import time board = pingo.detect.get_board() pot = board.pins['A0'] -pot.mode = pingo.ANALOG +pot.mode = Mode.ANALOG led = board.pins[6] -led.mode = pingo.PWM +led.mode = Mode.PWM display_pins = [board.pins[i] for i in range(8, 14) + [7]] seg_display = pingo.parts.led.SevenSegments(*display_pins) for pin in display_pins: - pin.mode = pingo.OUT + pin.mode = Mode.OUT def bar(pin): diff --git a/pingo/examples/dojo.py b/pingo/examples/dojo.py index e754ddd..00209f0 100644 --- a/pingo/examples/dojo.py +++ b/pingo/examples/dojo.py @@ -13,15 +13,16 @@ import time import pingo +from pingo import Mode board = pingo.detect.get_board() print('board: %s' % board) pot = board.pins['A0'] -pot.mode = pingo.ANALOG +pot.mode = Mode.ANALOG leds = board.digital_pins[6:14] for led in leds: - led.mode = pingo.OUT + led.mode = Mode.OUT led.low() while True: @@ -30,5 +31,5 @@ continue led.high() time.sleep(pot.ratio()) - print pot.ratio() + print(pot.ratio()) led.low() diff --git a/pingo/examples/dojo2.py b/pingo/examples/dojo2.py index 9652046..037c465 100644 --- a/pingo/examples/dojo2.py +++ b/pingo/examples/dojo2.py @@ -12,6 +12,7 @@ from time import sleep import pingo +from pingo import Mode POT_LOCATION = 'A0' PIN_LOCATIONS = range(6, 14) @@ -21,7 +22,7 @@ leds = (pingo.pins[loc] for loc in PIN_LOCATIONS if loc != 9) for led in leds: - led.mode = pingo.OUT + led.mode = Mode.OUT while True: for led in leds: diff --git a/pingo/examples/dojoxxl.py b/pingo/examples/dojoxxl.py index 609a07e..fe33d14 100644 --- a/pingo/examples/dojoxxl.py +++ b/pingo/examples/dojoxxl.py @@ -3,10 +3,11 @@ """ import pingo +from pingo import Mode import time ard = pingo.arduino.get_arduino() -print '*' * 60 +print('*' * 60) segments = [ 11, # A @@ -20,17 +21,17 @@ for seg in segments: pin = ard.pins[seg] - pin.mode = pingo.OUT + pin.mode = Mode.OUT pin.low() pot = ard.pins['A0'] -pot.mode = pingo.IN +pot.mode = Mode.IN while True: for seg in segments: pin = ard.pins[seg] pin.high() delay = pot.ratio() + 0.01 # add 0.01s delay for communication - print '%0.3f' % delay + print('%0.3f' % delay) time.sleep(delay) pin.low() diff --git a/pingo/examples/double_blink.py b/pingo/examples/double_blink.py index 60b2a53..df8461c 100644 --- a/pingo/examples/double_blink.py +++ b/pingo/examples/double_blink.py @@ -10,14 +10,15 @@ import time import pingo +from pingo import Mode board = pingo.rpi.RaspberryPiBPlus() local_led = board.pins[13] -local_led.mode = pingo.OUT +local_led.mode = Mode.OUT ard = pingo.arduino.get_arduino() remote_led = ard.pins[13] -remote_led.mode = pingo.OUT +remote_led.mode = Mode.OUT local_led.low() # for an common anode RGB LED remote_led.low() diff --git a/pingo/examples/dual_analog_bars.py b/pingo/examples/dual_analog_bars.py index 0660031..d9bfddc 100644 --- a/pingo/examples/dual_analog_bars.py +++ b/pingo/examples/dual_analog_bars.py @@ -1,20 +1,21 @@ import pingo import time +from pingo import Mode galileo = pingo.detect.get_board() arduino = pingo.arduino.get_arduino() pot_galileo = galileo.pins['A0'] -pot_galileo.mode = pingo.ANALOG +pot_galileo.mode = Mode.ANALOG pot_arduino = arduino.pins['A0'] -pot_arduino.mode = pingo.ANALOG +pot_arduino.mode = Mode.ANALOG def bar(pin1, pin2): bar1 = ('*' * int(pin1.ratio() * 40)).ljust(40) bar2 = ('*' * int(pin2.ratio() * 40)).rjust(40) - print bar2 + bar1 + print(bar2 + bar1) while True: bar(pot_galileo, pot_arduino) diff --git a/pingo/examples/galileo_analog_bars.py b/pingo/examples/galileo_analog_bars.py index 7cc2e98..ee34605 100644 --- a/pingo/examples/galileo_analog_bars.py +++ b/pingo/examples/galileo_analog_bars.py @@ -1,14 +1,15 @@ -import pingo import time +import pingo +from pingo import Mode board = pingo.galileo.Galileo2() pot = board.pins['A0'] -pot.mode = pingo.ANALOG +pot.mode = Mode.ANALOG def bar(pin): - print "*" * int(pin.ratio() * 70) + print("*" * int(pin.ratio() * 70)) while True: bar(pot) diff --git a/pingo/examples/map7segment.py b/pingo/examples/map7segment.py index 838fdd0..4da5111 100644 --- a/pingo/examples/map7segment.py +++ b/pingo/examples/map7segment.py @@ -3,14 +3,15 @@ """ import pingo +from pingo import Mode ard = pingo.arduino.get_arduino() -print '*' * 60 +print('*' * 60) segs = {} for i in range(14): pin = ard.pins[i] - pin.mode = pingo.OUT + pin.mode = Mode.OUT pin.hi() seg = raw_input('Pin %s -> segment: ' % i).strip() if seg: @@ -18,4 +19,4 @@ pin.low() for seg, pin in sorted(segs.items()): - print '%s, # %s' % (pin, seg) + print('%s, # %s' % (pin, seg)) diff --git a/pingo/examples/parts/7seg_demo.py b/pingo/examples/parts/7seg_demo.py index a48523a..9afd14d 100644 --- a/pingo/examples/parts/7seg_demo.py +++ b/pingo/examples/parts/7seg_demo.py @@ -1,7 +1,7 @@ """ Seven segment display demo using the Garoa Dojo Shield """ from time import sleep -from pingo import OUT, detect, parts +from pingo import Mode, detect, parts INTERVAL = 0.3 @@ -10,7 +10,7 @@ display_pins = [ard.pins[i] for i in range(8, 14) + [7]] for pin in display_pins: - pin.mode = OUT + pin.mode = Mode.OUT seg_display = parts.led.SevenSegments(*display_pins) diff --git a/pingo/examples/parts/led_demo.py b/pingo/examples/parts/led_demo.py index 0641029..c6b83df 100644 --- a/pingo/examples/parts/led_demo.py +++ b/pingo/examples/parts/led_demo.py @@ -13,5 +13,5 @@ a_state = '*A*' if a_led.lit else ' a ' b_state = '*B*' if b_led.lit else ' b ' - print time.strftime('%H:%M:%S'), a_state, b_state + print("%s %s %s" % (time.strftime('%H:%M:%S'), a_state, b_state)) time.sleep(.1) diff --git a/pingo/examples/pongo.py b/pingo/examples/pongo.py index 8e9e60c..5ffb221 100644 --- a/pingo/examples/pongo.py +++ b/pingo/examples/pongo.py @@ -14,6 +14,7 @@ import curses import pingo +from pingo import Mode import time import random @@ -64,10 +65,10 @@ def draw_paddle(x, y, color): arduino = pingo.arduino.get_arduino() pot_galileo = galileo.pins['A0'] - pot_galileo.mode = pingo.ANALOG + pot_galileo.mode = Mode.ANALOG pot_arduino = arduino.pins['A0'] - pot_arduino.mode = pingo.ANALOG + pot_arduino.mode = Mode.ANALOG # Read the initial values of the paddles paddle_1_pos = int(pot_arduino.ratio(to_min=MIN_Y, to_max=MAX_Y-PADDLE_SIZE)) diff --git a/pingo/examples/probe_pins.py b/pingo/examples/probe_pins.py index 508b292..40eb294 100644 --- a/pingo/examples/probe_pins.py +++ b/pingo/examples/probe_pins.py @@ -7,6 +7,7 @@ import sys import pingo +from pingo import Mode def probe(first, last): @@ -16,7 +17,7 @@ def probe(first, last): pins = board.digital_pins[first:last + 1] for pin in pins: - pin.mode = pingo.OUT + pin.mode = Mode.OUT for pin in pins: pin.hi() diff --git a/pingo/examples/pwm.py b/pingo/examples/pwm.py index e0a46e5..8d91669 100644 --- a/pingo/examples/pwm.py +++ b/pingo/examples/pwm.py @@ -5,14 +5,15 @@ from pprint import pprint import pingo +from pingo import Mode import time -board = pingo.detect.get_board() -print board +board = pingo.detect.get_board() +print(board) led = board.pins[6] -led.mode = pingo.PWM +led.mode = Mode.PWM pprint(board.pins, indent=4, width=1) diff --git a/pingo/examples/rgb_led.py b/pingo/examples/rgb_led.py index 18431af..a010e8d 100644 --- a/pingo/examples/rgb_led.py +++ b/pingo/examples/rgb_led.py @@ -1,13 +1,14 @@ import time import pingo +from pingo import Mode board = pingo.detect.get_board() rgb = [board.pins[i] for i in (11, 13, 15)] for pin in rgb: - pin.mode = pingo.OUT + pin.mode = Mode.OUT pin.low() while True: diff --git a/pingo/examples/rpi_examples/display7.py b/pingo/examples/rpi_examples/display7.py index 84f96db..06ba833 100644 --- a/pingo/examples/rpi_examples/display7.py +++ b/pingo/examples/rpi_examples/display7.py @@ -1,4 +1,5 @@ import pingo +from pingo import Mode import time rpi = pingo.rpi.RaspberryPi() @@ -9,7 +10,7 @@ if pin.location in led_locations] for pin in pins: - pin.mode = pingo.OUT + pin.mode = Mode.OUT for pin in pins: pin.high() diff --git a/pingo/examples/rpi_examples/display7_anim.py b/pingo/examples/rpi_examples/display7_anim.py index 4e61959..0699865 100644 --- a/pingo/examples/rpi_examples/display7_anim.py +++ b/pingo/examples/rpi_examples/display7_anim.py @@ -1,4 +1,5 @@ import pingo +from pingo import Mode from time import sleep rpi = pingo.rpi.RaspberryPi() @@ -9,7 +10,7 @@ pins = [rpi.pins[loc] for loc in led_locations[:6]] for pin in pins: - pin.mode = pingo.OUT + pin.mode = Mode.OUT pin.low() while True: diff --git a/pingo/examples/rpi_examples/display7_probe.py b/pingo/examples/rpi_examples/display7_probe.py index 9271cea..52df134 100644 --- a/pingo/examples/rpi_examples/display7_probe.py +++ b/pingo/examples/rpi_examples/display7_probe.py @@ -1,4 +1,5 @@ import pingo +from pingo import Mode rpi = pingo.rpi.RaspberryPi() @@ -7,7 +8,7 @@ pins = [rpi.pins[loc] for loc in led_locations] for pin in pins: - pin.mode = pingo.OUT + pin.mode = Mode.OUT pin.low() for pin in pins: diff --git a/pingo/examples/rpi_examples/display7_snake.py b/pingo/examples/rpi_examples/display7_snake.py index d20663e..e44b4c8 100644 --- a/pingo/examples/rpi_examples/display7_snake.py +++ b/pingo/examples/rpi_examples/display7_snake.py @@ -1,4 +1,5 @@ import pingo +from pingo import Mode from time import sleep rpi = pingo.rpi.RaspberryPi() @@ -9,7 +10,7 @@ pins = [rpi.pins[loc] for loc in led_sequence] for pin in pins: - pin.mode = pingo.OUT + pin.mode = Mode.OUT pin.low() prev_pin = pins[-1] diff --git a/pingo/examples/switch.py b/pingo/examples/switch.py index ed23ccc..befb435 100644 --- a/pingo/examples/switch.py +++ b/pingo/examples/switch.py @@ -15,13 +15,13 @@ def show_f(): global seg_display seg_display.digit = 0xF - print 'Fechado' # Closed + print('Fechado') # Closed def show_a(): global seg_display seg_display.digit = 0xA - print 'Aberto' # Open + print('Aberto') # Open pin_sw = board.pins[3] my_switch = pingo.parts.Switch(pin_sw) diff --git a/pingo/ghost/__init__.py b/pingo/ghost/__init__.py index 220e258..9e5e17c 100644 --- a/pingo/ghost/__init__.py +++ b/pingo/ghost/__init__.py @@ -1 +1 @@ -from ghost import GhostBoard # noqa +from .ghost import GhostBoard # noqa diff --git a/pingo/ghost/ghost.py b/pingo/ghost/ghost.py index 4d5a9f9..903ee33 100644 --- a/pingo/ghost/ghost.py +++ b/pingo/ghost/ghost.py @@ -1,4 +1,6 @@ import pingo +from pingo.compat import iteritems +from pingo.board import State class GhostBoard( @@ -48,7 +50,7 @@ def __init__(self, filepath=None): self._pin_states = pingo.util.StrKeyDict() # All pins start on LOW # FIXME: use "LOW" instead of 0 - for location, pin in self.pins.iteritems(): + for location, pin in iteritems(self.pins): self._pin_states[location] = 0 if hasattr(pin, 'state') else None # Pin 8 starts on HIGH @@ -68,12 +70,12 @@ def _set_pwm_mode(self, pin, mode): def _set_pin_state(self, pin, state): print('GhostBoard: %r state -> %s' % (pin, state)) - _state = 1 if state == pingo.HIGH else 0 + _state = 1 if state == State.HIGH else 0 self._pin_states[pin.location] = _state def _get_pin_state(self, pin): state = self._pin_states[pin.location] - return pingo.HIGH if state else pingo.LOW + return State.HIGH if state else State.LOW def _get_pin_value(self, pin): return self._pin_states[pin.location] diff --git a/pingo/intel/__init__.py b/pingo/intel/__init__.py index 3ca067b..aad7c74 100644 --- a/pingo/intel/__init__.py +++ b/pingo/intel/__init__.py @@ -1,2 +1,2 @@ -from intel import Galileo2 # noqa -from intel import Edison # noqa +from .intel import Galileo2 # noqa +from .intel import Edison # noqa diff --git a/pingo/iot/client.py b/pingo/iot/client.py new file mode 100644 index 0000000..de0a9e0 --- /dev/null +++ b/pingo/iot/client.py @@ -0,0 +1,69 @@ +import json +import pingo +try: + # Python 2.7 + from urllib2 import urlopen +except ImportError: + # Python 3+ + from urrlib.requests import urlopen + + +class HTTPBoard(pingo.Board): + """ + """ + + def __init__(self, server): + self.server = server + response = urlopen(server) + if response.code != 200: + raise Exception(u'HTTPBoard not found on server {}'.format(server)) + response = json.load(response) + pins = json.loads(response['pins']) + gpio_pins = [] + ground_pins = [] + vcc_pins = [] + pwm_pins = [] + for pin, value in pins.items(): + # TODO: serialize the pin (and/or the board) to do this in a better way + if 'GroundPin' in value: + ground_pins.append(pingo.GroundPin(self, pin)) + elif 'VccPin' in value: + voltage = float(value[:-2].split(' ')[1]) + vcc_pins.append(pingo.VccPin(self, pin, voltage)) + elif 'PwmPin' in value: + gpio_id = value.split(' ')[1].split('@')[0] + pwm_pins.append(pingo.PwmPin(self, pin, gpio_id)) + elif 'DigitalPin' in value: + gpio_id = value.split(' ')[1].split('@')[0] + gpio_pins.append(pingo.DigitalPin(self, pin, gpio_id)) + self._add_pins(ground_pins + vcc_pins + gpio_pins + pwm_pins) + + def _set_digital_mode(self, pin, mode): + mode = 'input' if pingo.IN else 'output' + url = '{server}mode/{mode}/{pin}'.format(server=self.server, + mode=mode, pin=pin.location) + urlopen(url) + + def _set_pin_state(self, pin, state): + mode = 'analog' if pin.is_analog else 'digital' + state = 1 if state == pingo.HIGH else 0 + url = '{server}{mode}/{pin}/{state}'.format(server=self.server, mode=mode, + pin=str(pin.location), + state=str(state)) + print(url) + response = urlopen(url) + if response.code != 200: + message = u'Pin {} could not be set to {}. HTTPBoard response: {}' + message.format(repr(pin), state, response.code) + raise Exception(message) + + def _get_pin_state(self, pin): + mode = 'analog' if pin.is_analog else 'digital' + url = '{server}{mode}/{pin}'.format(server=self.server, mode=mode, + pin=pin.location) + response = urlopen(url) + if response.code != 200: + message = u'Pin {} could not be read: HTTPBoard response: {}' + message.format(repr(pin), response.code) + raise Exception(message) + return response['input'] diff --git a/pingo/iot/server.py b/pingo/iot/server.py new file mode 100644 index 0000000..96a1584 --- /dev/null +++ b/pingo/iot/server.py @@ -0,0 +1,76 @@ +from bottle import Bottle +import json +import pingo +import sys + + +app = Bottle(__name__) +board = pingo.detect.get_board() + + +@app.route('/') +def main(): + pins = {key: repr(value) for key, value in board.pins} + return { + 'board': repr(board), + 'pins': json.dumps(pins) + } + + +@app.route('/mode//') +def mode(mode, pin): + assert mode in ('input', 'output') + mode = pingo.IN if 'input' else pingo.OUT + pin = board.pins[pin] + pin.mode = mode + + +@app.route('/analog') +def analog_pins(): + pins = {location: pin for location, pin in board.pins + if pin.is_analog} + return {'pins': str(pins)} + + +@app.route('/analog/') +def analog_input(pin): + pin = board.pins[pin] + pin.mode = pingo.IN + return {'input': pin.state} + + +@app.route('/analog//') +def analog_output(pin, signal): + pin = board.pins[pin] + pin.mode = pingo.OUT + pin.value = signal + return {'output': signal} + + +@app.route('/digital') +def digital_pins(): + pins = board.pins() + return {'pins': str(pins)} + + +@app.route('/digital/') +def digital_input(pin): + pin = board.pins[pin] + pin.mode = pingo.IN + return {'input': pin.state} + + +@app.route('/digital//') +def digital_output(pin, signal): + pin = board.pins[pin] + pin.mode = pingo.OUT + pin.high() if signal else pin.low() + return {'output': signal} + + +if __name__ == '__main__': + try: + kwargs = {'host': sys.argv[1]} + except IndexError: + kwargs = {} + app.run(debug=True, **kwargs) diff --git a/pingo/parts/button.py b/pingo/parts/button.py index ddc362c..d574efb 100644 --- a/pingo/parts/button.py +++ b/pingo/parts/button.py @@ -1,5 +1,6 @@ import time import threading +from pingo.board import State, Mode class Switch(object): @@ -10,7 +11,7 @@ def __init__(self, pin): :param pin: A instance of DigitalPin """ self.pin = pin - self.pin.mode = 'IN' + self.pin.mode = Mode.IN self.polling_task = None self._up_callback = lambda: None self._down_callback = lambda: None @@ -56,10 +57,10 @@ def run(self): while self.active: current_state = self.switch.pin.state if current_state != last_state: - if current_state == 'HIGH': + if current_state == State.HIGH: last_state = current_state self.switch._up_callback() - elif current_state == 'LOW': + elif current_state == State.LOW: last_state = current_state self.switch._down_callback() time.sleep(0.05) diff --git a/pingo/parts/led.py b/pingo/parts/led.py index 3c66825..f877213 100644 --- a/pingo/parts/led.py +++ b/pingo/parts/led.py @@ -1,4 +1,4 @@ -import pingo +from pingo import State, Mode import time import threading @@ -7,27 +7,27 @@ class Led(object): """A single LED""" - def __init__(self, pin, lit_state=pingo.HIGH): - """Set lit_state to pingo.LOW to turn on led by bringing + def __init__(self, pin, lit_state=State.HIGH): + """Set lit_state to State.LOW to turn on led by bringing cathode to low state. - :param lit_state: use pingo.HI for anode control, pingo.LOW + :param lit_state: use State.HIGH for anode control, State.LOW for cathode control """ - pin.mode = pingo.OUT + pin.mode = Mode.OUT self.pin = pin self.lit_state = lit_state self.blink_task = None def on(self): - if self.lit_state == pingo.HIGH: + if self.lit_state == State.HIGH: self.pin.high() else: self.pin.low() def off(self): - if self.lit_state == pingo.HIGH: + if self.lit_state == State.HIGH: self.pin.low() else: self.pin.high() @@ -85,7 +85,7 @@ class RGBLed(object): pure_colors_map = dict(PURE_COLORS) def __init__(self, red_pin, green_pin, blue_pin, - lit_state=pingo.LOW): + lit_state=State.LOW): self._leds = [Led(red_pin, lit_state), Led(green_pin, lit_state), Led(blue_pin, lit_state)] @@ -123,6 +123,7 @@ def __init__(self, led, times, on_delay, off_delay): :param off_delay: delay while LED is off """ self.led = led + self.led_pin_state_initial = self.led.pin.state self.active = True self.forever = times == 0 self.times_remaining = times @@ -143,7 +144,7 @@ def run(self): else: time.sleep(self.off_delay) else: - self.led.off() + self.led.pin.state = self.led_pin_state_initial self.active = False @@ -173,7 +174,7 @@ class SevenSegments(object): def __init__(self, pin_a, pin_b, pin_c, pin_d, pin_e, pin_f, pin_g, pin_dp=None, - lit_state=pingo.HIGH): + lit_state=State.HIGH): self._leds = [Led(pin_a, lit_state), Led(pin_b, lit_state), Led(pin_c, lit_state), Led(pin_d, lit_state), Led(pin_e, lit_state), Led(pin_f, lit_state), diff --git a/pingo/parts/serial/__init__.py b/pingo/parts/serial/__init__.py index 8343f95..38fbe2e 100644 --- a/pingo/parts/serial/__init__.py +++ b/pingo/parts/serial/__init__.py @@ -1 +1 @@ -from lcd import LCD16x2 # noqa +from .lcd import LCD16x2 # noqa diff --git a/pingo/parts/spi/__init__.py b/pingo/parts/spi/__init__.py index 847dda5..d869c3b 100644 --- a/pingo/parts/spi/__init__.py +++ b/pingo/parts/spi/__init__.py @@ -1 +1 @@ -from mcp3008 import Mcp3008 # noqa +from .mcp3008 import Mcp3008 # noqa diff --git a/pingo/parts/spi/mcp3008.py b/pingo/parts/spi/mcp3008.py index f037255..8a30ddb 100644 --- a/pingo/parts/spi/mcp3008.py +++ b/pingo/parts/spi/mcp3008.py @@ -64,7 +64,7 @@ def test(): res = ad_chip.read(1) volts = float(res) / 1023 * 3.3 ticks = int(round(float(res) / 1023 * 40)) * '=' - print display.format(count, res, volts, ticks) + print(display.format(count, res, volts, ticks)) time.sleep(.2) count += 1 diff --git a/pingo/parts/test/test_switch.py b/pingo/parts/test/test_switch.py index 085e433..c5e127f 100644 --- a/pingo/parts/test/test_switch.py +++ b/pingo/parts/test/test_switch.py @@ -2,12 +2,13 @@ import unittest from pingo.parts import Switch +from pingo.board import State, Mode class FakeDigitalPin(object): def __init__(self): - self.mode = 'IN' - self.state = 'LOW' + self.mode = Mode.IN + self.state = State.LOW class TestSwitch(unittest.TestCase): @@ -23,11 +24,11 @@ def callback_down(): self.my_switch.start() time.sleep(.1) - self.pin.state = 'HIGH' + self.pin.state = State.HIGH time.sleep(.1) - self.pin.state = 'LOW' + self.pin.state = State.LOW time.sleep(.1) - self.pin.state = 'HIGH' + self.pin.state = State.HIGH time.sleep(.1) self.my_switch.stop() @@ -45,11 +46,11 @@ def callback_up(): self.my_switch.start() time.sleep(.1) - self.pin.state = 'HIGH' + self.pin.state = State.HIGH time.sleep(.1) - self.pin.state = 'LOW' + self.pin.state = State.LOW time.sleep(.1) - self.pin.state = 'HIGH' + self.pin.state = State.HIGH time.sleep(.1) self.my_switch.stop() diff --git a/pingo/pcduino/__init__.py b/pingo/pcduino/__init__.py index 6a7c843..1198f63 100644 --- a/pingo/pcduino/__init__.py +++ b/pingo/pcduino/__init__.py @@ -1 +1 @@ -from pcduino import PcDuino # noqa +from .pcduino import PcDuino # noqa diff --git a/pingo/pcduino/pcduino.py b/pingo/pcduino/pcduino.py index a24faa2..0c617e1 100644 --- a/pingo/pcduino/pcduino.py +++ b/pingo/pcduino/pcduino.py @@ -1,5 +1,6 @@ -from pingo.board import Board, DigitalPin, AnalogPin, IN, OUT, HIGH, LOW +from pingo.board import Board, DigitalPin, AnalogPin from pingo.board import AnalogInputCapable +from pingo.board import State, Mode class PcDuino(Board, AnalogInputCapable): @@ -9,8 +10,8 @@ class PcDuino(Board, AnalogInputCapable): DIGITAL_PINS_PATH = '/sys/devices/virtual/misc/gpio/' ADC_PATH = '/proc/' - DIGITAL_PIN_MODES = {IN: '0', OUT: '1'} - DIGITAL_PIN_STATES = {HIGH: '1', LOW: '0'} + DIGITAL_PIN_MODES = {Mode.IN: '0', Mode.OUT: '1'} + DIGITAL_PIN_STATES = {State.HIGH: '1', State.LOW: '0'} LEN_DIGITAL_PINS = 14 ANALOG_PIN_RESOLUTIONS = [6, 6, 12, 12, 12, 12] @@ -40,7 +41,7 @@ def _get_pin_state(self, pin): sys_string = self.DIGITAL_PINS_PATH + 'pin/gpio%s' % pin.location with open(sys_string, 'r') as fp: state = fp.read().strip() - return HIGH if state == '1' else LOW + return State.HIGH if state == '1' else State.LOW def _get_pin_value(self, pin): sys_string = self.ADC_PATH + 'adc%s' % pin.location[1:] # eg. A5 diff --git a/pingo/rpi/__init__.py b/pingo/rpi/__init__.py index f6ec5ee..1169669 100644 --- a/pingo/rpi/__init__.py +++ b/pingo/rpi/__init__.py @@ -1,4 +1,4 @@ -from rpi import RaspberryPi # noqa -from rpi import RaspberryPiBPlus # noqa -from rpi import RaspberryPi2B # noqa -from grove import GrovePi # noqa +from .rpi import RaspberryPi # noqa +from .rpi import RaspberryPiBPlus # noqa +from .rpi import RaspberryPi2B # noqa +from .grove import GrovePi # noqa diff --git a/pingo/test/__init__.py b/pingo/test/__init__.py index 4ec2b54..d90a41f 100644 --- a/pingo/test/__init__.py +++ b/pingo/test/__init__.py @@ -1,3 +1,3 @@ -import level0 # noqa -import level1 # noqa -import level2 # noqa +import pingo.test.level0 # noqa +import pingo.test.level1 # noqa +import pingo.test.level2 # noqa diff --git a/pingo/test/level0/__init__.py b/pingo/test/level0/__init__.py index 245d9e7..72c7cac 100644 --- a/pingo/test/level0/__init__.py +++ b/pingo/test/level0/__init__.py @@ -1,2 +1,2 @@ -from cases import BoardBasics # noqa -from cases import BoardExceptions # noqa +from .cases import BoardBasics # noqa +from .cases import BoardExceptions # noqa diff --git a/pingo/test/level0/cases.py b/pingo/test/level0/cases.py index 6850c0d..7253660 100644 --- a/pingo/test/level0/cases.py +++ b/pingo/test/level0/cases.py @@ -2,6 +2,7 @@ import unittest import pingo +from pingo.board import Mode, State ''' In order to use this set of cases, it is necessary to set @@ -28,7 +29,7 @@ def test_list_pins(self): def test_led(self): pin = self.board.pins[self.digital_output_pin_number] - pin.mode = pingo.OUT + pin.mode = Mode.OUT pin.high() def test_filter(self): @@ -41,12 +42,12 @@ def test_filter(self): @unittest.skip("Not automatic enough.") def test_button(self): pin = self.board.pins[self.digital_input_pin_number] - pin.mode = pingo.IN - output = pingo.LOW + pin.mode = Mode.IN + output = State.LOW t0 = time.time() delay = 5 - while output == pingo.LOW: + while output == State.LOW: output = pin.state if time.time() - t0 > delay: break @@ -58,18 +59,18 @@ def test_button(self): def test_jumpwire(self): ''' Wire this DigitalPin directly into VDD ''' pin = self.board.pins[self.digital_input_pin_number] - pin.mode = pingo.IN + pin.mode = Mode.IN output = pin.state - assert output == pingo.HIGH + assert output == State.HIGH def test_toggle(self): pin = self.board.pins[self.digital_input_pin_number] - pin.mode = pingo.OUT + pin.mode = Mode.OUT pin.high() pin.toggle() - assert pin.state == pingo.LOW + assert pin.state == State.LOW def test_digital_pins(self): digital_pins = self.board.digital_pins @@ -94,13 +95,13 @@ def test_disabled_pin(self): def test_wrong_pin_mode_in(self): pin = self.board.pins[self.digital_input_pin_number] - pin.mode = pingo.IN + pin.mode = Mode.IN with self.assertRaises(pingo.WrongPinMode): pin.high() with self.assertRaises(pingo.WrongPinMode): - pin.state = pingo.HIGH + pin.state = State.HIGH # def test_wrong_pin_mode_out(self): # pin = self.board.pins[digital_output_pin_number] diff --git a/pingo/test/level1/__init__.py b/pingo/test/level1/__init__.py index 8073b13..189b11c 100644 --- a/pingo/test/level1/__init__.py +++ b/pingo/test/level1/__init__.py @@ -1,2 +1,2 @@ -from cases import AnalogReadBasics # noqa -from cases import AnalogExceptions # noqa +from .cases import AnalogReadBasics # noqa +from .cases import AnalogExceptions # noqa diff --git a/pingo/test/level1/cases.py b/pingo/test/level1/cases.py index 970bd79..06641bb 100644 --- a/pingo/test/level1/cases.py +++ b/pingo/test/level1/cases.py @@ -1,4 +1,5 @@ import pingo +from pingo.board import Mode ''' In order to use this set of cases, it is necessary to set @@ -18,7 +19,7 @@ class AnalogReadBasics(object): def test_200ohmRead(self): pin = self.board.pins[self.analog_input_pin_number] - pin.mode = pingo.ANALOG + pin.mode = Mode.ANALOG _input = pin.value # print "Value Read: ", _input @@ -26,7 +27,7 @@ def test_200ohmRead(self): def test_pin_ratio(self): pin = self.board.pins[self.analog_input_pin_number] - pin.mode = pingo.ANALOG + pin.mode = Mode.ANALOG bits_resolution = (2 ** pin.bits) - 1 _input = pin.ratio(0, bits_resolution, 0.0, 1.0) # print "Value Read: ", _input @@ -40,4 +41,4 @@ class AnalogExceptions(object): def test_wrong_output_mode(self): pin = self.board.pins[self.analog_input_pin_number] with self.assertRaises(pingo.ModeNotSuported): - pin.mode = pingo.OUT + pin.mode = Mode.OUT diff --git a/pingo/test/level2/__init__.py b/pingo/test/level2/__init__.py index 4d955af..a82daca 100644 --- a/pingo/test/level2/__init__.py +++ b/pingo/test/level2/__init__.py @@ -1,2 +1,2 @@ -from cases import PwmBasics # noqa -from cases import PwmExceptions # noqa +from .cases import PwmBasics # noqa +from .cases import PwmExceptions # noqa diff --git a/pingo/test/level2/cases.py b/pingo/test/level2/cases.py index 7e7564b..72f4907 100644 --- a/pingo/test/level2/cases.py +++ b/pingo/test/level2/cases.py @@ -1,4 +1,5 @@ import pingo +from pingo.board import Mode ''' In order to use this set of cases, it is necessary to set @@ -11,7 +12,7 @@ class PwmBasics(object): def test_dot50_duty_cycle(self): pin = self.board.pins[self.pwm_pin_number] - pin.mode = pingo.PWM + pin.mode = Mode.PWM pin.value = 0.50 _duty_cycle = pin.value @@ -21,7 +22,7 @@ def test_dot50_duty_cycle(self): def test_frequency(self): pin = self.board.pins[self.pwm_pin_number] - pin.mode = pingo.PWM + pin.mode = Mode.PWM pin.frequency = 440 _frequency = pin.frequency @@ -33,11 +34,11 @@ class PwmExceptions(object): def test_wrong_analog_mode(self): pin = self.board.pins[self.pwm_pin_number] with self.assertRaises(pingo.ModeNotSuported): - pin.mode = pingo.ANALOG + pin.mode = Mode.ANALOG def test_wrong_read_state(self): pin = self.board.pins[self.pwm_pin_number] - pin.mode = pingo.PWM + pin.mode = Mode.PWM with self.assertRaises(pingo.WrongPinMode): pin.state diff --git a/pingo/test_utils.py b/pingo/test_utils.py index 3057fec..c6dcfdb 100644 --- a/pingo/test_utils.py +++ b/pingo/test_utils.py @@ -1,12 +1,12 @@ import unittest -import util +from pingo.util import StrKeyDict class StrKeyDictTest(unittest.TestCase): def setUp(self): - self.d = util.StrKeyDict( + self.d = StrKeyDict( [('2', 'two'), ('4', 'four')] ) diff --git a/pingo/udoo/__init__.py b/pingo/udoo/__init__.py index 313eae0..c981717 100644 --- a/pingo/udoo/__init__.py +++ b/pingo/udoo/__init__.py @@ -1 +1 @@ -from udoo import Udoo # noqa +from .udoo import Udoo # noqa diff --git a/pingo/udoo/udoo.py b/pingo/udoo/udoo.py index 1e887ad..f49482e 100644 --- a/pingo/udoo/udoo.py +++ b/pingo/udoo/udoo.py @@ -8,7 +8,8 @@ import os -from pingo.board import Board, DigitalPin, IN, OUT, HIGH, LOW +from pingo.board import Board, DigitalPin +from pingo.board import State, Mode # there are no gaps in the Arduino digital pin numbering # of the Arduino Due embedded in the Udoo @@ -26,8 +27,8 @@ DIGITAL_PIN_MASK = 'gpio%d' DIGITAL_PIN_STATE_FILENAME = 'value' DIGITAL_PIN_MODE_FILENAME = 'direction' -DIGITAL_PIN_MODES = {IN: 'in', OUT: 'out'} -DIGITAL_PIN_STATES = {HIGH: '1', LOW: '0'} +DIGITAL_PIN_MODES = {Mode.IN: 'in', Mode.OUT: 'out'} +DIGITAL_PIN_STATES = {State.HIGH: '1', State.LOW: '0'} class Udoo(Board): @@ -76,6 +77,6 @@ def _set_pin_state(self, pin, state): def cleanup(self): for pin in (p for p in self.pins.values() if isinstance(p, DigitalPin)): - if pin.mode == OUT: + if pin.mode == Mode.OUT: pin.low() - pin.mode = IN + pin.mode = Mode.IN diff --git a/pingo/util.py b/pingo/util.py index 985296d..5cb83f3 100644 --- a/pingo/util.py +++ b/pingo/util.py @@ -1,8 +1,8 @@ import collections -import UserDict +from .compat import (UserDict, iterkeys) -class StrKeyDict(UserDict.UserDict): +class StrKeyDict(UserDict): """StrKeyDict always converts non-string keys to `str` Tests for item retrieval using `d[key]` notation:: @@ -67,7 +67,7 @@ def __setitem__(self, key, item): self.data[self.normalize(key)] = item def __iter__(self): - return self.iterkeys() + return iterkeys(self) def update(self, iterable=None, **kwds): if iterable is not None: diff --git a/requirements.txt b/requirements.txt index 444529c..267ec2f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,3 +15,4 @@ pytest-cov==1.6 pytest-pep8==1.0.6 wsgiref==0.1.2 pyserial==2.7 +enum34 diff --git a/scripts/vimrc b/scripts/vimrc index 7e3f8de..00bf943 100644 --- a/scripts/vimrc +++ b/scripts/vimrc @@ -20,10 +20,10 @@ hi TABs ctermbg=Red ctermfg=LightRed guibg=Red guifg=LightRed term=italic hi TrSp ctermbg=DarkGray ctermfg=Red guibg=DarkGray guifg=Red term=italic call matchadd("TABs", "\t") " All TABs but the trailing ones call matchadd("TrSp", "\\s\\+$") " Trailing TABs and spaces +autocmd BufWritePre * :%s/\s\+$//e " Removes trailing spaces when save buffer " Paste mouse selected text with Shift+Insert if has('gui_running') map map! endif - diff --git a/setup.py b/setup.py index 021ea66..08a7057 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,7 @@ def run_tests(self): install_requires = [ - '', + 'enum34', ] tests_require = [