-#include <usb.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
#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;
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");
#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;
}
-void do_get_buf (struct usb_dev_handle *sc)
+void do_get_buf (struct scope *sc)
{
FILE *fp;
int i, j, l, bp;
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?");
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);
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");
fwrite (data, bp, 1, fp);
fclose (fp);
- usbtmc_sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
+ sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
}
-void do_get_screen(struct usb_dev_handle *sc)
+unsigned char* get_lcd(struct scope *sc, int *imglen, int keylock)
{
unsigned char screen[320*234];
+ unsigned char *png;
+ int l;
+
+ if (keylock) {
+ /* Hide "RMT" from screen */
+ l = sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0);
+ usleep(30000);
+ }
+
+ 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);
+ return NULL;
+ }
+
+ png = lcd2png(screen, imglen);
+
+ return png;
+}
+
+void do_get_screen(struct scope *sc)
+{
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 = get_lcd(sc, &imglen, 1);
+ if (png == NULL) {
+ perror("get_lcd");
+ return;
}
- 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);
+ return;
}
while(imglen > 0) {
ret = write(fd, png, imglen);
if (ret == -1) {
perror("write");
- exit(EXIT_FAILURE);
+ return;
}
imglen -= ret;
}
}
}
+void do_display_screen(struct scope *sc)
+{
+ unsigned char *png;
+ int imglen;
+ int ret;
+ int pipefd[2];
+ pid_t display;
+
+ png = get_lcd(sc, &imglen, 1);
+ if (png == NULL) {
+ perror("get_lcd");
+ return;
+ }
+
+ if (pipe(pipefd) == -1) {
+ perror("pipe");
+ return;
+ }
+
+ display = fork();
+ switch(display) {
+ case 0:
+ close(pipefd[1]);
+ close(STDIN_FILENO);
+ dup2(pipefd[0], STDIN_FILENO);
+ execlp("display", "display", "-", NULL);
+ exit(0);
+ break;
+ case -1:
+ perror("fork");
+ break;
+ default:
+ close(pipefd[0]);
+ while(imglen > 0) {
+ ret = write(pipefd[1], png, imglen);
+ if (ret == -1) {
+ perror("write");
+ exit(EXIT_FAILURE);
+ }
+ imglen -= ret;
+ }
+ close(pipefd[1]);
+ free(png);
+ break;
+ }
+}