Skip to content

Commit 1350cd5

Browse files
authored
Use /sys/class/board-info/ to detect platform type (#383)
* Use /sys/class/board-info/ to detect platform type * Move get_current_platform() to core.py * PiStorms support * Update docstrings
1 parent 28b97f4 commit 1350cd5

File tree

4 files changed

+190
-32
lines changed

4 files changed

+190
-32
lines changed

ev3dev/auto.py

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,17 @@
11

2-
"""
3-
Use platform.machine() to determine the platform type, cache the
4-
results in /tmp/current_platform so that we do not have to import
5-
platform and run platform.machine() each time someone imports ev3dev.auto
6-
"""
7-
import os
2+
from ev3dev.core import get_current_platform
83

9-
filename = '/tmp/current_platform'
10-
current_platform = None
4+
current_platform = get_current_platform()
115

12-
if os.path.exists(filename):
13-
with open(filename, 'r') as fh:
14-
current_platform = fh.read().strip()
15-
16-
if not current_platform:
17-
import platform
18-
19-
def get_current_platform():
20-
"""
21-
Guess platform we are running on
22-
"""
23-
machine = platform.machine()
24-
25-
if machine == 'armv5tejl':
26-
return 'ev3'
27-
elif machine == 'armv6l':
28-
return 'brickpi'
29-
else:
30-
return 'unsupported'
6+
if current_platform == 'brickpi':
7+
from .brickpi import *
318

32-
current_platform = get_current_platform()
9+
elif current_platform == 'evb':
10+
from .evb import *
3311

34-
with open(filename, 'w') as fh:
35-
fh.write(current_platform + '\n')
12+
elif current_platform == 'pistorms':
13+
from .pistorms import *
3614

37-
if current_platform == 'brickpi':
38-
from .brickpi import *
3915
else:
4016
# Import ev3 by default, so that it is covered by documentation.
4117
from .ev3 import *

ev3dev/core.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,44 @@
6060
# update to 'running' in the "on_for_XYZ" methods of the Motor class
6161
WAIT_RUNNING_TIMEOUT = 100
6262

63+
64+
def get_current_platform():
65+
"""
66+
Look in /sys/class/board-info/ to determine the platform type.
67+
68+
This can return 'ev3', 'evb', 'pistorms', 'brickpi' or 'brickpi3'.
69+
"""
70+
board_info_dir = '/sys/class/board-info/'
71+
72+
for board in os.listdir(board_info_dir):
73+
uevent_filename = os.path.join(board_info_dir, board, 'uevent')
74+
75+
if os.path.exists(uevent_filename):
76+
with open(uevent_filename, 'r') as fh:
77+
for line in fh.readlines():
78+
(key, value) = line.strip().split('=')
79+
80+
if key == 'BOARD_INFO_MODEL':
81+
82+
if value == 'LEGO MINDSTORMS EV3':
83+
return 'ev3'
84+
85+
elif value in ('FatcatLab EVB', 'QuestCape'):
86+
return 'evb'
87+
88+
elif value == 'PiStorms':
89+
return 'pistorms'
90+
91+
# This is the same for both BrickPi and BrickPi+.
92+
# There is not a way to tell the difference.
93+
elif value == 'Dexter Industries BrickPi':
94+
return 'brickpi'
95+
96+
elif value == 'Dexter Industries BrickPi3':
97+
return 'brickpi3'
98+
return None
99+
100+
63101
# -----------------------------------------------------------------------------
64102
def list_device_names(class_path, name_pattern, **kwargs):
65103
"""

ev3dev/evb.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
2+
"""
3+
An assortment of classes modeling specific features of the EVB.
4+
"""
5+
6+
from .core import *
7+
8+
9+
OUTPUT_A = 'outA'
10+
OUTPUT_B = 'outB'
11+
OUTPUT_C = 'outC'
12+
OUTPUT_D = 'outD'
13+
14+
INPUT_1 = 'in1'
15+
INPUT_2 = 'in2'
16+
INPUT_3 = 'in3'
17+
INPUT_4 = 'in4'
18+
19+
20+
class Button(ButtonEVIO):
21+
"""
22+
EVB Buttons
23+
"""
24+
25+
_buttons = {
26+
'up': {'name': '/dev/input/by-path/platform-evb-buttons-event', 'value': 103},
27+
'down': {'name': '/dev/input/by-path/platform-evb-buttons-event', 'value': 108},
28+
'left': {'name': '/dev/input/by-path/platform-evb-buttons-event', 'value': 105},
29+
'right': {'name': '/dev/input/by-path/platform-evb-buttons-event', 'value': 106},
30+
'enter': {'name': '/dev/input/by-path/platform-evb-buttons-event', 'value': 28},
31+
'backspace': {'name': '/dev/input/by-path/platform-evb-buttons-event', 'value': 14},
32+
}
33+
34+
@property
35+
@staticmethod
36+
def on_up(state):
37+
"""
38+
This handler is called by `process()` whenever state of 'up' button
39+
has changed since last `process()` call. `state` parameter is the new
40+
state of the button.
41+
"""
42+
pass
43+
44+
@staticmethod
45+
def on_down(state):
46+
"""
47+
This handler is called by `process()` whenever state of 'down' button
48+
has changed since last `process()` call. `state` parameter is the new
49+
state of the button.
50+
"""
51+
pass
52+
53+
@staticmethod
54+
def on_left(state):
55+
"""
56+
This handler is called by `process()` whenever state of 'left' button
57+
has changed since last `process()` call. `state` parameter is the new
58+
state of the button.
59+
"""
60+
pass
61+
62+
@staticmethod
63+
def on_right(state):
64+
"""
65+
This handler is called by `process()` whenever state of 'right' button
66+
has changed since last `process()` call. `state` parameter is the new
67+
state of the button.
68+
"""
69+
pass
70+
71+
@staticmethod
72+
def on_enter(state):
73+
"""
74+
This handler is called by `process()` whenever state of 'enter' button
75+
has changed since last `process()` call. `state` parameter is the new
76+
state of the button.
77+
"""
78+
pass
79+
80+
@staticmethod
81+
def on_backspace(state):
82+
"""
83+
This handler is called by `process()` whenever state of 'backspace' button
84+
has changed since last `process()` call. `state` parameter is the new
85+
state of the button.
86+
"""
87+
pass
88+
89+
def up(self):
90+
"""
91+
Check if 'up' button is pressed.
92+
"""
93+
return 'up' in self.buttons_pressed
94+
95+
@property
96+
def down(self):
97+
"""
98+
Check if 'down' button is pressed.
99+
"""
100+
return 'down' in self.buttons_pressed
101+
102+
@property
103+
def left(self):
104+
"""
105+
Check if 'left' button is pressed.
106+
"""
107+
return 'left' in self.buttons_pressed
108+
109+
@property
110+
def right(self):
111+
"""
112+
Check if 'right' button is pressed.
113+
"""
114+
return 'right' in self.buttons_pressed
115+
116+
@property
117+
def enter(self):
118+
"""
119+
Check if 'enter' button is pressed.
120+
"""
121+
return 'enter' in self.buttons_pressed
122+
123+
@property
124+
def backspace(self):
125+
"""
126+
Check if 'backspace' button is pressed.
127+
"""
128+
return 'backspace' in self.buttons_pressed

ev3dev/pistorms.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
"""
3+
An assortment of classes modeling specific features of the PiStorms.
4+
"""
5+
6+
from .core import *
7+
8+
OUTPUT_A = 'pistorms:BBM1'
9+
OUTPUT_B = 'pistorms:BBM2'
10+
OUTPUT_C = 'pistorms:BAM2'
11+
OUTPUT_D = 'pistorms:BAM1'
12+
13+
INPUT_1 = 'pistorms:BBS1'
14+
INPUT_2 = 'pistorms:BBS2'
15+
INPUT_3 = 'pistorms:BAS2'
16+
INPUT_4 = 'pistorms:BAS1'

0 commit comments

Comments
 (0)