From 0b7a29d92ef3656b4c740b313f901a9d62f593a3 Mon Sep 17 00:00:00 2001 From: Michael Gernoth Date: Sun, 6 Jun 2010 14:38:44 +0200 Subject: [PATCH 1/1] move command functions in own file --- .gitignore | 1 + Makefile | 2 +- commands.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++ commands.h | 3 + rigol.c | 160 +------------------------------------------------- 5 files changed, 173 insertions(+), 160 deletions(-) create mode 100644 commands.c create mode 100644 commands.h diff --git a/.gitignore b/.gitignore index 1a30e02..79d323f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ rigol rigol.o png.o usbtmc.o +commands.o screen_*.png diff --git a/Makefile b/Makefile index e5415d4..d499492 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CFILES=rigol.c -OFILES=rigol.o png.o usbtmc.o +OFILES=rigol.o png.o usbtmc.o commands.o CFLAGS=-O2 -Wall LDFLAGS=-lusb -lreadline -lz CC=gcc diff --git a/commands.c b/commands.c new file mode 100644 index 0000000..f5b3973 --- /dev/null +++ b/commands.c @@ -0,0 +1,167 @@ +#include +#include +#include +#include +#include +#include + +#include "png.h" +#include "usbtmc.h" +#include "commands.h" + +void do_plot (struct usb_dev_handle *sc) +{ + unsigned char ch1[1024], ch2[1024]; + int i, l; + + static FILE *gnuplot=NULL; + FILE *fp; + + l = usbtmc_sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024); + + if (l != 1024) { + printf ("hmm. didnt' get 1024 bytes. \n"); + } + + l = usbtmc_sendscpi(sc, ":WAV:DATA? CHANNEL2", ch2, 1024); + + if (l != 1024) { + printf ("hmm. didnt' get 1024 bytes. \n"); + } + + 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); + + fprintf (gnuplot, "plot 'temp.dat' using 1 with lines, 'temp.dat' using 2 with lines\n"); + fflush (gnuplot); +} + + +#define ERROR -1e100 + +static double get_float_from_scope (struct usb_dev_handle *sc, char *var) +{ + unsigned char buf[1024]; + double temp; + int l; + + l = usbtmc_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; + char buf[1024]; + unsigned char ch1[1024]; + unsigned char data[512*1024]; + double sampfreq; + + usbtmc_sendscpi (sc, ":STOP", NULL, 0); + + sampfreq = get_float_from_scope (sc, ":ACQ:SAMP?"); + + printf ("Got sampling freq: %g\n", sampfreq); + + sprintf (buf, ":TIM:SCAL %.15f", 50 / sampfreq); + printf ("sending scale cmd: %s\n", buf); + usbtmc_sendscpi (sc, buf, NULL, 0); + + 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); + usbtmc_sendscpi (sc, buf, NULL, 0); + + l = usbtmc_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); + + fp = fopen ("ch1.dump", "w"); + fwrite (data, bp, 1, fp); + fclose (fp); + + usbtmc_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 ret; + int l; + int fd; + pid_t display; + + /* Hide "RMT" from screen */ + l = usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0); + usleep(20000); + + l = usbtmc_sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen)); + + if (l != sizeof(screen)) { + printf ("hmm. didnt' get %d bytes, but %d\n\n", sizeof(screen), l); + } + + png = lcd2png(screen, &imglen); + + lt = time(NULL); + strftime(filename, sizeof(filename), "screen_%Y%m%d-%H%M%S.png", localtime(<)); + fd=open(filename, O_CREAT|O_WRONLY, 0644); + if (fd == -1) { + perror("open"); + exit(EXIT_FAILURE); + } + + while(imglen > 0) { + ret = write(fd, png, imglen); + if (ret == -1) { + perror("write"); + exit(EXIT_FAILURE); + } + imglen -= ret; + } + close(fd); + free(png); + + 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; + } +} + diff --git a/commands.h b/commands.h new file mode 100644 index 0000000..67afc00 --- /dev/null +++ b/commands.h @@ -0,0 +1,3 @@ +void do_plot (struct usb_dev_handle *sc); +void do_get_buf (struct usb_dev_handle *sc); +void do_get_screen(struct usb_dev_handle *sc); diff --git a/rigol.c b/rigol.c index ebf01fb..22a5cea 100644 --- a/rigol.c +++ b/rigol.c @@ -24,8 +24,7 @@ rmmod uhci_hcd; modprobe uhci_hcd #include #include "usbtmc.h" -#include "png.h" - +#include "commands.h" #define MIN(a,b) (((a)<(b))?(a):(b)) @@ -85,163 +84,6 @@ void add_history (char *buf) } #endif - -void do_plot (struct usb_dev_handle *sc) -{ - unsigned char ch1[1024], ch2[1024]; - int i, l; - - static FILE *gnuplot=NULL; - FILE *fp; - - l = usbtmc_sendscpi(sc, ":WAV:DATA? CHANEL1", ch1, 1024); - - if (l != 1024) { - printf ("hmm. didnt' get 1024 bytes. \n"); - } - - l = usbtmc_sendscpi(sc, ":WAV:DATA? CHANNEL2", ch2, 1024); - - if (l != 1024) { - printf ("hmm. didnt' get 1024 bytes. \n"); - } - - 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); - - fprintf (gnuplot, "plot 'temp.dat' using 1 with lines, 'temp.dat' using 2 with lines\n"); - fflush (gnuplot); -} - - -#define ERROR -1e100 - -double get_float_from_scope (struct usb_dev_handle *sc, char *var) -{ - unsigned char buf[1024]; - double temp; - int l; - - l = usbtmc_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; - char buf[1024]; - unsigned char ch1[1024]; - unsigned char data[512*1024]; - double sampfreq; - - usbtmc_sendscpi (sc, ":STOP", NULL, 0); - - sampfreq = get_float_from_scope (sc, ":ACQ:SAMP?"); - - printf ("Got sampling freq: %g\n", sampfreq); - - sprintf (buf, ":TIM:SCAL %.15f", 50 / sampfreq); - printf ("sending scale cmd: %s\n", buf); - usbtmc_sendscpi (sc, buf, NULL, 0); - - 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); - usbtmc_sendscpi (sc, buf, NULL, 0); - - l = usbtmc_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); - - fp = fopen ("ch1.dump", "w"); - fwrite (data, bp, 1, fp); - fclose (fp); - - usbtmc_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 ret; - int l; - int fd; - pid_t display; - - /* Hide "RMT" from screen */ - l = usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0); - usleep(20000); - - l = usbtmc_sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen)); - - if (l != sizeof(screen)) { - printf ("hmm. didnt' get %d bytes, but %d\n\n", sizeof(screen), l); - } - - png = lcd2png(screen, &imglen); - - lt = time(NULL); - strftime(filename, sizeof(filename), "screen_%Y%m%d-%H%M%S.png", localtime(<)); - fd=open(filename, O_CREAT|O_WRONLY, 0644); - if (fd == -1) { - perror("open"); - exit(EXIT_FAILURE); - } - - while(imglen > 0) { - ret = write(fd, png, imglen); - if (ret == -1) { - perror("write"); - exit(EXIT_FAILURE); - } - imglen -= ret; - } - close(fd); - free(png); - - 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; -- 2.39.5