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