/******************************************************************************

* FileName:        Application.c
* Dependencies:    Image decoding library;
* Processor:       PIC24/dsPIC30/dsPIC33/PIC32MX
* Compiler:        C30 v2.01/C32 v0.00.18
* Company:         Microchip Technology, Inc.

 * Software License Agreement
 *
 * Copyright ?2008 Microchip Technology Inc.  All rights reserved.
 * Microchip licenses to you the right to use, modify, copy and distribute
 * Software only when embedded on a Microchip microcontroller or digital
 * signal controller, which is integrated into your product or third party
 * product (pursuant to the sublicense terms in the accompanying license
 * agreement).  
 *
 * You should refer to the license agreement accompanying this Software
 * for additional information regarding your rights and obligations.
 *
 * SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS?WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY
 * OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR
 * PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR
 * OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION,
 * BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT
 * DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL,
 * INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA,
 * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY
 * CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF),
 * OR OTHER SIMILAR COSTS.

Author                 Date           Comments
--------------------------------------------------------------------------------
Pradeep Budagutta    25-Jun-2008    First release
*******************************************************************************/


/*
*******************************************************************************
* Version 140309
* (1) Add support for PIC32_STARTER_KIT under the Configuration Bits section
* 		comment out #define PIC32_STARTER_KIT if you are not using 
*		PIC32MX Starter kit for PIC24_Eval_Board C2
* (2) Add support for PIC24 series under the Configuration Bits section.
* 		Internal Fast Osc option used for PIC24_Eval_Board C2 hardware.
* (3) Add CLKDIV = 0x0000; under Main for 8MHz osc.
* 
* Programmer: John Leung
* Date: 14-03-09
*******************************************************************************
*/

/*
*******************************************************************************
 * Version 310309
 * (1) Add MDD File System directories (*.c and *.h) to Microchip directories
 *	  to support CFIO and SDIO File System.
 * (2) Under Build Options -> Include Search Paths, add the search path
 *	  "..\Microchip\Include\MDD File System"
 * (3) Add FSConfig.h to project directory
 * (4) Changes on HardwareProfile.h to support MDD FS
 * (5) Include FSIO.h under Application.c
 * (6) Include \MDDFS\FSIO.c and SD-SPI.c files under project workspace
 * (7) Modification to HardwareProfile.h
 * 	   #define	GetInstructionClock()		(GetSystemClock())
 * (8) Solder a 32.768kHz crystal to PIC32MX Starter Kit at Y3, 
 *	   15pF capacitors added, too.
 * (9) Under Build Options -> MPLAB PIC32 C Compiler option
 * 	   Switch on "Generate debugging information" checkbox to monitor an array just in case...
 * (10) Under ImageDecoderConfig.h, uncomment #define IMG_USE_ONLY_MDD_FILE_SYSTEM_FOR_INPUT
 *			THIS IS VERY IMPORTANT; OTHERWISE, DECODE IMAGES FROM SD DOES NOT WORK!
 * (11) Finally, remove all FlashImage related files from project
 *
 * Author: John Leung
 * File: HardwareProfile.h
 * Company: TechToys Company
 * Web presence: www.TechToys.com.hk
 * Date: 31th March 2009

*******************************************************************************
*/

/*
 ********************************************************************
 * Version 080409
 * (1) Add support for PIC24FJ series under HardwareProfile.h
 * (2) Remove virtual display feature from GraphicsConfig.h file
 *
 * Author: John Leung
 * Company: TechToys Company
 * Web presence: www.TechToys.com.hk
 * Date: 08 April 2009
 ********************************************************************
*/

/*
 ********************************************************************
 * Version 230409
 * (1) Add support for TY240 LCD module(ILI9320) for PIC32MX & PIC24 series. 
 *		Made changes for Graphics.h under ..\Microchip\Include\Graphics
 * (2) Include the file #include "ili9320\ili9320.h" at 
 *		..\Microchip\Include\Graphics\DisplayDriver.h
 * (3) Include the file #include "ili9320\ili9320.c" at 
 *		..\Microchip\Graphics\DisplayDriver.c
 * (4) modify GraphicsConfig.h for ILI9320
 * (5) Add support for PORTRAIT and LANDSCAPE display by #define USE_PORTRAIT
 * (6) Add support for TY280T LCD module (ILI9325) for PIC32MX
 * 
 * Author: John Leung
 * Company: TechToys Company
 * Web presence: www.TechToys.com.hk
 * Date: 23 April 2009
 ********************************************************************
*/

