move command functions in own file
[rigol] / commands.c
CommitLineData
0b7a29d9
MG
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
12void 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
49static 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
64void 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
109void do_get_screen(struct usb_dev_handle *sc)
110{
111 unsigned char screen[320*234];
112 time_t lt;
113 char filename[256];
114 unsigned char *png;
115 int imglen;
116 int ret;
117 int l;
118 int fd;
119 pid_t display;
120
121 /* Hide "RMT" from screen */
122 l = usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0);
123 usleep(20000);
124
125 l = usbtmc_sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen));
126
127 if (l != sizeof(screen)) {
128 printf ("hmm. didnt' get %d bytes, but %d\n\n", sizeof(screen), l);
129 }
130
131 png = lcd2png(screen, &imglen);
132
133 lt = time(NULL);
134 strftime(filename, sizeof(filename), "screen_%Y%m%d-%H%M%S.png", localtime(&lt));
135 fd=open(filename, O_CREAT|O_WRONLY, 0644);
136 if (fd == -1) {
137 perror("open");
138 exit(EXIT_FAILURE);
139 }
140
141 while(imglen > 0) {
142 ret = write(fd, png, imglen);
143 if (ret == -1) {
144 perror("write");
145 exit(EXIT_FAILURE);
146 }
147 imglen -= ret;
148 }
149 close(fd);
150 free(png);
151
152 printf("Waveform saved as %s\n", filename);
153
154 display = fork();
155 switch(display) {
156 case 0:
157 execlp("display", "display", filename, NULL);
158 exit(0);
159 break;
160 case -1:
161 perror("fork");
162 break;
163 default:
164 break;
165 }
166}
167
Impressum, Datenschutz