waveshare_epaper

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

EPD_1in54_V2.c (9167B)


      1 /*****************************************************************************
      2 * | File      	:   EPD_1in54_V2.c
      3 * | Author      :   Waveshare team
      4 * | Function    :   1.54inch e-paper V2
      5 * | Info        :
      6 *----------------
      7 * |	This version:   V1.0
      8 * | Date        :   2019-06-11
      9 * | Info        :
     10 #
     11 # Permission is hereby granted, free of charge, to any person obtaining a copy
     12 # of this software and associated documnetation files (the "Software"), to deal
     13 # in the Software without restriction, including without limitation the rights
     14 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     15 # copies of the Software, and to permit persons to  whom the Software is
     16 # furished to do so, subject to the following conditions:
     17 #
     18 # The above copyright notice and this permission notice shall be included in
     19 # all copies or substantial portions of the Software.
     20 #
     21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     22 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     23 # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     24 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     25 # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     26 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     27 # THE SOFTWARE.
     28 #
     29 ******************************************************************************/
     30 #include "EPD_1in54_V2.h"
     31 #include "Debug.h"
     32 
     33 /******************************************************************************
     34 function :	Software reset
     35 parameter:
     36 ******************************************************************************/
     37 static void EPD_1IN54_V2_Reset(void)
     38 {
     39     DEV_Digital_Write(EPD_RST_PIN, 1);
     40     DEV_Delay_ms(200);
     41     DEV_Digital_Write(EPD_RST_PIN, 0);
     42     DEV_Delay_ms(10);
     43     DEV_Digital_Write(EPD_RST_PIN, 1);
     44     DEV_Delay_ms(200);
     45 }
     46 
     47 /******************************************************************************
     48 function :	send command
     49 parameter:
     50      Reg : Command register
     51 ******************************************************************************/
     52 static void EPD_1IN54_V2_SendCommand(UBYTE Reg)
     53 {
     54     DEV_Digital_Write(EPD_DC_PIN, 0);
     55     DEV_Digital_Write(EPD_CS_PIN, 0);
     56     DEV_SPI_WriteByte(Reg);
     57     DEV_Digital_Write(EPD_CS_PIN, 1);
     58 }
     59 
     60 /******************************************************************************
     61 function :	send data
     62 parameter:
     63     Data : Write data
     64 ******************************************************************************/
     65 static void EPD_1IN54_V2_SendData(UBYTE Data)
     66 {
     67     DEV_Digital_Write(EPD_DC_PIN, 1);
     68     DEV_Digital_Write(EPD_CS_PIN, 0);
     69     DEV_SPI_WriteByte(Data);
     70     DEV_Digital_Write(EPD_CS_PIN, 1);
     71 }
     72 
     73 /******************************************************************************
     74 function :	Wait until the busy_pin goes LOW
     75 parameter:
     76 ******************************************************************************/
     77 static void EPD_1IN54_V2_ReadBusy(void)
     78 {
     79     Debug("e-Paper busy\r\n");
     80     // UBYTE busy;
     81     // do {
     82         // EPD_1IN54_V2_SendCommand(0x71);
     83         // busy = DEV_Digital_Read(EPD_BUSY_PIN);
     84         // busy =!(busy & 0x01);
     85     // } while(busy);
     86     // DEV_Delay_ms(200);
     87     while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) {      //LOW: idle, HIGH: busy
     88         DEV_Delay_ms(100);
     89     }
     90     Debug("e-Paper busy release\r\n");
     91 }
     92 
     93 /******************************************************************************
     94 function :	Turn On Display full
     95 parameter:
     96 ******************************************************************************/
     97 static void EPD_1IN54_V2_TurnOnDisplay(void)
     98 {
     99     EPD_1IN54_V2_SendCommand(0x22);
    100     EPD_1IN54_V2_SendData(0xF7);
    101     EPD_1IN54_V2_SendCommand(0x20);
    102     EPD_1IN54_V2_ReadBusy();
    103 }
    104 
    105 /******************************************************************************
    106 function :	Turn On Display part
    107 parameter:
    108 ******************************************************************************/
    109 static void EPD_1IN54_V2_TurnOnDisplayPart(void)
    110 {
    111     EPD_1IN54_V2_SendCommand(0x22);
    112     EPD_1IN54_V2_SendData(0xFF);
    113     EPD_1IN54_V2_SendCommand(0x20);
    114     EPD_1IN54_V2_ReadBusy();
    115 }
    116 
    117 /******************************************************************************
    118 function :	Initialize the e-Paper register
    119 parameter:
    120 ******************************************************************************/
    121 void EPD_1IN54_V2_Init(void)
    122 {
    123     EPD_1IN54_V2_Reset();
    124 
    125     EPD_1IN54_V2_ReadBusy();
    126     EPD_1IN54_V2_SendCommand(0x12);  //SWRESET
    127     EPD_1IN54_V2_ReadBusy();
    128 
    129     EPD_1IN54_V2_SendCommand(0x01); //Driver output control
    130     EPD_1IN54_V2_SendData(0xC7);
    131     EPD_1IN54_V2_SendData(0x00);
    132     EPD_1IN54_V2_SendData(0x01);
    133 
    134     EPD_1IN54_V2_SendCommand(0x11); //data entry mode
    135     EPD_1IN54_V2_SendData(0x01);
    136 
    137     EPD_1IN54_V2_SendCommand(0x44); //set Ram-X address start/end position
    138     EPD_1IN54_V2_SendData(0x00);
    139     EPD_1IN54_V2_SendData(0x18);    //0x0C-->(18+1)*8=200
    140 
    141     EPD_1IN54_V2_SendCommand(0x45); //set Ram-Y address start/end position
    142     EPD_1IN54_V2_SendData(0xC7);   //0xC7-->(199+1)=200
    143     EPD_1IN54_V2_SendData(0x00);
    144     EPD_1IN54_V2_SendData(0x00);
    145     EPD_1IN54_V2_SendData(0x00);
    146 
    147     EPD_1IN54_V2_SendCommand(0x3C); //BorderWavefrom
    148     EPD_1IN54_V2_SendData(0x01);
    149 
    150     EPD_1IN54_V2_SendCommand(0x18);
    151     EPD_1IN54_V2_SendData(0x80);
    152 
    153     EPD_1IN54_V2_SendCommand(0x22); // //Load Temperature and waveform setting.
    154     EPD_1IN54_V2_SendData(0XB1);
    155     EPD_1IN54_V2_SendCommand(0x20);
    156 
    157     EPD_1IN54_V2_SendCommand(0x4E);   // set RAM x address count to 0;
    158     EPD_1IN54_V2_SendData(0x00);
    159     EPD_1IN54_V2_SendCommand(0x4F);   // set RAM y address count to 0X199;
    160     EPD_1IN54_V2_SendData(0xC7);
    161     EPD_1IN54_V2_SendData(0x00);
    162     EPD_1IN54_V2_ReadBusy();
    163 }
    164 
    165 /******************************************************************************
    166 function :	Clear screen
    167 parameter:
    168 ******************************************************************************/
    169 void EPD_1IN54_V2_Clear(void)
    170 {
    171     UWORD Width, Height;
    172     Width = (EPD_1IN54_V2_WIDTH % 8 == 0)? (EPD_1IN54_V2_WIDTH / 8 ): (EPD_1IN54_V2_WIDTH / 8 + 1);
    173     Height = EPD_1IN54_V2_HEIGHT;
    174 
    175     EPD_1IN54_V2_SendCommand(0x24);
    176     for (UWORD j = 0; j < Height; j++) {
    177         for (UWORD i = 0; i < Width; i++) {
    178             EPD_1IN54_V2_SendData(0XFF);
    179         }
    180     }
    181     EPD_1IN54_V2_TurnOnDisplay();
    182 }
    183 
    184 /******************************************************************************
    185 function :	Sends the image buffer in RAM to e-Paper and displays
    186 parameter:
    187 ******************************************************************************/
    188 void EPD_1IN54_V2_Display(UBYTE *Image)
    189 {
    190     UWORD Width, Height;
    191     Width = (EPD_1IN54_V2_WIDTH % 8 == 0)? (EPD_1IN54_V2_WIDTH / 8 ): (EPD_1IN54_V2_WIDTH / 8 + 1);
    192     Height = EPD_1IN54_V2_HEIGHT;
    193 
    194     UDOUBLE Addr = 0;
    195     EPD_1IN54_V2_SendCommand(0x24);
    196     for (UWORD j = 0; j < Height; j++) {
    197         for (UWORD i = 0; i < Width; i++) {
    198             Addr = i + j * Width;
    199             EPD_1IN54_V2_SendData(Image[Addr]);
    200         }
    201     }
    202     EPD_1IN54_V2_TurnOnDisplay();
    203 }
    204 
    205 /******************************************************************************
    206 function :	 The image of the previous frame must be uploaded, otherwise the
    207 		         first few seconds will display an exception.
    208 parameter:
    209 ******************************************************************************/
    210 void EPD_1IN54_V2_DisplayPartBaseImage(UBYTE *Image)
    211 {
    212     UWORD Width, Height;
    213     Width = (EPD_1IN54_V2_WIDTH % 8 == 0)? (EPD_1IN54_V2_WIDTH / 8 ): (EPD_1IN54_V2_WIDTH / 8 + 1);
    214     Height = EPD_1IN54_V2_HEIGHT;
    215 
    216     UDOUBLE Addr = 0;
    217     EPD_1IN54_V2_SendCommand(0x24);
    218     for (UWORD j = 0; j < Height; j++) {
    219         for (UWORD i = 0; i < Width; i++) {
    220             Addr = i + j * Width;
    221             EPD_1IN54_V2_SendData(Image[Addr]);
    222         }
    223     }
    224     EPD_1IN54_V2_SendCommand(0x26);
    225     for (UWORD j = 0; j < Height; j++) {
    226         for (UWORD i = 0; i < Width; i++) {
    227             Addr = i + j * Width;
    228             EPD_1IN54_V2_SendData(Image[Addr]);
    229         }
    230     }
    231     EPD_1IN54_V2_TurnOnDisplayPart();
    232 }
    233 
    234 /******************************************************************************
    235 function :	Sends the image buffer in RAM to e-Paper and displays
    236 parameter:
    237 ******************************************************************************/
    238 void EPD_1IN54_V2_DisplayPart(UBYTE *Image)
    239 {
    240     UWORD Width, Height;
    241     Width = (EPD_1IN54_V2_WIDTH % 8 == 0)? (EPD_1IN54_V2_WIDTH / 8 ): (EPD_1IN54_V2_WIDTH / 8 + 1);
    242     Height = EPD_1IN54_V2_HEIGHT;
    243 
    244     UDOUBLE Addr = 0;
    245     EPD_1IN54_V2_SendCommand(0x24);
    246     for (UWORD j = 0; j < Height; j++) {
    247         for (UWORD i = 0; i < Width; i++) {
    248             Addr = i + j * Width;
    249             EPD_1IN54_V2_SendData(Image[Addr]);
    250         }
    251     }
    252     EPD_1IN54_V2_TurnOnDisplayPart();
    253 }
    254 /******************************************************************************
    255 function :	Enter sleep mode
    256 parameter:
    257 ******************************************************************************/
    258 void EPD_1IN54_V2_Sleep(void)
    259 {
    260     EPD_1IN54_V2_SendCommand(0x10); //enter deep sleep
    261     EPD_1IN54_V2_SendData(0x01);
    262     DEV_Delay_ms(100);
    263 }