HW/IT/USBs/USBscope/capture.c

#include "usbscope.h"
#include <unistd.h>
#include <string.h>

int parse_coupling(char *str) {
  int ret = -1;
  if(!strcasecmp(str, "ac")) ret = SCOPE50_COUPLING_AC;
  if(!strcasecmp(str, "dc")) ret = SCOPE50_COUPLING_DC;
  return ret;
}

int parse_edge(char *str) {
  int ret = -1;
  if(*str == 'l') ret = SCOPE50_TRIG_TYPE_LESS;
  if(*str == 'g') ret = SCOPE50_TRIG_TYPE_GREATER;
  if(*str == 'u') ret = SCOPE50_TRIG_TYPE_EDGE_UP;
  if(*str == 'd') ret = SCOPE50_TRIG_TYPE_EDGE_DOWN;
  return ret;
}

int main(int argc, char *argv[]) {
  int opt, dev = 0, len = 3072, help = 0;
  int freq = 1E7, ran_code, ran_prec, cyc = 1,
      coup = SCOPE50_COUPLING_DC, ttype = SCOPE50_TRIG_TYPE_EDGE_UP,
      pretrig = 0, wait = 0;
  float thresh = -1000, ran = 3;
  unsigned char data[3072];
  int res, i, c;
  int fd;
  while((opt = getopt(argc, argv, "d:f:c:l:r:C:t:p:e:w:h?")) != -1)
    switch(opt) {
      case 'd':
        dev = atoi(optarg);
        break;
      case 'f':
        freq = (int)(atof(optarg));
        break;
      case 'r':
        ran = atof(optarg);
        break;
      case 'c':
        cyc = atoi(optarg);
        break;
      case 'l':
        len = atoi(optarg);
        break;
      case 'C':
        coup = parse_coupling(optarg);
        break;
      case 't':
        thresh = atof(optarg);
        break;
      case 'p':
        pretrig = atoi(optarg);
        break;
      case 'e':
        ttype = parse_edge(optarg);
        break;
      case 'w':
        wait = atoi(optarg);
        break;
      default:
        help = 1;
        break;
    }
  if(help || optind != argc || cyc < 1 || len < 1 || len > 3072 || ran > 300 ||
      ran < 0 || (thresh < -0.9*ran && thresh != -1000) || thresh > 0.9*ran ||
      pretrig > len || ttype < 0 || coup < 0 || wait < 0) {
    printf("Pouziti: %s [-d dev] [-f freq] [-r ran] [-c cyc] [-l len] [-C coup]\n"
        "    [-t thresh] [-p pretrig] [-e edge] [-w wait]\n"
        "  dev je cislo zarizeni %sn, na ktere se pripojit.\n"
        "  freq je pocet vzorku za vterinu: 25 - 5E7 (vychozi: 1E7).\n"
        "  ran je vertikalni rozsah osciloskopu: 0.3, 3 nebo 30 (vych. 3).\n"
        "  cyc je pocet snimacich cyklu, len jejich delka: 1 - 3072.\n"
        "  coup je rezim osciloskopu: AC nebo DC (vychozi: DC).\n"
        "  -t zapina trigger, thresh je spinaci uroven ve voltech.\n"
        "  pretrig je pocet vzorku pred spustenim triggeru.\n"
        "  edge je zpusob detekce hrany: less/l, greater/g, up/u, down/d.\n"
        "  wait je prodleva pred zacatkem mereni v sekundach.\n",
        argv[0], DEVNAME);
    return 0;
  }

  if(!opendev(dev, &fd)) {
    fprintf(stderr, "Nepodarilo se otevrit zarizeni %s%i!\n"
        "Zkontrolujte pritomnost specialniho souboru a pristupova prava.\n",
        DEVNAME, dev);
    return 1;
  }

  if(!scope50_init_scope(fd, 1)) return 1;
  if(ran < 0.4) {
    ran = 0.3;
    ran_code = SCOPE50_RANGE_3V;
    ran_prec = 3;
  } else if(ran <= 3) {
    ran = 3;
    ran_code = SCOPE50_RANGE_30V;
    ran_prec = 2;
  } else {
    ran = 30;
    ran_code = SCOPE50_RANGE_300V;
    ran_prec = 1;
  }
  if(!scope50_setup_front_end(fd, ran_code, coup, 0)) return 1;
  if(!scope50_set_sample_frequency(fd, freq)) return 1;
  if(!scope50_set_offset(fd, 0, NULL)) return 1;
  if(thresh > -1000) {
    if(!scope50_set_norm_trig(fd, 1)) return 1;
    if(!scope50_set_trig_thresh(fd, thresh*100.0/ran)) return 1;
    if(!scope50_set_trig_type(fd, ttype)) return 1;
    if(!scope50_set_pretrig_depth(pretrig/3000.0)) return 1;
  } else {
    if(!scope50_set_norm_trig(fd, 0)) return 1;
  }
  if(!scope50_set_trig_master(fd, 1)) return 1;

  for(c = 0; c < cyc; c++) {
    if(!scope50_acquisition_start(fd)) return 1;
    do {
      if(!scope50_get_acquisition_state(fd, &res)) return 1;
      if(res) usleep(100);
    } while(res);
    if(!scope50_get_buffer_blocks_raw(fd, data, 6)) return 1;
    for(i = 0; i < len; i++) printf("%.*f\n", ran_prec, (data[i]/255.0 - 0.5)*2*ran);
    if(cyc > 1) printf("\n");
    fflush(stdout);
  }

  close(fd);
  return 0;
}