waveshare_epaper

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

EPD_5in83bc.c (12918B)


      1 /*****************************************************************************
      2 * | File      	:   EPD_5IN83BC.c
      3 * | Author      :   Waveshare team
      4 * | Function    :   5.83inch e-paper b&c
      5 * | Info        :
      6 * |	This version:   V3.0
      7 * | Date        :   2019-06-13
      8 * | Info        :
      9 * -----------------------------------------------------------------------------
     10 * V3.0(2019-06-13):
     11 * 1.Change:
     12 *    EPD_Reset() => EPD_5IN83BC_Reset()
     13 *    EPD_SendCommand() => EPD_5IN83BC_SendCommand()
     14 *    EPD_SendData() => EPD_5IN83BC_SendData()
     15 *    EPD_WaitUntilIdle() => EPD_5IN83BC_ReadBusy()
     16 *    EPD_Init() => EPD_5IN83BC_Init()
     17 *    EPD_Clear() => EPD_5IN83BC_Clear()
     18 *    EPD_Display() => EPD_5IN83BC_Display()
     19 *    EPD_Sleep() => EPD_5IN83BC_Sleep()
     20 * 2.Remove:
     21 *   #define PANEL_SETTING                               0x00
     22 *   #define POWER_SETTING                               0x01
     23 *   #define POWER_OFF                                   0x02
     24 *   #define POWER_OFF_SEQUENCE_SETTING                  0x03
     25 *   #define POWER_ON                                    0x04
     26 *   #define POWER_ON_MEASURE                            0x05
     27 *   #define BOOSTER_SOFT_START                          0x06
     28 *   #define DEEP_SLEEP                                  0x07
     29 *   #define DATA_START_TRANSMISSION_1                   0x10
     30 *   #define DATA_STOP                                   0x11
     31 *   #define DISPLAY_REFRESH                             0x12
     32 *   #define IMAGE_PROCESS                               0x13
     33 *   #define LUT_FOR_VCOM                                0x20
     34 *   #define LUT_BLUE                                    0x21
     35 *   #define LUT_WHITE                                   0x22
     36 *   #define LUT_GRAY_1                                  0x23
     37 *   #define LUT_GRAY_2                                  0x24
     38 *   #define LUT_RED_0                                   0x25
     39 *   #define LUT_RED_1                                   0x26
     40 *   #define LUT_RED_2                                   0x27
     41 *   #define LUT_RED_3                                   0x28
     42 *   #define LUT_XON                                     0x29
     43 *   #define PLL_CONTROL                                 0x30
     44 *   #define TEMPERATURE_SENSOR_COMMAND                  0x40
     45 *   #define TEMPERATURE_CALIBRATION                     0x41
     46 *   #define TEMPERATURE_SENSOR_WRITE                    0x42
     47 *   #define TEMPERATURE_SENSOR_READ                     0x43
     48 *   #define VCOM_AND_DATA_INTERVAL_SETTING              0x50
     49 *   #define LOW_POWER_DETECTION                         0x51
     50 *   #define TCON_SETTING                                0x60
     51 *   #define TCON_RESOLUTION                             0x61
     52 *   #define SPI_FLASH_CONTROL                           0x65
     53 *   #define REVISION                                    0x70
     54 *   #define GET_STATUS                                  0x71
     55 *   #define AUTO_MEASUREMENT_VCOM                       0x80
     56 *   #define READ_VCOM_VALUE                             0x81
     57 *   #define VCM_DC_SETTING                              0x82
     58 * 3.add:
     59 *   EPD_5IN83BC_DisplayHalfScreen()
     60 * -----------------------------------------------------------------------------
     61 * V2.0(2018-11-09):
     62 * 1.Remove:ImageBuff[EPD_HEIGHT * EPD_WIDTH / 8]
     63 * 2.Change:EPD_Display(UBYTE *Image)
     64 *   Need to pass parameters: pointer to cached data
     65 #
     66 # Permission is hereby granted, free of charge, to any person obtaining a copy
     67 # of this software and associated documnetation files (the "Software"), to deal
     68 # in the Software without restriction, including without limitation the rights
     69 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     70 # copies of the Software, and to permit persons to  whom the Software is
     71 # furished to do so, subject to the following conditions:
     72 #
     73 # The above copyright notice and this permission notice shall be included in
     74 # all copies or substantial portions of the Software.
     75 #
     76 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     77 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     78 # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     79 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     80 # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     81 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     82 # THE SOFTWARE.
     83 #
     84 ******************************************************************************/
     85 #include "EPD_5in83bc.h"
     86 #include "Debug.h"
     87 
     88 /******************************************************************************
     89 function :	Software reset
     90 parameter:
     91 ******************************************************************************/
     92 static void EPD_5IN83BC_Reset(void)
     93 {
     94     DEV_Digital_Write(EPD_RST_PIN, 1);
     95     DEV_Delay_ms(200);
     96     DEV_Digital_Write(EPD_RST_PIN, 0);
     97     DEV_Delay_ms(200);
     98     DEV_Digital_Write(EPD_RST_PIN, 1);
     99     DEV_Delay_ms(200);
    100 }
    101 
    102 /******************************************************************************
    103 function :	send command
    104 parameter:
    105      Reg : Command register
    106 ******************************************************************************/
    107 static void EPD_5IN83BC_SendCommand(UBYTE Reg)
    108 {
    109     DEV_Digital_Write(EPD_DC_PIN, 0);
    110     DEV_Digital_Write(EPD_CS_PIN, 0);
    111     DEV_SPI_WriteByte(Reg);
    112     DEV_Digital_Write(EPD_CS_PIN, 1);
    113 }
    114 
    115 /******************************************************************************
    116 function :	send data
    117 parameter:
    118     Data : Write data
    119 ******************************************************************************/
    120 static void EPD_5IN83BC_SendData(UBYTE Data)
    121 {
    122     DEV_Digital_Write(EPD_DC_PIN, 1);
    123     DEV_Digital_Write(EPD_CS_PIN, 0);
    124     DEV_SPI_WriteByte(Data);
    125     DEV_Digital_Write(EPD_CS_PIN, 1);
    126 }
    127 
    128 /******************************************************************************
    129 function :	Wait until the busy_pin goes LOW
    130 parameter:
    131 ******************************************************************************/
    132 void EPD_5IN83BC_ReadBusy(void)
    133 {
    134     UBYTE busy;
    135     Debug("e-Paper busy\r\n");
    136     do {
    137         EPD_5IN83BC_SendCommand(0x71);
    138         busy = DEV_Digital_Read(EPD_BUSY_PIN);
    139         busy =!(busy & 0x01);
    140     } while(busy);
    141     Debug("e-Paper busy release\r\n");
    142 }
    143 
    144 /******************************************************************************
    145 function :	Turn On Display
    146 parameter:
    147 ******************************************************************************/
    148 static void EPD_5IN83BC_TurnOnDisplay(void)
    149 {
    150     EPD_5IN83BC_SendCommand(0x04); // POWER ON
    151     EPD_5IN83BC_ReadBusy();
    152     EPD_5IN83BC_SendCommand(0x12); // display refresh
    153     DEV_Delay_ms(100);
    154     EPD_5IN83BC_ReadBusy();
    155 
    156 }
    157 
    158 /******************************************************************************
    159 function :	Initialize the e-Paper register
    160 parameter:
    161 ******************************************************************************/
    162 void EPD_5IN83BC_Init(void)
    163 {
    164     EPD_5IN83BC_Reset();
    165 
    166     EPD_5IN83BC_SendCommand(0x01); // POWER_SETTING
    167     EPD_5IN83BC_SendData(0x37);
    168     EPD_5IN83BC_SendData(0x00);
    169 
    170     EPD_5IN83BC_SendCommand(0x00); // PANEL_SETTING
    171     EPD_5IN83BC_SendData(0xCF);
    172     EPD_5IN83BC_SendData(0x08);
    173 
    174     EPD_5IN83BC_SendCommand(0x30); // PLL_CONTROL
    175     EPD_5IN83BC_SendData(0x3A); //PLL:  0-15:0x3C, 15+:0x3A
    176 
    177     EPD_5IN83BC_SendCommand(0x82); // VCM_DC_SETTING
    178     EPD_5IN83BC_SendData(0x28); //all temperature  range
    179 
    180     EPD_5IN83BC_SendCommand(0x06); // BOOSTER_SOFT_START
    181     EPD_5IN83BC_SendData (0xc7);
    182     EPD_5IN83BC_SendData (0xcc);
    183     EPD_5IN83BC_SendData (0x15);
    184 
    185     EPD_5IN83BC_SendCommand(0x50); // VCOM AND DATA INTERVAL SETTING
    186     EPD_5IN83BC_SendData(0x77);
    187 
    188     EPD_5IN83BC_SendCommand(0x60); // TCON_SETTING
    189     EPD_5IN83BC_SendData(0x22);
    190 
    191     EPD_5IN83BC_SendCommand(0x65); // FLASH CONTROL
    192     EPD_5IN83BC_SendData(0x00);
    193 
    194     EPD_5IN83BC_SendCommand(0x61); // TCON_RESOLUTION
    195     EPD_5IN83BC_SendData (0x02); // source 600
    196     EPD_5IN83BC_SendData (0x58);
    197     EPD_5IN83BC_SendData (0x01); // gate 448
    198     EPD_5IN83BC_SendData (0xc0);
    199 
    200     EPD_5IN83BC_SendCommand(0xe5); // FLASH MODE
    201     EPD_5IN83BC_SendData(0x03);
    202 
    203 }
    204 
    205 /******************************************************************************
    206 function :	Clear screen
    207 parameter:
    208 ******************************************************************************/
    209 void EPD_5IN83BC_Clear(void)
    210 {
    211     UWORD Width, Height;
    212     Width = (EPD_5IN83BC_WIDTH % 8 == 0)? (EPD_5IN83BC_WIDTH / 8): (EPD_5IN83BC_WIDTH / 8 + 1);
    213     Height = EPD_5IN83BC_HEIGHT;
    214 
    215     EPD_5IN83BC_SendCommand(0x10);
    216     for (UWORD j = 0; j < Height; j++) {
    217         for (UWORD i = 0; i < Width; i++) {
    218             for(UBYTE k = 0; k < 4; k++) {
    219                 EPD_5IN83BC_SendData(0x33);
    220             }
    221         }
    222     }
    223 
    224     EPD_5IN83BC_TurnOnDisplay();
    225 }
    226 
    227 /******************************************************************************
    228 function :	Sends the image buffer in RAM to e-Paper and displays
    229 parameter:
    230 ******************************************************************************/
    231 void EPD_5IN83BC_Display(const UBYTE *blackimage, const UBYTE *ryimage)
    232 {
    233     UBYTE Data_Black, Data_RY, Data;
    234     UDOUBLE i, j, Width, Height;
    235     Width = (EPD_5IN83BC_WIDTH % 8 == 0)? (EPD_5IN83BC_WIDTH / 8 ): (EPD_5IN83BC_WIDTH / 8 + 1);
    236     Height = EPD_5IN83BC_HEIGHT;
    237 
    238     EPD_5IN83BC_SendCommand(0x10);
    239     for (j = 0; j < Height; j++) {
    240         for (i = 0; i < Width; i++) {
    241             Data_Black = blackimage[i + j * Width];
    242             Data_RY = ryimage[i + j * Width]; // red or yellow
    243             for(UBYTE k = 0; k < 8; k++) {
    244                 if ((Data_RY & 0x80) == 0x00) {
    245                     Data = 0x04;                //red
    246                 } else if ((Data_Black & 0x80) == 0x00) {
    247                     Data = 0x00;               //black
    248                 } else {
    249                     Data = 0x03;                //white
    250                 }
    251                 Data = (Data << 4) & 0xFF;
    252                 Data_Black = (Data_Black << 1) & 0xFF;
    253                 Data_RY = (Data_RY << 1) & 0xFF;
    254                 k += 1;
    255 
    256                 if((Data_RY & 0x80) == 0x00) {
    257                     Data |= 0x04;              //red
    258                 } else if ((Data_Black & 0x80) == 0x00) {
    259                     Data |= 0x00;              //black
    260                 } else {
    261                     Data |= 0x03;              //white
    262                 }
    263                 Data_Black = (Data_Black << 1) & 0xFF;
    264                 Data_RY = (Data_RY << 1) & 0xFF;
    265                 EPD_5IN83BC_SendData(Data);
    266             }
    267         }
    268     }
    269 
    270     EPD_5IN83BC_TurnOnDisplay();
    271 }
    272 
    273 /******************************************************************************
    274 function :	Sends the image buffer in RAM to e-Paper and displays
    275 parameter:
    276 ******************************************************************************/
    277 void EPD_5IN83BC_DisplayHalfScreen(const UBYTE *blackimage, const UBYTE *ryimage)
    278 {
    279     UBYTE Data_Black, Data_RY, Data;
    280     UDOUBLE i, j, Width, Height;
    281     Width = (EPD_5IN83BC_WIDTH % 8 == 0)? (EPD_5IN83BC_WIDTH / 8 ): (EPD_5IN83BC_WIDTH / 8 + 1);
    282     Height = EPD_5IN83BC_HEIGHT;
    283 
    284     EPD_5IN83BC_SendCommand(0x10);
    285     for (j = 0; j < Height / 2; j++) {
    286         for (i = 0; i < Width; i++) {
    287             Data_Black = blackimage[i + j * Width];
    288             Data_RY = ryimage[i + j * Width]; // red or yellow
    289             for(UBYTE k = 0; k < 8; k++) {
    290                 if ((Data_RY & 0x80) == 0x00) {
    291                     Data = 0x04;                //red
    292                 } else if ((Data_Black & 0x80) == 0x00) {
    293                     Data = 0x00;               //black
    294                 } else {
    295                     Data = 0x03;                //white
    296                 }
    297                 Data = (Data << 4) & 0xFF;
    298                 Data_Black = (Data_Black << 1) & 0xFF;
    299                 Data_RY = (Data_RY << 1) & 0xFF;
    300                 k += 1;
    301 
    302                 if((Data_RY & 0x80) == 0x00) {
    303                     Data |= 0x04;              //red
    304                 } else if ((Data_Black & 0x80) == 0x00) {
    305                     Data |= 0x00;              //black
    306                 } else {
    307                     Data |= 0x03;              //white
    308                 }
    309                 Data_Black = (Data_Black << 1) & 0xFF;
    310                 Data_RY = (Data_RY << 1) & 0xFF;
    311                 EPD_5IN83BC_SendData(Data);
    312             }
    313         }
    314     }
    315 
    316     for (j = 0; j < Height / 2; j++) {
    317         for (i = 0; i < Width; i++) {
    318             for(UBYTE k = 0; k < 4; k++) {
    319                 EPD_5IN83BC_SendData(0x33);
    320             }
    321         }
    322     }
    323 
    324     EPD_5IN83BC_TurnOnDisplay();
    325 }
    326 
    327 /******************************************************************************
    328 function :	Enter sleep mode
    329 parameter:
    330 ******************************************************************************/
    331 void EPD_5IN83BC_Sleep(void)
    332 {
    333     EPD_5IN83BC_SendCommand(0x02); // POWER_OFF
    334     EPD_5IN83BC_ReadBusy();
    335     EPD_5IN83BC_SendCommand(0x07); // DEEP_SLEEP
    336     EPD_5IN83BC_SendData(0XA5);
    337 }