#include "usbscope.h" #include #include #ifndef PATH_MAX #define PATH_MAX 1024 #endif 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, real = 0; float thresh = -1000, ran = 3; unsigned char data[3072]; int res, i, c; int fd; char fnread[PATH_MAX] = "", fnwrite[PATH_MAX] = ""; FILE *fcalib; float raw_offset, dac_scale; int trig_offset; while((opt = getopt(argc, argv, "d:f:c:l:r:C:t:p:e:W:L:S: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; case 'S': strcpy(fnwrite, optarg); break; case 'L': strcpy(fnread, 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] [-S|-L conf]\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 v pameti 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" " Volby -S/-L umoznuji ulozit / nacist kalibracni data pro rychlejsi spusteni.\n", argv[0], DEVNAME); return 0; } if(!opendev(dev, &fd)) { fprintf(stderr, deverror(dev)); return 1; } if(*fnread) { fcalib = fopen(fnread, "r"); if(!fcalib) { fprintf(stderr, "Nepodarilo se otevrit soubor %s pro cteni kalibracnich dat.\n", fnread); } else { if(fscanf(fcalib, "%f %f %i", &raw_offset, &dac_scale, &trig_offset) < 3) { fprintf(stderr, "Neplatna kalibracni data."); raw_offset = dac_scale = trig_offset = 0; } fclose(fcalib); } } if(!scope50_init_scope(fd, 1, &raw_offset, &dac_scale, &trig_offset)) return 1; if(*fnwrite) { fcalib = fopen(fnwrite, "w"); if(!fcalib) { fprintf(stderr, "Nepodarilo se otevrit soubor %s pro ulozeni kalibracnich dat.\n", fnwrite); } else { fprintf(fcalib, "%f %f %i", raw_offset, dac_scale, trig_offset); fclose(fcalib); } } 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, &real)) return 1; if(real != freq) fprintf(stderr, "Skutecna vzorkovaci frekvence: %i\n", real); 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/ran*100)) return 1; if(!scope50_set_trig_type(fd, ttype)) return 1; if(!scope50_set_pretrig_depth(pretrig)) 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); } closedev(dev, fd); return 0; }