Buttons and LEDs

lego_ev3_buttons_leds

The EV3 buttons and LEDs are handled using a singe device file (UI_DEVICE_NAME). This is possible, since the buttons are acceded using read file operations while the LEDs are set using write file commands. The EV3 hardware has four LEDs, which could be used separate. However, the current kernel module ignores the LED number command and executes the command on two LEDs always (left and right). The two EV3 hardware components can be used simultaneously. However, for demonstration purposes I show two separate examples, one for Buttons and another one for LEDs.

It is very important to know that the if the program is run within a Linux terminal, the buttons will continue to have the normal functions as designated by the Lego Virtual Machine (LVM). In order to  disable the LVM, the program must be run as a stand alone program called from the LVM menu.


Buttons Handling

/*
* Robot Navigation Program
* www.robotnav.com
*
* (C) Copyright 2013 - 2014 Navigation Solutions, LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include
#include
#include <sys/mman.h>
#include "lms2012.h"

int main()
{
//DEFINE VARIABLES
int file;
UI *pButtons;
int i;

//INITIALIZE DEVICE
//Open the device file
if((file = open(UI_DEVICE_NAME, O_RDWR | O_SYNC)) == -1)
{
printf("Failed to open device\n");
return -1;
}
//Map kernel device to be used at the user space level
pButtons = (UI*)mmap(0, sizeof(UI), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, file, 0);
if (pButtons == MAP_FAILED)
{
printf("Failed to map device\n");
return -1;
}
printf("Device ready\n");

//DO SOMETHING WITH THE DEVICE
//Read button states
while(1)
{
for(i = 0; i<BUTTONS; i++) if(pButtons->Pressed[i])
printf("Pressed Button # %d \n", i);
//See button definitions in bytecodes.h
if(pButtons->Pressed[BACK_BUTTON - 1])
break;
}

//CLOSE DEVICE
printf("Closing device\n");
close(file);
return 0;
}

After you have compiled the program, press the buttons on the brick. Numbers will show up on your computer screen as follows:
Up:0
Center:1
Down:2
Right:3
Left:4


LEDs Handling

/*
* Robot Navigation Program
* www.robotnav.com
*
* (C) Copyright 2013 - 2014 Navigation Solutions, LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include
#include
#include <sys/mman.h>
#include "lms2012.h"

int main()
{
//DEFINE VARIABLES
int file;
UI *pLeds;
int i;
//The first byte determines the color or pattern as specified in bytecodes.h -> LEDPATTERN
//The second byte (LED number) is not currently being used by the device driver, but is needed
char led_command[2]= { 0, 0};

//INITIALIZE DEVICE
//Open the device file
if((file = open(UI_DEVICE_NAME, O_RDWR | O_SYNC)) == -1)
{
printf("Failed to open device\n");
return -1;
}
printf("Device ready\n");

//DO SOMETHING WITH THE DEVICE
//Send LED Command
while(1)
{
for(i = 0; i<LEDPATTERNS; i++)
{
printf("LED Pattern = %d\n", i);
//The kernel driver will subtract a '0' offset before using the Color/Pattern instruction (see d_ui.c)
led_command[0] = '0' + i;
write(file, led_command, 2);
sleep(1);
}
}

//CLOSE DEVICE
printf("Closing device\n");
close(file);
return 0;
}

Prev >> Sensors
Next >> Examples