franta-hg@44: /**
franta-hg@44: * LPT signal generator
franta-hg@44: * Copyright © 2017 František Kučera (frantovo.cz)
franta-hg@44: *
franta-hg@44: * This program is free software: you can redistribute it and/or modify
franta-hg@44: * it under the terms of the GNU General Public License as published by
franta-hg@44: * the Free Software Foundation, either version 3 of the License, or
franta-hg@44: * (at your option) any later version.
franta-hg@44: *
franta-hg@44: * This program is distributed in the hope that it will be useful,
franta-hg@44: * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@44: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@44: * GNU General Public License for more details.
franta-hg@44: *
franta-hg@44: * You should have received a copy of the GNU General Public License
franta-hg@44: * along with this program. If not, see .
franta-hg@44: */
franta-hg@44:
franta-hg@44: #include
franta-hg@44: #include
franta-hg@44: #include
franta-hg@44: #include
franta-hg@44: #include
franta-hg@44: #include
franta-hg@46: #include // requires -std=c++11
franta-hg@44:
franta-hg@45: /**
franta-hg@45: * can not mix printf and wprintf
franta-hg@45: * see https://stackoverflow.com/questions/8681623/printf-and-wprintf-in-single-c-code
franta-hg@45: * > This is to be expected; your code is invoking undefined behavior.
franta-hg@45: * > Per the C standard, each FILE stream has associated with it an "orientation" (either "byte" or "wide)
franta-hg@45: * > which is set by the first operation performed on it, and which can be inspected with the fwide function.
franta-hg@45: * > Calling any function whose orientation conflicts with the orientation of the stream results in undefined behavior.
franta-hg@45: */
franta-hg@45: #include
franta-hg@45: #include
franta-hg@45:
franta-hg@45:
franta-hg@44: using namespace std;
franta-hg@44:
franta-hg@47: // run this program: make run
franta-hg@46: // depending on frequency and machine performance the total time will be more than given duration
franta-hg@45: // despite the real-time priority, because some additional time is spent in outb() functions
franta-hg@45: // so "duration" means total sleep time
franta-hg@44:
franta-hg@44: int main() {
franta-hg@45: //cout << "LPT!" << endl; // same as using printf → breaks all folllowing wprintf() calls, see note above
franta-hg@44:
franta-hg@45: /*
franta-hg@45: * if setlocale() is missing, unicode characters are replaced with ? or „→“ with „->“ because C/POSIX locale is used,
franta-hg@45: * see man setlocale:
franta-hg@45: * > On startup of the main program, the portable "C" locale is selected as default.
franta-hg@45: * > If locale is an empty string, "", each part of the locale that should be modified is set according to the environment variables.
franta-hg@45: */
franta-hg@45: setlocale(LC_ALL,"");
franta-hg@44:
franta-hg@45: int addr = 0xe400; // parallel port address; first number of given port in: cat /proc/ioports | grep parport
franta-hg@45: 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@45: int outputPower = 10; // duty cycle; 100 = 100 %
franta-hg@46: int duration = 1; // in seconds; total sleep time, see note above
franta-hg@44:
franta-hg@45: int valueWidth = 10; // just for padding of printed values
franta-hg@45: int labelWidth = -15; // just for padding of printed labels
franta-hg@44:
franta-hg@45: // ' = thousand separator
franta-hg@45: // * = padding
franta-hg@46: wprintf(L"%*ls %*x\n", labelWidth, L"Parallel port:", valueWidth, addr); // or %#*x – adds 0x prefix
franta-hg@45: wprintf(L"%*ls %'*d Hz\n", labelWidth, L"Base frequency:", valueWidth, baseFreq);
franta-hg@46: wprintf(L"%*ls %*d %% duty cycle\n", labelWidth, L"Output power:", valueWidth, outputPower);
franta-hg@46: wprintf(L"%*ls %'*d s\n", labelWidth, L"Duration:", valueWidth, duration);
franta-hg@44:
franta-hg@45: // in microseconds:
franta-hg@45: int oneSecond = 1000 * 1000;
franta-hg@45: int timeOn = oneSecond * outputPower / 100 / baseFreq;
franta-hg@45: int timeOff = oneSecond * (100 - outputPower) / 100 / baseFreq;
franta-hg@44:
franta-hg@45: int cycleCount = duration * baseFreq;
franta-hg@45: wprintf(L"%*ls %'*d ×\n", labelWidth, L"Cycle count:", valueWidth, cycleCount);
franta-hg@46: wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time on:", valueWidth, timeOn);
franta-hg@46: wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time off:", valueWidth, timeOff);
franta-hg@44:
franta-hg@46: //wprintf(L"%*ls %*ls\n", labelWidth, L"unicode test:", valueWidth, L"čeština → …");
franta-hg@44:
franta-hg@45:
franta-hg@45: if (ioperm(addr,1,1)) { fwprintf(stderr, L"Access denied to port %#x\n", addr), exit(1); }
franta-hg@45:
franta-hg@45: outb(0b00000000, addr);
franta-hg@45:
franta-hg@46: auto startTimestamp = chrono::high_resolution_clock::now();
franta-hg@46:
franta-hg@45: for (int i = 0; i < cycleCount; i++) {
franta-hg@46: outb(0b00000001, addr); // first data out pin = data out 0 = pin 2 on DB-25 connector
franta-hg@45: usleep(timeOn);
franta-hg@44: outb(0b00000000, addr);
franta-hg@45: usleep(timeOff);
franta-hg@44: }
franta-hg@44:
franta-hg@46: auto finishTimestamp = chrono::high_resolution_clock::now();
franta-hg@46: auto measuredDuration = chrono::duration_cast(finishTimestamp - startTimestamp).count();
franta-hg@46:
franta-hg@46: wprintf(L"%*ls %'*d μs 2× in each cycle\n", labelWidth, L"single outb():", valueWidth, (measuredDuration-duration*oneSecond*1000)/cycleCount/2/1000);
franta-hg@46: wprintf(L"%*ls %'*d ns 2× in each cycle\n", labelWidth, L"single outb():", valueWidth, (measuredDuration-duration*oneSecond*1000)/cycleCount/2);
franta-hg@44:
franta-hg@44: }