add abstraction for scope-access
[rigol] / rigol.c
1 /*
2 Sprite_tms hack to communicate with a Rigol DS1000-series scope using Linux.
3 This code is licensed under the GPL V3.
4
5 Warning: This code can in theory fubar the communications with the scope to a
6 point where the Linux USB-stack seems to get confused. Do a
7 rmmod uhci_hcd; modprobe uhci_hcd
8 (or alternately: use ohci_hcd) if that happens and you should be fine.
9 */
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <signal.h>
20 #include <time.h>
21
22 #include <readline/readline.h>
23 #include <readline/history.h>
24
25 #include "scope.h"
26 #include "commands.h"
27
28 #define MIN(a,b) (((a)<(b))?(a):(b))
29
30 inline char printable (char ch)
31 {
32 if (ch < ' ') return '.';
33 if (ch > '~') return '.';
34 return ch;
35 }
36
37 //Debugging: Print a buffers contents in hex
38 void printb (unsigned char *pkt, int len)
39 {
40 int i, j;
41
42 for (i=0;i<len;i+= 16) {
43 printf ("%04x: ", i);
44 for (j=0;j < MIN(len-i, 16);j++) {
45 printf (" %02x", pkt[i+j]);
46 if (j == 7) printf (" ");
47 }
48 if (j < 7) printf (" ");
49 for (;j<17;j++)
50 printf (" ");
51
52 for (j=0;j < MIN(len-i, 16);j++) {
53 printf ("%c", printable (pkt[i+j]));
54 }
55 printf ("\n");
56 }
57 }
58
59 #define HAVE_READLINE
60 #ifndef HAVE_READLINE
61
62 char *readline (prompt)
63 {
64 char *buf;
65
66 printf (prompt);
67 fflush (stdout);
68 buf = malloc (1024);
69
70 if (fgets (buf, 1023, stdin) == NULL) {
71 free (buf);
72 return NULL;
73 }
74 l = strlen (buf);
75 if (buf[l-1] == '\n') {
76 buf[l] = 0;
77 }
78 return buf;
79 }
80
81 void add_history (char *buf)
82 {
83 }
84 #endif
85
86 void child_reaper(int sig)
87 {
88 pid_t child;
89
90 do {
91 child = waitpid(-1, NULL, WNOHANG);
92 } while(child > 0);
93
94 }
95
96 int main(int argc, char **argv)
97 {
98 struct scope *sc;
99 char *scpi;
100 unsigned char *buff;
101 int l;
102 struct sigaction act;
103
104 //Initialize scope
105 sc = initscope();
106 claimscope(sc);
107 buff = malloc (1024*1024);
108 if (buff == NULL) {
109 perror("malloc");
110 exit(EXIT_FAILURE);
111 }
112
113 bzero(&act, sizeof(act));
114 act.sa_handler = child_reaper;
115 act.sa_flags = SA_NOCLDSTOP|SA_RESTART;
116 if (sigaction(SIGCHLD, &act, NULL) == -1) {
117 perror("sigaction");
118 exit(EXIT_FAILURE);
119 }
120
121 while (1) {
122 scpi = readline ("> ");
123
124 if (!scpi) break;
125 if (strlen (scpi) == 0) {
126 free (scpi);
127 continue;
128 }
129
130 add_history (scpi);
131
132 if (strncmp (scpi, "quit", 4) == 0) break;
133 if (strncmp (scpi, "plot", 4) == 0) {
134 do_plot (sc);
135 continue;
136 }
137 if (strncmp (scpi, "databuf", 7) == 0) {
138 do_get_buf(sc);
139 continue;
140 }
141 if (strncmp (scpi, "screen", 6) == 0) {
142 do_get_screen(sc);
143 continue;
144 }
145 if (strncmp (scpi, "display", 7) == 0) {
146 do_display_screen(sc);
147 continue;
148 }
149
150 l = strlen (scpi);
151 //printf ("got buf(%d): ", l);
152 //printb (scpi, l+2);
153 if (strchr (scpi, '?')) {
154 //printf ("Expecting reply\n");
155 l = sendscpi(sc, scpi, buff, 1024*1024);
156 //printf ("Got replylen = %d.\n", l);
157 buff[l] = 0; //zero-terminate
158 printb (buff, l);
159 } else {
160 //printf ("No reply expected\n");
161 l=sendscpi(sc,scpi,NULL,0);
162 }
163 free (scpi);
164 }
165 releasescope(sc);
166 closescope(sc);
167 return 0;
168 }
Impressum, Datenschutz