Other classes

Remote Control

class ev3dev.core.RemoteControl(sensor=None, channel=1)

EV3 Remote Controller

Event handlers

These will be called when state of the corresponding button is changed:

on_red_up
on_red_down
on_blue_up
on_blue_down
on_beacon

Member functions and properties

any()

Checks if any button is pressed.

beacon

Checks if beacon button is pressed.

blue_down

Checks if blue_down button is pressed.

blue_up

Checks if blue_up button is pressed.

buttons_pressed

Returns list of currently pressed buttons.

check_buttons(buttons=[])

Check if currently pressed buttons exactly match the given list.

on_beacon = None

Handles Beacon events.

on_blue_down = None

Handles Blue Down events.

on_blue_up = None

Handles Blue Up events.

static on_change(changed_buttons)

This handler is called by process() whenever state of any button has changed since last process() call. changed_buttons is a list of tuples of changed button names and their states.

on_red_down = None

Handles Red Down events.

on_red_up = None

Handles Red Up events.

process()

Check for currenly pressed buttons. If the new state differs from the old state, call the appropriate button event handlers.

red_down

Checks if red_down button is pressed.

red_up

Checks if red_up button is pressed.

Beacon Seeker

class ev3dev.core.BeaconSeeker(sensor=None, channel=1)

Seeks EV3 Remote Controller in beacon mode.

distance

Returns distance (0, 100) to the beacon on the given channel. Returns -128 when beacon is not found.

heading

Returns heading (-25, 25) to the beacon on the given channel.

heading_and_distance

Returns heading and distance to the beacon on the given channel as a tuple.

Button

class ev3dev.ev3.Button

EV3 Buttons

Event handlers

These will be called when state of the corresponding button is changed:

on_up
on_down
on_left
on_right
on_enter
on_backspace

Member functions and properties

any()

Checks if any button is pressed.

backspace

Check if ‘backspace’ button is pressed.

buttons_pressed

Returns list of names of pressed buttons.

check_buttons(buttons=[])

Check if currently pressed buttons exactly match the given list.

down

Check if ‘down’ button is pressed.

enter

Check if ‘enter’ button is pressed.

left

Check if ‘left’ button is pressed.

static 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.

static on_change(changed_buttons)

This handler is called by process() whenever state of any button has changed since last process() call. changed_buttons is a list of tuples of changed button names and their states.

static 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.

static on_enter(state)

This handler is called by process() whenever state of ‘enter’ button has changed since last process() call. state parameter is the new state of the button.

static 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.

static 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.

static 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.

process()

Check for currenly pressed buttons. If the new state differs from the old state, call the appropriate button event handlers.

right

Check if ‘right’ button is pressed.

up

Check if ‘up’ button is pressed.

Leds

class ev3dev.core.Led(address=None, name_pattern='*', name_exact=False, **kwargs)

Any device controlled by the generic LED driver. See https://www.kernel.org/doc/Documentation/leds/leds-class.txt for more details.

brightness

Sets the brightness level. Possible values are from 0 to max_brightness.

brightness_pct

Returns led brightness as a fraction of max_brightness

delay_off

The timer trigger will periodically change the LED brightness between 0 and the current brightness setting. The off time can be specified via delay_off attribute in milliseconds.

delay_on

The timer trigger will periodically change the LED brightness between 0 and the current brightness setting. The on time can be specified via delay_on attribute in milliseconds.

max_brightness

Returns the maximum allowable brightness value.

trigger

Sets the led trigger. A trigger is a kernel based source of led events. Triggers can either be simple or complex. A simple trigger isn’t configurable and is designed to slot into existing subsystems with minimal additional code. Examples are the ide-disk and nand-disk triggers.

Complex triggers whilst available to all LEDs have LED specific parameters and work on a per LED basis. The timer trigger is an example. The timer trigger will periodically change the LED brightness between 0 and the current brightness setting. The on and off time can be specified via delay_{on,off} attributes in milliseconds. You can change the brightness value of a LED independently of the timer trigger. However, if you set the brightness value to 0 it will also disable the timer trigger.

triggers

Returns a list of available triggers.

class ev3dev.ev3.Leds

The EV3 LEDs.

EV3 platform

Led groups:

LEFT
RIGHT

Colors:

RED
GREEN
AMBER
ORANGE
YELLOW

BrickPI platform

Led groups:

LED1
LED2

Colors:

BLUE
static all_off()

Turn all leds off

static set(group, **kwargs)

Set attributes for each led in group.

Example:

Leds.set(LEFT, brightness_pct=0.5, trigger='timer')
static set_color(group, color, pct=1)

Sets brigthness of leds in the given group to the values specified in color tuple. When percentage is specified, brightness of each led is reduced proportionally.

Example:

Leds.set_color(LEFT, AMBER)

Power Supply

class ev3dev.core.PowerSupply(address=None, name_pattern='*', name_exact=False, **kwargs)

