# HG changeset patch # User František Kučera # Date 1497179518 -7200 # Node ID 6b5c6a26693ffb12d885be06f282c1ee9b3186ed # Parent 514b9b433dc293f854c790d273dfff3e8f9014a9 lpt-signal-generator: odsazení: mezery → tabulátory diff -r 514b9b433dc2 -r 6b5c6a26693f c++/lpt-signal-generator/lpt.cpp --- a/c++/lpt-signal-generator/lpt.cpp Sun Jun 11 13:07:53 2017 +0200 +++ b/c++/lpt-signal-generator/lpt.cpp Sun Jun 11 13:11:58 2017 +0200 @@ -51,121 +51,121 @@ * mode info: https://blog.frantovo.cz/c/358/Paraleln%C3%AD%20port%20jako%20gener%C3%A1tor%20sign%C3%A1lu */ int main() { - //cout << "LPT!" << endl; // same as using printf → breaks all folllowing wprintf() calls, see note above + //cout << "LPT!" << endl; // same as using printf → breaks all folllowing wprintf() calls, see note above - /* - * if setlocale() is missing, unicode characters are replaced with ? or „→“ with „->“ because C/POSIX locale is used, - * see man setlocale: - * > On startup of the main program, the portable "C" locale is selected as default. - * > If locale is an empty string, "", each part of the locale that should be modified is set according to the environment variables. - */ - setlocale(LC_ALL,""); + /* + * if setlocale() is missing, unicode characters are replaced with ? or „→“ with „->“ because C/POSIX locale is used, + * see man setlocale: + * > On startup of the main program, the portable "C" locale is selected as default. + * > If locale is an empty string, "", each part of the locale that should be modified is set according to the environment variables. + */ + 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 - // ------------------ + // 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 + int valueWidth = 10; // just for padding of printed values + int labelWidth = -15; // just for padding of printed labels - // ' = thousand separator - // * = padding - wprintf(L"%*ls %*x\n", labelWidth, L"Parallel port:", valueWidth, addr); // or %#*x – adds 0x prefix - wprintf(L"%*ls %'*d Hz\n", labelWidth, L"Base frequency:", valueWidth, baseFreq); - wprintf(L"%*ls %*d %% duty cycle\n", labelWidth, L"Output power:", valueWidth, outputPower); - wprintf(L"%*ls %'*d s\n", labelWidth, L"Duration:", valueWidth, duration); + // ' = thousand separator + // * = padding + wprintf(L"%*ls %*x\n", labelWidth, L"Parallel port:", valueWidth, addr); // or %#*x – adds 0x prefix + wprintf(L"%*ls %'*d Hz\n", labelWidth, L"Base frequency:", valueWidth, baseFreq); + wprintf(L"%*ls %*d %% duty cycle\n", labelWidth, L"Output power:", valueWidth, outputPower); + wprintf(L"%*ls %'*d s\n", labelWidth, L"Duration:", valueWidth, duration); - // in microseconds: - auto oneSecond = 1000 * 1000; - auto timeOn = oneSecond * outputPower / 100 / baseFreq; - auto timeOff = oneSecond * (100 - outputPower) / 100 / baseFreq; + // in microseconds: + auto oneSecond = 1000 * 1000; + auto timeOn = oneSecond * outputPower / 100 / baseFreq; + auto timeOff = oneSecond * (100 - outputPower) / 100 / baseFreq; - auto 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); - wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time off:", valueWidth, timeOff); + auto 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); + wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time off:", valueWidth, timeOff); - //wprintf(L"%*ls %*ls\n", labelWidth, L"unicode test:", valueWidth, L"čeština → …"); + //wprintf(L"%*ls %*ls\n", labelWidth, L"unicode test:", valueWidth, L"čeština → …"); - wprintf(L"\n"); + wprintf(L"\n"); - // TODO: test whether this address is an parallel port - if (ioperm(addr,1,1)) { fwprintf(stderr, L"Access denied to port %#x\n", addr), exit(1); } + // TODO: test whether this address is an parallel port + if (ioperm(addr,1,1)) { fwprintf(stderr, L"Access denied to port %#x\n", addr), exit(1); } - // calibration - auto startTimestamp = chrono::high_resolution_clock::now(); - auto calibrationCycles = 10000; - auto calibrationSleepTime = 10; + // calibration + auto startTimestamp = chrono::high_resolution_clock::now(); + auto calibrationCycles = 10000; + auto calibrationSleepTime = 10; - for (auto i = calibrationCycles; i > 0; i--) { - outb(0b00000000, addr); - usleep(calibrationSleepTime); - outb(0b00000000, addr); - usleep(calibrationSleepTime); - } + for (auto i = calibrationCycles; i > 0; 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 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; + 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); + 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); + 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 (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; - } + 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"%*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"); + wprintf(L"\n"); - // actual signal generation - startTimestamp = chrono::high_resolution_clock::now(); + // actual signal generation + startTimestamp = chrono::high_resolution_clock::now(); - for (auto i = cycleCount; i > 0; i--) { - outb(0b00000001, addr); // first data out pin = data out 0 = pin 2 on DB-25 connector - usleep(timeOn); - outb(0b00000000, addr); - usleep(timeOff); - } + for (auto i = cycleCount; i > 0; i--) { + outb(0b00000001, addr); // first data out pin = data out 0 = pin 2 on DB-25 connector + usleep(timeOn); + outb(0b00000000, addr); + usleep(timeOff); + } - finishTimestamp = chrono::high_resolution_clock::now(); - 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 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); + 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); }