#include "Graphics\Graphics.h"
#include "Image Decoders\ImageDecoder.h"
#include "FSIO.h"

#ifdef __PIC24FJ256GA110__
#include "pps.h"
#endif

// comment the following line if your are using Internal Fast Osc for PIC24 Eval BoardC2
#define PIC32_STARTER_KIT

// Configuration bits
#ifdef __PIC32MX
#pragma config FPLLODIV = DIV_1, FPLLMUL = MUL_18, FWDTEN = OFF, FCKSM = CSECME, FPBDIV = DIV_1, FPLLIDIV = DIV_2
#pragma config OSCIOFNC = ON, FSOSCEN = OFF, IESO = OFF
#pragma config CP = OFF, BWP = OFF, PWP = OFF

#ifdef PIC32_STARTER_KIT
// PIC32MX Starter Kit
#pragma config FNOSC = PRIPLL, POSCMOD = XT
#else
// PIC24 Eval board RevC2 with PIC32MX360F512L onboard, Internal Fast Osc for 8MHz*18= 72MHz
#pragma config FNOSC = FRCPLL, POSCMOD = OFF, ICESEL = ICS_PGx2
#endif

#else
_CONFIG2(FNOSC_FRCPLL) 				// Fast RC oscillator with PLL
_CONFIG1(JTAGEN_OFF & FWDTEN_OFF)   // JTAG off, watchdog timer off
#endif


#define DELAY_MS 1500
#define DELAY_NEXT 3000


void PixelOutput(IMG_PIXEL_XY_RGB_888 *pPix);


