Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum #109

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open

Enum #109

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this is necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's necessary to install dependencies (enum34). So as enum34 is in dependencies of this package, installing package will install dependencies

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, because you added enum34 to setup.py and to requirements.txt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover it's a way to ensure that this package installs correctly

script:
- flake8 --version
- flake8 --ignore=E501 pingo
Expand Down
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
54 changes: 27 additions & 27 deletions pingo/__init__.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions pingo/arduino/__init__.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 6 additions & 5 deletions pingo/arduino/firmata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pingo/arduino/pyun.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 3 additions & 7 deletions pingo/arduino/test_util_firmata.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because of flake8 errors

# [0x6C] # CAPABILITY_RESPONSE

pinmap = pin_list_to_board_dict(data_arduino)
for key in test_layout.keys():
Expand Down
2 changes: 1 addition & 1 deletion pingo/bbb/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from bbb import BeagleBoneBlack # noqa
from .bbb import BeagleBoneBlack # noqa
64 changes: 32 additions & 32 deletions pingo/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

;-)


# 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):
Expand Down Expand Up @@ -249,24 +245,28 @@ 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
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.
Expand All @@ -279,60 +279,60 @@ 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)
self._state = 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)
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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):
"""
Expand Down
28 changes: 28 additions & 0 deletions pingo/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this name compat.py? compat = compatibility?

I don't know if you followed the early discussion. There was a consensus on supporting Python3 only if Python2 compatibility is assured. Read more on Py3k support #45

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compat.py is a quite standard name to provide imports and functions that work both with Python 2 and Python 3.
Try https://www.google.fr/webhp?q=compat.py#safe=off&q=compat.py

https://docs.python.org/3/howto/pyporting.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, That was a new one for me. Thanks.


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
2 changes: 1 addition & 1 deletion pingo/detect/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from detect import get_board
from .detect import get_board


def has_module(module_name):
Expand Down
3 changes: 1 addition & 2 deletions pingo/detect/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pingo/detect/test_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import platform

import pingo
import detect


class DetectBasics(unittest.TestCase):
Expand All @@ -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__':
Expand Down
Loading