From 713be7a43ed11947c358fc67c11b9df7b0be204c Mon Sep 17 00:00:00 2001 From: Michael Gernoth Date: Sun, 6 Jun 2010 20:35:29 +0200 Subject: [PATCH] add abstraction for scope-access this makes adding serial support later easier --- .gitignore | 1 + Makefile | 2 +- commands.c | 37 ++++++++++++++++---------------- commands.h | 10 ++++----- rigol.c | 17 +++++++-------- rigold.c | 26 ++++++++++++----------- scope.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ scope.h | 11 ++++++++++ usbtmc.c | 4 ---- usbtmc.h | 10 ++++----- 10 files changed, 126 insertions(+), 54 deletions(-) create mode 100644 scope.c create mode 100644 scope.h diff --git a/.gitignore b/.gitignore index c3ed002..4619f5c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ rigold.o png.o usbtmc.o commands.o +scope.o screen_*.png diff --git a/Makefile b/Makefile index a5f6f55..9e33a75 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -OFILES=png.o usbtmc.o commands.o +OFILES=png.o usbtmc.o commands.o scope.o CFLAGS=-O2 -Wall -I/opt/local/include LDFLAGS=-L/opt/local/lib -lusb -lreadline -lz CC=gcc diff --git a/commands.c b/commands.c index f3dc86c..cb8dd6d 100644 --- a/commands.c +++ b/commands.c @@ -1,15 +1,16 @@ -#include #include #include #include #include #include +#include +#include #include "png.h" -#include "usbtmc.h" +#include "scope.h" #include "commands.h" -void do_plot (struct usb_dev_handle *sc) +void do_plot (struct scope *sc) { unsigned char ch1[1024], ch2[1024]; int i, l; @@ -17,13 +18,13 @@ void do_plot (struct usb_dev_handle *sc) static FILE *gnuplot=NULL; FILE *fp; - l = usbtmc_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"); } - l = usbtmc_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"); @@ -46,13 +47,13 @@ void do_plot (struct usb_dev_handle *sc) #define ERROR -1e100 -static double get_float_from_scope (struct usb_dev_handle *sc, char *var) +static double get_float_from_scope (struct scope *sc, char *var) { unsigned char buf[1024]; double temp; int l; - l = usbtmc_sendscpi(sc, var, buf, 1024); + l = sendscpi(sc, var, buf, 1024); if (l > 0) { sscanf ((char*)buf, "%lf", &temp); return temp; @@ -61,7 +62,7 @@ static double get_float_from_scope (struct usb_dev_handle *sc, char *var) } -void do_get_buf (struct usb_dev_handle *sc) +void do_get_buf (struct scope *sc) { FILE *fp; int i, j, l, bp; @@ -70,7 +71,7 @@ void do_get_buf (struct usb_dev_handle *sc) unsigned char data[512*1024]; double sampfreq; - usbtmc_sendscpi (sc, ":STOP", NULL, 0); + sendscpi (sc, ":STOP", NULL, 0); sampfreq = get_float_from_scope (sc, ":ACQ:SAMP?"); @@ -78,7 +79,7 @@ void do_get_buf (struct usb_dev_handle *sc) sprintf (buf, ":TIM:SCAL %.15f", 50 / sampfreq); printf ("sending scale cmd: %s\n", buf); - usbtmc_sendscpi (sc, buf, NULL, 0); + sendscpi (sc, buf, NULL, 0); sleep (1); @@ -86,9 +87,9 @@ void do_get_buf (struct usb_dev_handle *sc) 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); + sendscpi (sc, buf, NULL, 0); - l = usbtmc_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"); @@ -103,10 +104,10 @@ void do_get_buf (struct usb_dev_handle *sc) fwrite (data, bp, 1, fp); fclose (fp); - usbtmc_sendscpi (sc, ":TIM:OFFSET 0", NULL, 0); + sendscpi (sc, ":TIM:OFFSET 0", NULL, 0); } -unsigned char* get_lcd(struct usb_dev_handle *sc, int *imglen, int keylock) +unsigned char* get_lcd(struct scope *sc, int *imglen, int keylock) { unsigned char screen[320*234]; unsigned char *png; @@ -114,11 +115,11 @@ unsigned char* get_lcd(struct usb_dev_handle *sc, int *imglen, int keylock) if (keylock) { /* Hide "RMT" from screen */ - l = usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0); + l = sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0); usleep(30000); } - l = usbtmc_sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen)); + l = sendscpi(sc, ":LCD:DATA?", screen, sizeof(screen)); if (l != sizeof(screen)) { printf ("hmm. didnt' get %d bytes, but %d\n\n", (int)sizeof(screen), l); @@ -129,7 +130,7 @@ unsigned char* get_lcd(struct usb_dev_handle *sc, int *imglen, int keylock) return png; } -void do_get_screen(struct usb_dev_handle *sc) +void do_get_screen(struct scope *sc) { time_t lt; char filename[256]; @@ -180,7 +181,7 @@ void do_get_screen(struct usb_dev_handle *sc) } } -void do_display_screen(struct usb_dev_handle *sc) +void do_display_screen(struct scope *sc) { unsigned char *png; int imglen; diff --git a/commands.h b/commands.h index ab013dc..1900263 100644 --- a/commands.h +++ b/commands.h @@ -1,5 +1,5 @@ -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); -void do_display_screen(struct usb_dev_handle *sc); -unsigned char* get_lcd(struct usb_dev_handle *sc, int *imglen, int keylock); +void do_plot (struct scope *sc); +void do_get_buf (struct scope *sc); +void do_get_screen(struct scope *sc); +void do_display_screen(struct scope *sc); +unsigned char* get_lcd(struct scope *sc, int *imglen, int keylock); diff --git a/rigol.c b/rigol.c index 3386fbf..06ebacb 100644 --- a/rigol.c +++ b/rigol.c @@ -8,7 +8,6 @@ rmmod uhci_hcd; modprobe uhci_hcd (or alternately: use ohci_hcd) if that happens and you should be fine. */ -#include #include #include #include @@ -23,7 +22,7 @@ rmmod uhci_hcd; modprobe uhci_hcd #include #include -#include "usbtmc.h" +#include "scope.h" #include "commands.h" #define MIN(a,b) (((a)<(b))?(a):(b)) @@ -96,15 +95,15 @@ void child_reaper(int sig) int main(int argc, char **argv) { - struct usb_dev_handle *sc; + struct scope *sc; char *scpi; unsigned char *buff; int l; struct sigaction act; //Initialize scope - sc = usbtmc_initscope(); - usbtmc_claim(sc); + sc = initscope(); + claimscope(sc); buff = malloc (1024*1024); if (buff == NULL) { perror("malloc"); @@ -153,17 +152,17 @@ int main(int argc, char **argv) //printb (scpi, l+2); if (strchr (scpi, '?')) { //printf ("Expecting reply\n"); - l = usbtmc_sendscpi(sc, scpi, buff, 1024*1024); + 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=usbtmc_sendscpi(sc,scpi,NULL,0); + l=sendscpi(sc,scpi,NULL,0); } free (scpi); } - usbtmc_release(sc); - usbtmc_close(sc); + releasescope(sc); + closescope(sc); return 0; } diff --git a/rigold.c b/rigold.c index 0be124f..974a814 100644 --- a/rigold.c +++ b/rigold.c @@ -5,9 +5,9 @@ #include #include #include -#include +#include -#include "usbtmc.h" +#include "scope.h" #include "commands.h" static int send_binary(int s, char *buf, int len) @@ -32,24 +32,26 @@ static int send_text(int s, char *buf) return send_binary(s, buf, strlen(buf)); } -static void serve_index(int s) +static void serve_index(int s, struct scope *sc) { send_text(s, "HTTP/1.0 200 OK"); send_text(s, "Content-type: text/html\n\n"); - send_text(s, "Rigol DS1000\n"); + send_text(s, ""); + send_text(s, scope_idn(sc)); + send_text(s, "\n"); send_text(s, "\n"); send_text(s, "\n"); } -static void serve_lcd(int s, struct usb_dev_handle *sc) +static void serve_lcd(int s, struct scope *sc) { char buf[256]; int imglen; unsigned char *png; - usbtmc_claim(sc); + claimscope(sc); png = get_lcd(sc, &imglen, 0); - usbtmc_release(sc); + releasescope(sc); if (png == NULL) return; @@ -63,7 +65,7 @@ static void serve_lcd(int s, struct usb_dev_handle *sc) free(png); } -static void parse_request(int s, struct usb_dev_handle *sc) +static void parse_request(int s, struct scope *sc) { int ret; char buf[1024]; @@ -107,7 +109,7 @@ static void parse_request(int s, struct usb_dev_handle *sc) } while(token != NULL); if (strcmp("/", file) == 0) { - serve_index(s); + serve_index(s, sc); } else if (strcmp("/lcd.png", file) == 0) { serve_lcd(s, sc); } @@ -118,11 +120,11 @@ int main(int argc, char **argv) int sock, csock; int opt; socklen_t slen; - struct usb_dev_handle *sc; + struct scope *sc; struct sockaddr_in sin, clientsin; unsigned short port = 8088; - sc = usbtmc_initscope(); + sc = initscope(); if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); @@ -161,6 +163,6 @@ int main(int argc, char **argv) close(csock); } - usbtmc_close(sc); + closescope(sc); return 0; } diff --git a/scope.c b/scope.c new file mode 100644 index 0000000..3152243 --- /dev/null +++ b/scope.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include + +#include "usbtmc.h" +#include "scope.h" + +/* Just USB for now... */ +int sendscpi(struct scope* sc, char* cmd, unsigned char *resp, int resplen) +{ + return usbtmc_sendscpi(sc->usbdev, cmd, resp, resplen); +} + +void closescope(struct scope* sc) +{ + return usbtmc_close(sc->usbdev); +} + +void claimscope(struct scope* sc) +{ + return usbtmc_claim(sc->usbdev); +} + +void releasescope(struct scope* sc) +{ + //Disable keylock, so the user doesn't have to press the 'force'-button + sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0); + return usbtmc_release(sc->usbdev); +} + +struct scope* initscope(void) +{ + struct usb_dev_handle *usbdev; + struct scope *sc; + + usbdev = usbtmc_initscope(); + + if (!usbdev) + return NULL; + + sc = calloc(1, sizeof(struct scope)); + if (sc == NULL) { + perror("malloc"); + exit(EXIT_FAILURE); + } + + sc->usbdev = usbdev; + + claimscope(sc); + sendscpi(sc, "*IDN?", (unsigned char*)sc->idn, sizeof(sc->idn)); + releasescope(sc); + + printf("Scope found (%s)\n", sc->idn); + + return sc; +} + +char *scope_idn(struct scope *sc) +{ + return sc->idn; +} diff --git a/scope.h b/scope.h new file mode 100644 index 0000000..119c150 --- /dev/null +++ b/scope.h @@ -0,0 +1,11 @@ +struct scope { + struct usb_dev_handle *usbdev; + char idn[128]; +}; + +int sendscpi(struct scope* sc, char* cmd, unsigned char *resp, int resplen); +struct scope* initscope(void); +void closescope(struct scope* sc); +void claimscope(struct scope* sc); +void releasescope(struct scope* sc); +char *scope_idn(struct scope *sc); diff --git a/usbtmc.c b/usbtmc.c index 054e49c..8a35dd3 100644 --- a/usbtmc.c +++ b/usbtmc.c @@ -110,8 +110,6 @@ void usbtmc_claim(usb_dev_handle *sc) void usbtmc_release(usb_dev_handle *sc) { - //Disable keylock, so the user doesn't have to press the 'force'-button - usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE",NULL,0); usb_release_interface(sc, 0); } @@ -128,8 +126,6 @@ usb_dev_handle* usbtmc_initscope(void) { if (!dev) { printf("No scope found.\n"); exit(1); - } else { - printf("Scope found.\n"); } usbtmc_claim(dev); //The following code isn't really necessary, the program works diff --git a/usbtmc.h b/usbtmc.h index a5ee8c8..1ac3005 100644 --- a/usbtmc.h +++ b/usbtmc.h @@ -1,5 +1,5 @@ -int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd, unsigned char *resp, int resplen); -usb_dev_handle* usbtmc_initscope(void); -void usbtmc_close(usb_dev_handle *sc); -void usbtmc_claim(usb_dev_handle *sc); -void usbtmc_release(usb_dev_handle *sc); +int usbtmc_sendscpi(struct usb_dev_handle *dev, char* cmd, unsigned char *resp, int resplen); +struct usb_dev_handle* usbtmc_initscope(void); +void usbtmc_close(struct usb_dev_handle *sc); +void usbtmc_claim(struct usb_dev_handle *sc); +void usbtmc_release(struct usb_dev_handle *sc); -- 2.39.2