From bf2e4e2960212c762186364f5726a73c8c1eb421 Mon Sep 17 00:00:00 2001 From: scls19fr Date: Thu, 26 Nov 2015 21:46:03 +0100 Subject: [PATCH 01/14] Python 2/3 support --- pingo/__init__.py | 56 ++++++++++++++++-------------- pingo/arduino/__init__.py | 8 ++--- pingo/arduino/firmata.py | 2 +- pingo/arduino/pyun.py | 2 +- pingo/arduino/test_util_firmata.py | 2 +- pingo/bbb/__init__.py | 2 +- pingo/compat.py | 25 +++++++++++++ pingo/detect/__init__.py | 2 +- pingo/detect/test_detect.py | 2 +- pingo/ghost/__init__.py | 2 +- pingo/ghost/ghost.py | 4 +-- pingo/intel/__init__.py | 4 +-- pingo/parts/serial/__init__.py | 2 +- pingo/parts/spi/__init__.py | 2 +- pingo/parts/spi/mcp3008.py | 2 +- pingo/pcduino/__init__.py | 2 +- pingo/rpi/__init__.py | 8 ++--- pingo/test/__init__.py | 6 ++-- pingo/test/level0/__init__.py | 4 +-- pingo/test/level1/__init__.py | 4 +-- pingo/test/level2/__init__.py | 4 +-- pingo/test_utils.py | 4 +-- pingo/udoo/__init__.py | 2 +- pingo/util.py | 7 ++-- 24 files changed, 92 insertions(+), 66 deletions(-) create mode 100644 pingo/compat.py diff --git a/pingo/__init__.py b/pingo/__init__.py index 2ad3e29..44eec99 100644 --- a/pingo/__init__.py +++ b/pingo/__init__.py @@ -1,32 +1,34 @@ +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 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 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..ab5b278 100644 --- a/pingo/arduino/firmata.py +++ b/pingo/arduino/firmata.py @@ -11,7 +11,7 @@ from pingo.board import Board, DigitalPin, AnalogPin, PwmPin 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 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..5f91ee5 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): 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/compat.py b/pingo/compat.py new file mode 100644 index 0000000..0ca1a1f --- /dev/null +++ b/pingo/compat.py @@ -0,0 +1,25 @@ +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 +else: + from collections import UserDict 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/test_detect.py b/pingo/detect/test_detect.py index ffc9481..ec00c73 100644 --- a/pingo/detect/test_detect.py +++ b/pingo/detect/test_detect.py @@ -2,7 +2,7 @@ import platform import pingo -import detect +import pingo.detect class DetectBasics(unittest.TestCase): 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..a8968ae 100644 --- a/pingo/ghost/ghost.py +++ b/pingo/ghost/ghost.py @@ -1,5 +1,5 @@ import pingo - +from pingo.compat import iteritems class GhostBoard( pingo.Board, @@ -48,7 +48,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 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/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/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/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/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/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_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/util.py b/pingo/util.py index 985296d..c5af22a 100644 --- a/pingo/util.py +++ b/pingo/util.py @@ -1,8 +1,7 @@ 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 +66,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: From 21e9a8265524d5ba4787405bd26853e10e5a074a Mon Sep 17 00:00:00 2001 From: Luiz Menezes Date: Mon, 14 Sep 2015 23:29:18 -0300 Subject: [PATCH 02/14] Untested (v0.1) implmentation of http board access (#68). --- pingo/iot/server.py | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 pingo/iot/server.py diff --git a/pingo/iot/server.py b/pingo/iot/server.py new file mode 100644 index 0000000..f33e5bf --- /dev/null +++ b/pingo/iot/server.py @@ -0,0 +1,73 @@ +from flask import Flask +from flask_restful import Resource, Api +from pingo.board import AnalogInputCapable +import pingo + + +app = Flask(__name__) +api = Api(app) + +board = pingo.detect.get_board() + + +class Main(Resource): + def get(self): + return { + 'board': repr(board), + 'pins': str(board.pins) + } + + +class DigitalPins(Resource): + def get(self, input_type): + pins = board.pins() + return {'pins': str(pins)} + + +class AnalogPins(Resource): + def get(self, input_type): + if issubclass(type(board), AnalogInputCapable): + pins = str(board.digital_pins) + else: + pins = [] + return {'pins': str(pins)} + + +class Input(Resource): + def get(self, input_type, pin): + pin = board.pins[pin] + pin.mode = pingo.IN + return {'input': pin.state} + + +class AnalogOutput(Resource): + def get(self, output_type, pin, signal): + pin = board.pins[pin] + pin.mode = pingo.OUT + pin.value = signal + return {'output': signal} + + +class DigitalOutput(Resource): + def get(self, output_type, pin, signal): + pin = board.pins[pin] + pin.mode = pingo.OUT + if signal: + pin.high() + else: + pin.low() + return {'output': signal} + + +api.add_resource(Main, '/') +api.add_resource(AnalogPins, '/analog') +api.add_resource(DigitalPins, '/digital') +api.add_resource(Input, '//') +api.add_resource(AnalogOutput, + '//analog//') +api.add_resource(DigitalOutput, + '//digital//') + + +if __name__ == '__main__': + app.run(debug=True) From 5f9eb2f45b35ebb9ba93ad05e5ccd2cef6d3a9d8 Mon Sep 17 00:00:00 2001 From: Luiz Menezes Date: Mon, 14 Sep 2015 23:43:42 -0300 Subject: [PATCH 03/14] adds command-line arguments to specify host and fixes url mapping --- pingo/iot/server.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pingo/iot/server.py b/pingo/iot/server.py index f33e5bf..d522cdc 100644 --- a/pingo/iot/server.py +++ b/pingo/iot/server.py @@ -2,6 +2,7 @@ from flask_restful import Resource, Api from pingo.board import AnalogInputCapable import pingo +import sys app = Flask(__name__) @@ -63,11 +64,13 @@ def get(self, output_type, pin, signal): api.add_resource(AnalogPins, '/analog') api.add_resource(DigitalPins, '/digital') api.add_resource(Input, '//') -api.add_resource(AnalogOutput, - '//analog//') -api.add_resource(DigitalOutput, - '//digital//') +api.add_resource(AnalogOutput, '/analog//') +api.add_resource(DigitalOutput, '/digital//') if __name__ == '__main__': - app.run(debug=True) + try: + kwargs = {'host': sys.argv[1]} + except: + kwargs = {} + app.run(debug=True, **kwargs) From 757f904eccaa7edaa15652a283f296ed546f590b Mon Sep 17 00:00:00 2001 From: Luiz Menezes Date: Sun, 20 Sep 2015 22:52:51 -0300 Subject: [PATCH 04/14] pingo.iot: Uses bottle instead of flask. Adds mode setup to the server --- pingo/iot/server.py | 102 ++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/pingo/iot/server.py b/pingo/iot/server.py index d522cdc..d92d4c3 100644 --- a/pingo/iot/server.py +++ b/pingo/iot/server.py @@ -1,76 +1,78 @@ -from flask import Flask -from flask_restful import Resource, Api -from pingo.board import AnalogInputCapable +from bottle import Bottle +from pingo.board import AnalogPin +import json import pingo import sys -app = Flask(__name__) -api = Api(app) - +app = Bottle(__name__) board = pingo.detect.get_board() -class Main(Resource): - def get(self): - return { - 'board': repr(board), - 'pins': str(board.pins) - } +@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 -class DigitalPins(Resource): - def get(self, input_type): - pins = board.pins() - return {'pins': str(pins)} +@app.route('/analog') +def analog_pins(): + # TODO: find a better way to check if the pin is analog + pins = {key: value for key, value in board.pins + if issubclass(type(board), AnalogPin)} + return {'pins': str(pins)} -class AnalogPins(Resource): - def get(self, input_type): - if issubclass(type(board), AnalogInputCapable): - pins = str(board.digital_pins) - else: - pins = [] - return {'pins': str(pins)} +@app.route('/analog/') +def analog_input(pin): + pin = board.pins[pin] + pin.mode = pingo.IN + return {'input': pin.state} -class Input(Resource): - def get(self, input_type, 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} -class AnalogOutput(Resource): - def get(self, output_type, 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)} -class DigitalOutput(Resource): - def get(self, output_type, pin, signal): - pin = board.pins[pin] - pin.mode = pingo.OUT - if signal: - pin.high() - else: - pin.low() - return {'output': signal} +@app.route('/digital/') +def digital_input(pin): + pin = board.pins[pin] + pin.mode = pingo.IN + return {'input': pin.state} -api.add_resource(Main, '/') -api.add_resource(AnalogPins, '/analog') -api.add_resource(DigitalPins, '/digital') -api.add_resource(Input, '//') -api.add_resource(AnalogOutput, '/analog//') -api.add_resource(DigitalOutput, '/digital//') +@app.route('/digtal//') +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: + except IndexError: kwargs = {} app.run(debug=True, **kwargs) From 2b37237e9cea9de12a3d8d19584f49225a0f7080 Mon Sep 17 00:00:00 2001 From: Luiz Menezes Date: Sun, 20 Sep 2015 23:01:17 -0300 Subject: [PATCH 05/14] Adds HTTPBoard to communicate to iot server using the board API --- pingo/iot/client.py | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 pingo/iot/client.py diff --git a/pingo/iot/client.py b/pingo/iot/client.py new file mode 100644 index 0000000..0a048f6 --- /dev/null +++ b/pingo/iot/client.py @@ -0,0 +1,70 @@ +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): + url = self.server + '/mode' + mode = 'input' if pingo.IN else 'output' + urlopen(url + '/' + mode + '/' + str(pin.location)) + + def _set_pin_state(self, pin, state): + url = self.server + # TODO: add is_analog method to Pin + if issubclass(type(pin), pingo.board.AnalogPin): + url += '/analog' + else: + url += '/digital' + response = urlopen(url + '/' + str(pin.location) + '/' + str(state)) + 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): + url = self.server + if issubclass(type(pin), pingo.board.AnalogPin): + url += '/analog' + else: + url += '/digital' + response = urlopen(url + '/' + str(pin.location)) + 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'] From f68298b7c876ee63459930c0b7f2f1978a0d1ae3 Mon Sep 17 00:00:00 2001 From: Luiz Menezes Date: Sun, 20 Sep 2015 23:08:11 -0300 Subject: [PATCH 06/14] pep8 fixes --- pingo/iot/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pingo/iot/server.py b/pingo/iot/server.py index d92d4c3..b2e7816 100644 --- a/pingo/iot/server.py +++ b/pingo/iot/server.py @@ -30,7 +30,7 @@ def mode(mode, pin): def analog_pins(): # TODO: find a better way to check if the pin is analog pins = {key: value for key, value in board.pins - if issubclass(type(board), AnalogPin)} + if issubclass(type(board), AnalogPin)} return {'pins': str(pins)} From 0857ac0b7d9663691895fce57bd43a7f23df4f45 Mon Sep 17 00:00:00 2001 From: Luiz Menezes Date: Wed, 23 Sep 2015 09:40:49 -0300 Subject: [PATCH 07/14] Adds method pin.is_analog --- pingo/board.py | 4 ++++ pingo/iot/client.py | 5 ++--- pingo/iot/server.py | 6 ++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pingo/board.py b/pingo/board.py index 4a296cc..e7d9ca7 100644 --- a/pingo/board.py +++ b/pingo/board.py @@ -267,6 +267,10 @@ def 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. diff --git a/pingo/iot/client.py b/pingo/iot/client.py index 0a048f6..2469c77 100644 --- a/pingo/iot/client.py +++ b/pingo/iot/client.py @@ -45,8 +45,7 @@ def _set_digital_mode(self, pin, mode): def _set_pin_state(self, pin, state): url = self.server - # TODO: add is_analog method to Pin - if issubclass(type(pin), pingo.board.AnalogPin): + if pin.is_analog: url += '/analog' else: url += '/digital' @@ -58,7 +57,7 @@ def _set_pin_state(self, pin, state): def _get_pin_state(self, pin): url = self.server - if issubclass(type(pin), pingo.board.AnalogPin): + if pin.is_analog: url += '/analog' else: url += '/digital' diff --git a/pingo/iot/server.py b/pingo/iot/server.py index b2e7816..d39953a 100644 --- a/pingo/iot/server.py +++ b/pingo/iot/server.py @@ -1,5 +1,4 @@ from bottle import Bottle -from pingo.board import AnalogPin import json import pingo import sys @@ -28,9 +27,8 @@ def mode(mode, pin): @app.route('/analog') def analog_pins(): - # TODO: find a better way to check if the pin is analog - pins = {key: value for key, value in board.pins - if issubclass(type(board), AnalogPin)} + pins = {location: pin for location, pin in board.pins + if pin.is_analog} return {'pins': str(pins)} From d5aad1a587a9c11ffbfdf95a2986f4a887ab08d2 Mon Sep 17 00:00:00 2001 From: Luiz Menezes Date: Fri, 16 Oct 2015 23:55:59 -0300 Subject: [PATCH 08/14] Fixes the usage of filters on routing --- pingo/iot/server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pingo/iot/server.py b/pingo/iot/server.py index d39953a..a65de74 100644 --- a/pingo/iot/server.py +++ b/pingo/iot/server.py @@ -39,7 +39,7 @@ def analog_input(pin): return {'input': pin.state} -@app.route('/analog//') +@app.route('/analog//') def analog_output(pin, signal): pin = board.pins[pin] pin.mode = pingo.OUT @@ -60,7 +60,7 @@ def digital_input(pin): return {'input': pin.state} -@app.route('/digtal//') +@app.route('/digtal//') def digital_output(pin, signal): pin = board.pins[pin] pin.mode = pingo.OUT From 4721c35483a35406847085b9e6564b9b5c081149 Mon Sep 17 00:00:00 2001 From: Luiz Menezes Date: Sat, 17 Oct 2015 01:29:42 -0300 Subject: [PATCH 09/14] Improves url handling with HTTPBoard --- pingo/iot/client.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pingo/iot/client.py b/pingo/iot/client.py index 2469c77..9e7c100 100644 --- a/pingo/iot/client.py +++ b/pingo/iot/client.py @@ -39,29 +39,29 @@ def __init__(self, server): self._add_pins(ground_pins + vcc_pins + gpio_pins + pwm_pins) def _set_digital_mode(self, pin, mode): - url = self.server + '/mode' mode = 'input' if pingo.IN else 'output' - urlopen(url + '/' + mode + '/' + str(pin.location)) + url = '{server}mode/{mode}/{pin}'.format(server=self.server, + mode=mode, pin=pin.location) + urlopen(url) def _set_pin_state(self, pin, state): - url = self.server - if pin.is_analog: - url += '/analog' - else: - url += '/digital' - response = urlopen(url + '/' + str(pin.location) + '/' + str(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 = 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): - url = self.server - if pin.is_analog: - url += '/analog' - else: - url += '/digital' - response = urlopen(url + '/' + str(pin.location)) + 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) From ab1f2a53298e5b8e1dd5e63f31972dc92a11745c Mon Sep 17 00:00:00 2001 From: Luiz Menezes Date: Sat, 17 Oct 2015 01:30:04 -0300 Subject: [PATCH 10/14] Fixes route in pingo server --- pingo/iot/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pingo/iot/server.py b/pingo/iot/server.py index a65de74..96a1584 100644 --- a/pingo/iot/server.py +++ b/pingo/iot/server.py @@ -60,7 +60,7 @@ def digital_input(pin): return {'input': pin.state} -@app.route('/digtal//') +@app.route('/digital//') def digital_output(pin, signal): pin = board.pins[pin] pin.mode = pingo.OUT From 5ea737f77201786c4040a4afc0d4c1bc3c9518e2 Mon Sep 17 00:00:00 2001 From: Luiz Menezes Date: Sat, 17 Oct 2015 01:35:18 -0300 Subject: [PATCH 11/14] pep8 fixes --- pingo/iot/client.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pingo/iot/client.py b/pingo/iot/client.py index 9e7c100..de0a9e0 100644 --- a/pingo/iot/client.py +++ b/pingo/iot/client.py @@ -41,15 +41,15 @@ def __init__(self, server): 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) + 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)) + pin=str(pin.location), + state=str(state)) print(url) response = urlopen(url) if response.code != 200: @@ -60,7 +60,7 @@ def _set_pin_state(self, pin, state): 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) + pin=pin.location) response = urlopen(url) if response.code != 200: message = u'Pin {} could not be read: HTTPBoard response: {}' From 7addd83f3816f03ac9f5b126d0655598fdb1740d Mon Sep 17 00:00:00 2001 From: scls19fr Date: Sat, 28 Nov 2015 10:01:16 +0000 Subject: [PATCH 12/14] Stop blinking shouldn't always switch off led It should fix https://github.com/pingo-io/pingo-py/issues/104 When led was on and user ask led to blink 3 times (for example), led shouldn't be switched off but should fallback to initial state (before blinking) --- pingo/parts/led.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pingo/parts/led.py b/pingo/parts/led.py index 3c66825..09e7364 100644 --- a/pingo/parts/led.py +++ b/pingo/parts/led.py @@ -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 From 56464b83d6355c89674829ecc763b7f053cdccdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Justen=20=28=40turicas=29?= Date: Mon, 5 Oct 2015 18:28:39 -0300 Subject: [PATCH 13/14] Enhance vim config: auto delete trailing spaces --- scripts/vimrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 - From c6bb5672a60198e4013877a5e9bb460bd5e0c8cf Mon Sep 17 00:00:00 2001 From: scls19fr Date: Sat, 23 Jan 2016 09:30:53 +0100 Subject: [PATCH 14/14] Enum for states and modes --- .travis.yml | 4 +- README.rst | 3 +- pingo/__init__.py | 12 ++-- pingo/arduino/firmata.py | 9 +-- pingo/arduino/test_util_firmata.py | 8 +-- pingo/board.py | 60 +++++++++---------- pingo/compat.py | 7 ++- pingo/detect/detect.py | 3 +- pingo/detect/test_detect.py | 3 +- pingo/examples/analog_bars.py | 3 +- pingo/examples/blink.py | 3 +- pingo/examples/blink_firmata.py | 3 +- pingo/examples/blink_firmata_auto.py | 3 +- pingo/examples/dimmer.py | 7 ++- pingo/examples/dojo.py | 7 ++- pingo/examples/dojo2.py | 3 +- pingo/examples/dojoxxl.py | 9 +-- pingo/examples/double_blink.py | 5 +- pingo/examples/dual_analog_bars.py | 7 ++- pingo/examples/galileo_analog_bars.py | 7 ++- pingo/examples/map7segment.py | 7 ++- pingo/examples/parts/7seg_demo.py | 4 +- pingo/examples/parts/led_demo.py | 2 +- pingo/examples/pongo.py | 5 +- pingo/examples/probe_pins.py | 3 +- pingo/examples/pwm.py | 7 ++- pingo/examples/rgb_led.py | 3 +- pingo/examples/rpi_examples/display7.py | 3 +- pingo/examples/rpi_examples/display7_anim.py | 3 +- pingo/examples/rpi_examples/display7_probe.py | 3 +- pingo/examples/rpi_examples/display7_snake.py | 3 +- pingo/examples/switch.py | 4 +- pingo/ghost/ghost.py | 6 +- pingo/parts/button.py | 7 ++- pingo/parts/led.py | 18 +++--- pingo/parts/test/test_switch.py | 17 +++--- pingo/pcduino/pcduino.py | 9 +-- pingo/test/level0/cases.py | 21 +++---- pingo/test/level1/cases.py | 7 ++- pingo/test/level2/cases.py | 9 +-- pingo/udoo/udoo.py | 11 ++-- pingo/util.py | 1 + requirements.txt | 1 + setup.py | 2 +- 44 files changed, 174 insertions(+), 148 deletions(-) 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 44eec99..e6405ac 100644 --- a/pingo/__init__.py +++ b/pingo/__init__.py @@ -1,12 +1,8 @@ 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 State # noqa +from .board import Mode # noqa from .board import ModeNotSuported # noqa from .board import WrongPinMode # noqa from .board import PwmOutputCapable # noqa @@ -18,7 +14,8 @@ from .board import GroundPin # noqa from .board import Pin # noqa from .board import VccPin # noqa -import pingo.parts #import * # noqa +import pingo.parts # import * # noqa + # boards import pingo.rpi # noqa @@ -29,6 +26,7 @@ import pingo.arduino # noqa import pingo.bbb # noqa + # resources import pingo.detect # noqa import pingo.test # noqa diff --git a/pingo/arduino/firmata.py b/pingo/arduino/firmata.py index ab5b278..37984ea 100644 --- a/pingo/arduino/firmata.py +++ b/pingo/arduino/firmata.py @@ -9,6 +9,7 @@ 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 @@ -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/test_util_firmata.py b/pingo/arduino/test_util_firmata.py index 5f91ee5..3951b2d 100644 --- a/pingo/arduino/test_util_firmata.py +++ b/pingo/arduino/test_util_firmata.py @@ -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/board.py b/pingo/board.py index e7d9ca7..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,11 +254,11 @@ 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 @@ -283,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) @@ -291,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) @@ -348,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() @@ -363,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() @@ -386,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 index 0ca1a1f..1fda172 100644 --- a/pingo/compat.py +++ b/pingo/compat.py @@ -3,6 +3,7 @@ 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'. @@ -13,13 +14,15 @@ def iteritems(obj, **kwargs): 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 + from UserDict import UserDict # noqa else: - from collections import UserDict + from collections import UserDict # noqa 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 ec00c73..659086f 100644 --- a/pingo/detect/test_detect.py +++ b/pingo/detect/test_detect.py @@ -2,7 +2,6 @@ import platform import pingo -import pingo.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/ghost.py b/pingo/ghost/ghost.py index a8968ae..903ee33 100644 --- a/pingo/ghost/ghost.py +++ b/pingo/ghost/ghost.py @@ -1,5 +1,7 @@ import pingo from pingo.compat import iteritems +from pingo.board import State + class GhostBoard( pingo.Board, @@ -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/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 09e7364..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)] @@ -174,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/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/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/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/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/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/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 c5af22a..5cb83f3 100644 --- a/pingo/util.py +++ b/pingo/util.py @@ -1,6 +1,7 @@ import collections from .compat import (UserDict, iterkeys) + class StrKeyDict(UserDict): """StrKeyDict always converts non-string keys to `str` 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/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 = [