/************************************************************************************
*  Copyright (c), 2019, HelTec Automatic(TM) Technology co.,LTD.
*            All rights reserved.
*
* Http:    www.heltec.cn
* Email:   suppor@heltec.cn
* WebShop: heltec.taobao.com
*
* File name: main.c
* Processor: STM32F103VBT6
* Compiler : MDK fo ARM 5.26
* 
* Author : Aaron.Lee
* Version: 1.00
* Date   : 2019.3.7
* Email  : suppor@heltec.cn
* Modification: none
* 
* Description: Heltec 420" 400*300 Black-White-Red E-Ink driver code
*
* Others: none;
*
* Function List:
*	1. void EPD_W21_WriteCMD(uint8_t command); 			// Write Command via hardware SPI bus;
* 2. void EPD_W21_WriteDATA(uint8_t command); 		// Write data to E-Ink driver;
* 3. void WriteBuffer(unsigned  char *picture);   // Write a picture data to display buffer;
* 4. void DisplayRefresh(void); 									// Refresh display, must run this function after WriteBuffer;
* 5. void CheckStatus(void);											// Check busy;
* 6. void Display_Init(void);											// Initial display.
*
* History: none;
*
*************************************************************************************/

#include "e_ink_420_BWR.h"

#include "stm32f1xx.h"

extern SPI_HandleTypeDef hspi1;


 void EPD_W21_WriteCMD(uint8_t command)
{
  HAL_GPIO_WritePin(GPIOD, CS, GPIO_PIN_RESET);                  
	HAL_GPIO_WritePin(GPIOE, DC, GPIO_PIN_RESET);		// command write
	HAL_SPI_Transmit(&hspi1,&command,sizeof(command),100);
	HAL_GPIO_WritePin(GPIOD, CS, GPIO_PIN_SET);
}
	
 void EPD_W21_WriteDATA(uint8_t command)
{
  HAL_GPIO_WritePin(GPIOD, CS, GPIO_PIN_RESET);                  
	HAL_GPIO_WritePin(GPIOE, DC, GPIO_PIN_SET);			// command write
	HAL_SPI_Transmit(&hspi1,&command,sizeof(command),100);
	HAL_GPIO_WritePin(GPIOD, CS, GPIO_PIN_SET);
}

void CheckStatus(void)
{
	while(1)
	{	 
		if(HAL_GPIO_ReadPin(GPIOE, BUSY)==1) break;
	}   
	HAL_Delay(100);                       
}

void Display_Init(void)
{		
//	  HAL_GPIO_WritePin(GPIOE, BS, GPIO_PIN_RESET); // command write
//		HAL_GPIO_WritePin(GPIOE, RST, GPIO_PIN_RESET); // command write
//  	HAL_Delay(100);
//		HAL_GPIO_WritePin(GPIOE, RST, GPIO_PIN_SET); // command write
	  HAL_Delay(100);
		
		EPD_W21_WriteCMD(0x06);         //boostÉč¶¨
		EPD_W21_WriteDATA (0x17);
		EPD_W21_WriteDATA (0x17);
		EPD_W21_WriteDATA (0x17);
		
		EPD_W21_WriteCMD(0x04);                       
		
		EPD_W21_WriteCMD(0X00);
		EPD_W21_WriteDATA(0x0f);	
		
		EPD_W21_WriteCMD(0X50);
		EPD_W21_WriteDATA(0x77);
		

    //EPD_W21_DispInit();		// pannel configure
}

void WriteBuffer(unsigned  char *picture)
{
	unsigned int i;
	for(i=0;i<15000;i++)
		EPD_W21_WriteDATA(picture[i]); //display black picture
//	HAL_Delay(1);
}

void DisplayRefresh(void)
{
	EPD_W21_WriteCMD(0x12);			//DISPLAY REFRESH 	
	HAL_Delay(20);	        //!!!The delay here is necessary, 200uS at least!!!     
	CheckStatus();
}

/***********************************************************
						end file
***********************************************************/

