c++/lpt-signal-generator/lpt.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 11 Jun 2017 03:05:27 +0200
changeset 49 e1e5db678ce8
parent 48 254d5d1bc659
child 50 75edae164ebc
permissions -rw-r--r--
lpt-signal-generator: calibration
     1 /**
     2  * LPT signal generator
     3  * Copyright © 2017 František Kučera (frantovo.cz)
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 
    19 #include <stdlib.h>
    20 #include <iostream>
    21 #include <stdio.h>
    22 #include <math.h>
    23 #include <sys/io.h>
    24 #include <unistd.h>
    25 #include <chrono> // requires -std=c++11
    26 
    27 /**
    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.
    34  */
    35 #include <wchar.h>
    36 #include <locale.h>
    37 
    38 
    39 using namespace std;
    40 
    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
    45 
    46 int main() {
    47   //cout << "LPT!" << endl; // same as using printf → breaks all folllowing wprintf() calls, see note above
    48 
    49   /*
    50    * if setlocale() is missing, unicode characters are replaced with ? or „→“ with „->“ because C/POSIX locale is used, 
    51    * see man setlocale:
    52    * > On startup of the main program, the portable "C" locale is selected as default.
    53    * > If locale is an empty string, "", each part of the locale that should be modified is set according to the environment variables.
    54    */
    55   setlocale(LC_ALL,"");
    56 
    57 
    58   // configuration ----
    59   int addr = 0xe400; // parallel port address; first number of given port in: cat /proc/ioports | grep parport
    60   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
    61   int outputPower = 20; // duty cycle; 100 = 100 %
    62   int duration = 1; // in seconds; total sleep time, see note above
    63   // ------------------
    64 
    65 
    66   int valueWidth =  10; // just for padding of printed values
    67   int labelWidth = -15; // just for padding of printed labels
    68 
    69   // ' = thousand separator
    70   // * = padding
    71   wprintf(L"%*ls %*x\n", labelWidth, L"Parallel port:", valueWidth, addr); // or %#*x – adds 0x prefix
    72   wprintf(L"%*ls %'*d Hz\n", labelWidth, L"Base frequency:", valueWidth, baseFreq);
    73   wprintf(L"%*ls %*d %% duty cycle\n",  labelWidth, L"Output power:", valueWidth, outputPower);
    74   wprintf(L"%*ls %'*d s\n", labelWidth, L"Duration:", valueWidth, duration);
    75 
    76   // in microseconds:
    77   int oneSecond = 1000 * 1000;
    78   int timeOn =  oneSecond *        outputPower  / 100 / baseFreq;
    79   int timeOff = oneSecond * (100 - outputPower) / 100 / baseFreq;
    80 
    81   int cycleCount = duration * baseFreq;
    82   wprintf(L"%*ls %'*d ×\n", labelWidth, L"Cycle count:", valueWidth, cycleCount);
    83   wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time on:",  valueWidth, timeOn);
    84   wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time off:", valueWidth, timeOff);
    85 
    86   //wprintf(L"%*ls %*ls\n", labelWidth, L"unicode test:", valueWidth, L"čeština → …");
    87 
    88   wprintf(L"\n");
    89 
    90   if (ioperm(addr,1,1)) { fwprintf(stderr, L"Access denied to port %#x\n", addr), exit(1); }
    91 
    92 
    93   // calibration
    94   auto startTimestamp = chrono::high_resolution_clock::now();
    95   auto calibrationCycles = 10000;
    96   auto calibrationSleepTime = 10;
    97 
    98   for (int i = 0; i < calibrationCycles; i++) {
    99     outb(0b00000000, addr);
   100     usleep(calibrationSleepTime);
   101     outb(0b00000000, addr);
   102     usleep(calibrationSleepTime);
   103   }
   104 
   105   auto finishTimestamp = chrono::high_resolution_clock::now();
   106   auto measuredDuration = chrono::duration_cast<chrono::nanoseconds>(finishTimestamp - startTimestamp).count();
   107 
   108   auto singleOutbCostNano = (measuredDuration - calibrationCycles*2*calibrationSleepTime*1000)/calibrationCycles/2;
   109   auto singleOutbCostMicro = singleOutbCostNano/1000;
   110 
   111   wprintf(L"%*ls %'*d μs 2× in each calibration cycle\n", labelWidth, L"Single outb():", valueWidth, singleOutbCostMicro);
   112   wprintf(L"%*ls %'*d ns 2× in each calibration cycle\n", labelWidth, L"Single outb():", valueWidth, singleOutbCostNano);
   113 
   114   auto minPower = 100*singleOutbCostNano/(1000*1000*1000/baseFreq);
   115   auto maxPower = 100-minPower;
   116   wprintf(L"%*ls %*d %% feasible duty cycle\n",  labelWidth, L"Minimum power:", valueWidth, minPower);
   117   wprintf(L"%*ls %*d %% feasible duty cycle\n",  labelWidth, L"Maximum power:", valueWidth, maxPower);
   118 
   119   if (singleOutbCostMicro < timeOn && singleOutbCostMicro < timeOff) {
   120     wprintf(L"%*ls %*ls both frequency and duty cycle should be correct\n",  labelWidth, L"Calibration:", valueWidth, L"OK");
   121     timeOn  -= singleOutbCostMicro;
   122     timeOff -= singleOutbCostMicro;
   123   } else if (2*singleOutbCostMicro < (timeOn + timeOff)) {
   124     wprintf(L"%*ls %*ls frequency should be OK, but duty cycle is not feasible\n",  labelWidth, L"Calibration:", valueWidth, L"WARNING");
   125     timeOn  -= singleOutbCostMicro;
   126     timeOff -= singleOutbCostMicro;
   127 
   128     if (timeOn < 0) {
   129       timeOff -= timeOn;
   130       timeOn = 0;
   131     } else {
   132       timeOn -= timeOff;
   133       timeOff = 0;
   134     }
   135   } else {
   136     wprintf(L"%*ls %*ls both frequency and duty cycle are not feasible\n",  labelWidth, L"Calibration:", valueWidth, L"ERROR");
   137     timeOn  = 0;
   138     timeOff = 0;
   139   }
   140 
   141   wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Sleep on:",  valueWidth, timeOn);
   142   wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Sleep off:", valueWidth, timeOff);
   143 
   144   wprintf(L"\n");
   145 
   146 
   147   // actual signal generation
   148   startTimestamp = chrono::high_resolution_clock::now();
   149 
   150   for (int i = 0; i < cycleCount; i++) {
   151     outb(0b00000001, addr); // first data out pin = data out 0 = pin 2 on DB-25 connector
   152     usleep(timeOn);
   153     outb(0b00000000, addr);
   154     usleep(timeOff);
   155   }
   156 
   157   finishTimestamp = chrono::high_resolution_clock::now();
   158   measuredDuration = chrono::duration_cast<chrono::nanoseconds>(finishTimestamp - startTimestamp).count();
   159 
   160   wprintf(L"%*ls %'*d μs in total\n", labelWidth, L"Deviation:", valueWidth, (measuredDuration-duration*oneSecond*1000)/1000);
   161   wprintf(L"%*ls %'*d ns in each cycle\n", labelWidth, L"Deviation:", valueWidth, (measuredDuration-duration*oneSecond*1000)/cycleCount);
   162 
   163 }