| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 | /****************************************************************************** | File      	:   EPD_2in13d* | Author      :   Waveshare team* | Function    :   2.13inch e-paper d* | Info        :*----------------* |	This version:   V4.0* | Date        :   2019-12-31* | Info        :## Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documnetation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to  whom the Software is# furished to do so, subject to the following conditions:## The above copyright notice and this permission notice shall be included in# all copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN# THE SOFTWARE.#******************************************************************************/#include "epd2in13d.h"#include <arduino.h>/** * full screen update LUT**/static const unsigned char EPD_2IN13D_lut_vcomDC[] = {    0x00, 0x08, 0x00, 0x00, 0x00, 0x02,    0x60, 0x28, 0x28, 0x00, 0x00, 0x01,    0x00, 0x14, 0x00, 0x00, 0x00, 0x01,    0x00, 0x12, 0x12, 0x00, 0x00, 0x01,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00,};static const unsigned char EPD_2IN13D_lut_ww[] = {    0x40, 0x08, 0x00, 0x00, 0x00, 0x02,    0x90, 0x28, 0x28, 0x00, 0x00, 0x01,    0x40, 0x14, 0x00, 0x00, 0x00, 0x01,    0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};static const unsigned char EPD_2IN13D_lut_bw[] = {    0x40, 0x17, 0x00, 0x00, 0x00, 0x02,    0x90, 0x0F, 0x0F, 0x00, 0x00, 0x03,    0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,    0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};static const unsigned char EPD_2IN13D_lut_wb[] = {    0x80, 0x08, 0x00, 0x00, 0x00, 0x02,    0x90, 0x28, 0x28, 0x00, 0x00, 0x01,    0x80, 0x14, 0x00, 0x00, 0x00, 0x01,    0x50, 0x12, 0x12, 0x00, 0x00, 0x01,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};static const unsigned char EPD_2IN13D_lut_bb[] = {    0x80, 0x08, 0x00, 0x00, 0x00, 0x02,    0x90, 0x28, 0x28, 0x00, 0x00, 0x01,    0x80, 0x14, 0x00, 0x00, 0x00, 0x01,    0x50, 0x12, 0x12, 0x00, 0x00, 0x01,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};/** * partial screen update LUT**/static const unsigned char EPD_2IN13D_lut_vcom1[] = {    0x00, 0x19, 0x01, 0x00, 0x00, 0x01,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00    ,0x00, 0x00,};static const unsigned char EPD_2IN13D_lut_ww1[] = {    0x00, 0x19, 0x01, 0x00, 0x00, 0x01,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};static const unsigned char EPD_2IN13D_lut_bw1[] = {    0x80, 0x19, 0x01, 0x00, 0x00, 0x01,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};static const unsigned char EPD_2IN13D_lut_wb1[] = {    0x40, 0x19, 0x01, 0x00, 0x00, 0x01,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};static const unsigned char EPD_2IN13D_lut_bb1[] = {    0x00, 0x19, 0x01, 0x00, 0x00, 0x01,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};Epd::~Epd(){};Epd::Epd(){    reset_pin = RST_PIN;    dc_pin = DC_PIN;    cs_pin = CS_PIN;    busy_pin = BUSY_PIN;    width = EPD_WIDTH;    height = EPD_HEIGHT;};/** *  @brief: basic function for sending commands */void Epd::SendCommand(unsigned char command){    DigitalWrite(dc_pin, LOW);    SpiTransfer(command);}/** *  @brief: basic function for sending data */void Epd::SendData(unsigned char data){    DigitalWrite(dc_pin, HIGH);    SpiTransfer(data);}/** *  @brief: module reset. *          often used to awaken the module in deep sleep, *          see Epd::Sleep(); */void Epd::Reset(void){    DigitalWrite(reset_pin, HIGH);    DelayMs(200);    DigitalWrite(reset_pin, LOW);                //module reset    DelayMs(10);    DigitalWrite(reset_pin, HIGH);    DelayMs(200);}/******************************************************************************function :	Wait until the busy_pin goes LOWparameter:******************************************************************************/void Epd::ReadBusy(void){    Serial.print("ReadBusy\n");    char busy;    do {        SendCommand(0x71);        busy = DigitalRead(busy_pin);        busy =!(busy & 0x01);    } while(busy);    Serial.print("ReadBusy over\n");    DelayMs(200);}/******************************************************************************function :	LUT downloadparameter:******************************************************************************/void Epd::SetFullReg(void){    SendCommand(0x82);    SendData(0x00);	    SendCommand(0X50);    SendData(0x97);    unsigned int count;    SendCommand(0x20);    for(count=0; count<44; count++) {        SendData(EPD_2IN13D_lut_vcomDC[count]);    }    SendCommand(0x21);    for(count=0; count<42; count++) {        SendData(EPD_2IN13D_lut_ww[count]);    }    SendCommand(0x22);    for(count=0; count<42; count++) {        SendData(EPD_2IN13D_lut_bw[count]);    }    SendCommand(0x23);    for(count=0; count<42; count++) {        SendData(EPD_2IN13D_lut_wb[count]);    }    SendCommand(0x24);    for(count=0; count<42; count++) {        SendData(EPD_2IN13D_lut_bb[count]);    }}/******************************************************************************function :	LUT downloadparameter:******************************************************************************/void Epd::SetPartReg(void){    SendCommand(0x82);			//vcom_DC setting    SendData(0x03);    SendCommand(0X50);    SendData(0x47);	    unsigned int count;    SendCommand(0x20);    for(count=0; count<44; count++) {        SendData(EPD_2IN13D_lut_vcom1[count]);    }    SendCommand(0x21);    for(count=0; count<42; count++) {        SendData(EPD_2IN13D_lut_ww1[count]);    }    SendCommand(0x22);    for(count=0; count<42; count++) {        SendData(EPD_2IN13D_lut_bw1[count]);    }    SendCommand(0x23);    for(count=0; count<42; count++) {        SendData(EPD_2IN13D_lut_wb1[count]);    }    SendCommand(0x24);    for(count=0; count<42; count++) {        SendData(EPD_2IN13D_lut_bb1[count]);    }}/******************************************************************************function :	Turn On Displayparameter:******************************************************************************/void Epd::TurnOnDisplay(void){    SendCommand(0x12);		 //DISPLAY REFRESH    DelayMs(10);     //!!!The delay here is necessary, 200uS at least!!!    ReadBusy();}/******************************************************************************function :	Initialize the e-Paper registerparameter:******************************************************************************/void Epd::Init(){    /* this calls the peripheral hardware interface, see epdif */    if (IfInit() != 0) {        return -1;    }    Reset();    SendCommand(0x01);	//POWER SETTING    SendData(0x03);    SendData(0x00);    SendData(0x2b);    SendData(0x2b);    SendData(0x03);    SendCommand(0x06);	//boost soft start    SendData(0x17);     //A    SendData(0x17);     //B    SendData(0x17);     //C    SendCommand(0x04);    ReadBusy();    SendCommand(0x00);	//panel setting    SendData(0xbf);     //LUT from OTP,128x296    SendData(0x0e);     //VCOM to 0V fast    SendCommand(0x30);	//PLL setting    SendData(0x3a);     // 3a 100HZ   29 150Hz 39 200HZ	31 171HZ    SendCommand(0x61);	//resolution setting    SendData(EPD_WIDTH);    SendData((EPD_HEIGHT >> 8) & 0xff);    SendData(EPD_HEIGHT& 0xff);    SendCommand(0x82);	//vcom_DC setting    SendData(0x28);}void Epd::Clear(void){    int w, h;    w = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);    h = EPD_HEIGHT;    SendCommand(0x10);    for (int j = 0; j < h; j++) {        for (int i = 0; i < w; i++) {            SendData(0x00);        }    }    SendCommand(0x13);    for (int j = 0; j < h; j++) {        for (int i = 0; i < w; i++) {            SendData(0xFF);        }    }    SetFullReg();    TurnOnDisplay();}/******************************************************************************function :	Sends the image buffer in RAM to e-Paper and displaysparameter:******************************************************************************/void Epd::Display(const unsigned char *Image){    int Width, Height;    Width = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);    Height = EPD_HEIGHT;    SendCommand(0x10);    for (int j = 0; j < Height; j++) {        for (int i = 0; i < Width; i++) {            SendData(0x00);        }    }    SendCommand(0x13);    for (int j = 0; j < Height; j++) {        for (int i = 0; i < Width; i++) {            SendData(Image[i + j * Width]);        }    }	SetFullReg();    TurnOnDisplay();}/******************************************************************************function :	Sends the image buffer in RAM to e-Paper and displaysparameter:******************************************************************************/void Epd::Displayimage(const unsigned char *Image){    int Width, Height;    Width = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);    Height = EPD_HEIGHT;    SendCommand(0x10);    for (int j = 0; j < Height; j++) {        for (int i = 0; i < Width; i++) {            SendData(0x00);        }    }    SendCommand(0x13);    for (int j = 0; j < Height; j++) {        for (int i = 0; i < Width; i++) {            SendData(pgm_read_byte(&Image[i + j * Width]));        }    }	SetFullReg();    TurnOnDisplay();}/******************************************************************************function :	Sends the image buffer in RAM to e-Paper and displaysparameter:******************************************************************************/void Epd::DisplayPart(const unsigned char *Image){    /* Set partial Windows */    SetPartReg();    SendCommand(0x91);		//This command makes the display enter partial mode    SendCommand(0x90);		//resolution setting    SendData(0);           //x-start    SendData(EPD_WIDTH - 1);       //x-end    SendData(0);    SendData(0);     //y-start    SendData(EPD_HEIGHT / 256);    SendData(EPD_HEIGHT % 256 - 1);  //y-end    SendData(0x28);    int Width = (EPD_WIDTH % 8 == 0)? (EPD_WIDTH / 8 ): (EPD_WIDTH / 8 + 1);        /* send data */    SendCommand(0x10);    for (int j = 0; j < EPD_HEIGHT; j++) {        for (int i = 0; i < Width; i++) {            SendData(~Image[i + j * Width]);        }    }    SendCommand(0x13);    for (int j = 0; j < EPD_HEIGHT; j++) {        for (int i = 0; i < Width; i++) {            SendData(Image[i + j * Width]);        }    }    /* Set partial refresh */        TurnOnDisplay();}/******************************************************************************function :	Enter sleep modeparameter:******************************************************************************/void Epd::Sleep(void){    SendCommand(0X50);    SendData(0xf7);    SendCommand(0X02);  	//power off    SendCommand(0X07);  	//deep sleep    SendData(0xA5);    DigitalWrite(reset_pin, LOW);}
 |