lpt-signal-generator: c++ auto, TODO komentáře
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 11 Jun 2017 12:51:19 +0200
changeset 5075edae164ebc
parent 49 e1e5db678ce8
child 51 514b9b433dc2
lpt-signal-generator: c++ auto, TODO komentáře
c++/lpt-signal-generator/lpt.cpp
     1.1 --- a/c++/lpt-signal-generator/lpt.cpp	Sun Jun 11 03:05:27 2017 +0200
     1.2 +++ b/c++/lpt-signal-generator/lpt.cpp	Sun Jun 11 12:51:19 2017 +0200
     1.3 @@ -43,6 +43,8 @@
     1.4  // despite the real-time priority, because some additional time is spent in outb() functions
     1.5  // so "duration" means total sleep time
     1.6  
     1.7 +// TODO: data types revision
     1.8 +
     1.9  int main() {
    1.10    //cout << "LPT!" << endl; // same as using printf → breaks all folllowing wprintf() calls, see note above
    1.11  
    1.12 @@ -74,11 +76,11 @@
    1.13    wprintf(L"%*ls %'*d s\n", labelWidth, L"Duration:", valueWidth, duration);
    1.14  
    1.15    // in microseconds:
    1.16 -  int oneSecond = 1000 * 1000;
    1.17 -  int timeOn =  oneSecond *        outputPower  / 100 / baseFreq;
    1.18 -  int timeOff = oneSecond * (100 - outputPower) / 100 / baseFreq;
    1.19 +  auto oneSecond = 1000 * 1000;
    1.20 +  auto timeOn =  oneSecond *        outputPower  / 100 / baseFreq;
    1.21 +  auto timeOff = oneSecond * (100 - outputPower) / 100 / baseFreq;
    1.22  
    1.23 -  int cycleCount = duration * baseFreq;
    1.24 +  auto cycleCount = duration * baseFreq;
    1.25    wprintf(L"%*ls %'*d ×\n", labelWidth, L"Cycle count:", valueWidth, cycleCount);
    1.26    wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time on:",  valueWidth, timeOn);
    1.27    wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time off:", valueWidth, timeOff);
    1.28 @@ -87,6 +89,7 @@
    1.29  
    1.30    wprintf(L"\n");
    1.31  
    1.32 +  // TODO: test whether this address is an parallel port
    1.33    if (ioperm(addr,1,1)) { fwprintf(stderr, L"Access denied to port %#x\n", addr), exit(1); }
    1.34  
    1.35  
    1.36 @@ -95,7 +98,7 @@
    1.37    auto calibrationCycles = 10000;
    1.38    auto calibrationSleepTime = 10;
    1.39  
    1.40 -  for (int i = 0; i < calibrationCycles; i++) {
    1.41 +  for (auto i = calibrationCycles; i > 0;  i--) {
    1.42      outb(0b00000000, addr);
    1.43      usleep(calibrationSleepTime);
    1.44      outb(0b00000000, addr);
    1.45 @@ -147,7 +150,7 @@
    1.46    // actual signal generation
    1.47    startTimestamp = chrono::high_resolution_clock::now();
    1.48  
    1.49 -  for (int i = 0; i < cycleCount; i++) {
    1.50 +  for (auto i = cycleCount; i > 0;  i--) {
    1.51      outb(0b00000001, addr); // first data out pin = data out 0 = pin 2 on DB-25 connector
    1.52      usleep(timeOn);
    1.53      outb(0b00000000, addr);