waveshare_epaper

Waveshare e-paper display shenanigans
git clone git://bsandro.tech/waveshare_epaper
Log | Files | Refs | README

EPD_2in13.c (9176B)


      1 /*****************************************************************************
      2 * | File      	:   EPD_2IN13.c
      3 * | Author      :   Waveshare team
      4 * | Function    :   2.13inch e-paper
      5 * | Info        :
      6 *----------------
      7 * |	This version:   V3.0
      8 * | Date        :   2019-06-12
      9 * | Info        :
     10 * -----------------------------------------------------------------------------
     11 * V3.0(2019-06-12):
     12 * 1.Change:
     13 *    EPD_Reset() => EPD_2IN13_Reset()
     14 *    EPD_SendCommand() => EPD_2IN13_SendCommand()
     15 *    EPD_SendData() => EPD_2IN13_SendData()
     16 *    EPD_WaitUntilIdle() => EPD_2IN13_ReadBusy()
     17 *    EPD_Init() => EPD_2IN13_Init()
     18 *    EPD_Clear() => EPD_2IN13_Clear()
     19 *    EPD_Display() => EPD_2IN13_Display()
     20 *    EPD_Sleep() => EPD_2IN13_Sleep()
     21 * -----------------------------------------------------------------------------
     22 * V2.0(2019-01-03):
     23 * 1.Remove:ImageBuff[EPD_2IN13_HEIGHT * EPD_2IN13_WIDTH / 8]
     24 * 2.Change:EPD_Display(UBYTE *Image)
     25 *   Need to pass parameters: pointer to cached data
     26 * 3.Change:
     27 *   EPD_RST -> EPD_RST_PIN
     28 *   EPD_DC -> EPD_DC_PIN
     29 *   EPD_CS -> EPD_CS_PIN
     30 *   EPD_BUSY -> EPD_BUSY_PIN
     31 #
     32 # Permission is hereby granted, free of charge, to any person obtaining a copy
     33 # of this software and associated documnetation files (the "Software"), to deal
     34 # in the Software without restriction, including without limitation the rights
     35 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     36 # copies of the Software, and to permit persons to  whom the Software is
     37 # furished to do so, subject to the following conditions:
     38 #
     39 # The above copyright notice and this permission notice shall be included in
     40 # all copies or substantial portions of the Software.
     41 #
     42 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     43 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     44 # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     45 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     46 # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     47 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     48 # THE SOFTWARE.
     49 #
     50 ******************************************************************************/
     51 #include "EPD_2in13.h"
     52 #include "Debug.h"
     53 
     54 const unsigned char EPD_2IN13_lut_full_update[] = {
     55     0x22, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x11,
     56     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     57     0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E,
     58     0x01, 0x00, 0x00, 0x00, 0x00, 0x00
     59 };
     60 
     61 const unsigned char EPD_2IN13_lut_partial_update[] = {
     62     0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     63     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     64     0x0F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     65     0x00, 0x00, 0x00, 0x00, 0x00, 0x00
     66 };
     67 /******************************************************************************
     68 function :	Software reset
     69 parameter:
     70 ******************************************************************************/
     71 static void EPD_2IN13_Reset(void)
     72 {
     73     DEV_Digital_Write(EPD_RST_PIN, 1);
     74     DEV_Delay_ms(200);
     75     DEV_Digital_Write(EPD_RST_PIN, 0);
     76     DEV_Delay_ms(10);
     77     DEV_Digital_Write(EPD_RST_PIN, 1);
     78     DEV_Delay_ms(200);
     79 }
     80 
     81 /******************************************************************************
     82 function :	send command
     83 parameter:
     84      Reg : Command register
     85 ******************************************************************************/
     86 static void EPD_2IN13_SendCommand(UBYTE Reg)
     87 {
     88     DEV_Digital_Write(EPD_DC_PIN, 0);
     89     DEV_Digital_Write(EPD_CS_PIN, 0);
     90     DEV_SPI_WriteByte(Reg);
     91     DEV_Digital_Write(EPD_CS_PIN, 1);
     92 }
     93 
     94 /******************************************************************************
     95 function :	send data
     96 parameter:
     97     Data : Write data
     98 ******************************************************************************/
     99 static void EPD_2IN13_SendData(UBYTE Data)
    100 {
    101     DEV_Digital_Write(EPD_DC_PIN, 1);
    102     DEV_Digital_Write(EPD_CS_PIN, 0);
    103     DEV_SPI_WriteByte(Data);
    104     DEV_Digital_Write(EPD_CS_PIN, 1);
    105 }
    106 
    107 /******************************************************************************
    108 function :	Wait until the busy_pin goes LOW
    109 parameter:
    110 ******************************************************************************/
    111 void EPD_2IN13_ReadBusy(void)
    112 {
    113     Debug("e-Paper busy\r\n");
    114     while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) {      //LOW: idle, HIGH: busy
    115         DEV_Delay_ms(100);
    116     }
    117     Debug("e-Paper busy release\r\n");
    118 }
    119 
    120 /******************************************************************************
    121 function :	Turn On Display
    122 parameter:
    123 ******************************************************************************/
    124 static void EPD_2IN13_TurnOnDisplay(void)
    125 {
    126     EPD_2IN13_SendCommand(0x22); // DISPLAY_UPDATE_CONTROL_2
    127     EPD_2IN13_SendData(0xC4);
    128     EPD_2IN13_SendCommand(0X20);	// MASTER_ACTIVATION
    129     EPD_2IN13_SendCommand(0xFF);	// TERMINATE_FRAME_READ_WRITE
    130 
    131     EPD_2IN13_ReadBusy();
    132 }
    133 
    134 static void EPD_2IN13_SetWindows(int x_start, int y_start, int x_end, int y_end)
    135 {
    136     EPD_2IN13_SendCommand(0x44);
    137     /* x point must be the multiple of 8 or the last 3 bits will be ignored */
    138     EPD_2IN13_SendData((x_start >> 3) & 0xFF);
    139     EPD_2IN13_SendData((x_end >> 3) & 0xFF);
    140     EPD_2IN13_SendCommand(0x45);
    141     EPD_2IN13_SendData(y_start & 0xFF);
    142     EPD_2IN13_SendData((y_start >> 8) & 0xFF);
    143     EPD_2IN13_SendData(y_end & 0xFF);
    144     EPD_2IN13_SendData((y_end >> 8) & 0xFF);
    145 }
    146 
    147 static void EPD_2IN13_SetCursor(int x, int y)
    148 {
    149     EPD_2IN13_SendCommand(0x4E);
    150     /* x point must be the multiple of 8 or the last 3 bits will be ignored */
    151     EPD_2IN13_SendData((x >> 3) & 0xFF);
    152     EPD_2IN13_SendCommand(0x4F);
    153     EPD_2IN13_SendData(y & 0xFF);
    154     EPD_2IN13_SendData((y >> 8) & 0xFF);
    155 //    EPD_2IN13_ReadBusy();
    156 }
    157 
    158 /******************************************************************************
    159 function :	Initialize the e-Paper register
    160 parameter:
    161 ******************************************************************************/
    162 void EPD_2IN13_Init(UBYTE Mode)
    163 {
    164     EPD_2IN13_Reset();
    165 
    166     EPD_2IN13_SendCommand(0x01); // DRIVER_OUTPUT_CONTROL
    167     EPD_2IN13_SendData((EPD_2IN13_HEIGHT - 1) & 0xFF);
    168     EPD_2IN13_SendData(((EPD_2IN13_HEIGHT - 1) >> 8) & 0xFF);
    169     EPD_2IN13_SendData(0x00);			// GD = 0; SM = 0; TB = 0;
    170 
    171     EPD_2IN13_SendCommand(0x0C);	// BOOSTER_SOFT_START_CONTROL
    172     EPD_2IN13_SendData(0xD7);
    173     EPD_2IN13_SendData(0xD6);
    174     EPD_2IN13_SendData(0x9D);
    175 
    176     EPD_2IN13_SendCommand(0x2C);	// WRITE_VCOM_REGISTER
    177     EPD_2IN13_SendData(0xA8);     // VCOM 7C
    178 
    179     EPD_2IN13_SendCommand(0x3A);	// SET_DUMMY_LINE_PERIOD
    180     EPD_2IN13_SendData(0x1A);			// 4 dummy lines per gate
    181 
    182     EPD_2IN13_SendCommand(0x3B);	// SET_GATE_TIME
    183     EPD_2IN13_SendData(0x08);			// 2us per line
    184 
    185     EPD_2IN13_SendCommand(0X3C);	// BORDER_WAVEFORM_CONTROL
    186     EPD_2IN13_SendData(0x63);
    187 
    188     EPD_2IN13_SendCommand(0X11);	// DATA_ENTRY_MODE_SETTING
    189     EPD_2IN13_SendData(0x03);			// X increment; Y increment
    190 
    191     //set the look-up table register
    192     EPD_2IN13_SendCommand(0x32);
    193     if(Mode == EPD_2IN13_FULL) {
    194         for (UWORD i = 0; i < 30; i++) {
    195             EPD_2IN13_SendData(EPD_2IN13_lut_full_update[i]);
    196         }
    197     } else if(Mode == EPD_2IN13_PART) {
    198         for (UWORD i = 0; i < 30; i++) {
    199             EPD_2IN13_SendData(EPD_2IN13_lut_partial_update[i]);
    200         }
    201     } else {
    202         Debug("error, the Mode is EPD_2IN13_FULL or EPD_2IN13_PART");
    203     }
    204 }
    205 /******************************************************************************
    206 function :	Clear screen
    207 parameter:
    208 ******************************************************************************/
    209 void EPD_2IN13_Clear(void)
    210 {
    211     UWORD Width, Height;
    212     Width = (EPD_2IN13_WIDTH % 8 == 0)? (EPD_2IN13_WIDTH / 8 ): (EPD_2IN13_WIDTH / 8 + 1);
    213     Height = EPD_2IN13_HEIGHT;
    214 
    215     EPD_2IN13_SetWindows(0, 0, EPD_2IN13_WIDTH, EPD_2IN13_HEIGHT);
    216     for (UWORD j = 0; j < Height; j++) {
    217         EPD_2IN13_SetCursor(0, j);
    218         EPD_2IN13_SendCommand(0x24);
    219         for (UWORD i = 0; i < Width; i++) {
    220             EPD_2IN13_SendData(0Xff);
    221         }
    222     }
    223     EPD_2IN13_TurnOnDisplay();
    224 }
    225 
    226 /******************************************************************************
    227 function :	Sends the image buffer in RAM to e-Paper and displays
    228 parameter:
    229 ******************************************************************************/
    230 void EPD_2IN13_Display(UBYTE *Image)
    231 {
    232     UWORD Width, Height;
    233     Width = (EPD_2IN13_WIDTH % 8 == 0)? (EPD_2IN13_WIDTH / 8 ): (EPD_2IN13_WIDTH / 8 + 1);
    234     Height = EPD_2IN13_HEIGHT;
    235 
    236     EPD_2IN13_SetWindows(0, 0, EPD_2IN13_WIDTH, EPD_2IN13_HEIGHT);
    237     for (UWORD j = 0; j < Height; j++) {
    238         EPD_2IN13_SetCursor(0, j);
    239         EPD_2IN13_SendCommand(0x24);
    240         for (UWORD i = 0; i < Width; i++) {
    241             EPD_2IN13_SendData(Image[i + j * Width]);
    242         }
    243     }
    244     EPD_2IN13_TurnOnDisplay();
    245 }
    246 
    247 /******************************************************************************
    248 function :	Enter sleep mode
    249 parameter:
    250 ******************************************************************************/
    251 void EPD_2IN13_Sleep(void)
    252 {
    253     EPD_2IN13_SendCommand(0x10); //DEEP_SLEEP_MODE
    254     EPD_2IN13_SendData(0x01);
    255 }