c++/lpt-signal-generator/lpt.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 04 Jun 2017 14:59:13 +0200
changeset 45 ea8642c17495
parent 44 896eca3d40d8
child 46 616e71c2d754
permissions -rw-r--r--
lpt-signal-generator: parametrizace, unicode
     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 
    26 /**
    27  * can not mix printf and wprintf
    28  * see https://stackoverflow.com/questions/8681623/printf-and-wprintf-in-single-c-code
    29  * > This is to be expected; your code is invoking undefined behavior.
    30  * > Per the C standard, each FILE stream has associated with it an "orientation" (either "byte" or "wide)
    31  * > which is set by the first operation performed on it, and which can be inspected with the fwide function.
    32  * > Calling any function whose orientation conflicts with the orientation of the stream results in undefined behavior.
    33  */
    34 #include <wchar.h>
    35 #include <locale.h>
    36 
    37 
    38 using namespace std;
    39 
    40 // run this program: g++ lpt.cpp && time chrt 1 ./a.out
    41 // depending to frequency and machine performance the total time will be more than given duration
    42 // despite the real-time priority, because some additional time is spent in outb() functions
    43 // so "duration" means total sleep time
    44 
    45 int main() {
    46   //cout << "LPT!" << endl; // same as using printf → breaks all folllowing wprintf() calls, see note above
    47 
    48   /*
    49    * if setlocale() is missing, unicode characters are replaced with ? or „→“ with „->“ because C/POSIX locale is used, 
    50    * see man setlocale:
    51    * > On startup of the main program, the portable "C" locale is selected as default.
    52    * > If locale is an empty string, "", each part of the locale that should be modified is set according to the environment variables.
    53    */
    54   setlocale(LC_ALL,"");
    55 
    56   int addr = 0xe400; // parallel port address; first number of given port in: cat /proc/ioports | grep parport
    57   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
    58   int outputPower = 10; // duty cycle; 100 = 100 %
    59   int duration = 5; // in seconds; total sleep time, see note above
    60 
    61   int valueWidth =  10; // just for padding of printed values
    62   int labelWidth = -15; // just for padding of printed labels
    63 
    64   // ' = thousand separator
    65   // * = padding
    66   wprintf(L"%*ls %*x\n",     labelWidth, L"Parallel port:", valueWidth, addr); // or %#*x – adds 0x prefix
    67   wprintf(L"%*ls %'*d Hz\n", labelWidth, L"Base frequency:", valueWidth, baseFreq);
    68   wprintf(L"%*ls %*d %%\n",  labelWidth, L"Output power:", valueWidth, outputPower);
    69   wprintf(L"%*ls %'*d s\n",  labelWidth, L"Duration:", valueWidth, duration);
    70 
    71   // in microseconds:
    72   int oneSecond = 1000 * 1000;
    73   int timeOn =  oneSecond *        outputPower  / 100 / baseFreq;
    74   int timeOff = oneSecond * (100 - outputPower) / 100 / baseFreq;
    75 
    76   int cycleCount = duration * baseFreq;
    77   wprintf(L"%*ls %'*d ×\n", labelWidth, L"Cycle count:", valueWidth, cycleCount);
    78   wprintf(L"%*ls %'*d μs in each cycle\n", labelWidth, L"Time on:",  valueWidth, timeOn);
    79   wprintf(L"%*ls %'*d μs in each cycle\n", labelWidth, L"Time off:", valueWidth, timeOff);
    80 
    81   wprintf(L"%*ls %*ls\n", labelWidth, L"unicode test:", valueWidth, L"čeština → …");
    82 
    83 
    84   if (ioperm(addr,1,1)) { fwprintf(stderr, L"Access denied to port %#x\n", addr), exit(1); }
    85 
    86   outb(0b00000000, addr);
    87 
    88   for (int i = 0; i < cycleCount; i++) {
    89     outb(0b00000001, addr);
    90     usleep(timeOn);
    91     outb(0b00000000, addr);
    92     usleep(timeOff);
    93   }
    94 
    95   wprintf(L"finished\n");
    96 
    97 }