// ----------------------------------------------------------------
// SPU-Toolbox
//
//        File: capture-polled-demo.cc (src/demos/capture-polled-demo.cc)
// Description: Demonstrates polled video capture with V4L++
// ----------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS OR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston MA
// 02111-1307, USA.
//
// This file may use Doxygen style comments to facilitate automatic
// documentation generation. More information on Doxygen can be found
// at "http://www.stack.nl/~dimitri/doxygen/index.html".
//
// Author: Ross J. Micheals
//         rjm2@cse.lehigh.edu
//
// (c) 1999-2002 Ross J. Micheals

#include <SPU-Toolbox/SPU-Toolbox.h>
#include <iostream>

bool done  = false;

V4L v4l;
DPPTU pantilt("/dev/ttyS0");

int dot_x = -1;
int dot_y = -1;
int color = -1;

int tint_color = 0;
unsigned char tint_intensity = 40;

void done_callback(void);

// Once registered, this callback will be executed when XIH receieves
// a keypress
//
void keypress_callback(XIH* xih, const XEvent *xev)
{
    char key = xih->extract_key_as_char(xev);
    bool success = true;
    double delta = 5.0;
    switch(key) {
        case 'l':
            delta *= -1;
        case 'h':
            success = pantilt.pan_by(delta);
            break;
        case 'k':
            delta *= -1;
        case 'j':
            success = pantilt.tilt_by(delta);
            break;
        case 'b':
            tint_color = 2;
            tint_intensity = 40;
            break;
        case 'g':
            tint_color = 1;
            tint_intensity = 40;
            break;
        case 'r':
            tint_color = 0;
            tint_intensity = 40;
            break;
        case 'c':
            tint_intensity = 0;
            break;
        case 'q':
            done_callback();
            break;
    }
    if(!success)
        cerr << "Pan/Tilt operation failed.\n";
    return;
}

// color = 0,1,2 corresponds to red, green, blue
void tint_image(SPU_Image image, int color, unsigned char intensity)
{
    if(color > 2 || color < 0)
        return;
    for(int i = 0; i < image.get_width(); ++i)
        for(int j = 0; j < image.get_height(); ++j)
            *(image.fast_get_pixel(i,j) + color) += intensity;
    return;
}

// Called when the main XIH control thread dies.
//
void done_callback(void) {

  // Tell V4L++ to stop capturing.
  v4l.stop();

  // Terminate the main loop
  done = true;

  return;
}

int main(int argc, char** argv) {

  char *cm = "rgb24";

  int width  = 640;
  int height = 480;

  XIH main_win;
  main_win.init(width, height, cm, "capture-polled-demo");
  main_win.register_callback(XIH::CB_KeyPress, keypress_callback);

  // Set the function to be called when XIH is finished. This must be
  // set *before* XIH is started.
  XIH_set_finished_callback(done_callback);

  if (!v4l.init(cm, 1, width, height, "/dev/video")) 
    exit(1);

  SPU_Image main_image(cm, width, height);

  // Start the XIH thread. This function will wait until the XIH
  // thread is started before it returns.
  XIH::start();

  // Start the V4L thread. Like the XIH thread, this funciton will
  // wait until the V4L thread is started before it returns. Notice
  // that unlike XIH, there is a V4L thread per instance/device.
  v4l.start_polled_mode();

  while (!done) {

    // Make sure we can lock and draw to the image first!
    if (XIH_lock() && main_win.can_draw()) {
      v4l.put_Image(main_image);
      tint_image(main_image, tint_color, tint_intensity);
      main_win.fill(main_image);
      main_win.refresh();
      XIH_unlock();
    }
  }


  // Prevent premature exit.
  XIH::stop_all();
  XIH::wait();

  return(0);
}






