waveshare_epaper

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

EPD_4in2b_V2.c (6321B)


      1 /*****************************************************************************
      2 * | File      	:   EPD_4in2b_V2.c
      3 * | Author      :   Waveshare team
      4 * | Function    :   4.2inch e-paper b V2
      5 * | Info        :
      6 *----------------
      7 * |	This version:   V1.0
      8 * | Date        :   2020-04-23
      9 * | Info        :
     10 * -----------------------------------------------------------------------------
     11 #
     12 # Permission is hereby granted, free of charge, to any person obtaining a copy
     13 # of this software and associated documnetation files (the "Software"), to deal
     14 # in the Software without restriction, including without limitation the rights
     15 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     16 # copies of the Software, and to permit persons to  whom the Software is
     17 # furished to do so, subject to the following conditions:
     18 #
     19 # The above copyright notice and this permission notice shall be included in
     20 # all copies or substantial portions of the Software.
     21 #
     22 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     23 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     24 # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     25 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     26 # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     27 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     28 # THE SOFTWARE.
     29 #
     30 ******************************************************************************/
     31 #include "EPD_4in2b_V2.h"
     32 #include "Debug.h"
     33 
     34 /******************************************************************************
     35 function :	Software reset
     36 parameter:
     37 ******************************************************************************/
     38 static void EPD_4IN2B_V2_Reset(void)
     39 {
     40     DEV_Digital_Write(EPD_RST_PIN, 1);
     41     DEV_Delay_ms(200);
     42     DEV_Digital_Write(EPD_RST_PIN, 0);
     43     DEV_Delay_ms(5);
     44     DEV_Digital_Write(EPD_RST_PIN, 1);
     45     DEV_Delay_ms(200);
     46 }
     47 
     48 /******************************************************************************
     49 function :	send command
     50 parameter:
     51      Reg : Command register
     52 ******************************************************************************/
     53 static void EPD_4IN2B_V2_SendCommand(UBYTE Reg)
     54 {
     55     DEV_Digital_Write(EPD_DC_PIN, 0);
     56     DEV_Digital_Write(EPD_CS_PIN, 0);
     57     DEV_SPI_WriteByte(Reg);
     58     DEV_Digital_Write(EPD_CS_PIN, 1);
     59 }
     60 
     61 /******************************************************************************
     62 function :	send data
     63 parameter:
     64     Data : Write data
     65 ******************************************************************************/
     66 static void EPD_4IN2B_V2_SendData(UBYTE Data)
     67 {
     68     DEV_Digital_Write(EPD_DC_PIN, 1);
     69     DEV_Digital_Write(EPD_CS_PIN, 0);
     70     DEV_SPI_WriteByte(Data);
     71     DEV_Digital_Write(EPD_CS_PIN, 1);
     72 }
     73 
     74 /******************************************************************************
     75 function :	Wait until the busy_pin goes LOW
     76 parameter:
     77 ******************************************************************************/
     78 void EPD_4IN2B_V2_ReadBusy(void)
     79 {
     80     Debug("e-Paper busy\r\n");
     81     unsigned char busy;
     82     do{
     83         EPD_4IN2B_V2_SendCommand(0x71);
     84 		busy = DEV_Digital_Read(EPD_BUSY_PIN);
     85 		busy =!(busy & 0x01); 
     86     }while(busy);
     87     Debug("e-Paper busy release\r\n");
     88     DEV_Delay_ms(200);
     89 }
     90 
     91 /******************************************************************************
     92 function :	Turn On Display
     93 parameter:
     94 ******************************************************************************/
     95 static void EPD_4IN2B_V2_TurnOnDisplay(void)
     96 {
     97     EPD_4IN2B_V2_SendCommand(0x12); // DISPLAY_REFRESH
     98     DEV_Delay_ms(100);
     99     EPD_4IN2B_V2_ReadBusy();
    100 }
    101 
    102 /******************************************************************************
    103 function :	Initialize the e-Paper register
    104 parameter:
    105 ******************************************************************************/
    106 void EPD_4IN2B_V2_Init(void)
    107 {
    108     EPD_4IN2B_V2_Reset();
    109     
    110     EPD_4IN2B_V2_SendCommand(0x04); 
    111     EPD_4IN2B_V2_ReadBusy();
    112 
    113     EPD_4IN2B_V2_SendCommand(0x00);
    114     EPD_4IN2B_V2_SendData(0x0f);
    115 }
    116 
    117 /******************************************************************************
    118 function :	Clear screen
    119 parameter:
    120 ******************************************************************************/
    121 void EPD_4IN2B_V2_Clear(void)
    122 {
    123     UWORD Width, Height;
    124     Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
    125     Height = EPD_4IN2B_V2_HEIGHT;
    126 
    127     EPD_4IN2B_V2_SendCommand(0x10);
    128     for (UWORD j = 0; j < Height; j++) {
    129         for (UWORD i = 0; i < Width; i++) {
    130             EPD_4IN2B_V2_SendData(0xFF);
    131         }
    132     }
    133 
    134     EPD_4IN2B_V2_SendCommand(0x13);
    135     for (UWORD j = 0; j < Height; j++) {
    136         for (UWORD i = 0; i < Width; i++) {
    137             EPD_4IN2B_V2_SendData(0xFF);
    138         }
    139     }
    140 
    141     EPD_4IN2B_V2_TurnOnDisplay();
    142 }
    143 
    144 /******************************************************************************
    145 function :	Sends the image buffer in RAM to e-Paper and displays
    146 parameter:
    147 ******************************************************************************/
    148 void EPD_4IN2B_V2_Display(const UBYTE *blackimage, const UBYTE *ryimage)
    149 {
    150     UWORD Width, Height;
    151     Width = (EPD_4IN2B_V2_WIDTH % 8 == 0)? (EPD_4IN2B_V2_WIDTH / 8 ): (EPD_4IN2B_V2_WIDTH / 8 + 1);
    152     Height = EPD_4IN2B_V2_HEIGHT;
    153 
    154     EPD_4IN2B_V2_SendCommand(0x10);
    155     for (UWORD j = 0; j < Height; j++) {
    156         for (UWORD i = 0; i < Width; i++) {
    157             EPD_4IN2B_V2_SendData(blackimage[i + j * Width]);
    158         }
    159     }
    160 
    161     EPD_4IN2B_V2_SendCommand(0x13);
    162     for (UWORD j = 0; j < Height; j++) {
    163         for (UWORD i = 0; i < Width; i++) {
    164             EPD_4IN2B_V2_SendData(ryimage[i + j * Width]);
    165         }
    166     }
    167 
    168     EPD_4IN2B_V2_TurnOnDisplay();
    169 }
    170 
    171 /******************************************************************************
    172 function :	Enter sleep mode
    173 parameter:
    174 ******************************************************************************/
    175 void EPD_4IN2B_V2_Sleep(void)
    176 {
    177     EPD_4IN2B_V2_SendCommand(0X50);
    178     EPD_4IN2B_V2_SendData(0xf7);		//border floating	
    179 
    180     EPD_4IN2B_V2_SendCommand(0X02);  	//power off
    181     EPD_4IN2B_V2_ReadBusy(); //waiting for the electronic paper IC to release the idle signal
    182     EPD_4IN2B_V2_SendCommand(0X07);  	//deep sleep
    183     EPD_4IN2B_V2_SendData(0xA5);
    184 }