Skip to content
Merged
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
40 changes: 8 additions & 32 deletions ev3dev/auto.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@

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

filename = '/tmp/current_platform'
current_platform = None
current_platform = get_current_platform()

if os.path.exists(filename):
with open(filename, 'r') as fh:
current_platform = fh.read().strip()

if not current_platform:
import platform

def get_current_platform():
"""
Guess platform we are running on
"""
machine = platform.machine()

if machine == 'armv5tejl':
return 'ev3'
elif machine == 'armv6l':
return 'brickpi'
else:
return 'unsupported'
if current_platform == 'brickpi':
from .brickpi import *

current_platform = get_current_platform()
elif current_platform == 'evb':
from .evb import *

with open(filename, 'w') as fh:
fh.write(current_platform + '\n')
elif current_platform == 'pistorms':
from .pistorms import *

if current_platform == 'brickpi':
from .brickpi import *
else:
# Import ev3 by default, so that it is covered by documentation.
from .ev3 import *
38 changes: 38 additions & 0 deletions ev3dev/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,44 @@
# update to 'running' in the "on_for_XYZ" methods of the Motor class
WAIT_RUNNING_TIMEOUT = 100


def get_current_platform():
"""
Look in /sys/class/board-info/ to determine the platform type.

This can return 'ev3', 'evb', 'pistorms', 'brickpi' or 'brickpi3'.
"""
board_info_dir = '/sys/class/board-info/'

for board in os.listdir(board_info_dir):
uevent_filename = os.path.join(board_info_dir, board, 'uevent')

if os.path.exists(uevent_filename):
with open(uevent_filename, 'r') as fh:
for line in fh.readlines():
(key, value) = line.strip().split('=')

if key == 'BOARD_INFO_MODEL':

if value == 'LEGO MINDSTORMS EV3':
return 'ev3'

elif value in ('FatcatLab EVB', 'QuestCape'):
return 'evb'

elif value == 'PiStorms':
return 'pistorms'

# This is the same for both BrickPi and BrickPi+.
# There is not a way to tell the difference.
elif value == 'Dexter Industries BrickPi':
return 'brickpi'

elif value == 'Dexter Industries BrickPi3':
return 'brickpi3'
return None


# -----------------------------------------------------------------------------
def list_device_names(class_path, name_pattern, **kwargs):
"""
Expand Down
128 changes: 128 additions & 0 deletions ev3dev/evb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@

"""
An assortment of classes modeling specific features of the EVB.
"""

from .core import *


OUTPUT_A = 'outA'
OUTPUT_B = 'outB'
OUTPUT_C = 'outC'
OUTPUT_D = 'outD'

INPUT_1 = 'in1'
INPUT_2 = 'in2'
INPUT_3 = 'in3'
INPUT_4 = 'in4'


class Button(ButtonEVIO):
"""
EVB Buttons
"""

_buttons = {
'up': {'name': '/dev/input/by-path/platform-evb-buttons-event', 'value': 103},
'down': {'name': '/dev/input/by-path/platform-evb-buttons-event', 'value': 108},
'left': {'name': '/dev/input/by-path/platform-evb-buttons-event', 'value': 105},
'right': {'name': '/dev/input/by-path/platform-evb-buttons-event', 'value': 106},
'enter': {'name': '/dev/input/by-path/platform-evb-buttons-event', 'value': 28},
'backspace': {'name': '/dev/input/by-path/platform-evb-buttons-event', 'value': 14},
}

@property
@staticmethod
def on_up(state):
"""
This handler is called by `process()` whenever state of 'up' button
has changed since last `process()` call. `state` parameter is the new
state of the button.
"""
pass

@staticmethod
def on_down(state):
"""
This handler is called by `process()` whenever state of 'down' button
has changed since last `process()` call. `state` parameter is the new
state of the button.
"""
pass

@staticmethod
def on_left(state):
"""
This handler is called by `process()` whenever state of 'left' button
has changed since last `process()` call. `state` parameter is the new
state of the button.
"""
pass

@staticmethod
def on_right(state):
"""
This handler is called by `process()` whenever state of 'right' button
has changed since last `process()` call. `state` parameter is the new
state of the button.
"""
pass

@staticmethod
def on_enter(state):
"""
This handler is called by `process()` whenever state of 'enter' button

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is this for?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is so you can do things like

def foobar():
    print("The user pressed ENTER")

btn = Button()
btn.on_enter = foobar

while True:
    btn.process()

I'm working on making this event driven as part of the buttons API work...today you have to call it in a while loop like that to make it do what you want.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, I see. Damn, Python is weird 😆 Is that a common idiom? Is there not an event framework of some sort?

Either way, I don't think it's blocking.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This project is the only place I've seen this approach used...but I've only been writing python for 4 years so who knows maybe it is wildly popular and I just haven't been exposed to it.

has changed since last `process()` call. `state` parameter is the new
state of the button.
"""
pass

@staticmethod
def on_backspace(state):
"""
This handler is called by `process()` whenever state of 'backspace' button
has changed since last `process()` call. `state` parameter is the new
state of the button.
"""
pass

def up(self):
"""
Check if 'up' button is pressed.
"""
return 'up' in self.buttons_pressed

@property
def down(self):
"""
Check if 'down' button is pressed.
"""
return 'down' in self.buttons_pressed

@property
def left(self):
"""
Check if 'left' button is pressed.
"""
return 'left' in self.buttons_pressed

@property
def right(self):
"""
Check if 'right' button is pressed.
"""
return 'right' in self.buttons_pressed

@property
def enter(self):
"""
Check if 'enter' button is pressed.
"""
return 'enter' in self.buttons_pressed

@property
def backspace(self):
"""
Check if 'backspace' button is pressed.
"""
return 'backspace' in self.buttons_pressed
16 changes: 16 additions & 0 deletions ev3dev/pistorms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

"""
An assortment of classes modeling specific features of the PiStorms.
"""

from .core import *

OUTPUT_A = 'pistorms:BBM1'
OUTPUT_B = 'pistorms:BBM2'
OUTPUT_C = 'pistorms:BAM2'
OUTPUT_D = 'pistorms:BAM1'

INPUT_1 = 'pistorms:BBS1'
INPUT_2 = 'pistorms:BBS2'
INPUT_3 = 'pistorms:BAS2'
INPUT_4 = 'pistorms:BAS1'