From: Michael Gernoth Date: Thu, 10 Jun 2010 20:40:58 +0000 (+0200) Subject: display some state-informations on the web X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/rigol/commitdiff_plain/78fb09842aa3a0b87a4fe84a1ab93bfc7252d934?ds=sidebyside display some state-informations on the web --- diff --git a/rigold.c b/rigold.c index 111c5d6..29712e0 100644 --- a/rigold.c +++ b/rigold.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "scope.h" #include "commands.h" @@ -30,19 +31,95 @@ static int send_binary(int s, char *buf, int len) return 0; } -static int send_text(int s, char *buf) +static int send_text(int s, char *fmt, ...) { - return send_binary(s, buf, strlen(buf)); + va_list argp; + char buf[4096]; + int cnt; + + va_start(argp, fmt); + cnt = vsnprintf(buf, sizeof(buf), fmt, argp); + va_end(argp); + + if (cnt < 0) + return cnt; + + return send_binary(s, buf, cnt); +} + +static int send_command_output(int s, struct scope *sc, char *cmd) +{ + unsigned char buf[1024*1024]; + int res; + + res = sendscpi(sc, cmd, buf, sizeof(buf)); + send_binary(s, (char*)buf, res); + + return res; } static void serve_index(int s, struct scope *sc, char *param) { send_text(s, "HTTP/1.0 200 OK\n"); send_text(s, "Content-type: text/html\n\n"); - send_text(s, ""); - send_text(s, scope_idn(sc)); - send_text(s, "\n"); + send_text(s, "%s\n", scope_idn(sc)); send_text(s, "\n"); + send_text(s, "
\n"); + + claimscope(sc); + update_scope_status(sc); + + send_text(s, "System: Language: %s, Counter: %d, Beep: %d
\n", + sc->status.system.lang, + sc->status.system.counter_enabled, + sc->status.system.beep_enabled); + + send_text(s, "Keyboard: Key Lock: %d
\n", + sc->status.keyboard.key_lock); + + send_text(s, "Acquire: Type: %s, Mode: %s, Averages: %d
\n", + sc->status.acquire.type, + sc->status.acquire.mode, + sc->status.acquire.averages); + + send_text(s, "Horizontal: Mode: %s, Offset: %lg, Delayed Offset: %lg, Scale: %lg, Format: %s
\n", + sc->status.timebase.mode, + sc->status.timebase.offset, + sc->status.timebase.delayed_offset, + sc->status.timebase.scale, + sc->status.timebase.format); + + send_text(s, "Display: "); + send_command_output(s, sc, ":DISP:TYPE?"); + send_text(s, ", "); + send_command_output(s, sc, ":DISP:SCR?"); + send_text(s, ", Grid: "); + send_command_output(s, sc, ":DISP:GRID?"); + send_text(s, ", Persistence: "); + send_command_output(s, sc, ":DISP:PERS?"); + send_text(s, ", Menu: "); + send_command_output(s, sc, ":DISP:MNUS?"); + send_text(s, ", Brightness: "); + send_command_output(s, sc, ":DISP:BRIG?"); + send_text(s, ", Intensity: "); + send_command_output(s, sc, ":DISP:INT?"); + send_text(s, "
\n"); + + send_text(s, "Channel 1: "); + send_command_output(s, sc, ":CHAN1:DISP?"); + send_text(s, ", "); + send_command_output(s, sc, ":CHAN1:MEMD?"); + send_text(s, " sample depth, %.10lg samples/s
\n", sc->status.acquire.srate_chan1); + send_text(s, "Channel 2: "); + send_command_output(s, sc, ":CHAN2:DISP?"); + send_text(s, ", "); + send_command_output(s, sc, ":CHAN2:MEMD?"); + send_text(s, " sample depth, %.10lg samples/s
\n", sc->status.acquire.srate_chan2); + + releasescope(sc); + + send_text(s, sc->status.system.lang); + send_text(s, "
\n"); send_text(s, "
\n"); send_text(s, "\n"); send_text(s, "\n"); send_text(s, "
\n"); + send_text(s, "RUN "); + send_text(s, "STOP "); + send_text(s, "FORCE "); if (param) { - unsigned char buf[1024*1024]; - int res; - claimscope(sc); if (strchr (param, '?')) { - res = sendscpi(sc, param, buf, sizeof(buf)); send_text(s, "
< ");
 			send_text(s, param);
 			send_text(s, "\n> ");
