#include <strings.h>
#include <unistd.h>
#include <signal.h>
+#include <stdarg.h>
#include "scope.h"
#include "commands.h"
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, "<html><head><title>");
- send_text(s, scope_idn(sc));
- send_text(s, "</title></head><body bgcolor=\"#ffffff\" text=\"#000000\">\n");
+ send_text(s, "<html><head><title>%s</title></head><body bgcolor=\"#ffffff\" text=\"#000000\">\n", scope_idn(sc));
send_text(s, "<img src=\"/cgi-bin/lcd\" height=\"234\" width=\"320\">\n");
+ send_text(s, "<br>\n");
+
+ claimscope(sc);
+ update_scope_status(sc);
+
+ send_text(s, "System: Language: %s, Counter: %d, Beep: %d<br>\n",
+ sc->status.system.lang,
+ sc->status.system.counter_enabled,
+ sc->status.system.beep_enabled);
+
+ send_text(s, "Keyboard: Key Lock: %d<br>\n",
+ sc->status.keyboard.key_lock);
+
+ send_text(s, "Acquire: Type: %s, Mode: %s, Averages: %d<br>\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<br>\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, "<br>\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<br>\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<br>\n", sc->status.acquire.srate_chan2);
+
+ releasescope(sc);
+
+ send_text(s, sc->status.system.lang);
+
send_text(s, "<br>\n");
send_text(s, "<form method=\"get\" action=\"\">\n");
send_text(s, "<input type=\"text\" name=\"cmd\" value=\"");
send_text(s, "\">\n");
send_text(s, "<input type=\"submit\">\n");
send_text(s, "</form>\n");
+ send_text(s, "<a href=\"?cmd=:RUN\">RUN</a> ");
+ send_text(s, "<a href=\"?cmd=:STOP\">STOP</a> ");
+ send_text(s, "<a href=\"?cmd=:FORC\">FORCE</a> ");
if (param) {
- unsigned char buf[1024*1024];
- int res;
-
claimscope(sc);
if (strchr (param, '?')) {
- res = sendscpi(sc, param, buf, sizeof(buf));
send_text(s, "<pre>< ");
send_text(s, param);
send_text(s, "\n> ");
- send_binary(s, (char*)buf, res);
+ send_command_output(s, sc, param);
send_text(s, "</pre>\n");
} else {
sendscpi(sc, param, NULL, 0);
memmove(token+1, token+3, strlen(token)-2);
}
+ while ((token = strchr(param, '+')) != NULL) {
+ *token = ' ';
+ }
}
if (strcmp("/", file) == 0) {
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
+#include <string.h>
+#include <strings.h>
#include "scope.h"
#include "usbtmc.h"
{
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;
+}