/*****************************************************************************
 * Microchip Graphics Library Demo Application
 * This program shows how to use the Graphics Primitives Layer.
 *****************************************************************************
 * FileName:        MainDemo.c
 * Dependencies:    MainDemo.h
 * Processor:       PIC24
 * Compiler:       	MPLAB C30
 * Linker:          MPLAB LINK30
 * Company:         Microchip Technology Incorporated
 *
 * Software License Agreement
 *
 * Copyright ?2007 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        Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Anton Alkhimenok		30/05/07	...
 *****************************************************************************/

/*
******************************************************************************
* This is a quality assurance demo for the hardware platform
* PIC24-Eval Rev C + TY240-240320-BO Rev 1B
* This program has been modified from the original Microchip Primitive Demo
* First the LCD will show simple RGB color, primitive graphic, and bmp file
* hard-code. Finally a screen showing the instruction to connect RS232 port
* to Windows HyperTerminal will pop up. Use the baud rate of 19200bps, 8-n-1
* for HyperTerminal. Follow the instruction to test the nandFlash.
* One major change to this project from previous versions is on the uart part.
* UART1 used in the past but this version swapped to UART2. For simplcity,
* the source code comm.c and monitor.c have been employed for stdio functions.
* A different version of printf as xprintf is now in effect.
*
* Programmer : John Leung (www.TechToys.com.hk)
* Date: 23 Jan 2009
******************************************************************************
*/

/*
******************************************************************************
* Revisions
*
* (1) Add directive #if defined (__PIC24FJ256GA110__) under uart_init()
* in the file comm.c for compatablity between PIC24FJ125GA010 amnd PIC24FJ256GA110
*
* Programmer : John Leung (www.TechToys.com.hk)
* Date: 9th Sept 09
******************************************************************************
*/


#include "MainDemo.h"

// Configuration bits
// Configure fast RC oscillator with PLL and RC15 pin for Fosc/2 function	
_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_OFF)
_CONFIG1(JTAGEN_OFF & FWDTEN_OFF)   // JTAG off, watchdog timer off

INT16U j;
INT8U dataInput;
INT16U DeviceID;
INT8U dataBuffer[NAND_PAGE_SIZE_BYTE];


///////////////////////////////// FONTS ///////////////////////////////////////
extern FONT_FLASH Font25;
extern FONT_FLASH Font35;


/////////////////////////////// PICTURES //////////////////////////////////////
extern BITMAP_FLASH flower16bit;
extern BITMAP_FLASH flower8bit;
extern BITMAP_FLASH flower4bit;
extern BITMAP_FLASH flower1bit;

