|
| 1 | +#include "Adafruit_UC8151D.h" |
| 2 | +#include "Adafruit_EPD.h" |
| 3 | + |
| 4 | +#define BUSY_WAIT 500 |
| 5 | + |
| 6 | +const unsigned char LUT_DATA[30] = { |
| 7 | + 0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22, 0x66, 0x69, |
| 8 | + 0x69, 0x59, 0x58, 0x99, 0x99, 0x88, 0x00, 0x00, 0x00, 0x00, |
| 9 | + 0xF8, 0xB4, 0x13, 0x51, 0x35, 0x51, 0x51, 0x19, 0x01, 0x00}; |
| 10 | + |
| 11 | +// clang-format off |
| 12 | + |
| 13 | +const uint8_t uc8151d_default_init_code[] { |
| 14 | + UC8151D_PON, 0, |
| 15 | + 0xFF, 10, |
| 16 | + UC8151D_PSR, 1, 0x1F, |
| 17 | + UC8151D_CDI, 1, 0x97, |
| 18 | + 0xFE}; |
| 19 | + |
| 20 | +// clang-format on |
| 21 | + |
| 22 | +/**************************************************************************/ |
| 23 | +/*! |
| 24 | + @brief constructor if using external SRAM chip and software SPI |
| 25 | + @param width the width of the display in pixels |
| 26 | + @param height the height of the display in pixels |
| 27 | + @param SID the SID pin to use |
| 28 | + @param SCLK the SCLK pin to use |
| 29 | + @param DC the data/command pin to use |
| 30 | + @param RST the reset pin to use |
| 31 | + @param CS the chip select pin to use |
| 32 | + @param SRCS the SRAM chip select pin to use |
| 33 | + @param MISO the MISO pin to use |
| 34 | + @param BUSY the busy pin to use |
| 35 | +*/ |
| 36 | +/**************************************************************************/ |
| 37 | +Adafruit_UC8151D::Adafruit_UC8151D(int width, int height, int8_t SID, |
| 38 | + int8_t SCLK, int8_t DC, int8_t RST, |
| 39 | + int8_t CS, int8_t SRCS, int8_t MISO, |
| 40 | + int8_t BUSY) |
| 41 | + : Adafruit_EPD(width, height, SID, SCLK, DC, RST, CS, SRCS, MISO, BUSY) { |
| 42 | + |
| 43 | + if ((width % 8) != 0) { |
| 44 | + width += 8 - (width % 8); |
| 45 | + } |
| 46 | + buffer1_size = ((uint32_t)width * (uint32_t)height) / 8; |
| 47 | + buffer2_size = buffer1_size; |
| 48 | + |
| 49 | + if (SRCS >= 0) { |
| 50 | + use_sram = true; |
| 51 | + buffer1_addr = 0; |
| 52 | + buffer2_addr = buffer1_size; |
| 53 | + buffer1 = buffer2 = NULL; |
| 54 | + } else { |
| 55 | + buffer1 = (uint8_t *)malloc(buffer1_size); |
| 56 | + buffer2 = (uint8_t *)malloc(buffer2_size); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// constructor for hardware SPI - we indicate DataCommand, ChipSelect, Reset |
| 61 | + |
| 62 | +/**************************************************************************/ |
| 63 | +/*! |
| 64 | + @brief constructor if using on-chip RAM and hardware SPI |
| 65 | + @param width the width of the display in pixels |
| 66 | + @param height the height of the display in pixels |
| 67 | + @param DC the data/command pin to use |
| 68 | + @param RST the reset pin to use |
| 69 | + @param CS the chip select pin to use |
| 70 | + @param SRCS the SRAM chip select pin to use |
| 71 | + @param BUSY the busy pin to use |
| 72 | +*/ |
| 73 | +/**************************************************************************/ |
| 74 | +Adafruit_UC8151D::Adafruit_UC8151D(int width, int height, int8_t DC, int8_t RST, |
| 75 | + int8_t CS, int8_t SRCS, int8_t BUSY, |
| 76 | + SPIClass *spi) |
| 77 | + : Adafruit_EPD(width, height, DC, RST, CS, SRCS, BUSY, spi) { |
| 78 | + |
| 79 | + if ((height % 8) != 0) { |
| 80 | + height += 8 - (height % 8); |
| 81 | + } |
| 82 | + buffer1_size = (uint16_t)width * (uint16_t)height / 8; |
| 83 | + buffer2_size = buffer1_size; |
| 84 | + |
| 85 | + if (SRCS >= 0) { |
| 86 | + use_sram = true; |
| 87 | + buffer1_addr = 0; |
| 88 | + buffer2_addr = buffer1_size; |
| 89 | + buffer1 = buffer2 = NULL; |
| 90 | + } else { |
| 91 | + buffer1 = (uint8_t *)malloc(buffer1_size); |
| 92 | + buffer2 = (uint8_t *)malloc(buffer2_size); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/**************************************************************************/ |
| 97 | +/*! |
| 98 | + @brief wait for busy signal to end |
| 99 | +*/ |
| 100 | +/**************************************************************************/ |
| 101 | +void Adafruit_UC8151D::busy_wait(void) { |
| 102 | + if (_busy_pin >= 0) { |
| 103 | + do { |
| 104 | + EPD_command(UC8151D_FLG); |
| 105 | + delay(10); |
| 106 | + } while (!digitalRead(_busy_pin)); |
| 107 | + } else { |
| 108 | + delay(BUSY_WAIT); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/**************************************************************************/ |
| 113 | +/*! |
| 114 | + @brief begin communication with and set up the display. |
| 115 | + @param reset if true the reset pin will be toggled. |
| 116 | +*/ |
| 117 | +/**************************************************************************/ |
| 118 | +void Adafruit_UC8151D::begin(bool reset) { |
| 119 | + Adafruit_EPD::begin(reset); |
| 120 | + setBlackBuffer(1, true); // black defaults to inverted |
| 121 | + setColorBuffer(0, true); // red defaults to inverted |
| 122 | + |
| 123 | + powerDown(); |
| 124 | +} |
| 125 | + |
| 126 | +/**************************************************************************/ |
| 127 | +/*! |
| 128 | + @brief signal the display to update |
| 129 | +*/ |
| 130 | +/**************************************************************************/ |
| 131 | +void Adafruit_UC8151D::update() { |
| 132 | + EPD_command(UC8151D_DRF); |
| 133 | + delay(100); |
| 134 | + busy_wait(); |
| 135 | +} |
| 136 | + |
| 137 | +/**************************************************************************/ |
| 138 | +/*! |
| 139 | + @brief start up the display |
| 140 | +*/ |
| 141 | +/**************************************************************************/ |
| 142 | +void Adafruit_UC8151D::powerUp() { |
| 143 | + uint8_t buf[5]; |
| 144 | + |
| 145 | + // Demo code resets 3 times! |
| 146 | + hardwareReset(); |
| 147 | + delay(10); |
| 148 | + hardwareReset(); |
| 149 | + delay(10); |
| 150 | + hardwareReset(); |
| 151 | + delay(10); |
| 152 | + |
| 153 | + const uint8_t *init_code = uc8151d_default_init_code; |
| 154 | + |
| 155 | + if (_epd_init_code != NULL) { |
| 156 | + init_code = _epd_init_code; |
| 157 | + } |
| 158 | + EPD_commandList(init_code); |
| 159 | + |
| 160 | + if (_epd_lut_code) { |
| 161 | + EPD_commandList(_epd_lut_code); |
| 162 | + } |
| 163 | + busy_wait(); |
| 164 | +} |
| 165 | + |
| 166 | +/**************************************************************************/ |
| 167 | +/*! |
| 168 | + @brief wind down the display |
| 169 | +*/ |
| 170 | +/**************************************************************************/ |
| 171 | + |
| 172 | +void Adafruit_UC8151D::powerDown(void) { |
| 173 | + uint8_t buf[1]; |
| 174 | + |
| 175 | + buf[0] = 0xF7; |
| 176 | + EPD_command(UC8151D_CDI, buf, 1); |
| 177 | + |
| 178 | + EPD_command(UC8151D_POF); // power off |
| 179 | + busy_wait(); |
| 180 | + |
| 181 | + buf[0] = 0xA5; |
| 182 | + EPD_command(UC8151D_DSLP, buf, 1); |
| 183 | +} |
| 184 | + |
| 185 | +/**************************************************************************/ |
| 186 | +/*! |
| 187 | + @brief Send the specific command to start writing to EPD display RAM |
| 188 | + @param index The index for which buffer to write (0 or 1 or tri-color |
| 189 | + displays) Ignored for monochrome displays. |
| 190 | + @returns The byte that is read from SPI at the same time as sending the |
| 191 | + command |
| 192 | +*/ |
| 193 | +/**************************************************************************/ |
| 194 | +uint8_t Adafruit_UC8151D::writeRAMCommand(uint8_t index) { |
| 195 | + if (index == 0) { |
| 196 | + return EPD_command(UC8151D_DTM1, false); |
| 197 | + } |
| 198 | + if (index == 1) { |
| 199 | + return EPD_command(UC8151D_DTM2, false); |
| 200 | + } |
| 201 | + return 0; |
| 202 | +} |
| 203 | + |
| 204 | +/**************************************************************************/ |
| 205 | +/*! |
| 206 | + @brief Some displays require setting the RAM address pointer |
| 207 | + @param x X address counter value |
| 208 | + @param y Y address counter value |
| 209 | +*/ |
| 210 | +/**************************************************************************/ |
| 211 | +void Adafruit_UC8151D::setRAMAddress(uint16_t x, uint16_t y) {} |
0 commit comments