Skip to content

Commit bbf1371

Browse files
committed
Merge pull request #46 from ddemidov/module-to-package
Module to package
2 parents 7099a36 + 933136c commit bbf1371

File tree

9 files changed

+277
-191
lines changed

9 files changed

+277
-191
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ __pycache__
44
dist
55
*.egg-info
66
RELEASE-VERSION
7+
ev3dev/version.py

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ sudo: false
55
install:
66
- pip install PIL --allow-external PIL --allow-unverified PIL
77
script:
8-
- "./tests/api_tests.py"
8+
- echo "__version__=''" > ev3dev/version.py
9+
- ./tests/api_tests.py
910
deploy:
1011
provider: pypi
1112
user: Denis.Demidov

ev3dev/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import platform
2+
from .version import __version__
3+
4+
# -----------------------------------------------------------------------------
5+
# Guess platform we are running on
6+
def current_platform():
7+
machine = platform.machine()
8+
if machine == 'armv5tejl':
9+
return 'ev3'
10+
elif machine == 'armv6l':
11+
return 'brickpi'
12+
else:
13+
return 'unsupported'
14+
15+
if current_platform() == 'brickpi':
16+
from .brickpi import *
17+
else:
18+
# Import ev3 by default, so that it is covered by documentation.
19+
from .ev3 import *

ev3dev/brickpi.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# ------------------------------------------------------------------------------
4+
# Copyright (c) 2015 Eric Pascual
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in
14+
# all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
# THE SOFTWARE.
23+
# -----------------------------------------------------------------------------
24+
25+
"""
26+
An assortment of classes modeling specific features of the BrickPi.
27+
"""
28+
29+
from .core import *
30+
31+
32+
OUTPUT_A = 'ttyAMA0:outA'
33+
OUTPUT_B = 'ttyAMA0:outB'
34+
OUTPUT_C = 'ttyAMA0:outC'
35+
OUTPUT_D = 'ttyAMA0:outD'
36+
37+
INPUT_1 = 'ttyAMA0:in1'
38+
INPUT_2 = 'ttyAMA0:in2'
39+
INPUT_3 = 'ttyAMA0:in3'
40+
INPUT_4 = 'ttyAMA0:in4'
41+
42+
43+
class Leds(object):
44+
"""
45+
The BrickPi LEDs.
46+
"""
47+
48+
# ~autogen led-colors platforms.brickpi.led>currentClass
49+
50+
blue_one = Led(name='brickpi1:blue:ev3dev')
51+
blue_two = Led(name='brickpi2:blue:ev3dev')
52+
53+
@staticmethod
54+
def mix_colors(blue):
55+
Leds.blue_one.brightness_pct = blue
56+
Leds.blue_two.brightness_pct = blue
57+
58+
@staticmethod
59+
def set_blue(pct):
60+
Leds.mix_colors(blue=1 * pct)
61+
62+
@staticmethod
63+
def blue_on():
64+
Leds.set_blue(1)
65+
66+
@staticmethod
67+
def all_off():
68+
Leds.blue_one.brightness = 0
69+
Leds.blue_two.brightness = 0
70+
71+
72+
# ~autogen

ev3dev.py renamed to ev3dev/core.py

Lines changed: 5 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import os
3131
import fnmatch
3232
import numbers
33-
import platform
3433
import fcntl
3534
import array
3635
import mmap
@@ -42,18 +41,6 @@
4241
from subprocess import Popen
4342

4443

45-
# -----------------------------------------------------------------------------
46-
# Guess platform we are running on
47-
def current_platform():
48-
machine = platform.machine()
49-
if machine == 'armv5tejl':
50-
return 'ev3'
51-
elif machine == 'armv6l':
52-
return 'brickpi'
53-
else:
54-
return 'unsupported'
55-
56-
5744
# -----------------------------------------------------------------------------
5845
# Attribute reader/writer with cached file access
5946
class FileCache(object):
@@ -1700,112 +1687,6 @@ def brightness_pct(self):
17001687
def brightness_pct(self, value):
17011688
self.brightness = value * self.max_brightness
17021689

