fix off by one
[rigol] / commands.c
CommitLineData
0b7a29d9
MG
1#include <stdio.h>
2#include <time.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
713be7a4
MG
6#include <unistd.h>
7#include <stdlib.h>
0b7a29d9
MG
8
9#include "png.h"
713be7a4 10#include "scope.h"
0b7a29d9
MG
11#include "commands.h"
12
713be7a4 13void do_plot (struct scope *sc)
0b7a29d9
MG
14{
15 unsigned char ch1[1024], ch2[1024];
16 int i, l;
17
18 static FILE *gnuplot=NULL;
19 FILE *fp;
20
713be7a4 21 l = sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
0b7a29d9
MG
22
23 if (l != 1024) {
24 printf ("hmm. didnt' get 1024 bytes. \n");
25 }
26
713be7a4 27 l = sendscpi(sc, ":WAV:DATA? CHANNEL2", ch2, 1024);
0b7a29d9
MG
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
713be7a4 50static double get_float_from_scope (struct scope *sc, char *var)
0b7a29d9
MG
51{
52 unsigned char buf[1024];
53 double temp;
54 int l;
55
713be7a4 56 l = sendscpi(sc, var, buf, 1024);
0b7a29d9
MG
57 if (l > 0) {
58 sscanf ((char*)buf, "%lf", &temp);
59 return temp;
60 }
61 return ERROR;
62}
63
64
713be7a4 65void do_get_buf (struct scope *sc)
0b7a29d9
MG
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
713be7a4 74 sendscpi (sc, ":STOP", NULL, 0);
0b7a29d9
MG
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);
713be7a4 82 sendscpi (sc, buf, NULL, 0);
0b7a29d9
MG
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);
713be7a4 90 sendscpi (sc, buf, NULL, 0);
0b7a29d9 91
713be7a4 92 l = sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
0b7a29d9
MG
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
713be7a4 107 sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
0b7a29d9
MG
108}
109
713be7a4 110unsigned char* get_lcd(struct scope *sc, int *imglen, int keylock)
0b7a29d9
MG
111{
112 unsigned char screen[320*234];
0b7a29d9 113 unsigned char *png;
0b7a29d9 114 int l;
0b7a29d9 115
ad9fbc05
MG
116 if (keylock) {
117 /* Hide "RMT" from screen */
713be7a4 118 l = sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0);
ad9fbc05
MG
119 usleep(30000);
120 }
0b7a29d9 121
713be7a4 122 l = sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen));
0b7a29d9
MG
123
124 if (l != sizeof(screen)) {
5ef9b719 125 printf ("hmm. didnt' get %d bytes, but %d\n\n", (int)sizeof(screen), l);
20074af7 126 return NULL;
0b7a29d9
MG
127 }
128
616d54b7
MG
129 png = lcd2png(screen, imglen);
130
131 return png;
132}
133
713be7a4 134void do_get_screen(struct scope *sc)
616d54b7
MG
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
ad9fbc05 144 png = get_lcd(sc, &imglen, 1);
616d54b7
MG
145 if (png == NULL) {
146 perror("get_lcd");
147 return;
148 }
0b7a29d9
MG
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");
616d54b7 155 return;
0b7a29d9
MG
156 }
157
158 while(imglen > 0) {
159 ret = write(fd, png, imglen);
160 if (ret == -1) {
161 perror("write");
616d54b7 162 return;
0b7a29d9
MG
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
713be7a4 185void do_display_screen(struct scope *sc)
616d54b7
MG
186{
187 unsigned char *png;
188 int imglen;
189 int ret;
190 int pipefd[2];
191 pid_t display;
192
ad9fbc05 193 png = get_lcd(sc, &imglen, 1);
616d54b7
MG
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]);
c1ec14bb 227 free(png);
616d54b7
MG
228 break;
229 }
230}
Impressum, Datenschutz