/* programme that outputs to a scope using the "LabMaster" set of libraries; MUST be linked with the Labmaster! eg gcc -Wall -O3 -ansi -pedantic -lm -llabmaster -o drawing drawing.c */ #include /* (f)printf stderr */ #include /* sscanf */ #include /* sqrt */ #include /* strncmp */ #include /* out_dac etc */ #include "dac.h" /* prototypes */ int main(int argc, char **argv); int VtoDAC(double v); void fillSinwave(int *buffer, double amp, int length); void fillTriangle(int *buffer, double amp, int length); void fillSquarewave(int *buffer, double amp, int length); void fillSawtooth(int *buffer, double amp, int length); void fillCalibration(int *buffer, double amp, int length); /* constants */ const static int timerDAC = 0; /* the timer DAC */ const static int outDAC = 1; /* the output DAC */ const static double pi = 3.141592653589793238462643383279502884197169399375105\ 820974944592307816406286208998; /* etc; since ansi apperantly does'n't define M_PI */ /* main (duh?) */ int main(int argc, char **argv) { void (*fn)(int *, double, int); int i; int length; /* length of the buffer @= the wavelength */ int *buffer; double amp, fre; /* initialize arguments */ if((argc > 1) && (!strncmp(argv[1], "-h", 2) || !strncmp(argv[1], "--h", 3))) { fprintf(stderr, "Usage: drawing [sinewave|triangle|squarewave|sawtooth] "); fprintf(stderr, "[amplitude] [frequency]\n"); return 1; } /* fill with def val */ fn = fillSinwave; amp = 1; length = 1024; /* user input */ if(argc > 1) { if (strncmp(argv[1], "sawtooth", 2) == 0) fn = fillSawtooth; else if(strncmp(argv[1], "squarewave", 2) == 0) fn = fillSquarewave; else if(strncmp(argv[1], "triangle", 2) == 0) fn = fillTriangle; else if(strncmp(argv[1], "sinewave", 2) == 0) fn = fillSinwave; else if(strncmp(argv[1], "calibration", 2) == 0) fn = fillCalibration; else { fprintf(stderr, "%s?\n", argv[1]); return 1; } } if(argc > 2) { if(sscanf(argv[2], "%lg", &) != 1) { fprintf(stderr, "Ca'n't parse number; use --h for help.\n"); return 1; } fprintf(stderr, "Using amp = %g.\n", amp); } if(argc > 3) { if(sscanf(argv[3], "%lg", &fre) != 1) { fprintf(stderr, "Ca'n't parse number; use --h for help.\n"); return 1; } if(fre <= 10) { fprintf(stderr, "Frequency must be 10.\n"); return 1; } length = 115000 / (double)fre; fprintf(stderr, "Using fre = %g, len = %d.\n", fre, length); } buffer = malloc(length * sizeof(int)); if(!buffer) { fprintf(stderr, "Wavelength too big for memory allocation: %d.", length); return 0; /* mucho scecthy! */ } /* call *fn to fill the buffer */ (*fn)(buffer, amp, length); /* "This step is critical," OK */ labmaster_initialize(); /* display instuctions */ printf("Connect DAC-%d to the timer trigger, ext.\n", timerDAC); printf("Connect DAC-%d to Y.\n", outDAC); /* system dependent */ printf("Ctrl-C to exit?\n"); /* display infinitaly (FIXME: timer?) */ for( ; ; ) { /* for every buffer[i] */ for(i = 0; i < length; i++) { /* -'ve pulse to trigger scope */ out_dac(timerDAC, (i == 0) ? (-2048) : (2047)); /* this is the output */ out_dac(outDAC, buffer[i]); } } /* I assume this step is critical too */ /* FIXME: but it does'n't get executed ever since Ctrl-C is the only way to end */ labmaster_terminate(); /* the fn that's never called */ free(buffer); return 0; } /* volts to DAC; in dac.h */ int VtoDAC (double v) { return (v - a[outDAC]) / b[outDAC]; } /* sinwave */ void fillSinwave(int *buffer, double amp, int length) { int i; double x; for(i = 0; i < length; i++) { x = i * 2 * pi / length; buffer[i] = VtoDAC(sin(x) * amp); } } /* triangle */ void fillTriangle(int *buffer, double amp, int length) { int i; double x; for(i = 0; i < length; i++) { x = i / (double)length; if(x < .5) buffer[i] = VtoDAC((-1 + 4 * x) * amp); else buffer[i] = VtoDAC(( 3 - 4 * x) * amp); } } /* squarewave */ void fillSquarewave(int *buffer, double amp, int length) { int i; double x; for(i = 0; i < length; i++) { x = i / (double)length; if(x < .5) buffer[i] = VtoDAC( amp); else buffer[i] = VtoDAC(-amp); } } /* sawtooth */ void fillSawtooth(int *buffer, double amp, int length) { int i; double x; for(i = 0; i < length; i++) { x = i / (double)length; buffer[i] = VtoDAC((-1 + 2 * x) * amp); } } /* calibration; FIXME: calibration is not implemented, the only thing to do is change the code, which is Bad, but suits our needs */ void fillCalibration(int *buffer, double amp, int length) { int i; fprintf(stderr, "Calibration . . . sampling frequency = 2 measured frequency.\n"); for(i = 0; i < length; i++) { buffer[i] = (i & 1) ? (2047) : (-2048); } }