waveshare_epaper

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

EPD_2in7b.c (12840B)


      1 /*****************************************************************************
      2 * | File      	:   EPD_2in7b.c
      3 * | Author      :   Waveshare team
      4 * | Function    :   2.7inch e-paper b
      5 * | Info        :
      6 *----------------
      7 * |	This version:   V3.0
      8 * | Date        :   2019-06-12
      9 * | Info        :
     10 * -----------------------------------------------------------------------------
     11 * V3.0(2019-06-12): 
     12 *    lut_vcom_dc[] => EPD_2in7B_lut_vcom_dc[]
     13 *    lut_ww[] => EPD_2in7B_lut_ww[] 
     14 *    lut_bw[] => EPD_2in7B_lut_bw[]
     15 *    lut_bb[] => EPD_2in7B_lut_bb[]
     16 *    lut_wb[] => EPD_2in7B_lut_wb[]
     17 *    EPD_Reset() => EPD_2in7B_Reset()
     18 *    EPD_2IN7B_SendCommand() => EPD_2in7B_SendCommand()
     19 *    EPD_2IN7B_SendData() => EPD_2in7B_SendData()
     20 *    EPD_2IN7B_ReadBusy() => EPD_2in7B_ReadBusy()
     21 *    EPD_SetLut() => EPD_2in7B_SetLut()
     22 *    EPD_Init() => EPD_2in7B_Init()
     23 *    EPD_Clear() => EPD_2in7B_Clear()
     24 *    EPD_Display() => EPD_2in7B_Display()
     25 *    EPD_Sleep() => EPD_2in7B_Sleep()
     26 * 2.remove commands define:
     27 *    #define PANEL_SETTING                               0x00
     28 *    #define POWER_SETTING                               0x01
     29 *    #define POWER_OFF                                   0x02
     30 *    #define POWER_OFF_SEQUENCE_SETTING                  0x03
     31 *    #define POWER_ON                                    0x04
     32 *    #define POWER_ON_MEASURE                            0x05
     33 *    #define BOOSTER_SOFT_START                          0x06
     34 *    #define DEEP_SLEEP                                  0x07
     35 *    #define DATA_START_TRANSMISSION_1                   0x10
     36 *    #define DATA_STOP                                   0x11
     37 *    #define DISPLAY_REFRESH                             0x12
     38 *    #define DATA_START_TRANSMISSION_2                   0x13
     39 *    #define PARTIAL_DATA_START_TRANSMISSION_1           0x14
     40 *    #define PARTIAL_DATA_START_TRANSMISSION_2           0x15
     41 *    #define PARTIAL_DISPLAY_REFRESH                     0x16
     42 *    #define LUT_FOR_VCOM                                0x20
     43 *    #define LUT_WHITE_TO_WHITE                          0x21
     44 *    #define LUT_BLACK_TO_WHITE                          0x22
     45 *    #define LUT_WHITE_TO_BLACK                          0x23
     46 *    #define LUT_BLACK_TO_BLACK                          0x24
     47 *    #define PLL_CONTROL                                 0x30
     48 *    #define TEMPERATURE_SENSOR_COMMAND                  0x40
     49 *    #define TEMPERATURE_SENSOR_CALIBRATION              0x41
     50 *    #define TEMPERATURE_SENSOR_WRITE                    0x42
     51 *    #define TEMPERATURE_SENSOR_READ                     0x43
     52 *    #define VCOM_AND_DATA_INTERVAL_SETTING              0x50
     53 *    #define LOW_POWER_DETECTION                         0x51
     54 *    #define TCON_SETTING                                0x60
     55 *    #define TCON_RESOLUTION                             0x61
     56 *    #define SOURCE_AND_GATE_START_SETTING               0x62
     57 *    #define GET_STATUS                                  0x71
     58 *    #define AUTO_MEASURE_VCOM                           0x80
     59 *    #define VCOM_VALUE                                  0x81
     60 *    #define VCM_DC_SETTING_REGISTER                     0x82
     61 *    #define PROGRAM_MODE                                0xA0
     62 *    #define ACTIVE_PROGRAM                              0xA1
     63 *    #define READ_OTP_DATA                               0xA2
     64 * -----------------------------------------------------------------------------
     65 * V2.0(2018-10-30):
     66 * 1.Remove:ImageBuff[EPD_2IN7B_HEIGHT * EPD_2IN7B_WIDTH / 8]
     67 * 2.Change:EPD_Display(UBYTE *Image)
     68 *   Need to pass parameters: pointer to cached data
     69 #
     70 # Permission is hereby granted, free of charge, to any person obtaining a copy
     71 # of this software and associated documnetation files (the "Software"), to deal
     72 # in the Software without restriction, including without limitation the rights
     73 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     74 # copies of the Software, and to permit persons to  whom the Software is
     75 # furished to do so, subject to the following conditions:
     76 #
     77 # The above copyright notice and this permission notice shall be included in
     78 # all copies or substantial portions of the Software.
     79 #
     80 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     81 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     82 # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     83 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     84 # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     85 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     86 # THE SOFTWARE.
     87 #
     88 ******************************************************************************/
     89 #include "EPD_2in7b.h"
     90 #include "Debug.h"
     91 
     92 static const unsigned char EPD_2IN7B_lut_vcom_dc[] = {
     93     0x00, 0x00,
     94     0x00, 0x1A, 0x1A, 0x00, 0x00, 0x01,
     95     0x00, 0x0A, 0x0A, 0x00, 0x00, 0x08,
     96     0x00, 0x0E, 0x01, 0x0E, 0x01, 0x10,
     97     0x00, 0x0A, 0x0A, 0x00, 0x00, 0x08,
     98     0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
     99     0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
    100     0x00, 0x23, 0x00, 0x00, 0x00, 0x01
    101 };
    102 
    103 //R21H
    104 static const unsigned char EPD_2IN7B_lut_ww[] = {
    105     0x90, 0x1A, 0x1A, 0x00, 0x00, 0x01,
    106     0x40, 0x0A, 0x0A, 0x00, 0x00, 0x08,
    107     0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
    108     0x80, 0x0A, 0x0A, 0x00, 0x00, 0x08,
    109     0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
    110     0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
    111     0x00, 0x23, 0x00, 0x00, 0x00, 0x01
    112 };
    113 
    114 //R22H    r
    115 static const unsigned char EPD_2IN7B_lut_bw[] = {
    116     0xA0, 0x1A, 0x1A, 0x00, 0x00, 0x01,
    117     0x00, 0x0A, 0x0A, 0x00, 0x00, 0x08,
    118     0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
    119     0x90, 0x0A, 0x0A, 0x00, 0x00, 0x08,
    120     0xB0, 0x04, 0x10, 0x00, 0x00, 0x05,
    121     0xB0, 0x03, 0x0E, 0x00, 0x00, 0x0A,
    122     0xC0, 0x23, 0x00, 0x00, 0x00, 0x01
    123 };
    124 
    125 //R23H    w
    126 static const unsigned char EPD_2IN7B_lut_bb[] = {
    127     0x90, 0x1A, 0x1A, 0x00, 0x00, 0x01,
    128     0x40, 0x0A, 0x0A, 0x00, 0x00, 0x08,
    129     0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
    130     0x80, 0x0A, 0x0A, 0x00, 0x00, 0x08,
    131     0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
    132     0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
    133     0x00, 0x23, 0x00, 0x00, 0x00, 0x01
    134 };
    135 
    136 //R24H    b
    137 static const unsigned char EPD_2IN7B_lut_wb[] = {
    138     0x90, 0x1A, 0x1A, 0x00, 0x00, 0x01,
    139     0x20, 0x0A, 0x0A, 0x00, 0x00, 0x08,
    140     0x84, 0x0E, 0x01, 0x0E, 0x01, 0x10,
    141     0x10, 0x0A, 0x0A, 0x00, 0x00, 0x08,
    142     0x00, 0x04, 0x10, 0x00, 0x00, 0x05,
    143     0x00, 0x03, 0x0E, 0x00, 0x00, 0x0A,
    144     0x00, 0x23, 0x00, 0x00, 0x00, 0x01
    145 };
    146 
    147 /******************************************************************************
    148 function :	Software reset
    149 parameter:
    150 ******************************************************************************/
    151 static void EPD_2IN7B_Reset(void)
    152 {
    153     DEV_Digital_Write(EPD_RST_PIN, 1);
    154     DEV_Delay_ms(200);
    155     DEV_Digital_Write(EPD_RST_PIN, 0);
    156     DEV_Delay_ms(10);
    157     DEV_Digital_Write(EPD_RST_PIN, 1);
    158     DEV_Delay_ms(200);
    159 }
    160 
    161 /******************************************************************************
    162 function :	send command
    163 parameter:
    164      Reg : Command register
    165 ******************************************************************************/
    166 static void EPD_2IN7B_SendCommand(UBYTE Reg)
    167 {
    168     DEV_Digital_Write(EPD_DC_PIN, 0);
    169     DEV_Digital_Write(EPD_CS_PIN, 0);
    170     DEV_SPI_WriteByte(Reg);
    171     DEV_Digital_Write(EPD_CS_PIN, 1);
    172 }
    173 
    174 /******************************************************************************
    175 function :	send data
    176 parameter:
    177     Data : Write data
    178 ******************************************************************************/
    179 static void EPD_2IN7B_SendData(UBYTE Data)
    180 {
    181     DEV_Digital_Write(EPD_DC_PIN, 1);
    182     DEV_Digital_Write(EPD_CS_PIN, 0);
    183     DEV_SPI_WriteByte(Data);
    184     DEV_Digital_Write(EPD_CS_PIN, 1);
    185 }
    186 
    187 /******************************************************************************
    188 function :	Wait until the busy_pin goes LOW
    189 parameter:
    190 ******************************************************************************/
    191 static void EPD_2IN7B_ReadBusy(void)
    192 {
    193     Debug("e-Paper busy\r\n");
    194     while(DEV_Digital_Read(EPD_BUSY_PIN) == 0) {      //0: busy, 1: idle
    195         DEV_Delay_ms(100);
    196     }    
    197     Debug("e-Paper busy release\r\n");
    198 }
    199 
    200 /******************************************************************************
    201 function :	set the look-up tables
    202 parameter:
    203 ******************************************************************************/
    204 static void EPD_2IN7B_SetLut(void)
    205 {
    206     unsigned int count;     
    207     EPD_2IN7B_SendCommand(0x20); //vcom
    208     for(count = 0; count < 44; count++) {
    209         EPD_2IN7B_SendData(EPD_2IN7B_lut_vcom_dc[count]);
    210     }
    211     
    212     EPD_2IN7B_SendCommand(0x21); //ww --
    213     for(count = 0; count < 42; count++) {
    214         EPD_2IN7B_SendData(EPD_2IN7B_lut_ww[count]);
    215     }   
    216     
    217     EPD_2IN7B_SendCommand(0x22); //bw r
    218     for(count = 0; count < 42; count++) {
    219         EPD_2IN7B_SendData(EPD_2IN7B_lut_bw[count]);
    220     } 
    221 
    222     EPD_2IN7B_SendCommand(0x23); //wb w
    223     for(count = 0; count < 42; count++) {
    224         EPD_2IN7B_SendData(EPD_2IN7B_lut_bb[count]);
    225     } 
    226 
    227     EPD_2IN7B_SendCommand(0x24); //bb b
    228     for(count = 0; count < 42; count++) {
    229         EPD_2IN7B_SendData(EPD_2IN7B_lut_wb[count]);
    230     } 
    231 }
    232 
    233 /******************************************************************************
    234 function :	Initialize the e-Paper register
    235 parameter:
    236 ******************************************************************************/
    237 void EPD_2IN7B_Init(void)
    238 {
    239     EPD_2IN7B_Reset();
    240     
    241     EPD_2IN7B_SendCommand(0x06);         //boost soft start
    242     EPD_2IN7B_SendData (0x07);		//A
    243     EPD_2IN7B_SendData (0x07);		//B
    244     EPD_2IN7B_SendData (0x17);		//C       
    245 
    246     EPD_2IN7B_SendCommand(0x04);  
    247     EPD_2IN7B_ReadBusy();//waiting for the electronic paper IC to release the idle signal
    248 
    249     EPD_2IN7B_SendCommand(0x00);			//panel setting
    250     EPD_2IN7B_SendData(0x0f);		//LUT from OTP£?128x296
    251 
    252     EPD_2IN7B_SendCommand(0x16);
    253     EPD_2IN7B_SendData(0x00);				//KW-BF   KWR-AF	BWROTP 0f	
    254 
    255     EPD_2IN7B_SendCommand(0xF8);         //boostéè?¨
    256     EPD_2IN7B_SendData (0x60);
    257     EPD_2IN7B_SendData(0xa5);
    258 
    259     EPD_2IN7B_SendCommand(0xF8);         //boostéè?¨
    260     EPD_2IN7B_SendData (0x90);
    261     EPD_2IN7B_SendData (0x00);
    262 
    263     EPD_2IN7B_SendCommand(0xF8);         //boostéè?¨
    264     EPD_2IN7B_SendData (0x93);
    265     EPD_2IN7B_SendData(0x2A);
    266 
    267     EPD_2IN7B_SendCommand(0x01); // PANEL_SETTING
    268     EPD_2IN7B_SendData(0x03); // VDS_EN, VDG_EN
    269     EPD_2IN7B_SendData(0x00); // VCOM_HV, VGHL_LV[1], VGHL_LV[0]
    270     EPD_2IN7B_SendData(0x2b); // VDH
    271     EPD_2IN7B_SendData(0x2b); // VDL
    272     EPD_2IN7B_SendData(0x2b); // VDHR
    273     
    274 }
    275 
    276 /******************************************************************************
    277 function :	Clear screen
    278 parameter:
    279 ******************************************************************************/
    280 void EPD_2IN7B_Clear(void)
    281 {    
    282     UWORD Width, Height;
    283     Width = (EPD_2IN7B_WIDTH % 8 == 0)? (EPD_2IN7B_WIDTH / 8 ): (EPD_2IN7B_WIDTH / 8 + 1);
    284     Height = EPD_2IN7B_HEIGHT;
    285 
    286     EPD_2IN7B_SendCommand(0x10);
    287     for (UWORD j = 0; j < Height; j++) {
    288         for (UWORD i = 0; i < Width; i++) {
    289             EPD_2IN7B_SendData(0X00);
    290         }
    291     }
    292     EPD_2IN7B_SendCommand(0x11); // DATA_STOP
    293 
    294     EPD_2IN7B_SendCommand(0x13);
    295     for (UWORD j = 0; j < Height; j++) {
    296         for (UWORD i = 0; i < Width; i++) {
    297             EPD_2IN7B_SendData(0X00);
    298         }
    299     }
    300     EPD_2IN7B_SendCommand(0x11); // DATA_STOP
    301     
    302     EPD_2IN7B_SendCommand(0x12);
    303     EPD_2IN7B_ReadBusy();
    304 }
    305 
    306 /******************************************************************************
    307 function :	Sends the image buffer in RAM to e-Paper and displays
    308 parameter:
    309 ******************************************************************************/
    310 void EPD_2IN7B_Display(UBYTE *Imageblack, UBYTE *Imagered)
    311 {
    312     UWORD Width, Height;
    313     Width = (EPD_2IN7B_WIDTH % 8 == 0)? (EPD_2IN7B_WIDTH / 8 ): (EPD_2IN7B_WIDTH / 8 + 1);
    314     Height = EPD_2IN7B_HEIGHT;
    315 
    316     EPD_2IN7B_SendCommand(0x10);
    317     for (UWORD j = 0; j < Height; j++) {
    318         for (UWORD i = 0; i < Width; i++) {
    319             EPD_2IN7B_SendData(~Imageblack[i + j * Width]);
    320         }
    321     }
    322     EPD_2IN7B_SendCommand(0x11); // DATA_STOP
    323     
    324     EPD_2IN7B_SendCommand(0x13);
    325     for (UWORD j = 0; j < Height; j++) {
    326         for (UWORD i = 0; i < Width; i++) {
    327             EPD_2IN7B_SendData(~Imagered[i + j * Width]);
    328         }
    329     }
    330  
    331     EPD_2IN7B_SendCommand(0x11); // DATA_STOP
    332     
    333     EPD_2IN7B_SendCommand(0x12);
    334     EPD_2IN7B_ReadBusy();
    335 }
    336 
    337 /******************************************************************************
    338 function :	Enter sleep mode
    339 parameter:
    340 ******************************************************************************/
    341 void EPD_2IN7B_Sleep(void)
    342 {
    343     EPD_2IN7B_SendCommand(0X50);
    344     EPD_2IN7B_SendData(0xf7);
    345     EPD_2IN7B_SendCommand(0X02); //power off
    346     EPD_2IN7B_SendCommand(0X07); //deep sleep
    347     EPD_2IN7B_SendData(0xA5);
    348 }