1703-
if current_platform() == 'ev3':
1704-
# ~autogen led-colors platforms.ev3.led>currentClass
1705-
1706-
Led.red_left = Led(name='ev3-left0:red:ev3dev')
1707-
Led.red_right = Led(name='ev3-right0:red:ev3dev')
1708-
Led.green_left = Led(name='ev3-left1:green:ev3dev')
1709-
Led.green_right = Led(name='ev3-right1:green:ev3dev')
1710-
1711-
@staticmethod
1712-
def _Led_mix_colors(red, green):
1713-
Led.red_left.brightness_pct = red
1714-
Led.red_right.brightness_pct = red
1715-
Led.green_left.brightness_pct = green
1716-
Led.green_right.brightness_pct = green
1717-
Led.mix_colors = _Led_mix_colors
1718-
1719-
@staticmethod
1720-
def _Led_set_red(pct):
1721-
Led.mix_colors(red=1*pct, green=0*pct)
1722-
Led.set_red = _Led_set_red
1723-
1724-
@staticmethod
1725-
def _Led_red_on():
1726-
Led.set_red(1)
1727-
Led.red_on = _Led_red_on
1728-
1729-
@staticmethod
1730-
def _Led_set_green(pct):
1731-
Led.mix_colors(red=0*pct, green=1*pct)
1732-
Led.set_green = _Led_set_green
1733-
1734-
@staticmethod
1735-
def _Led_green_on():
1736-
Led.set_green(1)
1737-
Led.green_on = _Led_green_on
1738-
1739-
@staticmethod
1740-
def _Led_set_amber(pct):
1741-
Led.mix_colors(red=1*pct, green=1*pct)
1742-
Led.set_amber = _Led_set_amber
1743-
1744-
@staticmethod
1745-
def _Led_amber_on():
1746-
Led.set_amber(1)
1747-
Led.amber_on = _Led_amber_on
1748-
1749-
@staticmethod
1750-
def _Led_set_orange(pct):
1751-
Led.mix_colors(red=1*pct, green=0.5*pct)
1752-
Led.set_orange = _Led_set_orange
1753-
1754-
@staticmethod
1755-
def _Led_orange_on():
1756-
Led.set_orange(1)
1757-
Led.orange_on = _Led_orange_on
1758-
1759-
@staticmethod
1760-
def _Led_set_yellow(pct):
1761-
Led.mix_colors(red=0.5*pct, green=1*pct)
1762-
Led.set_yellow = _Led_set_yellow
1763-
1764-
@staticmethod
1765-
def _Led_yellow_on():
1766-
Led.set_yellow(1)
1767-
Led.yellow_on = _Led_yellow_on
1768-
1769-
@staticmethod
1770-
def _Led_all_off():
1771-
Led.red_left.brightness = 0
1772-
Led.red_right.brightness = 0
1773-
Led.green_left.brightness = 0
1774-
Led.green_right.brightness = 0
1775-
Led.all_off = _Led_all_off
1776-
1777-
1778-
# ~autogen
1779-
elif current_platform() == 'brickpi':
1780-
# ~autogen led-colors platforms.brickpi.led>currentClass
1781-
1782-
Led.blue_one = Led(name='brickpi1:blue:ev3dev')
1783-
Led.blue_two = Led(name='brickpi2:blue:ev3dev')
1784-
1785-
@staticmethod
1786-
def _Led_mix_colors(blue):
1787-
Led.blue_one.brightness_pct = blue
1788-
Led.blue_two.brightness_pct = blue
1789-
Led.mix_colors = _Led_mix_colors
1790-
1791-
@staticmethod
1792-
def _Led_set_blue(pct):
1793-
Led.mix_colors(blue=1*pct)
1794-
Led.set_blue = _Led_set_blue
1795-
1796-
@staticmethod
1797-
def _Led_blue_on():
1798-
Led.set_blue(1)
1799-
Led.blue_on = _Led_blue_on
1800-
1801-
@staticmethod
1802-
def _Led_all_off():
1803-
Led.blue_one.brightness = 0
1804-
Led.blue_two.brightness = 0
1805-
Led.all_off = _Led_all_off
1806-
1807-
1808-
# ~autogen
18091690

