summaryrefslogtreecommitdiff
path: root/i/serialplot/serialplot.c
blob: ec412e2a824716a22584d5c6ce5eab7b232d0b3e (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*TODO : refaire un calcul de moyenne : OK
 * 	 faire un syst�me d'�chelle plus lisible : partiellement fait
 * 	 int�grer la lecture serie dans le programme : OK
 * 	 plus de protection sur l'option t :
*/

#include <SDL.h>
#include <SDL_syswm.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "serialplot_sdl.h"
#include "serialplot_io.h"
#include "serialplot_RdMch.h"

SDL_Surface *screen;

int old_value[256];
unsigned long cpt[8];
struct termios oldconf;


int
main (int argc, char **argv)
{
    int rep;
    char *cvalue = NULL;
    char *cvalue2 = NULL;
    enum R_stat etat_lect;
    opterr = 0;
    int input_flag =0; // flag de choix de l'entre 0 = stdin
    
    
    // decodeage de la ligne de commande
    while((rep= getopt (argc, argv,"hvs:t:")) != -1)
	switch (rep)
	{
		case 'h':
			printf("serialplot -h cet aide\n\n");
			printf("serialplot -s /dev/ttyS0 permet de choisir\n");
			printf("              entre le port serie /dev/ttyS0 \n");
			printf("              au lieu de l'entre standard\n");
			printf("           -t un nombre dans cet intervalle\n");
			printf("              [1-255], le nb de bit � 1\n");
			printf("              correspond au nombre de valeur\n");
			printf("              a afficher");
			printf("\nserialplot -v affiche les credits\n\n");
			exit(0);
		case 'v': 
			printf("\n%s by __--''\\_TB_/''--__\n\n",argv[0]);
			exit(0);
		case 's':
			cvalue = optarg;
			input_flag = 1;
			break;
		case 't':
			cvalue2 = optarg;
			if ((etat_lect = read_std_param(cvalue2)) == EPRINT) 
			{
				fprintf(stderr,
					"\n %s est un mauvais parametre\n\n",
					cvalue2);
				exit(-1);
			}
			printf("decode type: %d\n",conv_Rd_type(etat_lect)); 
			break;
		case '?':
			if (isprint(optopt))
				fprintf(stderr,
				"Option inconnu `-%c'.\n", 
				optopt);
			else
				fprintf(stderr,
				"\ncaractere d'option inconnu '\\x%x'.\n",
				optopt);
			exit(-1);
		default:
			abort();
	}
    static int fd_x11;
    int quit=0;
    SDL_Event event;
    /* Initialise SDL. */
    if (SDL_Init (SDL_INIT_TIMER | SDL_INIT_VIDEO) == -1) erreur ();
    atexit (SDL_Quit);
    
    /* Selectionne un mode video. */
    screen = SDL_SetVideoMode (640, 480, 0, SDL_HWSURFACE /*| SDL_FULLSCREEN*/
			       | SDL_DOUBLEBUF);
    if (!screen) erreur ();
    printf("init SDL : done\n");
    SDL_SysWMinfo info;
    SDL_VERSION(&info.version);
    if ( SDL_GetWMInfo(&info) <0 ) 
    {
	    exit(-1);
    }
    fd_x11 = ConnectionNumber(info.info.x11.display);
    printf("fd X11 defined\n");
    
    int fd_input;
    char input[8];
    if (input_flag == 1)
    {
	    /*ouvre le port serie*/
	    fd_input = init_serial(cvalue);
	    printf("init serial port : done\n");

	    //commande specifique a l'appli
	    char command[10];
	    // Not yet implemented
	    //command = "!z";
	    //write_serial(fd_input,&command);
	    //fgets(input,sizeof (input) / sizeof (input[0]),fdopen(fd_input,"r+"))
		    
	    strcpy(command,"!c");
	    write_serial(fd_input,command);

// lecture blocante donc marche pas   
//	    fgets(input,sizeof (input) / sizeof (input[0]),fdopen(fd_input,"r+"));
//	    if((input[0]=='!') && (input[1]=='C'))
//		    fprintf(stdout,"Calibration: OK\n");
//	    else
//	    {
//		    fprintf(stderr,"Erreur � la calibration\n");
//		    quit ++;
//	    }
    }
    else
	    fd_input = 0 ;
    int melt = 0;
    
    int vals[8];	// valeur � afficher par voies
    double scale[8];	// facteur d'echelle par voies
    int position[8];	// position sur la voie
    int i;
    for (i=0; i<8;i++)
    {
	    scale[i] = 1;
	    position[i] = 0;
    }
	    
    enum R_stat etat_c = E1;
    
    /* initialisation du select */
    fd_set fsdcR;
    int retval;
    FD_ZERO(&fsdcR);
    
    FILE * FILE_input = ( input_flag == 1) ? fdopen(fd_input,"r+") : stdin;
    
    while(!quit)
    {
	    FD_SET(fd_input,&fsdcR);
	    FD_SET(fd_x11,&fsdcR);
	    retval = select ( (fd_input > fd_x11) ? fd_input + 1 : fd_x11 + 1,
			    &fsdcR,
			    NULL,
			    NULL,
			    NULL
			    );
	    if (retval)
	    {
		    if (FD_ISSET(fd_x11,&fsdcR))
		    {
			    while(SDL_PollEvent(&event))
			    {
				    if(event.type == SDL_KEYDOWN)
				    {
					    Uint8 *keystate;
					    switch (event.key.keysym.sym)
					    {
						    case SDLK_ESCAPE:
						    case SDLK_q:
							    quit=1;
							    break;
						    case SDLK_m:
							    melt = !melt;
							    break;
						    case SDLK_KP_MINUS:
							    keystate = SDL_GetKeyState(NULL);	    
							    if ( keystate[SDLK_F1])
								    scale[0] /=2 ;
							    if ( keystate[SDLK_F2])
								    scale[1] /=2 ;
							    break;
						    case SDLK_KP_PLUS:
							    keystate = SDL_GetKeyState(NULL);	    
							    if ( keystate[SDLK_F1])
								    scale[0] *=2;
							    if ( keystate[SDLK_F2])
								    scale[1] *=2;
							    break;
						    case SDLK_PAGEUP:
							    keystate = SDL_GetKeyState(NULL);	    
							    if ( keystate[SDLK_F1])
								    position[0] -=5;
							    if ( keystate[SDLK_F2])
								    position[1] -=5;
							    break;
						    case SDLK_PAGEDOWN:
							    keystate = SDL_GetKeyState(NULL);	    
							    if ( keystate[SDLK_F1])
								    position[0] +=5;
							    if ( keystate[SDLK_F2])
								    position[1] +=5;
							    break; 

						    default : //printf("touche sans fonction\n");
							      break;
					    }
				    }
				    if (event.type == SDL_QUIT)
				    {
					    quit++;
				    }
			    }
		    }
		    if (FD_ISSET(fd_input,&fsdcR))
		    {
			    if (fgets (input,
						    sizeof (input) / sizeof (input[0]),
						    FILE_input ))
			    {
				    if (input[0] == '!')
				    {
					    read_line(vals,&etat_c,etat_lect,input);
				    }
			    }
		    }

	    }
	    // Etat d'affichage
	    if (etat_c == EPRINT)
	    {
		    int i;
		    i =conv_Rd_type(etat_lect);
		    draw(melt,i,vals,scale,position);
		    draw_inf(i,vals);
		    nextFrame();
		    etat_c = E1;
	    } 


/*
	    if (fgets (input, sizeof (input) / sizeof (input[0]), stdin))
	    {
		    s = input;
		    for (i = 0; i < sizeof (vals) / sizeof (vals[0]); i++)
		    {
			    vals[i] = strtod (s, &es);
			    if (es == s)
				    break;
			    s = es;
		    }

		    draw(melt, i, vals);

		    draw_inf(i, vals);

		    nextFrame();
	    }
	    */
    }
    /* ferme le port serie */
    if (input_flag ==1) close_serial(fd_input);
    printf("close\n");
    return 0;
}