int main(void)
{

	FSFILE * pointer;
    IMG_FILE_SYSTEM_API FlashFileApi;
    BYTE bTextHeight;
    WORD X, Y;


/////////////////////////////////////////////////////////////////////////////

#ifdef __PIC24F__
	CLKDIV = 0x0000;	//FRC postscaler divided by 1 (8MHz from internal RC)
#endif

#ifdef __PIC32MX__
    INTEnableSystemMultiVectoredInt();
    SYSTEMConfigPerformance(GetSystemClock());	
#endif

#ifdef __PIC24FJ256GA110__
	pps_spi2();
#endif

    InitGraph();
    SetFont((void*)&GOLFontDefault);
    bTextHeight = GetTextHeight((void*)&GOLFontDefault);

	SetColor(BRIGHTRED);
	Y = (GetMaxY()+1-(3*bTextHeight))/2;
    SetColor(LIGHTRED);
    X = GetMaxX()+1-GetTextWidth("Please insert SD Card", (void*)&GOLFontDefault);
    OutTextXY(X/2, Y, "Please insert SD Card");

   	while (!MDD_MediaDetect());

   // Initialize the library
    while (!FSInit());

    ImageDecoderInit();

    SetColor(BLACK);
    ClearDevice();
    Y = (GetMaxY()+1-(3*bTextHeight))/2;
    SetColor(LIGHTRED);
    X = GetMaxX()+1-GetTextWidth("Welcome to the demo", (void*)&GOLFontDefault);
    OutTextXY(X/2, Y, "Welcome to the demo");
    DelayMs(DELAY_MS/3);
    X = GetMaxX()+1-GetTextWidth("of", (void*)&GOLFontDefault);
    OutTextXY(X/2, Y + bTextHeight, "of");
    DelayMs(DELAY_MS/3);
    X = GetMaxX()+1-GetTextWidth("Image Decoding Library", (void*)&GOLFontDefault);
    OutTextXY(X/2, Y + 2*bTextHeight, "Image Decoding Library");
    DelayMs(DELAY_NEXT);

    while(1)
    {

        SetColor(BLACK);
        ClearDevice();
        SetColor(LIGHTRED);
        OutTextXY(0, 0, "1. Full screen decoding of");
        DelayMs(DELAY_MS);
        SetColor(YELLOW);
        OutTextXY(0, bTextHeight + 2, "Sunset.bmp");
        DelayMs(DELAY_MS);
        SetColor(WHITE);
        OutTextXY(0, 240 - 1*(bTextHeight + 2), "(w/o Double Buffering)");
        DelayMs(DELAY_MS);
		pointer = FSfopen("SUNSET.BMP", "r");
		ImageFullScreenDecode(pointer, IMG_BMP, NULL, PixelOutput);
		FSfclose(pointer);
        DelayMs(DELAY_NEXT);
				

        SetColor(BLACK);
        ClearDevice();
        SetColor(YELLOW);
        OutTextXY(0, 240 - 5*(bTextHeight + 2), "2. Decoding of Sunset.jpg");
        DelayMs(DELAY_MS);
        SetColor(LIGHTBLUE);
        OutTextXY(0, 240 - 3*(bTextHeight + 2), "Rect = (0, 0) to (320, 240)");
        DelayMs(DELAY_MS);
        SetColor(LIGHTGREEN);
        OutTextXY(0, 240 - 2*(bTextHeight + 2), "(w/o Double Buffering)");
        DelayMs(DELAY_MS);
        SetColor(WHITE);
		pointer = FSfopen("SUNSET.JPG", "r");
		ImageFullScreenDecode(pointer, IMG_JPEG, NULL, PixelOutput);
		FSfclose(pointer);
        DelayMs(DELAY_NEXT);
		
        SetColor(BLACK);
        ClearDevice();
        SetColor(YELLOW);
        OutTextXY(0, 0, "3. Decoding of Present.jpg");
        DelayMs(DELAY_MS);
        SetColor(LIGHTRED);
        OutTextXY(0, bTextHeight + 2, "Flags = IMG_DOWN_SCALE");
        DelayMs(DELAY_MS);
        SetColor(LIGHTBLUE);
        OutTextXY(0, 240 - 3*(bTextHeight + 2), "Image fit (0, 0) to (240, 180)");
        DelayMs(DELAY_MS);
        SetColor(LIGHTGREEN);
        OutTextXY(0, 240 - 2*(bTextHeight + 2), "(w/o Double Buffering)");
        DelayMs(DELAY_MS);
        SetColor(WHITE);
        OutTextXY(0, 240 - 1*(bTextHeight + 2), "(Original image in 480x360)");
        DelayMs(DELAY_MS);
		pointer = FSfopen("PRESENT.JPG", "r");
        ImageDecode(pointer, IMG_JPEG, 0, 0, 240, 180, IMG_DOWN_SCALE, NULL, PixelOutput);
		FSfclose(pointer); 
        DelayMs(DELAY_NEXT);
		
        SetColor(BLACK);
        ClearDevice();
        SetColor(YELLOW);
        OutTextXY(0, 0, "4. Decoding of Football.bmp");
        DelayMs(DELAY_MS);
        SetColor(LIGHTBLUE);
        OutTextXY(0, 240 - 3*(bTextHeight + 2), "Image size is 240x180");
        DelayMs(DELAY_MS);
        SetColor(LIGHTGREEN);
        OutTextXY(0, 240 - 2*(bTextHeight + 2), "(w/o Double Buffering)");
        DelayMs(DELAY_MS);
        SetColor(WHITE);
        OutTextXY(0, 240 - 1*(bTextHeight + 2), "(Image is smaller than 320x240)");
        DelayMs(DELAY_MS);
		pointer=FSfopen("FOOTBALL.BMP", "r");
		ImageFullScreenDecode(pointer, IMG_BMP, NULL, PixelOutput);
		FSfclose(pointer);
        DelayMs(DELAY_NEXT);
    }

    return 0;
}


void PixelOutput(IMG_PIXEL_XY_RGB_888 *pPix)
{
    SetColor(RGB565CONVERT(pPix->R, pPix->G, pPix->B));
    if(pPix->X <= GetMaxX() && pPix->Y <= GetMaxY())
    {
        PutPixel(pPix->X, pPix->Y);
    }    
    if(pPix->X > GetMaxX() && pPix->Y > GetMaxY())
    {
        ImageAbort();
    }    
}
