Added lirc.
[irreco.git] / lirc-0.8.4a / tools / smode2.c
blob9ce58114c9c2e6ce7cc6ae9330ad4df85ccdc912
1 /* $Id: smode2.c,v 5.12 2003/03/30 12:26:44 lirc Exp $ */
3 /****************************************************************************
4 ** smode2.c ****************************************************************
5 ****************************************************************************
7 * smode2 - shows the ir waveform of an IR signal
9 * Copyright (C) 1998.11.18 Sinkovics Zoltan <sinko@szarvas.hu>
11 * This program is based on the mode2.c file which is a part of the
12 * LIRC distribution. The main purpose of this program is to check
13 * operation of LIRC receiver hardware, and to see the IR waveform of
14 * the remote controller without an expensive oscilloscope. The time
15 * division is variable from 1 ms/div to extremely high values (integer
16 * type) but there is no point increasing this value above 20 ms/div,
17 * because one pulse is about 1 ms. I think this kind of presentation
18 * is much more exciting than the simple pulse&space output showed by
19 * mode2.
21 * Usage: smode2 [-t (ms/div)] , default division is 5 ms/div
22 * */
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <getopt.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/ioctl.h>
37 #include <vga.h>
38 #include <vgagl.h>
40 #include "drivers/lirc.h"
42 GraphicsContext *screen;
43 GraphicsContext *physicalscreen;
44 GraphicsContext *backscreen;
46 void initscreen(void)
48 int vgamode;
50 vga_init();
52 vgamode = G640x480x16;
54 if (!vga_hasmode(vgamode)) {
55 printf("Mode not available.\n");
56 exit(-1);
58 vga_setmode(vgamode);
60 /* Create virtual screen. */
61 gl_setcontextvgavirtual(vgamode);
62 backscreen = gl_allocatecontext();
63 gl_getcontext(backscreen);
65 /* Physical screen context. */
66 vga_setmode(vgamode);
67 gl_setcontextvga(vgamode);
68 physicalscreen = gl_allocatecontext();
69 gl_getcontext(physicalscreen);
71 gl_setcontext(backscreen);
72 /*drawgraypalette();*/
74 gl_clearscreen(0);
76 // gl_setcontextvga(vgamode);
77 gl_enableclipping();
78 gl_setclippingwindow(0,0,639,479);
79 gl_setwritemode(WRITEMODE_OVERWRITE | FONT_COMPRESSED);
80 gl_setfont(8, 8, gl_font8x8);
81 gl_setfontcolors(0, 1) ;
84 void closescreen(void)
86 vga_setmode(TEXT);
89 int main(int argc, char **argv)
91 int fd;
92 unsigned long mode;
93 lirc_t data;
94 lirc_t x1,y1,x2,y2;
95 int result;
96 int c=10;
97 char textbuffer[80];
98 int div=5;
99 char *device=LIRC_DRIVER_DEVICE;
100 char *progname;
101 struct stat s;
103 progname="smode2";
104 while(1)
106 int c;
107 static struct option long_options[] =
109 {"help",no_argument,NULL,'h'},
110 {"version",no_argument,NULL,'v'},
111 {"device",required_argument,NULL,'d'},
112 {"timediv",required_argument,NULL,'t'},
113 {0, 0, 0, 0}
115 c = getopt_long(argc,argv,"hvd:t:",long_options,NULL);
116 if(c==-1)
117 break;
118 switch (c)
120 case 'h':
121 printf("Usage: %s [options]\n",progname);
122 printf("\t -h --help\t\tdisplay usage summary\n");
123 printf("\t -v --version\t\tdisplay version\n");
124 printf("\t -d --device=device\tread from given device\n");
125 printf("\t -t --timediv=value\tms per unit\n");
126 return(EXIT_SUCCESS);
127 case 'v':
128 printf("%s %s\n",progname, VERSION);
129 return(EXIT_SUCCESS);
130 case 'd':
131 device=optarg;
132 break;
133 case 't': /* timediv */
134 div = strtol(optarg,NULL,10);
135 break;
136 default:
137 printf("Usage: %s [options]\n",progname);
138 return(EXIT_FAILURE);
141 if (optind < argc-1)
143 fprintf(stderr,"%s: too many arguments\n",progname);
144 return(EXIT_FAILURE);
147 fd=open(device,O_RDONLY);
148 if(fd==-1) {
149 perror(progname);
150 fprintf(stderr,"%s: error opening %s\n",progname,device);
151 exit(EXIT_FAILURE);
153 if ( (fstat(fd,&s)!=-1) && (S_ISFIFO(s.st_mode)) )
155 /* can't do ioctls on a pipe */
157 else if(ioctl(fd,LIRC_GET_REC_MODE,&mode)==-1 || mode!=LIRC_MODE_MODE2)
159 printf("This program is only intended for receivers "
160 "supporting the pulse/space layer.\n");
161 printf("Note that this is no error, but this program simply "
162 "makes no sense for your\nreceiver.\n");
163 printf("In order to test your setup run lircd with the "
164 "--nodaemon option and \n"
165 "then check if the remote works with the irw tool.\n");
166 close(fd);
167 exit(EXIT_FAILURE);
170 initscreen();
172 y1=20;
173 x1=x2=0;
174 for (y2=0;y2<640;y2+=20) gl_line(y2,0,y2,480,1);
175 sprintf(textbuffer,"%d ms/unit",div);
176 gl_write(500,10,textbuffer);
177 gl_copyscreen(physicalscreen);
179 while(1)
181 result=read(fd,&data,sizeof(data));
182 if (result==sizeof(data))
184 // printf("%.8lx\t",(unsigned long) data);
185 x2=(data&PULSE_MASK)/(div*50);
186 if (x2>400)
188 y1+=15;
189 x1=0;
190 gl_copyscreen(physicalscreen);
192 else
194 if (x1<640)
196 gl_line(x1, ((data&PULSE_BIT)?y1:y1+10), x1+x2, ((data&PULSE_BIT)?y1:y1+10), c) ;
197 x1+=x2;
198 gl_line(x1, ((data&PULSE_BIT)?y1:y1+10), x1, ((data&PULSE_BIT)?y1+10:y1), c) ;
201 if (y1>480)
203 y1=20;
204 gl_clearscreen(0);
205 for (y2=0;y2<640;y2+=10) gl_line(y2,0,y2,480,1);
206 gl_write(500,10,textbuffer);
209 // gl_copyscreen(physicalscreen);
211 closescreen();
212 exit(EXIT_SUCCESS);