/*##############################################################*/
/* 																*/
/* File		: examples.c 										*/
/*																*/
/* Project	: TFT for Raspberry Pi Revision 2					*/
/* 																*/
/* Date		: 2014-08-14   	    last update: 2014-08-14			*/
/* 																*/
/* Author	: Hagen Ploog   									*/
/*		  	  Timo Pfander										*/
/* 																*/
/* IDE	 	: Geany 1.22										*/
/* Compiler : gcc (Debian 4.6.3-14+rpi1) 4.6.3					*/
/*																*/
/* Copyright (C) 2013 admatec GmbH								*/
/*																*/
/*																*/	
/* Description  :												*/
/* 	Exmples.c includes the following functions to demonstrate 	*/
/*	some opportunities:											*/
/*		- depict one BMP file on the TFT 						*/
/*		- depict basic drawings which were created with the 	*/
/*		  RAIO graphic controller								*/
/*		- write some text on the TFT							*/
/*		- draw functions like sine, cosine, parabola, ...		*/
/*																*/
/*																*/
/* License:														*/
/*																*/
/*	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 3 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, 			*/
/*	see <http://www.gnu.org/licenses/>.							*/
/*																*/
/*																*/
/* Revision History:											*/
/*																*/
/*	Version 1.0 - Initial release								*/
/*																*/
/*																*/
/*																*/
/*##############################################################*/


#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include "RAIO8870.h"
#include "bmp.h"
#include "examples.h"


// load and depict a BMP file
// ---------------------------------------------
void example_DepictBMP( char const *file_name  )
{
	uint16_t picture[1][ PICTURE_PIXELS ];
	
	RAIO_set_cursor( 0, 0 );
	Read_bmp2memory ( file_name, &picture[0][ PICTURE_PIXELS-1 ] );
	RAIO_Write_Picture ( &picture[0][0], PICTURE_PIXELS );	
}


// draw with RAIO
// ---------------------------------------------
void example_DrawWithRAIO( void )
{	
	// clear display with black color
	Text_Background_Color ( COLOR_BLACK );
	RAIO_clear_screen();
	
	// rectangle		
	Text_Foreground_Color ( COLOR_BLUE );
	Draw_Square (210, 150, 260, 200);
	RAIO_StartDrawing ( SQUARE_FILL );
	
	// line
	Text_Foreground_Color ( COLOR_GREEN );
	Draw_Line ( 10, 230, 310, 10 );
	RAIO_StartDrawing ( LINE );
	
	// circle
	Text_Foreground_Color ( COLOR_RED );
	Draw_Circle ( 90, 65, 25 );
	RAIO_StartDrawing ( CIRCLE_FILL );
}


// write text
// ---------------------------------------------
void example_WriteText( unsigned char *text )
{
	// clear display with black color
	Text_Background_Color ( COLOR_BLACK );
	RAIO_clear_screen();
	
	// write text in different size and colors
    RAIO_SetFontSizeFactor ( 0 );
    RAIO_print_text ( 10, 10, text, COLOR_BLACK , COLOR_WHITE );
    
    RAIO_SetFontSizeFactor ( 5 );
    RAIO_print_text( 30, 30, text, COLOR_BLACK, COLOR_RED );
       
    RAIO_SetFontSizeFactor ( 10 );
    RAIO_print_text ( 60, 70, text, COLOR_BLACK, COLOR_BLUE );
    
    RAIO_SetFontSizeFactor ( 15 );
    RAIO_print_text ( 80, 120, text, COLOR_BLACK, COLOR_GREEN );
}


// draw sinus
// ---------------------------------------------

// defines the x-intercept and y-intercept on the TFT
#define window_left 	-M_PI
#define window_right	 M_PI
#define window_up		 2	
#define window_down		-2


//  				 	   windows_up
//
//							   ↑
//  				  _ _ _ _ _|_ _ _ _ _
//  				 |		   |		 |  
//  				 |		   |		 |
//  				 | 		   |		 |
//   windows_left  --|---------|---------|--→  windows_right
//  				 |	 	   | 		 |
//  				 | 	 	   |		 |
//  			 	 |_ _ _ _ _|_ _ _ _ _| 
//							   |
//					
//  				      windows_down



