]> git.zerfleddert.de Git - rigol/blob - commands.c
646d3dec8d6cba04e646f72e12257f37e8f3e425
[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 static unsigned char* get_lcd(struct usb_dev_handle *sc, int *imglen)
110 {
111 unsigned char screen[320*234];
112 unsigned char *png;
113 int l;
114
115 /* Hide "RMT" from screen */
116 l = usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0);
117 usleep(20000);
118
119 l = usbtmc_sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen));
120
121 if (l != sizeof(screen)) {
122 printf ("hmm. didnt' get %d bytes, but %d\n\n", sizeof(screen), l);
123 }
124
125 png = lcd2png(screen, imglen);
126
127 return png;
128 }
129
130 void do_get_screen(struct usb_dev_handle *sc)
131 {
132 time_t lt;
133 char filename[256];
134 unsigned char *png;
135 int imglen;
136 int ret;
137 int fd;
138 pid_t display;
139
140 png = get_lcd(sc, &imglen);
141 if (png == NULL) {
142 perror("get_lcd");
143 return;
144 }
145
146 lt = time(NULL);
147 strftime(filename, sizeof(filename), "screen_%Y%m%d-%H%M%S.png", localtime(&lt));
148 fd=open(filename, O_CREAT|O_WRONLY, 0644);
149 if (fd == -1) {
150 perror("open");
151 return;
152 }
153
154 while(imglen > 0) {
155 ret = write(fd, png, imglen);
156 if (ret == -1) {
157 perror("write");
158 return;
159 }
160 imglen -= ret;
161 }
162 close(fd);
163 free(png);
164
165 printf("Waveform saved as %s\n", filename);
166
167 display = fork();
168 switch(display) {
169 case 0:
170 execlp("display", "display", filename, NULL);
171 exit(0);
172 break;
173 case -1:
174 perror("fork");
175 break;
176 default:
177 break;
178 }
179 }
180
181 void do_display_screen(struct usb_dev_handle *sc)
182 {
183 unsigned char *png;
184 int imglen;
185 int ret;
186 int pipefd[2];
187 pid_t display;
188
189 png = get_lcd(sc, &imglen);
190 if (png == NULL) {
191 perror("get_lcd");
192 return;
193 }
194
195 if (pipe(pipefd) == -1) {
196 perror("pipe");
197 return;
198 }
199
200 display = fork();
201 switch(display) {
202 case 0:
203 close(pipefd[1]);
204 close(STDIN_FILENO);
205 dup2(pipefd[0], STDIN_FILENO);
206 execlp("display", "display", "-", NULL);
207 exit(0);
208 break;
209 case -1:
210 perror("fork");
211 break;
212 default:
213 close(pipefd[0]);
214 while(imglen > 0) {
215 ret = write(pipefd[1], png, imglen);
216 if (ret == -1) {
217 perror("write");
218 exit(EXIT_FAILURE);
219 }
220 imglen -= ret;
221 }
222 close(pipefd[1]);
223 break;
224 }
225 }
226
Impressum, Datenschutz