/*
* Sample IR user level program
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include "ir.h"
/* ioctl commands for user level applications*/
#define IR_IOC_MAGIC 'I'
#define IR_IOCSETREPEATKEYS _IO(IR_IOC_MAGIC, 0)
#define IR_IOCGETREPEATKEYS _IO(IR_IOC_MAGIC, 1)
#define IR_IOCSETWAITPERIOD _IO(IR_IOC_MAGIC, 2)
#define IR_IOCGETWAITPERIOD _IO(IR_IOC_MAGIC, 3)
/* IR device node and file handle */
static const char *dev = "/dev/ir";
int fno = -1;
unsigned long repeatkey[] = {0xeb14e608,0xe916e608,0xe21de608,0xee11e608};
int ir_get_repetition_keys(unsigned int *num_keys, unsigned long *keys)
{
unsigned long buf[256];
int res;
res = ioctl(fno, IR_IOCGETREPEATKEYS, (unsigned long)buf);
if (res == 0)
{
if (buf[0] < *num_keys)
*num_keys = buf[0]; /* # of kets and followed bt the keys */
else
printf("Got %ld auto-repeat keys, can report only %d\n", buf[0], *num_keys);
memcpy(keys, &buf[1], sizeof(unsigned long) * *num_keys);
}
return res;
}
int ir_set_repetition_keys(unsigned int num_keys, unsigned long *keys)
{
unsigned long buf[256];
int res;
buf[0] = num_keys;
memcpy(&buf[1], keys, sizeof(unsigned long) * buf[0]);
res = ioctl(fno, IR_IOCSETREPEATKEYS, (unsigned long)buf);
return res;
}
int openkey(void)
{
unsigned flags = O_RDONLY;
if ((fno = open(dev, flags)) < 0)
{
printf("error opening %s\n", dev);
return 0;
}
return 1;
}
int closekey(void)
{
if(fno)
{
close(fno);
fno = -1;
}
return 0;
}
unsigned long getkey(void)
{
unsigned long l = 0;
fd_set rfds;
struct timeval tv;
int ret;
FD_ZERO(&rfds);
FD_SET(fno, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 10000;
ret = select(fno + 1, &rfds, NULL, NULL, &tv);
if (read(fno, &l, sizeof(unsigned long)) > 0)
return l;
else
return 0;
}
int main(int argc, char *argv[])
{
unsigned long key;
if(!openkey())
return 0;
ir_set_repetition_keys(4,repeatkey);
while(1)
{
key=getkey();
fprintf(stderr,"key = 0x%x\n\n\n\n",key);
}
closekey();
return 0;
}
评论0