/*!
  \file
  \brief データ通信速度の計測用プログラム

  \author Satofumi KAMIMURA

  $Id: measureCaptureSecond.cpp 1739 2010-03-06 07:43:32Z satofumi $
*/

#include "UrgDevice.h"
#include "ticks.h"
#include "delay.h"
#include <iostream>
#include <cstdlib>
#include <SDL.h>

using namespace qrk;
using namespace std;


int main(int argc, char *argv[])
{
  //const char* device = "COM3";
  //const char* device = "/dev/usb/ttyUSB0";
  const char* device = "/dev/ttyACM0";

  UrgDevice urg;
  if (! urg.connect(device, 57600)) {
    cout << "UrgDevice::connect: " << urg.what() << endl;
    exit(1);
  }

  urg.setCaptureMode(AutoCapture);

  enum { CaptureTimes = 20 };
  std::vector<long> data;

  int first_ticks = ticks();
  int previous_ticks = first_ticks;
  for (int i = 0; i < CaptureTimes;) {
    int n = urg.capture(data);
    if (n <= 0) {
      delay(1);
      continue;
    }
    ++i;
    //cout << "n = " << n << endl;
    int current_ticks = ticks();
    cout << i << " [times], " << (current_ticks - first_ticks) << " [msec], "
         << " (" << (current_ticks - previous_ticks) << ")" << endl;
    previous_ticks = current_ticks;
  }
  int last_ticks = ticks();

  int total_msec = last_ticks - first_ticks;
  cout << CaptureTimes << " [scans]" << endl;
  cout << (1.0 * total_msec / CaptureTimes) << " [msec] / [scans]" << endl;

  return 0;
}