void example_DrawFunction( int16_t function )
{
	float_t x_real, y_real;
	int16_t count;
	
	// clear display with black color
	Text_Background_Color ( COLOR_BLACK );
	RAIO_clear_screen();
		
	// draw x-axis 
	draw_coords_line( window_left, 0, window_right, 0 );
	for( count = (int16_t)window_left; count < (int16_t)window_right; count++ )
	{		
		Draw_Line ( scale_x( count ), scale_y( window_up*0.01 ), scale_x( count ), scale_y( window_down*0.01 ) );
		Text_Foreground_Color ( COLOR_WHITE );
		RAIO_StartDrawing ( LINE );
	}
				
	// draw y-axis 
	draw_coords_line( 0, window_up, 0, window_down );
	for( count = (int16_t)window_down; count < (int16_t)window_up; count++ )
	{		
		Draw_Line ( scale_x( window_left*0.01 ), scale_y( count ), scale_x( window_right*0.01 ), scale_y( count ) );
		Text_Foreground_Color ( COLOR_WHITE );
		RAIO_StartDrawing ( LINE );
	}
	
			
	// draw function	
	for( x_real = window_left; x_real < window_right; x_real=x_real+0.02 )
	{
		switch (function) // -> see FUNCTIONS
		{			
			case SIN:      y_real = sin( x_real );   break;
			case COS:      y_real = cos( x_real );   break; 
			case TAN:      y_real = tan( x_real );   break;
			case PARABOLA: y_real = x_real * x_real; break; 
			case EXPONENT: y_real = exp( x_real );   break; 
			case LOGN    : y_real = log( x_real );   break; 
			default: break;			
		};
		set_point( x_real, y_real);
	} 	
}


// transform real world data in TFT data
// ---------------------------------------------

// transform x-value
uint16_t scale_x( float_t x )
{
	return ( (uint16_t) (( x - window_left ) * ( DISPLAY_WIDTH / (window_right-window_left))) );
}

// transform y-value
uint16_t scale_y( float_t y )
{
	uint16_t temp_y;
	
	temp_y = DISPLAY_HEIGHT - (uint16_t) (( y - window_down) *  (DISPLAY_HEIGHT / (window_up-window_down)));	
	if( temp_y > 239 ) { temp_y--; }
	
	return ( temp_y );
}


// draw coordinate system and function points
// ---------------------------------------------

// draw coordinate system
void draw_coords_line (float_t x1, float_t y1, float_t x2, float_t y2)
{
	Text_Foreground_Color( COLOR_WHITE );
	Draw_Line( scale_x( x1 ), scale_y( y1 ), scale_x( x2 ), scale_y( y2 ) );
	RAIO_StartDrawing( LINE );
}

// draw function point
void set_point ( float_t x_real, float_t y_real )
{
	if ((x_real> window_left) & (x_real < window_right) & (y_real > window_down) & (y_real <window_up))
	{		
		Text_Foreground_Color( COLOR_WHITE );
		Draw_Line( scale_x( x_real ), scale_y( y_real ), scale_x( x_real ), scale_y( y_real ) );		
		RAIO_StartDrawing( LINE );
	}
}


