add command "screen" to save a screenshot
authorMichael Gernoth <michael@gernoth.net>
Sun, 6 Jun 2010 00:09:32 +0000 (02:09 +0200)
committerMichael Gernoth <michael@gernoth.net>
Sun, 6 Jun 2010 00:09:32 +0000 (02:09 +0200)
rigol.c

diff --git a/rigol.c b/rigol.c
index 24a4a100ba6bc4cabee94cc76c19eb776ae3bec2..e7497ac65444fa4b9c10eaaf99e09ceced8968a8 100644 (file)
--- a/rigol.c
+++ b/rigol.c
@@ -13,8 +13,12 @@ rmmod uhci_hcd; modprobe uhci_hcd
 #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>
@@ -293,7 +297,73 @@ void do_get_buf (struct usb_dev_handle *sc)
        sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
 }
 
+void do_get_screen(struct usb_dev_handle *sc)
+{
+       unsigned char screen[320*234];
+       unsigned char screen_conv[320*234*3];
+       unsigned char lut[256][3];
+       time_t lt;
+       char filename[256];
+       int i;
+       int l;
+       FILE *fp;
+       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); 
+       }
 
+       for(i = 0; i < 256; i++) {
+               lut[i][0] = ((i >> 6) * 0x55);
+               lut[i][1] = ((((i >> 3) & 7) * 0x49) >> 1);
+               lut[i][2] = (((i & 7) * 0x49) >> 1);
+       }
+
+       for(i = 0; i < sizeof(screen_conv); i += 3) {
+               screen_conv[i] = lut[screen[i/3]][0];
+               screen_conv[i+1] = lut[screen[i/3]][1];
+               screen_conv[i+2] = lut[screen[i/3]][2];
+       }
+
+       lt = time(NULL);
+       strftime(filename, sizeof(filename), "screen_%Y%m%d-%H%M%S.ppm", localtime(&lt));
+
+       fp = fopen (filename, "w");
+       fprintf(fp, "P6\n320 234\n255\n");
+       fwrite(screen_conv, sizeof(screen_conv), 1, fp);
+       fclose (fp); 
+
+       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) 
 {
@@ -301,6 +371,8 @@ int main(int argc, char **argv)
        char *scpi;
        unsigned char *buff;
        int l;
+       struct sigaction act;
+
        //Init libusb
        usb_init();
        //Locate and open the scope
@@ -314,6 +386,19 @@ int main(int argc, char **argv)
        //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 ("> ");
 
@@ -330,10 +415,14 @@ int main(int argc, char **argv)
                        do_plot (sc);
                        continue;
                }
-               if (strncmp (scpi, "databuf", 4) == 0) {
+               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); 
Impressum, Datenschutz