IR Remote

By default, the Ev3 IR remote sensor works as a light intensity measuring device. However, this sensor has five modes of operation (see type 33 in Device Types table). In order to use this device as a remote control, the device must be configured to work in mode 2, corresponding to IR-REMOTE. The following code shows how to sets the IR sensor mode for this mode of operation. The program assumes that the controller is set in channel 1 (number inside red slot) and the IR receiver is connected in port one. The output of this program is a number that gives a unique code for every button (or combination of them) being pressed on the controller side as follows:
1 = UP LEFT
2 = DOWN LEFT
3 = UP RIGHT
4 = DOWN RIGHT
5 = UP LEFT and RIGHT
6 = UP LEFT and DOWN RIGHT
7 = UP RIGHT and DOWN LEFT
8 = DOWN LEFT and RIGHT
9 = FRONT BUTTON
10 = UP and DOWN LEFT
11 = UP and DOWN RIGHT

/*
* 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 <sys/ioctl.h>
#include "lms2012.h"

//IR remote constants
const char PORT = 0x0; //EV3 label minus 1
//More information about modes can be found at http://python-ev3.org/types.html
const char IR_REMOTE_MODE = 2; // Use IR in remote control mode
const char IR_REMOTE_CONTROL_CHANNEL = 0; //Channel number in IR control unit minus 1
const char IR_SENSOR_TYPE = 33;
//Other constanst
const int MAX_SAMPLES = 100;

int main()
{
//DEFINE VARIABLES
int file;
UART *pIrRemote; //IR remote user level handler
DEVCON DevCon; // Configuration parameters
int i;

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

//SPECIAL CONFIGURATION
//Set IR as remote control
DevCon.Mode[PORT] = IR_REMOTE_MODE;
DevCon.Connection[PORT] = CONN_INPUT_UART;
DevCon.Type[PORT] = IR_SENSOR_TYPE; //This instruction has no effect in the code
ioctl(file, UART_SET_CONN, &DevCon);
printf("Device is ready \n");

//PROCESS SENSOR DATA
//Print results
for(i = 0; i<MAX_SAMPLES; i++)
{
printf("IR Remote Button: %d\n", (unsigned char)pIrRemote->Raw[PORT][pIrRemote->Actual[PORT]][IR_REMOTE_CONTROL_CHANNEL]);
sleep(1);
}

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