A generic interface to read data from the system’s power_supply class. Uses the built-in legoev3-battery if none is specified.

max_voltage
measured_amps

The measured current that the battery is supplying (in amps)

measured_current

The measured current that the battery is supplying (in microamps)

measured_voltage

The measured voltage that the battery is supplying (in microvolts)

measured_volts

The measured voltage that the battery is supplying (in volts)

min_voltage
technology
type

Sound

class ev3dev.core.Sound

Sound-related functions. The class has only static methods and is not intended for instantiation. It can beep, play wav files, or convert text to speech.

Note that all methods of the class spawn system processes and return subprocess.Popen objects. The methods are asynchronous (they return immediately after child process was spawned, without waiting for its completion), but you can call wait() on the returned result.

Examples:

# Play 'bark.wav', return immediately:
Sound.play('bark.wav')

# Introduce yourself, wait for completion:
Sound.speak('Hello, I am Robot').wait()

# Play a small song
Sound.play_song((
    ('D4', 'e3'),
    ('D4', 'e3'),
    ('D4', 'e3'),
    ('G4', 'h'),
    ('D5', 'h')
))
static beep(args='')

Call beep command with the provided arguments (if any). See beep man page and google linux beep music for inspiration.

static get_volume(channel=None)

Gets the current sound volume by parsing the output of amixer get <channel>. If the channel is not specified, it tries to determine the default one by running amixer scontrols. If that fails as well, it uses the Playback channel, as that is the only channel on the EV3.

static play(wav_file)

Play wav file.

classmethod play_song(song, tempo=120, delay=50)

Plays a song provided as a list of tuples containing the note name and its value using music conventional notation instead of numerical values for frequency and duration.

