]> git.zerfleddert.de Git - rigol/blob - commands.c
update to libusb-1.0
[rigol] / commands.c
1 #include <stdio.h>
2 #include <time.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8
9 #include "png.h"
10 #include "scope.h"
11 #include "commands.h"
12
13 void do_plot (struct scope *sc)
14 {
15 unsigned char ch1[1024], ch2[1024];
16 int i, l;
17
18 static FILE *gnuplot=NULL;
19 FILE *fp;
20
21 l = sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
22
23 if (l != 1024) {
24 printf ("hmm. didnt' get 1024 bytes. \n");
25 }
26
27 l = sendscpi(sc, ":WAV:DATA? CHANNEL2", ch2, 1024);
28
29 if (l != 1024) {
30 printf ("hmm. didnt' get 1024 bytes. \n");
31 }
32
33 if (!gnuplot) {
34 gnuplot = popen ("gnuplot", "w");
35 }
36
37 fp = fopen ("temp.dat", "w");
38 for (i=0xd4;i<0x32c;i++)
39 //for (i=0;i<0x400;i++)
40 fprintf (fp, "%d %d\n", 255 - ch1[i], 255 - ch2[i]);
41 fclose (fp);
42
43 fprintf (gnuplot, "plot 'temp.dat' using 1 with lines, 'temp.dat' using 2 with lines\n");
44 fflush (gnuplot);
45 }
46
47
48 #define ERROR -1e100
49
50 static double get_float_from_scope (struct scope *sc, char *var)
51 {
52 unsigned char buf[1024];
53 double temp;
54 int l;
55
56 l = sendscpi(sc, var, buf, 1024);
57 if (l > 0) {
58 sscanf ((char*)buf, "%lf", &temp);
59 return temp;
60 }
61 return ERROR;
62 }
63
64
65 void do_get_buf (struct scope *sc)
66 {
67 FILE *fp;
68 int i, j, l, bp;
69 char buf[1024];
70 unsigned char ch1[1024];
71 unsigned char data[512*1024];
72 double sampfreq;
73
74 sendscpi (sc, ":STOP", NULL, 0);
75
76 sampfreq = get_float_from_scope (sc, ":ACQ:SAMP?");
77
78 printf ("Got sampling freq: %g\n", sampfreq);
79
80 sprintf (buf, ":TIM:SCAL %.15f", 50 / sampfreq);
81 printf ("sending scale cmd: %s\n", buf);
82 sendscpi (sc, buf, NULL, 0);
83
84 sleep (1);
85
86 bp=0;
87 for (i=-254*1024;i< 254*1024;i += 600) {
88 sprintf (buf, ":TIM:OFFSET %.15f", i / sampfreq);
89 printf ("Sending offset cmd: %s\n", buf);
90 sendscpi (sc, buf, NULL, 0);
91
92 l = sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
93
94 if (l != 1024) {
95 printf ("hmm. didnt' get 1024 bytes. \n");
96 }
97
98 for (j=0;j<600;j++)
99 data[bp++] = ch1[j+0xd4];
100 }
101 printf ("Got %d bytes of data. \n", bp);
102
103 fp = fopen ("ch1.dump", "w");
104 fwrite (data, bp, 1, fp);
105 fclose (fp);
106
107 sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
108 }
109
110 unsigned char* get_lcd(struct scope *sc, int *imglen, int keylock)
111 {
112 unsigned char screen[320*234];
113 unsigned char *png;
114 int l;
115
116 if (keylock) {
117 /* Hide "RMT" from screen */
118 l = sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0);
119 usleep(30000);
120 }
121
122 l = sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen));
123
124 if (l != sizeof(screen)) {
125 printf ("hmm. didnt' get %d bytes, but %d\n\n", (int)sizeof(screen), l);
126 return NULL;
127 }
128
129 png = lcd2png(screen, imglen);
130
131 return png;
132 }
133
134 void do_get_screen(struct scope *sc)
135 {
136 time_t lt;
137 char filename[256];
138 unsigned char *png;
139 int imglen;
140 int ret;
141 int fd;
142 pid_t display;
143
144 png = get_lcd(sc, &imglen, 1);
145 if (png == NULL) {
146 perror("get_lcd");
147 return;
148 }
149
150 lt = time(NULL);
151 strftime(filename, sizeof(filename), "screen_%Y%m%d-%H%M%S.png", localtime(&lt));
152 fd=open(filename, O_CREAT|O_WRONLY, 0644);
153 if (fd == -1) {
154 perror("open");
155 return;
156 }
157
158 while(imglen > 0) {
159 ret = write(fd, png, imglen);
160 if (ret == -1) {
161 perror("write");
162 return;
163 }
164 imglen -= ret;
165 }
166 close(fd);
167 free(png);
168
169 printf("Waveform saved as %s\n", filename);
170
171 display = fork();
172 switch(display) {
173 case 0:
174 execlp("display", "display", filename, NULL);
175 exit(0);
176 break;
177 case -1:
178 perror("fork");
179 break;
180 default:
181 break;
182 }
183 }
184
185 void do_display_screen(struct scope *sc)
186 {
187 unsigned char *png;
188 int imglen;
189 int ret;
190 int pipefd[2];
191 pid_t display;
192
193 png = get_lcd(sc, &imglen, 1);
194 if (png == NULL) {
195 perror("get_lcd");
196 return;
197 }
198
199 if (pipe(pipefd) == -1) {
200 perror("pipe");
201 return;
202 }
203
204 display = fork();
205 switch(display) {
206 case 0:
207 close(pipefd[1]);
208 close(STDIN_FILENO);
209 dup2(pipefd[0], STDIN_FILENO);
210 execlp("display", "display", "-", NULL);
211 exit(0);
212 break;
213 case -1:
214 perror("fork");
215 break;
216 default:
217 close(pipefd[0]);
218 while(imglen > 0) {
219 ret = write(pipefd[1], png, imglen);
220 if (ret == -1) {
221 perror("write");
222 exit(EXIT_FAILURE);
223 }
224 imglen -= ret;
225 }
226 close(pipefd[1]);
227 free(png);
228 break;
229 }
230 }
Impressum, Datenschutz