|
| 1 | +/** |
| 2 | + * @filename : epd13in3k.cpp |
| 3 | + * @brief : Implements for e-paper library |
| 4 | + * @author : Yehui from Waveshare |
| 5 | + * |
| 6 | + * Copyright (C) Waveshare September 25 2023 |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documnetation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdlib.h> |
| 28 | +#include "epd13in3k.h" |
| 29 | + |
| 30 | +Epd::~Epd() { |
| 31 | +}; |
| 32 | + |
| 33 | +Epd::Epd() { |
| 34 | + reset_pin = RST_PIN; |
| 35 | + dc_pin = DC_PIN; |
| 36 | + cs_pin = CS_PIN; |
| 37 | + busy_pin = BUSY_PIN; |
| 38 | + width = EPD_WIDTH; |
| 39 | + height = EPD_HEIGHT; |
| 40 | +}; |
| 41 | + |
| 42 | +int Epd::Init(void) { |
| 43 | + if (IfInit() != 0) { |
| 44 | + return -1; |
| 45 | + } |
| 46 | + Reset(); |
| 47 | + ReadBusy(); |
| 48 | + |
| 49 | + SendCommand(0x12); |
| 50 | + ReadBusy(); |
| 51 | + |
| 52 | + SendCommand(0x0C); |
| 53 | + SendData(0xAE); |
| 54 | + SendData(0xC7); |
| 55 | + SendData(0xC3); |
| 56 | + SendData(0xC0); |
| 57 | + SendData(0x80); |
| 58 | + |
| 59 | + SendCommand(0x01); |
| 60 | + SendData(0xA7); |
| 61 | + SendData(0x02); |
| 62 | + SendData(0x00); |
| 63 | + |
| 64 | + SendCommand(0x11); |
| 65 | + SendData(0x03); |
| 66 | + |
| 67 | + SendCommand(0x44); |
| 68 | + SendData(0x00); |
| 69 | + SendData(0x00); |
| 70 | + SendData(0xBF); |
| 71 | + SendData(0x03); |
| 72 | + |
| 73 | + SendCommand(0x45); |
| 74 | + SendData(0x00); |
| 75 | + SendData(0x00); |
| 76 | + SendData(0xA7); |
| 77 | + SendData(0x02); |
| 78 | + |
| 79 | + SendCommand(0x3C); |
| 80 | + SendData(0x05); |
| 81 | + |
| 82 | + SendCommand(0x18); |
| 83 | + SendData(0x80); |
| 84 | + |
| 85 | + SendCommand(0x4E); |
| 86 | + SendData(0x00); |
| 87 | + |
| 88 | + SendCommand(0x4F); |
| 89 | + SendData(0x00); |
| 90 | + SendData(0x00); |
| 91 | + |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * @brief: basic function for sending commands |
| 97 | + */ |
| 98 | +void Epd::SendCommand(unsigned char command) { |
| 99 | + DigitalWrite(dc_pin, LOW); |
| 100 | + SpiTransfer(command); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * @brief: basic function for sending data |
| 105 | + */ |
| 106 | +void Epd::SendData(unsigned char data) { |
| 107 | + DigitalWrite(dc_pin, HIGH); |
| 108 | + SpiTransfer(data); |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * @brief: Wait until the busy_pin goes HIGH |
| 113 | + */ |
| 114 | +void Epd::ReadBusy(void) { |
| 115 | + unsigned char busy; |
| 116 | + Serial.print("e-Paper Busy\r\n "); |
| 117 | + do{ |
| 118 | + DelayMs(20); |
| 119 | + busy = DigitalRead(busy_pin); |
| 120 | + }while(busy == 1); |
| 121 | + Serial.print("e-Paper Busy Release\r\n "); |
| 122 | + DelayMs(20); |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * @brief: Refresh function |
| 127 | + */ |
| 128 | +void Epd::TurnOnDisplay(void) { |
| 129 | + SendCommand(0x22); |
| 130 | + SendData(0xF7); |
| 131 | + SendCommand(0x20); |
| 132 | + ReadBusy(); |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * @brief: module reset. |
| 137 | + * often used to awaken the module in deep sleep, |
| 138 | + * see Epd::Sleep(); |
| 139 | + */ |
| 140 | +void Epd::Reset(void) { |
| 141 | + DigitalWrite(reset_pin, HIGH); |
| 142 | + DelayMs(20); |
| 143 | + DigitalWrite(reset_pin, LOW); //module reset |
| 144 | + DelayMs(4); |
| 145 | + DigitalWrite(reset_pin, HIGH); |
| 146 | + DelayMs(20); |
| 147 | +} |
| 148 | + |
| 149 | +void Epd::DisplayFrame(const unsigned char* frame_buffer) { |
| 150 | + |
| 151 | + SendCommand(0x24); |
| 152 | + for (unsigned long j = 0; j < height; j++) { |
| 153 | + for (unsigned long i = 0; i < width/8; i++) { |
| 154 | + SendData(frame_buffer[i + j * width/8]); |
| 155 | + } |
| 156 | + } |
| 157 | + TurnOnDisplay(); |
| 158 | +} |
| 159 | + |
| 160 | +void Epd::Displaypart(const unsigned char* pbuffer, unsigned long xStart, unsigned long yStart,unsigned long Picture_Width,unsigned long Picture_Height) { |
| 161 | + SendCommand(0x24); |
| 162 | + for (unsigned long j = 0; j < height; j++) { |
| 163 | + for (unsigned long i = 0; i < width/8; i++) { |
| 164 | + if( (j>=yStart) && (j<yStart+Picture_Height) && (i*8>=xStart) && (i*8<xStart+Picture_Width)){ |
| 165 | + SendData((pgm_read_byte(&(pbuffer[i-xStart/8 + (Picture_Width)/8*(j-yStart)]))) ); |
| 166 | + // SendData(0xff); |
| 167 | + }else { |
| 168 | + SendData(0xFF); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + TurnOnDisplay(); |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * @brief: After this command is transmitted, the chip would enter the |
| 177 | + * deep-sleep mode to save power. |
| 178 | + * The deep sleep mode would return to standby by hardware reset. |
| 179 | + * The only one parameter is a check code, the command would be |
| 180 | + * executed if check code = 0xA5. |
| 181 | + * You can use EPD_Reset() to awaken |
| 182 | + */ |
| 183 | +void Epd::Sleep(void) { |
| 184 | + SendCommand(0x10); |
| 185 | + SendData(0x01); |
| 186 | + DelayMs(100); |
| 187 | + ReadBusy(); |
| 188 | +} |
| 189 | + |
| 190 | +void Epd::Clear(void) { |
| 191 | + |
| 192 | + SendCommand(0x24); |
| 193 | + for(unsigned long i=0; i<height*width/8; i++) { |
| 194 | + SendData(0xFF); |
| 195 | + } |
| 196 | + TurnOnDisplay(); |
| 197 | +} |
| 198 | + |
| 199 | + |
| 200 | +/* END OF FILE */ |
| 201 | + |
| 202 | + |
0 commit comments