summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchatty1999-01-19 13:06:56 +0000
committerchatty1999-01-19 13:06:56 +0000
commitd5c5e0bcd952c2b58fd02409d01b10da83872ef0 (patch)
tree09551e4362ea2286d2e6c37e111d4097e2c8f3a5
parent42349d1a03a5d67099e1e777ce91ee3846514b15 (diff)
downloadirbox-d5c5e0bcd952c2b58fd02409d01b10da83872ef0.zip
irbox-d5c5e0bcd952c2b58fd02409d01b10da83872ef0.tar.gz
irbox-d5c5e0bcd952c2b58fd02409d01b10da83872ef0.tar.bz2
irbox-d5c5e0bcd952c2b58fd02409d01b10da83872ef0.tar.xz
Changed callback mechanism
Added a callack for initialization failures.
-rw-r--r--irdev.c60
-rw-r--r--irdev.h6
2 files changed, 25 insertions, 41 deletions
diff --git a/irdev.c b/irdev.c
index 96e2feb..f0d17f0 100644
--- a/irdev.c
+++ b/irdev.c
@@ -34,17 +34,16 @@
#define CLR(n, p) ((p)[(n)/NBITS] &= ~((unsigned)1 << ((n) % NBITS)))
#define ISSET(n, p) ((p)[(n)/NBITS] & ((unsigned)1 << ((n) % NBITS)))
-char BTVUP[] = { 0xf2, 0xf0, 0xd0, 0xfc, 0xbc, 0x34 };
-
struct ir_state {
int fd;
IrEvtCallback callback;
+ IrFailCallback fail_callback;
IrTimerSetter set_timeout;
IrTimerCanceller cancel_timeout;
/* state machine stuff */
char state;
- char value[6];
+ unsigned char value[6];
void *checkinit_id;
@@ -57,30 +56,15 @@ struct ir_state {
unsigned char errorCount;/* count of bad messages for recovery */
};
-static void
-IrDefaultCallback (IrState *ir, IrEvent event, const char *value)
-{
- switch (event ) {
- case EVENT_BTN_PRESS:
- fprintf (stderr, "IR default callback EVENT_BTN_PRESS %s\n", value );
- break;
- case EVENT_BTN_RELEASE:
- fprintf (stderr, "IR default callback EVENT_BTN_PRESS %s\n", value );
- break;
- case EVENT_BTN_PRESS_TV_VOL_UP:
- fprintf (stderr, "IR default callback EVENT_BTN_PRESS_TV_VOL_UP %s\n", value );
- break;
- }
-
-}
static int IrInitDevice (IrState*);
int
-IrInit (IrState *ir, IrEvtCallback cb, IrTimerSetter st, IrTimerCanceller ct)
+IrInit (IrState *ir, IrEvtCallback cb, IrFailCallback f, IrTimerSetter st, IrTimerCanceller ct)
{
ir->callback = cb;
+ ir->fail_callback = f;
ir->set_timeout = st;
ir->cancel_timeout = ct;
return IrInitDevice (ir);
@@ -96,7 +80,9 @@ IrCheckInit (void * id, IrState *ir, int delta)
IrInitDevice (ir);
return;
} else {
- fprintf(stderr,"Ir box not responding.\n");
+ /* pas de reponse, on abandonne */
+ if (ir->fail_callback)
+ (*ir->fail_callback)(ir);
}
}
ir->checkinit_id= 0;
@@ -141,9 +127,10 @@ IrInitDevice (IrState *ir)
/* discard all unread or unwritten data */
tcflush (ir->fd, TCIOFLUSH );
-
- fprintf (stderr, "IR box initialization in progress.....\n");
+#ifdef DEBUG
+ fprintf (stderr, "IR box initialization in progress.....\n");
+#endif
dcmd[0] = 'I';
dcmd[1] = 'R';
CHECKEDWRITE(ir->fd, dcmd, sizeof(dcmd));
@@ -181,7 +168,9 @@ IrDecodeInit (IrState *ir, char ch)
case 1:
if ( ch == 'K' ) {
ir->state = 0;
+#ifdef DEBUG
fprintf (stderr,"IR box initialized OK.\n");
+#endif
IrSetup (ir);
ir->errorCount = 0;
} else {
@@ -189,6 +178,7 @@ IrDecodeInit (IrState *ir, char ch)
if (ir->errorCount++ > 5) {
/* try to reinitialize */
fprintf (stderr,"Reinitializing IR box\n");
+
IrInitDevice (ir);
}
}
@@ -204,21 +194,16 @@ IrDecodeInit (IrState *ir, char ch)
static void
IrDecodeFrame (IrState *ir, char ch)
{
- /* trame normale de 6 carateres */
- if (ir->state == 5) {
- /* fin de trame */
-#ifdef DEBUG_RECEIVE
- fprintf (stdout, "Valeur BTVUP: %s\n",BTVUP);
- fprintf (stdout, "Valeur recue: %s\n",ir->value);
+ ir->value[(int)(ir->state++)] = ch;
+
+ /* fin de trame de 6 caracteres */
+ if (ir->state == 6) {
+#ifdef DEBUG
+ fprintf (stdout, "%.3d %.3d %.3d %.3d %.3d %.3d\n", ir->value[0], ir->value[1],ir->value[2],ir->value[3],ir->value[4],ir->value[5]);
#endif
- if (strcmp (ir->value,BTVUP) == 0)
- (*ir->callback)(ir, EVENT_BTN_PRESS_TV_VOL_UP, ir->value );
- else
- (*ir->callback)(ir, EVENT_BTN_PRESS, ir->value );
+ if (ir->callback)
+ (*ir->callback)(ir, ir->value );
ir->state = 0;
- } else {
- /* un caractere de la trame */
- ir->value[(int)(ir->state++)] = ch;
}
}
@@ -235,7 +220,7 @@ IrIntr (IrState *ir)
if ((len = read(ir->fd, buf, sizeof(buf))) < 0)
return;
#ifdef DEBUG_RECEIVE
- fprintf(stderr, "receive %d bytes from device \n",len);
+ fprintf (stderr, "received %d bytes from device \n",len);
for ( i = 0; i < len ; i ++ )
fprintf(stderr, "0x%02x ",buf[i]);
fprintf(stderr, "\n");
@@ -276,7 +261,6 @@ IrOpen (const char *name )
ir->state = 0;
- ir->callback = IrDefaultCallback;
return ir;
}
diff --git a/irdev.h b/irdev.h
index f8875f6..a371666 100644
--- a/irdev.h
+++ b/irdev.h
@@ -16,21 +16,21 @@
#define IR_H 1
typedef enum {
- EVENT_BTN_PRESS_TV_VOL_UP,
EVENT_BTN_PRESS,
EVENT_BTN_RELEASE
} IrEvent;
typedef struct ir_state IrState;
-typedef void (*IrEvtCallback) (IrState *ir, IrEvent event, const char *value );
+typedef void (*IrEvtCallback) (IrState *ir, const unsigned char *code );
+typedef void (*IrFailCallback) (IrState *ir);
typedef void (*IrTimerCallback) (void * id, IrState *ir, int delta );
typedef void* (*IrTimerSetter) (IrTimerCallback cb, long value, void *data );
typedef void (*IrTimerCanceller) (void *timerid );
extern IrState* IrOpen (const char *name);
extern void IrClose (IrState *ir);
-extern int IrInit (IrState *ir, IrEvtCallback cb, IrTimerSetter st, IrTimerCanceller ct);
+extern int IrInit (IrState *ir, IrEvtCallback cb, IrFailCallback f, IrTimerSetter st, IrTimerCanceller ct);
extern void IrIntr (IrState *ir);
extern int IrGetFd (IrState *ir);