]> git.zerfleddert.de Git - rigol/blobdiff - rigol.c
add png output for screenshots
[rigol] / rigol.c
diff --git a/rigol.c b/rigol.c
index fff48466edebba36171fcadde1469748c4451281..818b7eac797ce22dad13eb170611604a7d13dd71 100644 (file)
--- a/rigol.c
+++ b/rigol.c
@@ -6,164 +6,172 @@ Warning: This code can in theory fubar the communications with the scope to a
 point where the Linux USB-stack seems to get confused. Do a 
 rmmod uhci_hcd; modprobe uhci_hcd
 (or alternately: use ohci_hcd) if that happens and you should be fine.
-*/
+ */
 
 #include <usb.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <signal.h>
+#include <time.h>
 
 #include <readline/readline.h>
 #include <readline/history.h>
 
+#include "png.h"
 
-unsigned char seq=0;
 
 //This routine locates a scope by VID/PID and returns an opened handle to it.
 usb_dev_handle *find_scope() {
-    struct usb_bus *bus;
-    struct usb_device *dev=NULL;
-    usb_find_busses();
-    usb_find_devices();
-    for (bus=usb_busses; bus; bus=bus->next) {
-       for (dev=bus->devices; dev; dev=dev->next) {
-//         fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
-           if (dev->descriptor.idVendor==0x400 && dev->descriptor.idProduct==0x5dc) {
-               return usb_open(dev);
-           }
+       struct usb_bus *bus;
+       struct usb_device *dev=NULL;
+       usb_find_busses();
+       usb_find_devices();
+       for (bus=usb_busses; bus; bus=bus->next) {
+               for (dev=bus->devices; dev; dev=dev->next) {
+                       //fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
+                       if (dev->descriptor.idVendor==0x400 && dev->descriptor.idProduct==0x5dc) {
+                               return usb_open(dev);
+                       }
+               }
        }
-    }
-    return NULL;
+       return NULL;
 }
 
 //Helper-routine: Convert a little-endian 4-byte word to an int
 void int2chars(unsigned char *buff,unsigned int a) {
-    buff[3]=(a>>24)&0xff;
-    buff[2]=(a>>16)&0xff;
-    buff[1]=(a>>8)&0xff;
-    buff[0]=(a)&0xff;
+       buff[3]=(a>>24)&0xff;
+       buff[2]=(a>>16)&0xff;
+       buff[1]=(a>>8)&0xff;
+       buff[0]=(a)&0xff;
 }
 
 //Helper-routine: Convert an int to  little-endian 4-byte word
 unsigned int chars2int(unsigned char *buff) {
-    unsigned int a;
-    a=buff[3]<<24;
-    a+=buff[2]<<16;
-    a+=buff[1]<<8;
-    a+=buff[0];
-    return a;
+       unsigned int a;
+       a=buff[3]<<24;
+       a+=buff[2]<<16;
+       a+=buff[1]<<8;
+       a+=buff[0];
+       return a;
 }
 
 #define MIN(a,b) (((a)<(b))?(a):(b))
 
 inline char printable (char ch)
 {
-  if (ch < ' ') return '.';
-  if (ch > '~') return '.';
-  return ch;
+       if (ch < ' ') return '.';
+       if (ch > '~') return '.';
+       return ch;
 }
 
 //Debugging: Print a buffers contents in hex
 void printb (unsigned char *pkt, int len)
 {
-  int i, j;
-
-  for (i=0;i<len;i+= 16) {
-    printf ("%04x:  ", i);
-    for (j=0;j < MIN(len-i, 16);j++) {
-      printf (" %02x", pkt[i+j]);
-      if (j == 7) printf ("  ");
-    }
-    if (j < 7) printf ("  ");
-    for (;j<17;j++)
-      printf ("   ");
-
-    for (j=0;j < MIN(len-i, 16);j++) {
-      printf ("%c", printable (pkt[i+j]));
-    }
-    printf ("\n");
-  }
+       int i, j;
+
+       for (i=0;i<len;i+= 16) {
+               printf ("%04x:  ", i);
+               for (j=0;j < MIN(len-i, 16);j++) {
+                       printf (" %02x", pkt[i+j]);
+                       if (j == 7) printf ("  ");
+               }
+               if (j < 7) printf ("  ");
+               for (;j<17;j++)
+                       printf ("   ");
+
+               for (j=0;j < MIN(len-i, 16);j++) {
+                       printf ("%c", printable (pkt[i+j]));
+               }
+               printf ("\n");
+       }
 }
 
 //Send a scpi-command to the scope. The response goes into the buffer
 //called resp, with a size of resplen. If resp==NULL, no response
 //is requested.
-int sendscpi(usb_dev_handle *dev, unsigned char* cmd, 
-             unsigned char *resp, int resplen) {
-    char *buff;
-    int len,r,i;
-    buff=malloc(0x40);
-    seq++;
-    buff[0]=1; //func
-    buff[1]=seq; buff[2]=~seq; //nseq
-    buff[3]=0;
-    int2chars(buff+4,strlen(cmd));
-    buff[8]=1;
-    buff[9]=0x37;
-    buff[10]=0x39;
-    buff[11]=0x39;
-    //    fprintf(stderr,"Writing header len=%d\n", strlen (cmd));
-//    printb(buff,12);
-    r=usb_bulk_write(dev,1,buff,12,1000);
-//    fprintf(stderr,"%i bytes written. Writing cmd\n",r);
-//    printb(cmd,strlen(cmd));
-    r=usb_bulk_write(dev,1,cmd,strlen(cmd),1000);
-//    fprintf(stderr,"%i bytes written.\n",r);
-    if (resp != NULL && resplen != 0) {
-       //send read command
-       buff[0]=2; //func
+int sendscpi(usb_dev_handle *dev, char* cmd, 
+               unsigned char *resp, int resplen) {
+       unsigned char *buff;
+       int len,r,i;
+       int cmdlen = strlen(cmd);
+       static unsigned char seq=0;
+
+
+       buff=malloc(0x40);
        seq++;
+       buff[0]=1; //func
        buff[1]=seq; buff[2]=~seq; //nseq
-       int2chars(buff+4,0x40);
+       buff[3]=0;
+       int2chars(buff+4, cmdlen);
        buff[8]=1;
-       buff[9]=0xA;
-       buff[10]=0;
-       buff[11]=0;
-//     fprintf(stderr,"Writing resp req header\n");
-//     printb(buff,12);
-       r=usb_bulk_write(dev,1,buff,12,1000);
-//     fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
-       r=usb_bulk_read(dev,2,buff,0x40,1000);
-//     printb(buff,r);
-       len=chars2int(buff+4);
-       //      fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
-       for (i=0; i<(r-12); i++) {
-           if (i<resplen) resp[i] = buff[i+12];
-       }
-//     printb(resp,r-12);
-       if (len > 0x40-12) {
-//         fprintf(stderr," Reading response:\n");
-           if (resplen<len) len=resplen;
-           r=usb_bulk_read(dev,2,resp+(0x40-12),len-(0x40-12),1000);
-           //      fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
-           return r+(0x40-12);
+       buff[9]=0x37;
+       buff[10]=0x39;
+       buff[11]=0x39;
+       //fprintf(stderr,"Writing header len=%d\n", cmdlen);
+       //printb(buff,12);
+       r=usb_bulk_write(dev, 1, (char*)buff, 12, 1000);
+       //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
+       //printb(cmd, cmdlen);
+       r=usb_bulk_write(dev, 1, cmd, cmdlen, 1000);
+       //fprintf(stderr,"%i bytes written.\n",r);
+       if (resp != NULL && resplen != 0) {
+               //send read command
+               buff[0]=2; //func
+               seq++;
+               buff[1]=seq; buff[2]=~seq; //nseq
+               int2chars(buff+4,0x40);
+               buff[8]=1;
+               buff[9]=0xA;
+               buff[10]=0;
+               buff[11]=0;
+               //fprintf(stderr,"Writing resp req header\n");
+               //printb(buff,12);
+               r=usb_bulk_write(dev, 1, (char*)buff, 12, 1000);
+               //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
+               r=usb_bulk_read(dev, 2, (char*)buff, 0x40, 1000);
+               //printb(buff,r);
+               len=chars2int(buff+4);
+               //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
+               for (i=0; i<(r-12); i++) {
+                       if (i<resplen) resp[i] = buff[i+12];
+               }
+               //printb(resp,r-12);
+               if (len > 0x40-12) {
+                       //fprintf(stderr," Reading response:\n");
+                       if (resplen<len) len=resplen;
+                       r=usb_bulk_read(dev, 2, (char*)resp+(0x40-12), len-(0x40-12),1000);
+                       //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
+                       return r+(0x40-12);
+               }
+               return len;
        }
-       return len;
-    }
-    return 0;
+       return 0;
 }
 
 //Initialize the scope.
 void initscope(usb_dev_handle *dev) {
-    int r;
-    char buff[10];
-    usb_claim_interface(dev,0);
-    //The following code isn't really necessary, the program works
-    //OK without it too.
-    r=usb_control_msg(dev, 0xC8, 9, 0, 0, buff, 4, 1000);
-    if (r < 0) {
-        fprintf (stderr, "Error %d sending init message: %s\n", 
-               r, strerror (-r));
-        fprintf (stderr, "Do you have permission on the USB device?\n");
-       exit (1);
-    }
-    if (chars2int(buff)!=0x40004dc) {
-       fprintf(stderr,"Init: buff[%i]=%x\n",r,chars2int(buff));
-    }
-    return;
+       int r;
+       unsigned char buff[10];
+       usb_claim_interface(dev,0);
+       //The following code isn't really necessary, the program works
+       //OK without it too.
+       r=usb_control_msg(dev, 0xC8, 9, 0, 0, (char*)buff, 4, 1000);
+       if (r < 0) {
+               fprintf (stderr, "Error %d sending init message: %s\n", 
+                               r, strerror (-r));
+               fprintf (stderr, "Do you have permission on the USB device?\n");
+               exit (1);
+       }
+       if (chars2int(buff)!=0x40004dc) {
+               fprintf(stderr,"Init: buff[%i]=%x\n",r,chars2int(buff));
+       }
+       return;
 }
 
 #define HAVE_READLINE
@@ -171,21 +179,21 @@ void initscope(usb_dev_handle *dev) {
 
 char *readline (prompt)
 {
-  char *buf;
-
-  printf (prompt); 
-  fflush (stdout); 
-  buf = malloc (1024); 
-  
-  if (fgets (buf, 1023, stdin) == NULL) {
-    free (buf); 
-    return NULL; 
-  }
-  l = strlen (buf); 
-  if (buf[l-1] == '\n') {
-    buf[l] = 0;
-  }
-  return buf; 
+       char *buf;
+
+       printf (prompt); 
+       fflush (stdout); 
+       buf = malloc (1024); 
+
+       if (fgets (buf, 1023, stdin) == NULL) {
+               free (buf); 
+               return NULL; 
+       }
+       l = strlen (buf); 
+       if (buf[l-1] == '\n') {
+               buf[l] = 0;
+       }
+       return buf; 
 }
 
 void add_history (char *buf)
@@ -196,36 +204,36 @@ void add_history (char *buf)
 
 void do_plot (struct usb_dev_handle *sc)
 {
-  unsigned char ch1[1024], ch2[1024];
-  int i, l;
+       unsigned char ch1[1024], ch2[1024];
+       int i, l;
 
-  static FILE *gnuplot=NULL;
-  FILE *fp;
+       static FILE *gnuplot=NULL;
+       FILE *fp;
 
-  l = sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
+       l = sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
 
-  if (l != 1024) {
-    printf ("hmm. didnt' get 1024 bytes. \n"); 
-  }
+       if (l != 1024) {
+               printf ("hmm. didnt' get 1024 bytes. \n"); 
+       }
 
-  l = sendscpi(sc, ":WAV:DATA? CHANNEL2", ch2, 1024);
+       l = sendscpi(sc, ":WAV:DATA? CHANNEL2", ch2, 1024);
 
-  if (l != 1024) {
-    printf ("hmm. didnt' get 1024 bytes. \n"); 
-  }
+       if (l != 1024) {
+               printf ("hmm. didnt' get 1024 bytes. \n"); 
+       }
 
-  if (!gnuplot) {
-    gnuplot = popen ("gnuplot", "w"); 
-  }
+       if (!gnuplot) {
+               gnuplot = popen ("gnuplot", "w"); 
+       }
 
-  fp = fopen ("temp.dat", "w");
-  for (i=0xd4;i<0x32c;i++)
-    //  for (i=0;i<0x400;i++)
-    fprintf (fp, "%d %d\n", 255 - ch1[i], 255 - ch2[i]);
-  fclose (fp); 
+       fp = fopen ("temp.dat", "w");
+       for (i=0xd4;i<0x32c;i++)
+               //for (i=0;i<0x400;i++)
+               fprintf (fp, "%d %d\n", 255 - ch1[i], 255 - ch2[i]);
+       fclose (fp); 
 
-  fprintf (gnuplot, "plot 'temp.dat' using 1 with lines, 'temp.dat' using 2 with lines\n");
-  fflush (gnuplot);
+       fprintf (gnuplot, "plot 'temp.dat' using 1 with lines, 'temp.dat' using 2 with lines\n");
+       fflush (gnuplot);
 }
 
 
@@ -233,124 +241,196 @@ void do_plot (struct usb_dev_handle *sc)
 
 double get_float_from_scope (struct usb_dev_handle *sc, char *var)
 {
-  unsigned char buf[1024];
-  double temp;
-  int l;
-
-  l = sendscpi(sc, var, buf, 1024);
-  if (l > 0) {
-    sscanf (buf, "%lf", &temp); 
-    return temp;
-  }
-  return ERROR;
+       unsigned char buf[1024];
+       double temp;
+       int l;
+
+       l = sendscpi(sc, var, buf, 1024);
+       if (l > 0) {
+               sscanf ((char*)buf, "%lf", &temp); 
+               return temp;
+       }
+       return ERROR;
 }
 
 
 void do_get_buf (struct usb_dev_handle *sc)
 {
-  FILE *fp;
-  int i, j, l, bp;
-  unsigned char buf[1024], ch1[1024];
-  unsigned char data[512*1024];
-  double sampfreq;
+       FILE *fp;
+       int i, j, l, bp;
+       char buf[1024];
+       unsigned char ch1[1024];
+       unsigned char data[512*1024];
+       double sampfreq;
 
-  sendscpi (sc, ":STOP", NULL, 0); 
+       sendscpi (sc, ":STOP", NULL, 0); 
 
-  sampfreq = get_float_from_scope (sc, ":ACQ:SAMP?");
+       sampfreq = get_float_from_scope (sc, ":ACQ:SAMP?");
 
-  printf ("Got sampling freq: %g\n", sampfreq);  
+       printf ("Got sampling freq: %g\n", sampfreq);  
 
-  sprintf (buf, ":TIM:SCAL %.15f", 50 / sampfreq);
-  printf ("sending scale cmd: %s\n", buf); 
-  sendscpi (sc, buf, NULL, 0);
+       sprintf (buf, ":TIM:SCAL %.15f", 50 / sampfreq);
+       printf ("sending scale cmd: %s\n", buf); 
+       sendscpi (sc, buf, NULL, 0);
 
-  sleep (1);
+       sleep (1);
 
-  bp=0;
-  for (i=-254*1024;i< 254*1024;i += 600) {
-    sprintf (buf, ":TIM:OFFSET %.15f", i / sampfreq);
-    printf ("Sending offset cmd:  %s\n", buf); 
-    sendscpi (sc, buf, NULL, 0);
+       bp=0;
+       for (i=-254*1024;i< 254*1024;i += 600) {
+               sprintf (buf, ":TIM:OFFSET %.15f", i / sampfreq);
+               printf ("Sending offset cmd:  %s\n", buf); 
+               sendscpi (sc, buf, NULL, 0);
 
-    l = sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
+               l = sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024);
 
-    if (l != 1024) {
-      printf ("hmm. didnt' get 1024 bytes. \n"); 
-    }
-    
-    for (j=0;j<600;j++)
-      data[bp++] = ch1[j+0xd4];
-  }
-  printf ("Got %d bytes of data. \n", bp);
+               if (l != 1024) {
+                       printf ("hmm. didnt' get 1024 bytes. \n"); 
+               }
+
+               for (j=0;j<600;j++)
+                       data[bp++] = ch1[j+0xd4];
+       }
+       printf ("Got %d bytes of data. \n", bp);
 
-  fp = fopen ("ch1.dump", "w");
-  fwrite (data, bp, 1, fp);
-  fclose (fp); 
+       fp = fopen ("ch1.dump", "w");
+       fwrite (data, bp, 1, fp);
+       fclose (fp); 
 
-  sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
+       sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
 }
 
+void do_get_screen(struct usb_dev_handle *sc)
+{
+       unsigned char screen[320*234];
+       time_t lt;
+       char filename[256];
+       unsigned char *png;
+       int imglen;
+       int l;
+       int fd;
+       pid_t display;
+
+       /* Hide "RMT" from screen */
+       l = sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0); 
+       usleep(20000);
+
+       l = sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen));
+
+       if (l != sizeof(screen)) {
+               printf ("hmm. didnt' get %d bytes, but %d\n\n", sizeof(screen), l); 
+       }
+
+       strftime(filename, sizeof(filename), "screen_%Y%m%d-%H%M%S.png", localtime(&lt));
+       fd=open(filename, O_CREAT|O_WRONLY, 0644);
+       png = lcd2png(screen, &imglen);
+       write(fd, png, imglen);
+       close(fd);
+
+       printf("Waveform saved as %s\n", filename);
+
+       display = fork();
+       switch(display) {
+               case 0:
+                       execlp("display", "display", filename, NULL);
+                       exit(0);
+                       break;
+               case -1:
+                       perror("fork");
+                       break;
+               default:
+                       break;
+       }
+}
+
+void child_reaper(int sig)
+{
+       pid_t child;
 
+       do {
+               child = waitpid(-1, NULL, WNOHANG);
+       } while(child > 0);
+       
+}
 
 int main(int argc, char **argv) 
 {
-  struct usb_dev_handle *sc;
-  char *scpi, *buff;
-  int l;
-  //Init libusb
-  usb_init();
-  //Locate and open the scope
-  sc=find_scope();
-  if (!sc) {
-    printf("No scope found.\n");
-    exit(1);
-  } else {
-    printf("Scope found.\n");
-  }
-  //Initialize scope
-  initscope(sc);
-  buff = malloc (1024*1024); 
-  while (1) {
-    scpi = readline ("> ");
-
-    if (!scpi) break;
-    if (strlen (scpi) == 0) {
-      free (scpi); 
-      continue;
-    }
-    
-    add_history (scpi); 
-
-    if (strncmp (scpi, "quit", 4) == 0) break;
-    if (strncmp (scpi, "plot", 4) == 0) {
-      do_plot (sc);
-      continue;
-    }
-    if (strncmp (scpi, "databuf", 4) == 0) {
-      do_get_buf (sc);
-      continue;
-    }
-
-    l = strlen (scpi); 
-    //       printf ("got buf(%d): ", l); 
-    //       printb (scpi, l+2); 
-    if (strchr (scpi, '?')) {
-      //       printf ("Expecting reply\n");
-      l = sendscpi(sc, scpi, buff, 1024*1024);
-      //          printf ("Got replylen = %d.\n", l); 
-      buff[l] = 0; //zero-terminate
-      printb (buff, l);
-    } else {
-      //       printf ("No reply expected\n");
-      l=sendscpi(sc,scpi,NULL,0);
-    }
-    free (scpi);
-  }
-  //Disable keylock, so the user doesn't have to press the 'force'-button
-  l=sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0); 
-  
-  //Free up and exit
-  usb_release_interface(sc,0);
-  usb_close(sc);
-  return 0;
+       struct usb_dev_handle *sc;
+       char *scpi;
+       unsigned char *buff;
+       int l;
+       struct sigaction act;
+
+       //Init libusb
+       usb_init();
+       //Locate and open the scope
+       sc=find_scope();
+       if (!sc) {
+               printf("No scope found.\n");
+               exit(1);
+       } else {
+               printf("Scope found.\n");
+       }
+       //Initialize scope
+       initscope(sc);
+       buff = malloc (1024*1024); 
+       if (buff == NULL) {
+               perror("malloc");
+               exit(EXIT_FAILURE);
+       }
+
+       bzero(&act, sizeof(act));
+       act.sa_handler = child_reaper;
+       act.sa_flags = SA_NOCLDSTOP|SA_RESTART;
+       if (sigaction(SIGCHLD, &act, NULL) == -1) {
+               perror("sigaction");
+               exit(EXIT_FAILURE);
+       }
+
+       while (1) {
+               scpi = readline ("> ");
+
+               if (!scpi) break;
+               if (strlen (scpi) == 0) {
+                       free (scpi); 
+                       continue;
+               }
+
+               add_history (scpi); 
+
+               if (strncmp (scpi, "quit", 4) == 0) break;
+               if (strncmp (scpi, "plot", 4) == 0) {
+                       do_plot (sc);
+                       continue;
+               }
+               if (strncmp (scpi, "databuf", 7) == 0) {
+                       do_get_buf (sc);
+                       continue;
+               }
+               if (strncmp (scpi, "screen", 6) == 0) {
+                       do_get_screen (sc);
+                       continue;
+               }
+
+               l = strlen (scpi); 
+               //printf ("got buf(%d): ", l); 
+               //printb (scpi, l+2); 
+               if (strchr (scpi, '?')) {
+                       //printf ("Expecting reply\n");
+                       l = sendscpi(sc, scpi, buff, 1024*1024);
+                       //printf ("Got replylen = %d.\n", l); 
+                       buff[l] = 0; //zero-terminate
+                       printb (buff, l);
+               } else {
+                       //printf ("No reply expected\n");
+                       l=sendscpi(sc,scpi,NULL,0);
+               }
+               free (scpi);
+       }
+       //Disable keylock, so the user doesn't have to press the 'force'-button
+       l=sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0); 
+
+       //Free up and exit
+       usb_release_interface(sc,0);
+       usb_close(sc);
+       return 0;
 }
Impressum, Datenschutz