home *** CD-ROM | disk | FTP | other *** search
/ ABBUC Magazin 60B / ABBUC_Magazin_60_2000_ABBUC_Side_B.atr / irgdemo.c < prev    next >
C/C++ Source or Header  |  2023-02-26  |  6KB  |  279 lines

  1. /* IRG Daemon to translate IR remote functions.  Listens for
  2.  * recognized packets, and sends out other packets.
  3.  *
  4.  * Copyright 1994 David Deaven version 0.1
  5.  * Permission is granted to use, copy, and distribute this code,
  6.  * provided that the use is non-commercial.
  7.  * deaven@iastate.edu
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "irg.h"
  13. #ifdef M6502
  14. #define void int
  15. #else
  16. int start(FILE *f);
  17. int hextoc(char *s);
  18. int which(char *s, char c);
  19. void printp(char *s);
  20. int gettok(FILE *f, char *t);
  21. int iswhite(int c);
  22. void *pmalloc(size_t n);
  23. int getkey();
  24. #endif
  25.  
  26. /* These values work well with Mitsubishi, Sony, and Sharp remotes.
  27.  */
  28. int sample = 2;            /* timebase */
  29. int ton = 20, toff = 20;    /* pulse width -- sending */
  30. int repeat = 3;            /* repeat send code */
  31. int tonmin = 10, toffmax = 30;    /* pulse width -- receiving */
  32. int qtime = 100, totime = 25;    /* quiet time, timeout */
  33.  
  34. struct IRcode {
  35.   char *name;        /* user-defined string */
  36.   char *packet;        /* IRG buffer */
  37.   void *map;        /* pointer to a mapped code */
  38.   void *next;        /* next code in list */
  39. };
  40.  
  41. struct Program {
  42.   char act;        /* action type */
  43.   void *action;        /* pointer to that action */
  44.   void *nxt;        /* next action in program */
  45. };
  46.  
  47. enum acts { NONE, CODE, EXEC };
  48.  
  49. struct IRcode *vocabulary = NULL;
  50.  
  51. #define BUFLEN 16
  52. char buffer[BUFLEN];
  53.  
  54. char *digit = "0123456789abcdef";
  55.  
  56. /* Search for a packet in the known vocabulary.
  57.  */
  58. struct IRcode *identify(packet)
  59. char *packet;
  60. {
  61.   struct IRcode *p;
  62.   p = vocabulary;
  63.   while(strcmp(packet, p->packet))if((p = p->next) == NULL)return NULL;
  64.   return p;
  65. }
  66.  
  67. /* Search for a packet by name in the known vocabulary.
  68.  */
  69. struct IRcode *nameid(name)
  70. char *name;
  71. {
  72.   struct IRcode *p;
  73.   p = vocabulary;
  74.   while(strcmp(name, p->name))if((p = p->next) == NULL)return NULL;
  75.   return p;
  76. }
  77.  
  78. /* Execute a program.
  79.  */
  80. void doProg(p)
  81. struct Program *p;
  82. {
  83.   struct IRcode *irc;
  84.   printf("(execute: ");
  85.   while(p != NULL) {
  86.     switch(p->act) {
  87.       case CODE:
  88.     irc = p->action;
  89.     printf(" %s", irc->name);
  90.     IRGwrite(irc->packet);
  91.     break;
  92.       case EXEC:
  93.     printf("[no EXEC yet]\n");
  94.     break;
  95.       default:
  96.     break;
  97.     }
  98.     p = p->nxt;
  99.   }
  100.   printf(")\n");
  101. }
  102.  
  103. void main()
  104. {
  105.   int r;
  106.   struct IRcode *irc;
  107.   FILE *f;
  108.  
  109.   printf("IRG driver demo version 0.1\n");
  110.   printf("David Deaven c.1994 deaven@iastate.edu\n");
  111.  
  112.   if((f = fopen("D:IRGDEMO.RC","r")) == NULL)
  113.     fprintf(stderr, "Cannot open init file\n");
  114.   else start(f);
  115.  
  116.   printf("starting IRG\n");
  117.   IRGinit(sample, ton, toff, repeat, tonmin, toffmax, qtime, totime);
  118.  
  119.   while(1) {
  120.     if((r = getkey()) > 0) {
  121.       buffer[0] = 0xff;
  122.       buffer[1] = r;
  123.       r = 2;
  124.     } else r = IRGread(buffer, BUFLEN);
  125.     if(r) {
  126.       buffer[r] = '\0'; printp(buffer);
  127.       printf("...");
  128.       if((irc = identify(buffer)) == NULL) {
  129.         printf("unknown packet\n(copied)\n");
  130.     IRGwrite(buffer);
  131.       } else {
  132.         printf("%s\n", irc->name);
  133.     if(irc->map)doProg(irc->map);
  134.       }
  135.     }
  136.   }
  137. }
  138.  
  139. /* Read definitions from file f
  140.  */
  141. int start(f)
  142. FILE *f;
  143. {
  144.   struct IRcode *irc, *ircn, *v;
  145.   struct Program *irl, *vl;
  146.   char s[64];
  147.   int i, n;
  148.   vocabulary=NULL;
  149.   while(gettok(f, s)) {
  150.     if(strcmp(s, "def") == 0) {
  151.       if(gettok(f, s) == 0)goto eoff;
  152.       if((irc = pmalloc(sizeof(struct IRcode))) == NULL)return 3;
  153.       irc->name = strdup(s);
  154.       if(gettok(f, s) == 0)goto eoff;
  155.       for(i=n=0; i<strlen(s); i+=2) s[n++] = hextoc(s+i); s[n++] = '\0';
  156.       irc->packet = strdup(s);
  157.       irc->map = NULL;
  158.       irc->next = NULL;
  159.       printf("define %s = ", irc->name);
  160.       printp(irc->packet);
  161.       printf("\n");
  162.       if(vocabulary == NULL)vocabulary = v = irc;
  163.       else {
  164.     v->next = irc; v = irc;
  165.       }
  166.     } else if(strcmp(s, "map") == 0) {
  167.       if(gettok(f, s) == 0)goto eoff;
  168.       if((irc = nameid(s)) == NULL)goto unk;
  169.       printf("map %s to (", irc->name);
  170.       while(1) {
  171.         if(gettok(f, s) == 0)goto eoff;
  172.         if(strcmp(s, "end") == 0)break;
  173.         if((ircn = nameid(s)) == NULL)goto unk;
  174.     if((irl = pmalloc(sizeof(struct Program))) == NULL)return 3;
  175.     printf(" %s", ircn->name);
  176.         irl->action = ircn;
  177.         irl->act = CODE;
  178.         irl->nxt = NULL;
  179.         if(irc->map == NULL)irc->map = vl = irl;
  180.         else {
  181.       vl->nxt = irl; vl = irl;
  182.         }
  183.       }
  184.       printf(" )\n");
  185.     } else {
  186.       printf("input error near: %s\n", s);
  187.       return 1;
  188.     }
  189.   }
  190.   return 0;
  191. eoff:
  192.   printf("unexpected end of file\n");
  193.   return 1;
  194. unk:
  195.   printf("unknown tag %s\n", s);
  196.   return 1;
  197. }
  198.  
  199. /* Convert two hex digits to a char.
  200.  */
  201. int hextoc(s)
  202. char *s;
  203. {
  204.   int hi, lo;
  205.   hi = which(digit, s[0]);
  206.   lo = which(digit, s[1]);
  207.   return lo + (hi << 4);
  208. }
  209.  
  210. /* Determine which character in s is c
  211.  */
  212. int which(s,c)
  213. char *s;
  214. char c;
  215. {
  216.   int i;
  217.   for(i=0; s[i]!='\0'; ++i)if(s[i] == c)return i;
  218.   return -1;
  219. }
  220.  
  221. /* Print a string in hexadecimal
  222.  */
  223. void printp(s)
  224. char *s;
  225. {
  226.   while(s[0]!='\0') {
  227.     fputc(digit[s[0]>>4 & 0xf], stdout);
  228.     fputc(digit[s[0] & 0xf], stdout);
  229.     ++s;
  230.   }
  231. }
  232.  
  233. /* Read one token from f
  234.  */
  235. int gettok(f, t)
  236. FILE *f;
  237. char *t;
  238. {
  239.   int i, n;
  240.   do {
  241.     i = fgetc(f);
  242.     if(i == EOF)return 0;
  243.   } while(iswhite(i));
  244.  
  245.   t[n = 0] = i;
  246.  
  247.   do {
  248.     i=fgetc(f);
  249.     if(i == EOF)return 0;
  250.     t[++n] = i;
  251.   } while(!iswhite(i));
  252.  
  253.   t[n] = '\0';
  254.   return 1;
  255. }
  256.  
  257. /* White space on the Atari
  258.  */
  259. int iswhite(c)
  260. int c;
  261. {
  262.   if(c==' ' || c=='\t' || c==155 || c=='\n')return 1;
  263.   return 0;
  264. }
  265.  
  266. int getkey()
  267. {
  268. #ifdef M6502
  269.   int r;
  270.   char *ch;
  271.   ch = 0x2fc;
  272.   if((r = (*ch)) != 0xff) {
  273.     (*ch) = 0xff;
  274.     return r;
  275.   }
  276. #endif
  277.   return -1;
  278. }
  279.