/**
* LPT signal generator
* Copyright © 2017 František Kučera (frantovo.cz)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include
#include
#include
#include
#include
#include
#include // requires -std=c++11
/**
* can not mix printf and wprintf
* see https://stackoverflow.com/questions/8681623/printf-and-wprintf-in-single-c-code
* > This is to be expected; your code is invoking undefined behavior.
* > Per the C standard, each FILE stream has associated with it an "orientation" (either "byte" or "wide)
* > which is set by the first operation performed on it, and which can be inspected with the fwide function.
* > Calling any function whose orientation conflicts with the orientation of the stream results in undefined behavior.
*/
#include
#include
using namespace std;
// run this program: make run
// depending on frequency and machine performance the total time will be more than given duration
// despite the real-time priority, because some additional time is spent in outb() functions
// so "duration" means total sleep time
// TODO: data types revision
int main() {
//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,"");
// 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
// ' = 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;
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"\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); }
// 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);
}
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 (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();
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);
}