Skip to content

Commit

Permalink
Enum for states and modes
Browse files Browse the repository at this point in the history
  • Loading branch information
s-celles committed Jun 21, 2016
1 parent 56464b8 commit c6bb567
Show file tree
Hide file tree
Showing 44 changed files with 174 additions and 148 deletions.
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
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
12 changes: 5 additions & 7 deletions pingo/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -29,6 +26,7 @@
import pingo.arduino # noqa
import pingo.bbb # noqa


# resources
import pingo.detect # noqa
import pingo.test # noqa
9 changes: 5 additions & 4 deletions pingo/arduino/firmata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
8 changes: 2 additions & 6 deletions pingo/arduino/test_util_firmata.py
Original file line number Diff line number Diff line change
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)
# [0x6C] # CAPABILITY_RESPONSE

pinmap = pin_list_to_board_dict(data_arduino)
for key in test_layout.keys():
Expand Down
60 changes: 28 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')

# 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,20 +245,20 @@ 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
Expand All @@ -283,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 @@ -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()
Expand All @@ -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()
Expand All @@ -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):
"""
Expand Down
7 changes: 5 additions & 2 deletions pingo/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand All @@ -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
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 pingo.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
3 changes: 2 additions & 1 deletion pingo/examples/analog_bars.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
3 changes: 2 additions & 1 deletion pingo/examples/blink.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion pingo/examples/blink_firmata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion pingo/examples/blink_firmata_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 4 additions & 3 deletions pingo/examples/dimmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading

0 comments on commit c6bb567

Please sign in to comment.