Skip to content

Instantly share code, notes, and snippets.

@topikachu
Last active December 27, 2015 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save topikachu/7319065 to your computer and use it in GitHub Desktop.
Save topikachu/7319065 to your computer and use it in GitHub Desktop.
ev3 iic poc
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include "lms2012.h"
//The ports are designated as PORT_NUMBER-1
const char PORT = 0x0;
const int MAX_SAMPLES = 100;
int main()
{
int file,dcmfile;
IIC *pIic;
IICDAT IicDat;
int i;
int idx;
//Open the device file
if((file = open(IIC_DEVICE_NAME, O_RDWR | O_SYNC)) == -1)
{
printf("Failed to open device\n");
return -1;
}
pIic = (IIC*)mmap(0, sizeof(IIC), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, file, 0);
if (pIic == MAP_FAILED)
{
printf("Map failed\n");
return -1;
}
if ((dcmfile=open(DCM_DEVICE_NAME,O_RDWR | O_SYNC))==-1)
{
printf("Failed to open dcm device");
return -1;
}
DEVCON devcon;
devcon.Connection[PORT]=CONN_NONE;
devcon.Type[PORT]=0;
devcon.Mode[PORT]=0;
ioctl(file,IIC_SET_CONN, &devcon);
sleep(1);
devcon.Connection[PORT]=CONN_NXT_IIC;
devcon.Type[PORT]=TYPE_IIC_UNKNOWN;
devcon.Mode[PORT]=255;
ioctl(file, IIC_SET_CONN, &devcon);
sleep(1);
ioctl(file, IIC_SET_CONN, &devcon);
sleep(1);
DATA8 buf[4] = {'-','-','-','-'};
buf[PORT]='F';
write(dcmfile,buf,4);
// Setup I2C comunication
printf("Device is ready\n");
// setup mindsensor psp controller command
IicDat.Port = PORT;
IicDat.Time = 0;
IicDat.Repeat = 0;
IicDat.RdLng = -8;
IicDat.WrLng = 3;
// Set the device I2C address
IicDat.WrData[0] = 0x01;
// Specify the register that will be read (0x41 = command)
IicDat.WrData[1] = 0x41;
IicDat.WrData[2] = 0x49;
IicDat.Result=-1;
while (IicDat.Result)
{
ioctl(file,IIC_SETUP,&IicDat);
};
// read the button bits
int round;
for (round=0;round<100;round++)
{
IicDat.Port = PORT;
IicDat.Time = 0;
IicDat.Repeat = 0;
IicDat.RdLng = -8;
IicDat.WrLng = 2;
// Set the device I2C address
IicDat.WrData[0] = 0x01;
// Specify the register that will be read (0x42 = button bits map)
IicDat.WrData[1] = 0x42;
IicDat.Result=-1;
memset (IicDat.RdData,0x00,IIC_DATA_LENGTH);
while (IicDat.Result)
{
ioctl(file,IIC_SETUP,&IicDat);
};
printf("%02X\n",(unsigned)(unsigned char)IicDat.RdData[0]);
}
// Close the device file
printf("Clossing device\n");
close(dcmfile);
close(file);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment