3 * Copyright © 2017 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <chrono> // requires -std=c++11
28 * can not mix printf and wprintf
29 * see https://stackoverflow.com/questions/8681623/printf-and-wprintf-in-single-c-code
30 * > This is to be expected; your code is invoking undefined behavior.
31 * > Per the C standard, each FILE stream has associated with it an "orientation" (either "byte" or "wide)
32 * > which is set by the first operation performed on it, and which can be inspected with the fwide function.
33 * > Calling any function whose orientation conflicts with the orientation of the stream results in undefined behavior.
41 // run this program: make run
42 // depending on frequency and machine performance the total time will be more than given duration
43 // despite the real-time priority, because some additional time is spent in outb() functions
44 // so "duration" means total sleep time
46 // TODO: data types revision
47 // TODO: time units s, μs, ns – naming convention / unification
50 * generates square wave signal on a parallel port pin with given frequency and duty cycle
51 * mode info: https://blog.frantovo.cz/c/358/Paraleln%C3%AD%20port%20jako%20gener%C3%A1tor%20sign%C3%A1lu
54 //cout << "LPT!" << endl; // same as using printf → breaks all folllowing wprintf() calls, see note above
57 * if setlocale() is missing, unicode characters are replaced with ? or „→“ with „->“ because C/POSIX locale is used,
59 * > On startup of the main program, the portable "C" locale is selected as default.
60 * > If locale is an empty string, "", each part of the locale that should be modified is set according to the environment variables.
66 int addr = 0xe400; // parallel port address; first number of given port in: cat /proc/ioports | grep parport
67 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
68 int outputPower = 20; // duty cycle; 100 = 100 %
69 int duration = 1; // in seconds; total sleep time, see note above
73 int valueWidth = 10; // just for padding of printed values
74 int labelWidth = -15; // just for padding of printed labels
76 // ' = thousand separator
78 wprintf(L"%*ls %*x\n", labelWidth, L"Parallel port:", valueWidth, addr); // or %#*x – adds 0x prefix
79 wprintf(L"%*ls %'*d Hz\n", labelWidth, L"Base frequency:", valueWidth, baseFreq);
80 wprintf(L"%*ls %*d %% duty cycle\n", labelWidth, L"Output power:", valueWidth, outputPower);
81 wprintf(L"%*ls %'*d s\n", labelWidth, L"Duration:", valueWidth, duration);
84 auto oneSecond = 1000 * 1000;
85 auto timeOn = oneSecond * outputPower / 100 / baseFreq;
86 auto timeOff = oneSecond * (100 - outputPower) / 100 / baseFreq;
88 auto cycleCount = duration * baseFreq;
89 wprintf(L"%*ls %'*d ×\n", labelWidth, L"Cycle count:", valueWidth, cycleCount);
90 wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time on:", valueWidth, timeOn);
91 wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time off:", valueWidth, timeOff);
93 //wprintf(L"%*ls %*ls\n", labelWidth, L"unicode test:", valueWidth, L"čeština → …");
97 // TODO: test whether this address is an parallel port
98 if (ioperm(addr,1,1)) { fwprintf(stderr, L"Access denied to port %#x\n", addr), exit(1); }
102 auto startTimestamp = chrono::high_resolution_clock::now();
103 auto calibrationCycles = 10000;
104 auto calibrationSleepTime = 10;
106 for (auto i = calibrationCycles; i > 0; i--) {
107 outb(0b00000000, addr);
108 usleep(calibrationSleepTime);
109 outb(0b00000000, addr);
110 usleep(calibrationSleepTime);
113 auto finishTimestamp = chrono::high_resolution_clock::now();
114 auto measuredDuration = chrono::duration_cast<chrono::nanoseconds>(finishTimestamp - startTimestamp).count();
116 auto singleOutbCostNano = (measuredDuration - calibrationCycles*2*calibrationSleepTime*1000)/calibrationCycles/2;
117 auto singleOutbCostMicro = singleOutbCostNano/1000;
119 wprintf(L"%*ls %'*d μs 2× in each calibration cycle\n", labelWidth, L"Single outb():", valueWidth, singleOutbCostMicro);
120 wprintf(L"%*ls %'*d ns 2× in each calibration cycle\n", labelWidth, L"Single outb():", valueWidth, singleOutbCostNano);
122 auto minPower = 100*singleOutbCostNano/(1000*1000*1000/baseFreq);
123 auto maxPower = 100-minPower;
124 wprintf(L"%*ls %*d %% feasible duty cycle\n", labelWidth, L"Minimum power:", valueWidth, minPower);
125 wprintf(L"%*ls %*d %% feasible duty cycle\n", labelWidth, L"Maximum power:", valueWidth, maxPower);
127 if (singleOutbCostMicro < timeOn && singleOutbCostMicro < timeOff) {
128 wprintf(L"%*ls %*ls both frequency and duty cycle should be correct\n", labelWidth, L"Calibration:", valueWidth, L"OK");
129 timeOn -= singleOutbCostMicro;
130 timeOff -= singleOutbCostMicro;
131 } else if (2*singleOutbCostMicro < (timeOn + timeOff)) {
132 wprintf(L"%*ls %*ls frequency should be OK, but duty cycle is not feasible\n", labelWidth, L"Calibration:", valueWidth, L"WARNING");
133 timeOn -= singleOutbCostMicro;
134 timeOff -= singleOutbCostMicro;
144 wprintf(L"%*ls %*ls both frequency and duty cycle are not feasible\n", labelWidth, L"Calibration:", valueWidth, L"ERROR");
149 wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Sleep on:", valueWidth, timeOn);
150 wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Sleep off:", valueWidth, timeOff);
155 // actual signal generation
156 startTimestamp = chrono::high_resolution_clock::now();
158 for (auto i = cycleCount; i > 0; i--) {
159 outb(0b00000001, addr); // first data out pin = data out 0 = pin 2 on DB-25 connector
161 outb(0b00000000, addr);
165 finishTimestamp = chrono::high_resolution_clock::now();
166 measuredDuration = chrono::duration_cast<chrono::nanoseconds>(finishTimestamp - startTimestamp).count();
168 wprintf(L"%*ls %'*d μs in total\n", labelWidth, L"Deviation:", valueWidth, (measuredDuration-duration*oneSecond*1000)/1000);
169 wprintf(L"%*ls %'*d ns in each cycle\n", labelWidth, L"Deviation:", valueWidth, (measuredDuration-duration*oneSecond*1000)/cycleCount);