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