franta-hg@44
|
1 |
/**
|
franta-hg@44
|
2 |
* LPT signal generator
|
franta-hg@44
|
3 |
* Copyright © 2017 František Kučera (frantovo.cz)
|
franta-hg@44
|
4 |
*
|
franta-hg@44
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@44
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@44
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@44
|
8 |
* (at your option) any later version.
|
franta-hg@44
|
9 |
*
|
franta-hg@44
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@44
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@44
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@44
|
13 |
* GNU General Public License for more details.
|
franta-hg@44
|
14 |
*
|
franta-hg@44
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@44
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@44
|
17 |
*/
|
franta-hg@44
|
18 |
|
franta-hg@44
|
19 |
#include <stdlib.h>
|
franta-hg@44
|
20 |
#include <iostream>
|
franta-hg@44
|
21 |
#include <stdio.h>
|
franta-hg@44
|
22 |
#include <math.h>
|
franta-hg@44
|
23 |
#include <sys/io.h>
|
franta-hg@44
|
24 |
#include <unistd.h>
|
franta-hg@46
|
25 |
#include <chrono> // requires -std=c++11
|
franta-hg@44
|
26 |
|
franta-hg@45
|
27 |
/**
|
franta-hg@45
|
28 |
* can not mix printf and wprintf
|
franta-hg@45
|
29 |
* see https://stackoverflow.com/questions/8681623/printf-and-wprintf-in-single-c-code
|
franta-hg@45
|
30 |
* > This is to be expected; your code is invoking undefined behavior.
|
franta-hg@45
|
31 |
* > Per the C standard, each FILE stream has associated with it an "orientation" (either "byte" or "wide)
|
franta-hg@45
|
32 |
* > which is set by the first operation performed on it, and which can be inspected with the fwide function.
|
franta-hg@45
|
33 |
* > Calling any function whose orientation conflicts with the orientation of the stream results in undefined behavior.
|
franta-hg@45
|
34 |
*/
|
franta-hg@45
|
35 |
#include <wchar.h>
|
franta-hg@45
|
36 |
#include <locale.h>
|
franta-hg@45
|
37 |
|
franta-hg@45
|
38 |
|
franta-hg@44
|
39 |
using namespace std;
|
franta-hg@44
|
40 |
|
franta-hg@50
|
41 |
// TODO: data types revision
|
franta-hg@51
|
42 |
// TODO: time units s, μs, ns – naming convention / unification
|
franta-hg@50
|
43 |
|
franta-hg@51
|
44 |
/**
|
franta-hg@51
|
45 |
* generates square wave signal on a parallel port pin with given frequency and duty cycle
|
franta-hg@51
|
46 |
* mode info: https://blog.frantovo.cz/c/358/Paraleln%C3%AD%20port%20jako%20gener%C3%A1tor%20sign%C3%A1lu
|
franta-hg@51
|
47 |
*/
|
franta-hg@44
|
48 |
int main() {
|
franta-hg@52
|
49 |
//cout << "LPT!" << endl; // same as using printf → breaks all folllowing wprintf() calls, see note above
|
franta-hg@44
|
50 |
|
franta-hg@52
|
51 |
/*
|
franta-hg@52
|
52 |
* if setlocale() is missing, unicode characters are replaced with ? or „→“ with „->“ because C/POSIX locale is used,
|
franta-hg@52
|
53 |
* see man setlocale:
|
franta-hg@52
|
54 |
* > On startup of the main program, the portable "C" locale is selected as default.
|
franta-hg@52
|
55 |
* > If locale is an empty string, "", each part of the locale that should be modified is set according to the environment variables.
|
franta-hg@52
|
56 |
*/
|
franta-hg@52
|
57 |
setlocale(LC_ALL,"");
|
franta-hg@44
|
58 |
|
franta-hg@49
|
59 |
|
franta-hg@52
|
60 |
// configuration ----
|
franta-hg@52
|
61 |
int addr = 0xe400; // parallel port address; first number of given port in: cat /proc/ioports | grep parport
|
franta-hg@52
|
62 |
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
|
franta-hg@52
|
63 |
int outputPower = 20; // duty cycle; 100 = 100 %
|
franta-hg@52
|
64 |
int duration = 1; // in seconds; total sleep time, see note above
|
franta-hg@52
|
65 |
// ------------------
|
franta-hg@49
|
66 |
|
franta-hg@44
|
67 |
|
franta-hg@52
|
68 |
int valueWidth = 10; // just for padding of printed values
|
franta-hg@52
|
69 |
int labelWidth = -15; // just for padding of printed labels
|
franta-hg@44
|
70 |
|
franta-hg@52
|
71 |
// ' = thousand separator
|
franta-hg@52
|
72 |
// * = padding
|
franta-hg@52
|
73 |
wprintf(L"%*ls %*x\n", labelWidth, L"Parallel port:", valueWidth, addr); // or %#*x – adds 0x prefix
|
franta-hg@52
|
74 |
wprintf(L"%*ls %'*d Hz\n", labelWidth, L"Base frequency:", valueWidth, baseFreq);
|
franta-hg@52
|
75 |
wprintf(L"%*ls %*d %% duty cycle\n", labelWidth, L"Output power:", valueWidth, outputPower);
|
franta-hg@52
|
76 |
wprintf(L"%*ls %'*d s\n", labelWidth, L"Duration:", valueWidth, duration);
|
franta-hg@44
|
77 |
|
franta-hg@52
|
78 |
// in microseconds:
|
franta-hg@52
|
79 |
auto oneSecond = 1000 * 1000;
|
franta-hg@52
|
80 |
auto timeOn = oneSecond * outputPower / 100 / baseFreq;
|
franta-hg@52
|
81 |
auto timeOff = oneSecond * (100 - outputPower) / 100 / baseFreq;
|
franta-hg@44
|
82 |
|
franta-hg@52
|
83 |
auto cycleCount = duration * baseFreq;
|
franta-hg@52
|
84 |
wprintf(L"%*ls %'*d ×\n", labelWidth, L"Cycle count:", valueWidth, cycleCount);
|
franta-hg@52
|
85 |
wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time on:", valueWidth, timeOn);
|
franta-hg@52
|
86 |
wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time off:", valueWidth, timeOff);
|
franta-hg@44
|
87 |
|
franta-hg@52
|
88 |
//wprintf(L"%*ls %*ls\n", labelWidth, L"unicode test:", valueWidth, L"čeština → …");
|
franta-hg@44
|
89 |
|
franta-hg@52
|
90 |
wprintf(L"\n");
|
franta-hg@45
|
91 |
|
franta-hg@52
|
92 |
// TODO: test whether this address is an parallel port
|
franta-hg@52
|
93 |
if (ioperm(addr,1,1)) { fwprintf(stderr, L"Access denied to port %#x\n", addr), exit(1); }
|
franta-hg@45
|
94 |
|
franta-hg@45
|
95 |
|
franta-hg@52
|
96 |
// calibration
|
franta-hg@52
|
97 |
auto startTimestamp = chrono::high_resolution_clock::now();
|
franta-hg@52
|
98 |
auto calibrationCycles = 10000;
|
franta-hg@52
|
99 |
auto calibrationSleepTime = 10;
|
franta-hg@49
|
100 |
|
franta-hg@52
|
101 |
for (auto i = calibrationCycles; i > 0; i--) {
|
franta-hg@52
|
102 |
outb(0b00000000, addr);
|
franta-hg@52
|
103 |
usleep(calibrationSleepTime);
|
franta-hg@52
|
104 |
outb(0b00000000, addr);
|
franta-hg@52
|
105 |
usleep(calibrationSleepTime);
|
franta-hg@52
|
106 |
}
|
franta-hg@49
|
107 |
|
franta-hg@52
|
108 |
auto finishTimestamp = chrono::high_resolution_clock::now();
|
franta-hg@52
|
109 |
auto measuredDuration = chrono::duration_cast<chrono::nanoseconds>(finishTimestamp - startTimestamp).count();
|
franta-hg@49
|
110 |
|
franta-hg@52
|
111 |
auto singleOutbCostNano = (measuredDuration - calibrationCycles*2*calibrationSleepTime*1000)/calibrationCycles/2;
|
franta-hg@52
|
112 |
auto singleOutbCostMicro = singleOutbCostNano/1000;
|
franta-hg@49
|
113 |
|
franta-hg@52
|
114 |
wprintf(L"%*ls %'*d μs 2× in each calibration cycle\n", labelWidth, L"Single outb():", valueWidth, singleOutbCostMicro);
|
franta-hg@52
|
115 |
wprintf(L"%*ls %'*d ns 2× in each calibration cycle\n", labelWidth, L"Single outb():", valueWidth, singleOutbCostNano);
|
franta-hg@49
|
116 |
|
franta-hg@52
|
117 |
auto minPower = 100*singleOutbCostNano/(1000*1000*1000/baseFreq);
|
franta-hg@52
|
118 |
auto maxPower = 100-minPower;
|
franta-hg@52
|
119 |
wprintf(L"%*ls %*d %% feasible duty cycle\n", labelWidth, L"Minimum power:", valueWidth, minPower);
|
franta-hg@52
|
120 |
wprintf(L"%*ls %*d %% feasible duty cycle\n", labelWidth, L"Maximum power:", valueWidth, maxPower);
|
franta-hg@49
|
121 |
|
franta-hg@52
|
122 |
if (singleOutbCostMicro < timeOn && singleOutbCostMicro < timeOff) {
|
franta-hg@52
|
123 |
wprintf(L"%*ls %*ls both frequency and duty cycle should be correct\n", labelWidth, L"Calibration:", valueWidth, L"OK");
|
franta-hg@52
|
124 |
timeOn -= singleOutbCostMicro;
|
franta-hg@52
|
125 |
timeOff -= singleOutbCostMicro;
|
franta-hg@52
|
126 |
} else if (2*singleOutbCostMicro < (timeOn + timeOff)) {
|
franta-hg@52
|
127 |
wprintf(L"%*ls %*ls frequency should be OK, but duty cycle is not feasible\n", labelWidth, L"Calibration:", valueWidth, L"WARNING");
|
franta-hg@52
|
128 |
timeOn -= singleOutbCostMicro;
|
franta-hg@52
|
129 |
timeOff -= singleOutbCostMicro;
|
franta-hg@49
|
130 |
|
franta-hg@52
|
131 |
if (timeOn < 0) {
|
franta-hg@52
|
132 |
timeOff -= timeOn;
|
franta-hg@52
|
133 |
timeOn = 0;
|
franta-hg@52
|
134 |
} else {
|
franta-hg@52
|
135 |
timeOn -= timeOff;
|
franta-hg@52
|
136 |
timeOff = 0;
|
franta-hg@52
|
137 |
}
|
franta-hg@52
|
138 |
} else {
|
franta-hg@52
|
139 |
wprintf(L"%*ls %*ls both frequency and duty cycle are not feasible\n", labelWidth, L"Calibration:", valueWidth, L"ERROR");
|
franta-hg@52
|
140 |
timeOn = 0;
|
franta-hg@52
|
141 |
timeOff = 0;
|
franta-hg@52
|
142 |
}
|
franta-hg@49
|
143 |
|
franta-hg@52
|
144 |
wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Sleep on:", valueWidth, timeOn);
|
franta-hg@52
|
145 |
wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Sleep off:", valueWidth, timeOff);
|
franta-hg@49
|
146 |
|
franta-hg@52
|
147 |
wprintf(L"\n");
|
franta-hg@49
|
148 |
|
franta-hg@49
|
149 |
|
franta-hg@52
|
150 |
// actual signal generation
|
franta-hg@52
|
151 |
startTimestamp = chrono::high_resolution_clock::now();
|
franta-hg@46
|
152 |
|
franta-hg@52
|
153 |
for (auto i = cycleCount; i > 0; i--) {
|
franta-hg@52
|
154 |
outb(0b00000001, addr); // first data out pin = data out 0 = pin 2 on DB-25 connector
|
franta-hg@52
|
155 |
usleep(timeOn);
|
franta-hg@52
|
156 |
outb(0b00000000, addr);
|
franta-hg@52
|
157 |
usleep(timeOff);
|
franta-hg@52
|
158 |
}
|
franta-hg@44
|
159 |
|
franta-hg@52
|
160 |
finishTimestamp = chrono::high_resolution_clock::now();
|
franta-hg@52
|
161 |
measuredDuration = chrono::duration_cast<chrono::nanoseconds>(finishTimestamp - startTimestamp).count();
|
franta-hg@46
|
162 |
|
franta-hg@52
|
163 |
wprintf(L"%*ls %'*d μs in total\n", labelWidth, L"Deviation:", valueWidth, (measuredDuration-duration*oneSecond*1000)/1000);
|
franta-hg@52
|
164 |
wprintf(L"%*ls %'*d ns in each cycle\n", labelWidth, L"Deviation:", valueWidth, (measuredDuration-duration*oneSecond*1000)/cycleCount);
|
franta-hg@44
|
165 |
|
franta-hg@44
|
166 |
}
|