// touch the soft buttons on TFT
// ---------------------------------------------
void example_Touch ()
{
	int i=0, rt;
	uint8_t btn_old_state = up;
			
	RAIO_clear_screen();
	
	RAIO_SetFontSizeFactor ( 0 );
	

	while(1)
	{	
		// get new touch value
		rt = RAIO_gettouch();
		
		/*
		// show touch values and state in console
		printf( "\n.... x=%d", my_touch.touch_x );
		printf( "\n.... y=%d", my_touch.touch_y );		
		printf( "\n state = %d", my_touch.state );
		*/
		
		// check for exit button
		if ( (my_touch.state == pressed) && (my_touch.touch_x > 90) && (my_touch.touch_x < 115) && (my_touch.touch_y > 86) && (my_touch.touch_y < 106) )
		{
			// draw exit button 
			Text_Foreground_Color ( COLOR_YELLOW );
			Draw_Square( (DISPLAY_WIDTH/2)-25, (DISPLAY_HEIGHT/2)-15, (DISPLAY_WIDTH/2)+25, (DISPLAY_HEIGHT/2)+15 );
			RAIO_StartDrawing ( SQUARE_FILL );
			RAIO_print_text( (DISPLAY_WIDTH/2)-16, (DISPLAY_HEIGHT/2)-9, "EXIT",  COLOR_YELLOW, COLOR_BLACK );
			
			// wait for leaving the touch panel
			while((my_touch.state == pressed))
			{	
				bcm2835_delayMicroseconds( 5000 );
				rt = RAIO_gettouch();		
			}
			
			// exit while loop
			break;
		}		
		else if ( ((my_touch.state == pressed) || (my_touch.state == down) || (my_touch.state == up)) && (btn_old_state != my_touch.state) )
		{	
			// safe old touch state
			btn_old_state = my_touch.state;
			
			if( my_touch.touch_x > 25 && my_touch.touch_x < 102 )
			{	
				// button 1 is active
				if( my_touch.touch_y > 21 && my_touch.touch_y < 96 )
				{			
					Text_Foreground_Color ( COLOR_GREEN );
					Draw_Square( 6, 6, (DISPLAY_WIDTH/2)-3, (DISPLAY_HEIGHT/2)-3 );
					RAIO_StartDrawing ( SQUARE_FILL );
					RAIO_print_text( (DISPLAY_WIDTH/4)-45, (DISPLAY_HEIGHT/4)-20, "Button 1 is", COLOR_GREEN, COLOR_BLACK );
					switch (my_touch.state)
					{
						case down : 	RAIO_print_text( (DISPLAY_WIDTH/4)-16, (DISPLAY_HEIGHT/4), "down", COLOR_GREEN, COLOR_BLACK );
										break;
						case up : 		RAIO_print_text( (DISPLAY_WIDTH/4)-9, (DISPLAY_HEIGHT/4), "up", COLOR_GREEN, COLOR_BLACK );
										break;
						case pressed:	RAIO_print_text( (DISPLAY_WIDTH/4)-30, (DISPLAY_HEIGHT/4), "pressed", COLOR_GREEN, COLOR_BLACK );
										break;
						default : 		break;
					}			
				}
				// button 3 is active
				if( my_touch.touch_y > 101 && my_touch.touch_y < 178 )
				{
					Text_Foreground_Color ( COLOR_GREEN );
					Draw_Square( 6, (DISPLAY_HEIGHT/2)+4, (DISPLAY_WIDTH/2)-3, DISPLAY_HEIGHT-6 );
					RAIO_StartDrawing ( SQUARE_FILL );
					RAIO_print_text( (DISPLAY_WIDTH/4)-45, (DISPLAY_HEIGHT/4)*3-20, "Button 3 is", COLOR_GREEN, COLOR_BLACK );
					switch (my_touch.state)
					{
						case down : 	RAIO_print_text( (DISPLAY_WIDTH/4)-16, (DISPLAY_HEIGHT/4)*3, "down", COLOR_GREEN, COLOR_BLACK );
										break;
						case up : 		RAIO_print_text( (DISPLAY_WIDTH/4)-9, (DISPLAY_HEIGHT/4)*3, "up", COLOR_GREEN, COLOR_BLACK );
										break;
						case pressed:	RAIO_print_text( (DISPLAY_WIDTH/4)-30, (DISPLAY_HEIGHT/4)*3, "pressed", COLOR_GREEN, COLOR_BLACK );
										break;
						default : 		break;
					}
				}
				
			}
			
			if( my_touch.touch_x > 107 && my_touch.touch_x < 182 )
			{	
				// button 2 is active
				if( my_touch.touch_y > 21 && my_touch.touch_y < 96 )
				{
					Text_Foreground_Color ( COLOR_GREEN );
					Draw_Square( (DISPLAY_WIDTH/2)+4, 6, DISPLAY_WIDTH-6, (DISPLAY_HEIGHT/2)-3 );
					RAIO_StartDrawing ( SQUARE_FILL );
					RAIO_print_text( (DISPLAY_WIDTH/4)*3-45, (DISPLAY_HEIGHT/4)-20, "Button 2 is", COLOR_GREEN, COLOR_BLACK );
					switch (my_touch.state)
					{
						case down : 	RAIO_print_text( (DISPLAY_WIDTH/4)*3-16, (DISPLAY_HEIGHT/4), "down", COLOR_GREEN, COLOR_BLACK );
										break;
						case up : 		RAIO_print_text( (DISPLAY_WIDTH/4)*3-9, (DISPLAY_HEIGHT/4), "up", COLOR_GREEN, COLOR_BLACK );
										break;
						case pressed:	RAIO_print_text( (DISPLAY_WIDTH/4)*3-30, (DISPLAY_HEIGHT/4), "pressed", COLOR_GREEN, COLOR_BLACK );
										break;
						default : 		break;
					}
				}
				// button 4 is active
				if( my_touch.touch_y > 101 && my_touch.touch_y < 178 )
				{
					Text_Foreground_Color ( COLOR_GREEN );
					Draw_Square( (DISPLAY_WIDTH/2)+4, (DISPLAY_HEIGHT/2)+4, DISPLAY_WIDTH-6, DISPLAY_HEIGHT-6 );
					RAIO_StartDrawing ( SQUARE_FILL );
					RAIO_print_text( (DISPLAY_WIDTH/4)*3-45, (DISPLAY_HEIGHT/4)*3-20, "Button 4 is", COLOR_GREEN, COLOR_BLACK );
					switch (my_touch.state)
					{
						case down : 	RAIO_print_text( (DISPLAY_WIDTH/4)*3-16, (DISPLAY_HEIGHT/4)*3, "down", COLOR_GREEN, COLOR_BLACK );
										break;
						case up : 		RAIO_print_text( (DISPLAY_WIDTH/4)*3-9, (DISPLAY_HEIGHT/4)*3, "up", COLOR_GREEN, COLOR_BLACK );
										break;
						case pressed:	RAIO_print_text( (DISPLAY_WIDTH/4)*3-30, (DISPLAY_HEIGHT/4)*3, "pressed", COLOR_GREEN, COLOR_BLACK );
										break;
						default : 		break;
					}
				}
			}
		}
		else if ( (btn_old_state != my_touch.state) ) 
		{
			// safe old touch state
			btn_old_state = my_touch.state;
			
			switch (my_touch.state)
			{
				case no_touch : btn_set_all_open();
								break;	
				default : 		break;
			}
		}	
		// time of minimum 15000µs is required		
		bcm2835_delayMicroseconds( 100000 );
	}
}