It supports symbolic notes (e.g. A4, D#3, Gb5) and durations (e.g. q, h).

For an exhaustive list of accepted note symbols and values, have a look at the _NOTE_FREQUENCIES and _NOTE_VALUES private dictionaries in the source code.

The value can be suffixed by modifiers:

  • a divider introduced by a / to obtain triplets for instance (e.g. q/3 for a triplet of eight note)
  • a multiplier introduced by * (e.g. *1.5 is a dotted note).

Shortcuts exist for common modifiers:

  • 3 produces a triplet member note. For instance e3 gives a triplet of eight notes, i.e. 3 eight notes in the duration of a single quarter. You must ensure that 3 triplets notes are defined in sequence to match the count, otherwise the result will not be the expected one.
  • . produces a dotted note, i.e. which duration is one and a half the base one. Double dots are not currently supported.

Example:

>>> # A long time ago in a galaxy far,
>>> # far away...
>>> Sound.play_song((
>>>     ('D4', 'e3'),      # intro anacrouse
>>>     ('D4', 'e3'),
>>>     ('D4', 'e3'),
>>>     ('G4', 'h'),       # meas 1
>>>     ('D5', 'h'),
>>>     ('C5', 'e3'),      # meas 2
>>>     ('B4', 'e3'),
>>>     ('A4', 'e3'),
>>>     ('G5', 'h'),
>>>     ('D5', 'q'),
>>>     ('C5', 'e3'),      # meas 3
>>>     ('B4', 'e3'),
>>>     ('A4', 'e3'),
>>>     ('G5', 'h'),
>>>     ('D5', 'q'),
>>>     ('C5', 'e3'),      # meas 4
>>>     ('B4', 'e3'),
>>>     ('C5', 'e3'),
>>>     ('A4', 'h.'),
>>> ))

Important

Only 4/4 signature songs are supported with respect to note durations.

Args:
song (iterable[tuple(str, str)]): the song tempo (int): the song tempo, given in quarters per minute delay (int): delay in ms between notes
Returns:
subprocess.Popen: the spawn subprocess
static set_volume(pct, channel=None)

Sets the sound volume to the given percentage [0-100] by calling amixer -q set <channel> <pct>%. If the channel is not specified, it tries to determine the default one by running amixer scontrols. If that fails as well, it uses the Playback channel, as that is the only channel on the EV3.

static speak(text, espeak_opts='-a 200 -s 130')

Speak the given text aloud.

static tone(*args)

tone(tone_sequence)

Play tone sequence. The tone_sequence parameter is a list of tuples, where each tuple contains up to three numbers. The first number is frequency in Hz, the second is duration in milliseconds, and the third is delay in milliseconds between this and the next tone in the sequence.

Here is a cheerful example:

Sound.tone([
    (392, 350, 100), (392, 350, 100), (392, 350, 100), (311.1, 250, 100),
    (466.2, 25, 100), (392, 350, 100), (311.1, 250, 100), (466.2, 25, 100),
    (392, 700, 100), (587.32, 350, 100), (587.32, 350, 100),
    (587.32, 350, 100), (622.26, 250, 100), (466.2, 25, 100),
    (369.99, 350, 100), (311.1, 250, 100), (466.2, 25, 100), (392, 700, 100),
    (784, 350, 100), (392, 250, 100), (392, 25, 100), (784, 350, 100),
    (739.98, 250, 100), (698.46, 25, 100), (659.26, 25, 100),
    (622.26, 25, 100), (659.26, 50, 400), (415.3, 25, 200), (554.36, 350, 100),
    (523.25, 250, 100), (493.88, 25, 100), (466.16, 25, 100), (440, 25, 100),
    (466.16, 50, 400), (311.13, 25, 200), (369.99, 350, 100),
    (311.13, 250, 100), (392, 25, 100), (466.16, 350, 100), (392, 250, 100),
    (466.16, 25, 100), (587.32, 700, 100), (784, 350, 100), (392, 250, 100),
    (392, 25, 100), (784, 350, 100), (739.98, 250, 100), (698.46, 25, 100),
    (659.26, 25, 100), (622.26, 25, 100), (659.26, 50, 400), (415.3, 25, 200),
    (554.36, 350, 100), (523.25, 250, 100), (493.88, 25, 100),
    (466.16, 25, 100), (440, 25, 100), (466.16, 50, 400), (311.13, 25, 200),
    (392, 350, 100), (311.13, 250, 100), (466.16, 25, 100),
    (392.00, 300, 150), (311.13, 250, 100), (466.16, 25, 100), (392, 700)
    ]).wait()

tone(frequency, duration)

Play single tone of given frequency (Hz) and duration (milliseconds).

Screen

class ev3dev.core.Screen

Bases: ev3dev.core.FbMem

A convenience wrapper for the FbMem class. Provides drawing functions from the python imaging library (PIL).

clear()

Clears the screen

draw

Returns a handle to PIL.ImageDraw.Draw class associated with the screen.

Example:

screen.draw.rectangle((10,10,60,20), fill='black')
image

Returns a handle to PIL.Image class that is backing the screen. This can be accessed for blitting images to the screen.

Example:

screen.image.paste(picture, (0, 0))
shape

Dimensions of the screen.

update()

Applies pending changes to the screen. Nothing will be drawn on the screen until this function is called.

xres

Horizontal screen resolution

yres

Vertical screen resolution

Bitmap fonts

The Screen class allows to write text on the LCD using python imaging library (PIL) interface (see description of the text() method here). The ev3dev.fonts module contains bitmap fonts in PIL format that should look good on a tiny EV3 screen:

import ev3dev.fonts as fonts
screen.draw.text((10,10), 'Hello World!', font=fonts.load('luBS14'))
ev3dev.fonts.available()

Returns list of available font names.

ev3dev.fonts.load(name)

Loads the font specified by name and returns it as an instance of PIL.ImageFont class.

The following image lists all available fonts. The grid lines correspond to EV3 screen size:

_images/fonts.png

Lego Port

class ev3dev.core.LegoPort(address=None, name_pattern='*', name_exact=False, **kwargs)

The lego-port class provides an interface for working with input and output ports that are compatible with LEGO MINDSTORMS RCX/NXT/EV3, LEGO WeDo and LEGO Power Functions sensors and motors. Supported devices include the LEGO MINDSTORMS EV3 Intelligent Brick, the LEGO WeDo USB hub and various sensor multiplexers from 3rd party manufacturers.

Some types of ports may have multiple modes of operation. For example, the input ports on the EV3 brick can communicate with sensors using UART, I2C or analog validate signals - but not all at the same time. Therefore there are multiple modes available to connect to the different types of sensors.

In most cases, ports are able to automatically detect what type of sensor or motor is connected. In some cases though, this must be manually specified using the mode and set_device attributes. The mode attribute affects how the port communicates with the connected device. For example the input ports on the EV3 brick can communicate using UART, I2C or analog voltages, but not all at the same time, so the mode must be set to the one that is appropriate for the connected sensor. The set_device attribute is used to specify the exact type of sensor that is connected. Note: the mode must be correctly set before setting the sensor type.

Ports can be found at /sys/class/lego-port/port<N> where <N> is incremented each time a new port is registered. Note: The number is not related to the actual port at all - use the address attribute to find a specific port.

address

Returns the name of the port. See individual driver documentation for the name that will be returned.

driver_name

Returns the name of the driver that loaded this device. You can find the complete list of drivers in the [list of port drivers].

mode

Reading returns the currently selected mode. Writing sets the mode. Generally speaking when the mode changes any sensor or motor devices associated with the port will be removed new ones loaded, however this this will depend on the individual driver implementing this class.

modes

Returns a list of the available modes of the port.

set_device

For modes that support it, writing the name of a driver will cause a new device to be registered for that driver and attached to this port. For example, since NXT/Analog sensors cannot be auto-detected, you must use this attribute to load the correct driver. Returns -EOPNOTSUPP if setting a device is not supported.

status

In most cases, reading status will return the same value as mode. In cases where there is an auto mode additional values may be returned, such as no-device or error. See individual port driver documentation for the full list of possible values.