// ----------------------------------------------------------------------------------------
//
//  demos/hammmer_i2c/main.cpp
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
//    
//  Demonstrate support for the TinCan Tools Hammer w/ I2C communications
//
//  Copyright (C) 2009 Bob Cook
//
//      This program is free software; you can redistribute it and/or modify it
//      under the terms of the GNU General Public License as published by the
//      Free Software Foundation; either version 2 of the License, or (at your
//      option) any later version.
//    
//      This program is distributed in the hope that it will be useful, but 
//      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
//      or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
//      for more details.
//    
//      You should have received a copy of the GNU General Public License along
//      with this program; if not, write to the Free Software Foundation, Inc., 
//      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
// ----------------------------------------------------------------------------------------


#include <stdio.h>
#include <unistd.h>

#include "packages/s3c2410a/cfax_iic/cfax_iic.h"
#include "packages/s3c2410a/device/gpio.h"
#include "packages/common/util/misc.h"


// ----------------------------------------------------------------------------------------

static void led_init()
{
    gpio_configure_portf_pin( 0, gpio_mode_output );
}

static void led_on()
{
    gpio_clear_portf_pin( 0 );
}

static void led_off()
{
    gpio_set_portf_pin( 0 );
}


// ----------------------------------------------------------------------------------------

int main( int argc, const char* argv[] )
{
    led_init();

    cfaxiic_clear_screen();

    cfaxiic_write( "Hello World!", 0, 0 );
    cfaxiic_write( "CFAX 128 x 64 LCD", 0, 1 );
    cfaxiic_write( "Neat Stuff from SFE", 0, 3 );

    cfaxiic_rect( 10, 50, 10, 7 );
    cfaxiic_fill( 23, 50, 10, 7 );
    cfaxiic_fill( 10, 40, 10, 7 );

    cfaxiic_line( 25, 40, 60, 60, true );
    cfaxiic_line( 25, 40, 70, 60, true );
    cfaxiic_line( 25, 40, 80, 60, true );
    cfaxiic_line( 25, 40, 90, 60, true );

    uint8_t y0  = 40;
    int8_t  dir = 1;

    for ( ;; )
    {
        uint8_t y1 = 100 - y0;

        cfaxiic_line( 100, y0, 120, y1, true );
        usleep( 125000 );
        cfaxiic_line( 100, y0, 120, y1, false );

        y0 += dir;

        if ( y0 > 59 )
        {
            dir = -1;
        }
        else if ( y0 < 41 )
        {
            dir = 1;
        }

        uint8_t touch_x;
        uint8_t touch_y;

        if ( cfaxiic_touchscreen( &touch_x, &touch_y ) )
        {
            led_on();
            printf( "touch: (%d, %d)\n", touch_x, touch_y );
        }
        else
        {
            led_off();
        }
    }

    led_off();

    return 0;
}


// ----------------------------------------------------------------------------------------
//  end of main.cpp
// ----------------------------------------------------------------------------------------