18101691
class ButtonBase(object):
18111692
"""
@@ -1847,26 +1728,23 @@ def process(self):
18471728
self.on_change([(button, button in new_state) for button in state_diff])
18481729

18491730

1850-
# ~autogen button-class classes.button>currentClass
1851-
class Button(ButtonBase):
1731+
class ButtonEVIO(ButtonBase):
18521732

18531733
"""
1854-
Provides a generic button reading mechanism that can be adapted
1855-
to platform specific implementations. Each platform's specific
1856-
button capabilites are enumerated in the 'platforms' section
1857-
of this specification
1734+
Provides a generic button reading mechanism that works with event interface
1735+
and may be adapted to platform specific implementations.
18581736
18591737
This implementation depends on the availability of the EVIOCGKEY ioctl
18601738
to be able to read the button state buffer. See Linux kernel source
18611739
in /include/uapi/linux/input.h for details.
18621740
"""
18631741

1864-
# ~autogen
1865-
18661742
KEY_MAX = 0x2FF
18671743
KEY_BUF_LEN = int((KEY_MAX + 7) / 8)
18681744
EVIOCGKEY = (2 << (14 + 8 + 8) | KEY_BUF_LEN << (8 + 8) | ord('E') << 8 | 0x18)
18691745

1746+
_buttons = {}
1747+
18701748
def __init__(self):
18711749
self._file_cache = FileCache()
18721750
self._buffer_cache = {}
@@ -1895,51 +1773,7 @@ def buttons_pressed(self):
18951773
pressed += [k]
18961774
return pressed
18971775

1898-
if current_platform() == 'ev3':
1899-
# ~autogen button-property platforms.ev3.button>currentClass
1900-
1901-
on_up = None
1902-
on_down = None
1903-
on_left = None
1904-
on_right = None
1905-
on_enter = None
1906-
on_backspace = None
1907-
1908-
_buttons = {
1909-
'up': {'name': '/dev/input/by-path/platform-gpio-keys.0-event', 'value': 103},
1910-
'down': {'name': '/dev/input/by-path/platform-gpio-keys.0-event', 'value': 108},
1911-
'left': {'name': '/dev/input/by-path/platform-gpio-keys.0-event', 'value': 105},
1912-
'right': {'name': '/dev/input/by-path/platform-gpio-keys.0-event', 'value': 106},
1913-
'enter': {'name': '/dev/input/by-path/platform-gpio-keys.0-event', 'value': 28},
1914-
'backspace': {'name': '/dev/input/by-path/platform-gpio-keys.0-event', 'value': 14},
1915-
}
1916-
1917-
@property
1918-
def up(self):
1919-
return 'up' in self.buttons_pressed
19201776

1921-
@property
1922-
def down(self):
1923-
return 'down' in self.buttons_pressed
1924-
1925-
@property
1926-
def left(self):
1927-
return 'left' in self.buttons_pressed
1928-
1929-
@property
1930-
def right(self):
1931-
return 'right' in self.buttons_pressed
1932-
1933-
@property
1934-
def enter(self):
1935-
return 'enter' in self.buttons_pressed
1936-
1937-
@property
1938-
def backspace(self):
1939-
return 'backspace' in self.buttons_pressed
1940-
1941-
1942-
# ~autogen
19431777
# ~autogen remote-control classes.infraredSensor.remoteControl>currentClass
19441778
class RemoteControl(ButtonBase):
19451779
"""

0 commit comments

Comments
 (0)