From d5c5e0bcd952c2b58fd02409d01b10da83872ef0 Mon Sep 17 00:00:00 2001 From: chatty Date: Tue, 19 Jan 1999 13:06:56 +0000 Subject: Changed callback mechanism Added a callack for initialization failures. --- irdev.c | 60 ++++++++++++++++++++++-------------------------------------- 1 file changed, 22 insertions(+), 38 deletions(-) (limited to 'irdev.c') 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; } -- cgit v1.1