// set all soft-buttons to open
// ---------------------------------------------
void btn_set_all_open()
{
	// draw button dimensions
	Text_Foreground_Color( COLOR_BLACK );
	Draw_Square( 5, 5, (DISPLAY_WIDTH/2)-2, (DISPLAY_HEIGHT/2)-2 );
	Draw_Square( (DISPLAY_WIDTH/2)+3, 5, DISPLAY_WIDTH-5, (DISPLAY_HEIGHT/2)-2 );
	Draw_Square( 5, (DISPLAY_HEIGHT/2)+3, (DISPLAY_WIDTH/2)-2, DISPLAY_HEIGHT-5 );
	Draw_Square( (DISPLAY_WIDTH/2)+3, (DISPLAY_HEIGHT/2)+3, DISPLAY_WIDTH-5, DISPLAY_HEIGHT-5 );
	
	// set text and color for each open buttons
	Text_Foreground_Color ( COLOR_RED );
	Draw_Square( 6, 6, (DISPLAY_WIDTH/2)-3, (DISPLAY_HEIGHT/2)-3 );
	RAIO_StartDrawing ( SQUARE_FILL );
	RAIO_print_text( (DISPLAY_WIDTH/4)-45, (DISPLAY_HEIGHT/4)-20, "Button 1 is", COLOR_RED, COLOR_BLACK );
	RAIO_print_text( (DISPLAY_WIDTH/4)-16, (DISPLAY_HEIGHT/4)   , "open", COLOR_RED, COLOR_BLACK );
	
	Text_Foreground_Color ( COLOR_RED );
	Draw_Square( (DISPLAY_WIDTH/2)+4, 6, DISPLAY_WIDTH-6, (DISPLAY_HEIGHT/2)-3 );
	RAIO_StartDrawing ( SQUARE_FILL );
	RAIO_print_text( (DISPLAY_WIDTH/4)*3-45, (DISPLAY_HEIGHT/4)-20, "Button 2 is", COLOR_RED, COLOR_BLACK );
	RAIO_print_text( (DISPLAY_WIDTH/4)*3-16, (DISPLAY_HEIGHT/4)   , "open",  COLOR_RED, COLOR_BLACK );
	
	Text_Foreground_Color ( COLOR_RED );
	Draw_Square( 6, (DISPLAY_HEIGHT/2)+4, (DISPLAY_WIDTH/2)-3, DISPLAY_HEIGHT-6 );
	RAIO_StartDrawing ( SQUARE_FILL );
	RAIO_print_text( (DISPLAY_WIDTH/4)-45, (DISPLAY_HEIGHT/4)*3-20, "Button 3 is",  COLOR_RED, COLOR_BLACK );
	RAIO_print_text( (DISPLAY_WIDTH/4)-16, (DISPLAY_HEIGHT/4)*3   , "open",  COLOR_RED, COLOR_BLACK );
		
	Text_Foreground_Color ( COLOR_RED );
	Draw_Square( (DISPLAY_WIDTH/2)+4, (DISPLAY_HEIGHT/2)+4, DISPLAY_WIDTH-6, DISPLAY_HEIGHT-6 );
	RAIO_StartDrawing ( SQUARE_FILL );
	RAIO_print_text( (DISPLAY_WIDTH/4)*3-45, (DISPLAY_HEIGHT/4)*3-20, "Button 4 is",  COLOR_RED, COLOR_BLACK );
	RAIO_print_text( (DISPLAY_WIDTH/4)*3-16, (DISPLAY_HEIGHT/4)*3   , "open",  COLOR_RED, COLOR_BLACK );
	
	// draw exit button 
	Text_Foreground_Color ( COLOR_BLUE );
	Draw_Square( (DISPLAY_WIDTH/2)-25, (DISPLAY_HEIGHT/2)-15, (DISPLAY_WIDTH/2)+25, (DISPLAY_HEIGHT/2)+15 );
	RAIO_StartDrawing ( SQUARE_FILL );
	RAIO_print_text( (DISPLAY_WIDTH/2)-16, (DISPLAY_HEIGHT/2)-9, "EXIT",  COLOR_BLUE, COLOR_WHITE );
}
