# HG changeset patch # User František Kučera # Date 1497143127 -7200 # Node ID e1e5db678ce841bfc69e4b34603bbdbe935e3cf3 # Parent 254d5d1bc659b2295b51b91dbbca50992426a910 lpt-signal-generator: calibration diff -r 254d5d1bc659 -r e1e5db678ce8 c++/lpt-signal-generator/lpt.cpp --- a/c++/lpt-signal-generator/lpt.cpp Sun Jun 11 01:25:56 2017 +0200 +++ b/c++/lpt-signal-generator/lpt.cpp Sun Jun 11 03:05:27 2017 +0200 @@ -54,10 +54,14 @@ */ setlocale(LC_ALL,""); + + // configuration ---- int addr = 0xe400; // parallel port address; first number of given port in: cat /proc/ioports | grep parport int baseFreq = 10000; // base frequency in Hz, should be between 5 000 between 10 000 Hz; lower frequency leads to dashed/dotted lines instead of greyscale int outputPower = 20; // duty cycle; 100 = 100 % int duration = 1; // in seconds; total sleep time, see note above + // ------------------ + int valueWidth = 10; // just for padding of printed values int labelWidth = -15; // just for padding of printed labels @@ -74,9 +78,6 @@ int timeOn = oneSecond * outputPower / 100 / baseFreq; int timeOff = oneSecond * (100 - outputPower) / 100 / baseFreq; - timeOn -= 13; - timeOff -= 13; - int cycleCount = duration * baseFreq; wprintf(L"%*ls %'*d ×\n", labelWidth, L"Cycle count:", valueWidth, cycleCount); wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time on:", valueWidth, timeOn); @@ -84,12 +85,67 @@ //wprintf(L"%*ls %*ls\n", labelWidth, L"unicode test:", valueWidth, L"čeština → …"); + wprintf(L"\n"); if (ioperm(addr,1,1)) { fwprintf(stderr, L"Access denied to port %#x\n", addr), exit(1); } - outb(0b00000000, addr); + // calibration auto startTimestamp = chrono::high_resolution_clock::now(); + auto calibrationCycles = 10000; + auto calibrationSleepTime = 10; + + for (int i = 0; i < calibrationCycles; i++) { + outb(0b00000000, addr); + usleep(calibrationSleepTime); + outb(0b00000000, addr); + usleep(calibrationSleepTime); + } + + auto finishTimestamp = chrono::high_resolution_clock::now(); + auto measuredDuration = chrono::duration_cast(finishTimestamp - startTimestamp).count(); + + auto singleOutbCostNano = (measuredDuration - calibrationCycles*2*calibrationSleepTime*1000)/calibrationCycles/2; + auto singleOutbCostMicro = singleOutbCostNano/1000; + + wprintf(L"%*ls %'*d μs 2× in each calibration cycle\n", labelWidth, L"Single outb():", valueWidth, singleOutbCostMicro); + wprintf(L"%*ls %'*d ns 2× in each calibration cycle\n", labelWidth, L"Single outb():", valueWidth, singleOutbCostNano); + + auto minPower = 100*singleOutbCostNano/(1000*1000*1000/baseFreq); + auto maxPower = 100-minPower; + wprintf(L"%*ls %*d %% feasible duty cycle\n", labelWidth, L"Minimum power:", valueWidth, minPower); + wprintf(L"%*ls %*d %% feasible duty cycle\n", labelWidth, L"Maximum power:", valueWidth, maxPower); + + if (singleOutbCostMicro < timeOn && singleOutbCostMicro < timeOff) { + wprintf(L"%*ls %*ls both frequency and duty cycle should be correct\n", labelWidth, L"Calibration:", valueWidth, L"OK"); + timeOn -= singleOutbCostMicro; + timeOff -= singleOutbCostMicro; + } else if (2*singleOutbCostMicro < (timeOn + timeOff)) { + wprintf(L"%*ls %*ls frequency should be OK, but duty cycle is not feasible\n", labelWidth, L"Calibration:", valueWidth, L"WARNING"); + timeOn -= singleOutbCostMicro; + timeOff -= singleOutbCostMicro; + + if (timeOn < 0) { + timeOff -= timeOn; + timeOn = 0; + } else { + timeOn -= timeOff; + timeOff = 0; + } + } else { + wprintf(L"%*ls %*ls both frequency and duty cycle are not feasible\n", labelWidth, L"Calibration:", valueWidth, L"ERROR"); + timeOn = 0; + timeOff = 0; + } + + wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Sleep on:", valueWidth, timeOn); + wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Sleep off:", valueWidth, timeOff); + + wprintf(L"\n"); + + + // actual signal generation + startTimestamp = chrono::high_resolution_clock::now(); for (int i = 0; i < cycleCount; i++) { outb(0b00000001, addr); // first data out pin = data out 0 = pin 2 on DB-25 connector @@ -98,10 +154,10 @@ usleep(timeOff); } - auto finishTimestamp = chrono::high_resolution_clock::now(); - auto measuredDuration = chrono::duration_cast(finishTimestamp - startTimestamp).count(); + finishTimestamp = chrono::high_resolution_clock::now(); + measuredDuration = chrono::duration_cast(finishTimestamp - startTimestamp).count(); - wprintf(L"%*ls %'*d μs 2× in each cycle\n", labelWidth, L"single outb():", valueWidth, (measuredDuration-duration*oneSecond*1000)/cycleCount/2/1000); - wprintf(L"%*ls %'*d ns 2× in each cycle\n", labelWidth, L"single outb():", valueWidth, (measuredDuration-duration*oneSecond*1000)/cycleCount/2); + wprintf(L"%*ls %'*d μs in total\n", labelWidth, L"Deviation:", valueWidth, (measuredDuration-duration*oneSecond*1000)/1000); + wprintf(L"%*ls %'*d ns in each cycle\n", labelWidth, L"Deviation:", valueWidth, (measuredDuration-duration*oneSecond*1000)/cycleCount); }