#include <stdio.h>
#include <unistd.h>
#include "2in9_epd.h"
#include "Ap_29demo.h"
#include "epd_gui.h"

/****************************
 * PIN_CS      8
 * PIN_RST     17
 * PIN_DC      25
 * PIN_BUSY    24
 ***************************/
/***
2.9inch_EPD    Raspberry Pi
	  VCC   --->   3.3V
	  GND   --->   GND
	  RST   --->   D17
	  BUSY  --->   D24
	  D/C   --->   D25
	  MOSI  --->   MOSI
	  CLK   --->   CLK
	  CS    --->   (CE0)D8
	  *
screen coordinates gor gui functions
-----------------> x (0~128)
|
|
|
|
y(0~296)
*****************************/

void delay_ms(unsigned int t)
{
	while (t)
	{
		usleep(1000);
		t--;
	}
}

unsigned char image[9472];
// Tips//
/*
1.When the e-paper is refreshed in full screen, the picture flicker is a normal phenomenon, and the main function is to clear the display afterimage in the previous picture.
2.When the partial refresh is performed, the screen does not flash.
3.After the e-paper is refreshed, you need to put it into sleep mode, please do not delete the sleep command.
4.Please do not take out the electronic paper when power is on.
5.Wake up from sleep, need to re-initialize the e-paper.
6.When you need to transplant the driver, you only need to change the corresponding IO. The BUSY pin is the input mode and the others are the output mode.
*/
int main(void)
{
	unsigned char i;
	printf("2.9inch epaper demo\r\n");
	EPD_GPIO_Init();
	EPD_HW_Init();		   // Full screen refresh initialization.
	PIC_display(gImage_1); // To Display one image using full screen refresh.
	EPD_DeepSleep();	   // Enter the sleep mode and please do not delete it, otherwise it will reduce the lifespan of the screen.
	delay_ms(4000);		   // Delay for 4s.

	EPD_HW_Init();
	EPD_WhiteScreen_Red();
	EPD_DeepSleep(); // EPD_DeepSleep,Sleep instruction is necessary, please do not delete!!!
	delay_ms(1000);

	EPD_HW_Init();
	EPD_WhiteScreen_Yellow();
	EPD_DeepSleep(); // EPD_DeepSleep,Sleep instruction is necessary, please do not delete!!!
	delay_ms(1000);

	EPD_HW_Init(); // Full screen refresh initialization.
	Image_Init(image, EPD_WIDTH, EPD_HEIGHT, 270, EPD_2IN9G_WHITE);
	//			  /**************Drawing**********************/
	Gui_Clear(EPD_2IN9G_WHITE);
	// Point
	Gui_Draw_Point(3, 1, EPD_2IN9G_BLACK, PIXEL_1X1, DOT_STYLE_DFT);
	Gui_Draw_Point(3, 15, EPD_2IN9G_YELLOW, PIXEL_2X2, DOT_STYLE_DFT);
	Gui_Draw_Point(5, 40, EPD_2IN9G_RED, PIXEL_3X3, DOT_STYLE_DFT);
	Gui_Draw_Point(5, 55, EPD_2IN9G_BLACK, PIXEL_4X4, DOT_STYLE_DFT);
	// Line
	Gui_Draw_Line(15, 5, 55, 110, EPD_2IN9G_BLACK, PIXEL_1X1, SOLID);
	Gui_Draw_Line(15, 50, 55, 10, EPD_2IN9G_BLACK, PIXEL_1X1, SOLID);
	// Rectangle
	Gui_Draw_Rectangle(15, 5, 55, 45, EPD_2IN9G_BLACK, EMPTY, PIXEL_1X1);
	Gui_Draw_Rectangle(90, 5, 130, 45, EPD_2IN9G_BLACK, FULL, PIXEL_1X1);
	// Circle
	Gui_Draw_Circle(170, 25, 18, EPD_2IN9G_YELLOW, EMPTY, PIXEL_1X1);
	Gui_Draw_Circle(226, 25, 18, EPD_2IN9G_RED, FULL, PIXEL_1X1);
	Gui_Draw_Str(60, 50, "296x128", &Font16, EPD_2IN9G_WHITE, EPD_2IN9G_YELLOW);	   // 7*12
	Gui_Draw_Str(145, 50, "SPI interface", &Font16, EPD_2IN9G_WHITE, EPD_2IN9G_RED);   // 11*16
	Gui_Draw_Str(80, 75, "2.9\" E-Paper", &Font20, EPD_2IN9G_WHITE, EPD_2IN9G_YELLOW); // 14*20
	Gui_Draw_Str(80, 100, "SEENGREAT", &Font24, EPD_2IN9G_WHITE, EPD_2IN9G_RED);	   // 17*24

	GUI_display(image);
	EPD_DeepSleep(); // EPD_DeepSleep,Sleep instruction is necessary, please do not delete!!!
	delay_ms(2000);	 // 2s
	delay_ms(2000);	 // 2s

	EPD_HW_Init(); // Full screen refresh initialization.
	EPD_WhiteScreen_White();
	EPD_DeepSleep(); // Enter the sleep mode and please do not delete it, otherwise it will reduce the lifespan of the screen.
	delay_ms(3000);	 // Delay for 3s.

	return 0;
}