int main(void){

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


SHORT width, height;
SHORT counter;
const SHORT polyPoints[] = {
160,60,
240,120,
160,180,
80,120,
160,60,
};

    InitGraph();
	uart_init();

    while(1){

		SetColor(WHITE);
		ClearDevice();

		DelayMs(1000);

		SetColor(BRIGHTRED);
		ClearDevice();

		DelayMs(1000);

		SetColor(BRIGHTGREEN);
		ClearDevice();

		DelayMs(1000);

		SetColor(BRIGHTBLUE);
		ClearDevice();

		DelayMs(1000);

		SetColor(WHITE);

        for(counter=0; counter<GetMaxX(); counter+=20){
            Line(counter,0,GetMaxX()-1-counter,GetMaxY()-1);
        }

        DelayMs(2000);  

        SetColor(BRIGHTRED);

        for(counter=10; counter<GetMaxY()>>1; counter+=10){
            Circle(GetMaxX()>>1,GetMaxY()>>1,counter);
        }

        DelayMs(2000);  

        SetColor(BRIGHTBLUE);
        FillCircle(GetMaxX()>>1,GetMaxY()>>1,60);
        SetColor(BRIGHTGREEN);
        FillCircle(GetMaxX()>>1,GetMaxY()>>1,40);
        SetColor(BRIGHTRED);
        FillCircle(GetMaxX()>>1,GetMaxY()>>1,20);

        DelayMs(2000);  

        SetColor(BLACK);
        ClearDevice();

        SetColor(BRIGHTBLUE);
        Bevel((GetMaxX()>>1)-60,(GetMaxY()>>1)-60,(GetMaxX()>>1)+60,(GetMaxY()>>1)+60,30);
        SetColor(BRIGHTGREEN);
        Bevel((GetMaxX()>>1)-40,(GetMaxY()>>1)-40,(GetMaxX()>>1)+40,(GetMaxY()>>1)+40,30);
        SetColor(BRIGHTRED);
        Bevel((GetMaxX()>>1)-20,(GetMaxY()>>1)-20,(GetMaxX()>>1)+20,(GetMaxY()>>1)+20,30);

        DelayMs(2000);  

        SetColor(BLACK);
        ClearDevice();

        SetColor(BRIGHTBLUE);
        Arc((GetMaxX()>>1)-60,(GetMaxY()>>1)-60,(GetMaxX()>>1)+60,(GetMaxY()>>1)+60,20,30,0xFF);
        SetColor(BRIGHTGREEN);
        Arc((GetMaxX()>>1)-40,(GetMaxY()>>1)-40,(GetMaxX()>>1)+40,(GetMaxY()>>1)+40,20,30,0xFF);
        SetColor(BRIGHTRED);
        Arc((GetMaxX()>>1)-20,(GetMaxY()>>1)-20,(GetMaxX()>>1)+20,(GetMaxY()>>1)+20,20,30,0xFF);

        DelayMs(2000);  

        SetColor(BRIGHTBLUE);
        FillBevel((GetMaxX()>>1)-60,(GetMaxY()>>1)-60,(GetMaxX()>>1)+60,(GetMaxY()>>1)+60,30);
        SetColor(BRIGHTGREEN);
        FillBevel((GetMaxX()>>1)-40,(GetMaxY()>>1)-40,(GetMaxX()>>1)+40,(GetMaxY()>>1)+40,30);
        SetColor(BRIGHTRED);
        FillBevel((GetMaxX()>>1)-20,(GetMaxY()>>1)-20,(GetMaxX()>>1)+20,(GetMaxY()>>1)+20,30);

        DelayMs(2000);  

        SetColor(BLACK);
        ClearDevice();

        SetColor(BRIGHTBLUE);
        Arc((GetMaxX()>>1),(GetMaxY()>>1)-50,(GetMaxX()>>1),(GetMaxY()>>1)+50,50,60,0x11);
        SetColor(BRIGHTGREEN);
        Arc((GetMaxX()>>1),(GetMaxY()>>1)-50,(GetMaxX()>>1),(GetMaxY()>>1)+50,50,60,0x22);
        SetColor(BRIGHTRED);
        Arc((GetMaxX()>>1),(GetMaxY()>>1)-50,(GetMaxX()>>1),(GetMaxY()>>1)+50,50,60,0x44);
        SetColor(BRIGHTYELLOW);
        Arc((GetMaxX()>>1),(GetMaxY()>>1)-50,(GetMaxX()>>1),(GetMaxY()>>1)+50,50,60,0x88);

        SetColor(BRIGHTBLUE);
        Arc((GetMaxX()>>1),(GetMaxY()>>1)-30,(GetMaxX()>>1),(GetMaxY()>>1)+30,35,45,0x11);
        SetColor(BRIGHTGREEN);
        Arc((GetMaxX()>>1),(GetMaxY()>>1)-30,(GetMaxX()>>1),(GetMaxY()>>1)+30,35,45,0x22);
        SetColor(BRIGHTRED);
        Arc((GetMaxX()>>1),(GetMaxY()>>1)-30,(GetMaxX()>>1),(GetMaxY()>>1)+30,35,45,0x44);
        SetColor(BRIGHTYELLOW);
        Arc((GetMaxX()>>1),(GetMaxY()>>1)-30,(GetMaxX()>>1),(GetMaxY()>>1)+30,35,45,0x88);

        SetColor(BRIGHTBLUE);
        Arc((GetMaxX()>>1),(GetMaxY()>>1),(GetMaxX()>>1),(GetMaxY()>>1),20,30,0x11);
        SetColor(BRIGHTGREEN);
        Arc((GetMaxX()>>1),(GetMaxY()>>1),(GetMaxX()>>1),(GetMaxY()>>1),20,30,0x22);
        SetColor(BRIGHTRED);
        Arc((GetMaxX()>>1),(GetMaxY()>>1),(GetMaxX()>>1),(GetMaxY()>>1),20,30,0x44);
        SetColor(BRIGHTYELLOW);
        Arc((GetMaxX()>>1),(GetMaxY()>>1),(GetMaxX()>>1),(GetMaxY()>>1),20,30,0x88);

        DelayMs(2000);  

        SetColor(BLACK);
        ClearDevice();

        SetColor(BRIGHTBLUE);

        for(counter=0; counter<GetMaxY()>>1; counter+=20){
            Rectangle(GetMaxX()/2-counter,
                      GetMaxY()/2-counter,
                      GetMaxX()/2+counter,
                      GetMaxY()/2+counter);
        }

        DelayMs(2000);  

        SetColor(BRIGHTBLUE);
        Bar(GetMaxX()/2-80,GetMaxY()/2-80,
            GetMaxX()/2+80,GetMaxY()/2+80);
        SetColor(BRIGHTGREEN);
        Bar(GetMaxX()/2-60,GetMaxY()/2-60,
            GetMaxX()/2+60,GetMaxY()/2+60);
        SetColor(BRIGHTRED);
        Bar(GetMaxX()/2-40,GetMaxY()/2-40,
            GetMaxX()/2+40,GetMaxY()/2+40);


        DelayMs(2000);  
        SetColor(BLACK);
        ClearDevice();

        SetColor(WHITE);
        DrawPoly(5,(SHORT*)polyPoints);

        DelayMs(2000);  
        SetColor(BLACK);
        ClearDevice();


        SetFont(&Font25);
        SetColor(BRIGHTGREEN);
        width = GetTextWidth("Microchip Technology Inc.",&Font25);
        height = GetTextHeight(&Font25);

        OutTextXY( (GetMaxX()-width)>>1,
                   (GetMaxY()-height)>>1,
                    "Microchip Technology Inc.");

        DelayMs(2000);  
        SetColor(BLACK);
        ClearDevice();

        SetFont(&Font35);
        SetColor(WHITE);
        width = GetTextWidth("Microchip Tech.",&Font35);
        height = GetTextHeight(&Font35);

        OutTextXY( (GetMaxX()-width)>>1,
                   (GetMaxY()-height)>>1,
                    "Microchip Tech.");

        DelayMs(2000);  
        SetColor(BLACK);
        ClearDevice();

        PutImage(0,0,&flower1bit,2);
        SetColor(WHITE);
        OutTextXY(100,280,"1BPP");
        DelayMs(2000);  
        SetColor(BLACK);
        ClearDevice();

        PutImage(0,0,&flower4bit,2);
        SetColor(WHITE);
        OutTextXY(100,280,"4BPP");
        DelayMs(2000);  
        SetColor(BLACK);
        ClearDevice();

        PutImage(0,0,&flower8bit,2);
        SetColor(WHITE);
        OutTextXY(100,280,"8BPP");
        DelayMs(2000);  
        SetColor(BLACK);
        ClearDevice();

        PutImage(0,0,&flower16bit,2);
        SetColor(WHITE);
        OutTextXY(100,280,"16BPP");
        DelayMs(2000);  
        SetColor(BLACK);
        ClearDevice();

        PutImage(60,0,&flower1bit,1);
        PutImage(145,0,&flower4bit,1);
        PutImage(60,114,&flower8bit,1);
        PutImage(145,114,&flower16bit,1);
        DelayMs(2000);  
        SetColor(BLACK);
        ClearDevice();
		
		xprintf("HYNIX HY27(U/S)S(08/16)561A Nand Flash Demo\r\nPlease input your action :\r\n1=Read Device ID\r\n2=Erase the first block\r\n3=Write the first page of the block with your input\r\n4=Verify the written content\r\nEsc=Exit\r\n");

		SetColor(WHITE);
        SetFont(&Font25);
		OutTextXY(2,20,"Entering nandFlash Demo");
		OutTextXY(2,50,"Connect Hyperterminal");
		OutTextXY(2,80,"to RS232 port, set baud");
		OutTextXY(2,110,"rate to 19200bps and");
		OutTextXY(2,140,"follow the instruction");
		dataInput = uart_get();
		nandInit();					//initialize NAND Flash here
		while(dataInput!=0x1B)
		{
			switch(dataInput)
			{
			case '1' : 	DeviceID = nandChkID();
						xprintf("DeviceId:%X\r\n",DeviceID); 
						break;
			case '2' :	//Prior to program a particular page, it is required to perform a block erase first
						nandEraseBlk(0);
						xputs("Block erase complete\r\n");
						break;
			case '3' :	xputs("Waiting for your input:\r\n");
						dataInput=uart_get();
						xprintf("You are writing '%c' to nand Flash page\r\n", dataInput);
						for(j=0;j<NAND_PAGE_SIZE_BYTE;j++) dataBuffer[j]=dataInput;
						nandWrPage(0,0, 0, &dataBuffer[0]);
						break;
			case '4' :	//clear the buffer, then refill the buffer with data read from nand Flash
						for(j=0;j<NAND_PAGE_SIZE_BYTE;j++) dataBuffer[j]=0x00;	
						nandRdPage(0,0, 0, &dataBuffer[0]);
						for(j=0;j<NAND_PAGE_SIZE_BYTE;j++) xprintf("Data at %u: %c\r\n", j, dataBuffer[j]);
						break;
			case 0x1B : xputs("GoodBye nandFlash demo\r\n");
						break;	
			default	 :  xputs("Input not valid, try again\r\n");
			}
			dataInput = uart_get();
		}
			dataInput = 0;	//reset dataInput variable

        SetColor(BLACK);
        ClearDevice();
		
		DelayMs(1000);

    } 

}
