# HG changeset patch
# User František Kučera <franta-hg@frantovo.cz>
# Date 1497178279 -7200
# Node ID 75edae164ebc5397550fdee2235461072fbd1c3e
# Parent  e1e5db678ce841bfc69e4b34603bbdbe935e3cf3
lpt-signal-generator: c++ auto, TODO komentáře

diff -r e1e5db678ce8 -r 75edae164ebc c++/lpt-signal-generator/lpt.cpp
--- a/c++/lpt-signal-generator/lpt.cpp	Sun Jun 11 03:05:27 2017 +0200
+++ b/c++/lpt-signal-generator/lpt.cpp	Sun Jun 11 12:51:19 2017 +0200
@@ -43,6 +43,8 @@
 // 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
 
@@ -74,11 +76,11 @@
   wprintf(L"%*ls %'*d s\n", labelWidth, L"Duration:", valueWidth, duration);
 
   // in microseconds:
-  int oneSecond = 1000 * 1000;
-  int timeOn =  oneSecond *        outputPower  / 100 / baseFreq;
-  int timeOff = oneSecond * (100 - outputPower) / 100 / baseFreq;
+  auto oneSecond = 1000 * 1000;
+  auto timeOn =  oneSecond *        outputPower  / 100 / baseFreq;
+  auto timeOff = oneSecond * (100 - outputPower) / 100 / baseFreq;
 
-  int cycleCount = duration * 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);
@@ -87,6 +89,7 @@
 
   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); }
 
 
@@ -95,7 +98,7 @@
   auto calibrationCycles = 10000;
   auto calibrationSleepTime = 10;
 
-  for (int i = 0; i < calibrationCycles; i++) {
+  for (auto i = calibrationCycles; i > 0;  i--) {
     outb(0b00000000, addr);
     usleep(calibrationSleepTime);
     outb(0b00000000, addr);
@@ -147,7 +150,7 @@
   // actual signal generation
   startTimestamp = chrono::high_resolution_clock::now();
 
-  for (int i = 0; i < cycleCount; i++) {
+  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);