HW/DAS/USBscopes/usbtm/test/xdemo.c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include "usbscope.h"

Display *dpy;
Window win;
XEvent report;
GC white_gc, black_gc, line1_gc, line2_gc;
int white, black;

int fd;
int pre = 30;

void readloop() {
  int res;
  int i;
  unsigned char data[3072];
  XPoint pt[3072];
  for(;;) {
    if(!scope50_acquisition_start(fd)) return;
    do {
      while(XCheckMaskEvent(dpy, KeyPressMask, &report)) {
        switch(report.type) {
          case KeyPress:
            if (XLookupKeysym(&report.xkey, 0) == XK_q)
              return;
        }
      }
      if(!scope50_get_acquisition_state(fd, &res)) return;
      if(res) usleep(100);
    } while(res);
    if(!scope50_get_buffer_blocks_raw(fd, data, 6)) return;
    for(i = 0; i < 3072; i++) {
      pt[i].x = i/3;
      pt[i].y = 255-data[i];
    }
    XClearWindow(dpy, win);
    XDrawLine(dpy, win, line1_gc, pre/3, 0, pre/3, 256);
    for(i = 0; i < 3072; i += 1000)
      XDrawLine(dpy, win, line2_gc, (pre+i)/3, 0, (pre+i)/3, 256);
    for(i = 1; i < 6; i++)
      XDrawLine(dpy, win, line2_gc, 0, 256*i/6, 1024, 256*i/6);
    XDrawLine(dpy, win, line1_gc, 0, 128, 1024, 128);
    XDrawLines(dpy, win, white_gc, pt, 3072, CoordModeOrigin);
    XFlush(dpy);
  }
}


int init_scope() {
  if(!scope50_init_scope(fd, 1, NULL, NULL, NULL)) return 0;
  if(!scope50_setup_front_end(fd, SCOPE50_RANGE_30V, SCOPE50_COUPLING_DC, 0)) return 0;
  //if(!scope50_set_sample_frequency(fd, 1.25E7, NULL)) return 0;
  if(!scope50_set_sample_frequency(fd, 1E4, NULL)) return 0;
  if(!scope50_set_offset(fd, 0, NULL)) return 0;
  if(!scope50_set_norm_trig(fd, 1)) return 0;
  if(!scope50_set_trig_thresh(fd, 0.5)) return 0;
  if(!scope50_set_trig_type(fd, SCOPE50_TRIG_TYPE_EDGE_UP)) return 0;
  if(!scope50_set_pretrig_depth(pre)) return 0;
  if(!scope50_set_trig_master(fd, 1)) return 0;
  return 1;
}

int main(int argc, char *argv[]) {
  char dim[] = "#4040C0";
  Colormap cmap;
  XColor dim_c;
  if(!opendev(0, &fd)) {
    fprintf(stderr, "Error opening device.\n");
    return 1;
  }
  if(!init_scope()) return 1;
  dpy = XOpenDisplay(NULL);
  white = WhitePixel(dpy, DefaultScreen(dpy));
  black = BlackPixel(dpy, DefaultScreen(dpy));
  win = XCreateSimpleWindow(dpy, RootWindow(dpy, 0), 1, 1, 1024, 256, 0, black, black);
  XMapWindow(dpy, win);
  cmap = DefaultColormap(dpy, 0);
  XParseColor(dpy, cmap, dim, &dim_c);
  XAllocColor(dpy, cmap, &dim_c);
  white_gc = XCreateGC(dpy, win, 0, 0);
  black_gc = XCreateGC(dpy, win, 0, 0);
  line1_gc = XCreateGC(dpy, win, 0, 0);
  line2_gc = XCreateGC(dpy, win, 0, 0);
  XSetForeground(dpy, white_gc, white);
  XSetForeground(dpy, black_gc, black);
  XSetForeground(dpy, line1_gc, dim_c.pixel);
  XSetForeground(dpy, line2_gc, dim_c.pixel);
  XSetLineAttributes(dpy, line2_gc, 0, LineOnOffDash, 0, 0);
  XSelectInput(dpy, win, KeyPressMask);
  readloop();
  closedev(0, fd);
  return 0;
}