-			send_binary(s, (char*)buf, res);
+			send_command_output(s, sc, param);
 			send_text(s, "
\n"); } else { sendscpi(sc, param, NULL, 0); @@ -188,6 +264,9 @@ static void parse_request(int s, struct scope *sc) memmove(token+1, token+3, strlen(token)-2); } + while ((token = strchr(param, '+')) != NULL) { + *token = ' '; + } } if (strcmp("/", file) == 0) { diff --git a/scope.c b/scope.c index c316ee5..30f175a 100644 --- a/scope.c +++ b/scope.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include "scope.h" #include "usbtmc.h" @@ -58,3 +60,119 @@ char *scope_idn(struct scope *sc) { return sc->idn; } + +#define COPY_SCOPE_STRING(sc, cmd, dst) { \ + char *buf; \ + buf = scope_get_string(sc, cmd, sizeof(dst)); \ + if (buf) { \ + strcpy(dst, buf); \ + free(buf); \ + }\ + } + +char *scope_get_string(struct scope *sc, char *cmd, int maxlen) +{ + unsigned char *buf; + int res; + + buf = malloc(maxlen); + if (buf == NULL) { + perror("malloc(scope_get_strings)"); + exit(EXIT_FAILURE); + } + + res = sendscpi(sc, cmd, buf, maxlen); + if (res < 0) { + fprintf(stderr, "Command %s failed with %d\n", cmd, res); + free(buf); + return NULL; + } + + buf[res] = 0; + + return (char*)buf; +} + +int scope_get_truth_value(struct scope *sc, char *cmd) +{ + char buf[128]; + int res; + + bzero(buf, sizeof(buf)); + res = sendscpi(sc, cmd, (unsigned char*)buf, sizeof(buf)-1); + if (res < 0) { + fprintf(stderr, "Command %s failed with %d\n", cmd, res); + return 0; + } + + printf("%s %s\n", cmd, buf); + + if (strcasecmp(buf, "on") == 0) { + return 1; + } else if (strcasecmp(buf, "enable") == 0) { + return 1; + } + + return 0; +} + +int scope_get_int(struct scope *sc, char *cmd) +{ + char buf[128]; + int res; + + bzero(buf, sizeof(buf)); + res = sendscpi(sc, cmd, (unsigned char*)buf, sizeof(buf)-1); + if (res < 0) { + fprintf(stderr, "Command %s failed with %d\n", cmd, res); + return 0; + } + + return atoi(buf); +} + +double scope_get_double(struct scope *sc, char*cmd) +{ + char buf[128]; + int res; + double ret; + + bzero(buf, sizeof(buf)); + res = sendscpi(sc, cmd, (unsigned char*)buf, sizeof(buf)-1); + if (res < 0) { + fprintf(stderr, "Command %s failed with %d\n", cmd, res); + return 0.0; + } + + ret = strtod(buf, NULL); + + return ret; +} + +int update_scope_status(struct scope *sc) +{ + bzero(&(sc->status), sizeof(sc->status)); + + COPY_SCOPE_STRING(sc, ":INFO:LANG?", sc->status.system.lang); + + sc->status.system.counter_enabled = scope_get_truth_value(sc, ":COUN:ENAB?"); + sc->status.system.beep_enabled = scope_get_truth_value(sc, ":BEEP:ENAB?"); + + sc->status.keyboard.key_lock = scope_get_truth_value(sc, ":KEY:LOCK?"); + + COPY_SCOPE_STRING(sc, ":ACQ:TYPE?", sc->status.acquire.type); + COPY_SCOPE_STRING(sc, ":ACQ:MODE?", sc->status.acquire.mode); + + sc->status.acquire.averages = scope_get_int(sc, ":ACQ:AVER?"); + sc->status.acquire.srate_chan1 = scope_get_double(sc, ":ACQ:SAMP? CHAN1"); + sc->status.acquire.srate_chan2 = scope_get_double(sc, ":ACQ:SAMP? CHAN2"); + sc->status.acquire.srate_digital = scope_get_double(sc, ":ACQ:SAMP? DIGITAL"); + + COPY_SCOPE_STRING(sc, ":TIM:MODE?", sc->status.timebase.mode); + sc->status.timebase.offset = scope_get_double(sc, ":TIM:OFFS?"); + sc->status.timebase.delayed_offset = scope_get_double(sc, ":TIM:DEL:OFFS?"); + sc->status.timebase.scale = scope_get_double(sc, ":TIM:SCAL?"); + COPY_SCOPE_STRING(sc, ":TIM:FORM?", sc->status.timebase.format); + + return 0; +} diff --git a/scope.h b/scope.h index 2f3a61a..cef5e65 100644 --- a/scope.h +++ b/scope.h @@ -8,6 +8,58 @@ struct scope { int brokenRigol; struct usbtmc_capabilities *cap; } usb; + struct { + struct { + char lang[32]; + int counter_enabled; + int beep_enabled; + } system; + + struct { + int key_lock; + } keyboard; + + struct { + /* TODO */ + } measure; + + struct { + char type[32]; + char mode[32]; + int averages; + double srate_chan1; + double srate_chan2; + double srate_digital; + } acquire; + + struct { + /* TODO */ + } display; + + struct { + /* TODO */ + } channel; + + struct { + char mode[32]; + double offset; + double delayed_offset; + double scale; + char format[32]; + } timebase; + + struct { + /* TODO */ + } trigger; + + struct { + /* TODO */ + } la; + + struct { + /* TODO */ + } math; + } status; char idn[128]; }; @@ -18,3 +70,8 @@ void claimscope(struct scope* sc); void releasescope(struct scope* sc); void resetscope(struct scope* sc); char *scope_idn(struct scope *sc); +char *scope_get_string(struct scope *sc, char *cmd, int maxlen); +int scope_get_truth_value(struct scope *sc, char *cmd); +int scope_get_int(struct scope *sc, char *cmd); +double scope_get_double(struct scope *sc, char*cmd); +int update_scope_status(struct scope *sc);