1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/* $Id: hw_creative.c,v 5.6 2003/02/16 19:00:55 lirc Exp $ */
/****************************************************************************
** hw_creative.c ***********************************************************
****************************************************************************
*
* routines for Creative receiver
*
* Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
*
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include "hardware.h"
#include "serial.h"
#include "ir_remote.h"
#include "lircd.h"
#include "hw_creative.h"
#define NUMBYTES 6
#define TIMEOUT 20000
extern struct ir_remote *repeat_remote,*last_remote;
unsigned char b[NUMBYTES];
struct timeval start,end,last;
lirc_t gap,signal_length;
ir_code pre,code;
unsigned char mapping[]=
{0xd8,
0x04,
0x38,
0xb4,
0x78,
0x98,
0xf8,
0x14,
0x84,
0x44,
0xc4,
0xf4,
0xa4,
0x64,
0xe4,
0x34,
0x94,
0x54,
0xd4,
0x0c,
0x24,
0x74,
0x58,
0xb8,
0x00};
struct hardware hw_creative=
{
LIRC_DRIVER_DEVICE, /* default device */
-1, /* fd */
LIRC_CAN_REC_LIRCCODE, /* features */
0, /* send_mode */
LIRC_MODE_LIRCCODE, /* rec_mode */
32, /* code_length */
creative_init, /* init_func */
NULL, /* config_func */
creative_deinit, /* deinit_func */
NULL, /* send_func */
creative_rec, /* rec_func */
creative_decode, /* decode_func */
NULL, /* readdata */
"creative"
};
int creative_decode(struct ir_remote *remote,
ir_code *prep,ir_code *codep,ir_code *postp,
int *repeat_flagp,lirc_t *remaining_gapp)
{
if(!map_code(remote,prep,codep,postp,
16,pre,16,code,0,0))
{
return(0);
}
gap=0;
if(start.tv_sec-last.tv_sec>=2) /* >1 sec */
{
*repeat_flagp=0;
}
else
{
gap=(start.tv_sec-last.tv_sec)*1000000+
start.tv_usec-last.tv_usec;
if(gap<remote->remaining_gap*(100+remote->eps)/100
|| gap<=remote->remaining_gap+remote->aeps)
*repeat_flagp=1;
else
*repeat_flagp=0;
}
*remaining_gapp=is_const(remote) ?
(remote->gap>signal_length ? remote->gap-signal_length:0):
remote->gap;
LOGPRINTF(1,"pre: %llx",(unsigned long long) *prep);
LOGPRINTF(1,"code: %llx",(unsigned long long) *codep);
LOGPRINTF(1,"repeat_flag: %d",*repeat_flagp);
LOGPRINTF(1,"gap: %lu",(unsigned long) gap);
LOGPRINTF(1,"rem: %lu",(unsigned long) remote->remaining_gap);
LOGPRINTF(1,"signal length: %lu",(unsigned long) signal_length);
return(1);
}
int creative_init(void)
{
signal_length=108000;
if(!tty_create_lock(hw.device))
{
logprintf(LOG_ERR,"could not create lock files");
return(0);
}
if((hw.fd=open(hw.device,O_RDWR|O_NONBLOCK|O_NOCTTY))<0)
{
logprintf(LOG_ERR,"could not open %s",hw.device);
logperror(LOG_ERR,"creative_init()");
tty_delete_lock();
return(0);
}
if(!tty_reset(hw.fd))
{
logprintf(LOG_ERR,"could not reset tty");
creative_deinit();
return(0);
}
if(!tty_setbaud(hw.fd,2400))
{
logprintf(LOG_ERR,"could not set baud rate");
creative_deinit();
return(0);
}
return(1);
}
int creative_deinit(void)
{
close(hw.fd);
tty_delete_lock();
return(1);
}
char *creative_rec(struct ir_remote *remotes)
{
char *m;
int i;
b[0]=0x4d;
b[1]=0x05;
b[4]=0xac;
b[5]=0x21;
last=end;
gettimeofday(&start,NULL);
for(i=0;i<NUMBYTES;i++)
{
if(i>0)
{
if(!waitfordata(TIMEOUT))
{
logprintf(LOG_ERR,"timeout reading byte %d",i);
return(NULL);
}
}
if(read(hw.fd,&b[i],1)!=1)
{
logprintf(LOG_ERR,"reading of byte %d failed",i);
logperror(LOG_ERR,NULL);
return(NULL);
}
if(b[0]!=0x4d ||
b[1]!=0x05 /* || b[4]!=0xac || b[5]!=0x21 */)
{
logprintf(LOG_ERR,"bad envelope");
return(NULL);
}
if(i==5)
{
if(b[2]!=((~b[3])&0xff))
{
logprintf(LOG_ERR,"bad checksum");
return(NULL);
}
}
LOGPRINTF(1,"byte %d: %02x",i,b[i]);
}
gettimeofday(&end,NULL);
/* pre=0x8435; */
pre=reverse((((ir_code) b[4])<<8) | ((ir_code) b[5]),16);
for(i=0;mapping[i]!=0x00;i++)
{
if(mapping[i]==b[3])
{
code=(ir_code) (i<<8)|((~i)&0xff);
break;
}
}
if(mapping[i]==0x00)
{
logprintf(LOG_ERR,"unknown code");
return(NULL);
}
m=decode_all(remotes);
return(m);
}
|