EPD display library. Check this version (it should work)
Fork of GDEP015OC1 by
GDEP015OC1.cpp@2:1c8f73aa2a16, 2016-09-15 (annotated)
- Committer:
- Anunnaki
- Date:
- Thu Sep 15 09:19:30 2016 +0000
- Revision:
- 2:1c8f73aa2a16
- Parent:
- 1:4fff95e316c9
- Child:
- 3:dc7b794b59b7
Written the basic functions and confirmed the library functionality.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Anunnaki | 1:4fff95e316c9 | 1 | #include "mbed.h" |
Anunnaki | 1:4fff95e316c9 | 2 | #include "GDEP015OC1.h" |
Anunnaki | 1:4fff95e316c9 | 3 | |
Anunnaki | 2:1c8f73aa2a16 | 4 | static const unsigned char _lutFull[] = { |
Anunnaki | 2:1c8f73aa2a16 | 5 | 0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22, 0x66, 0x69, 0x69, 0x59, 0x58, 0x99, 0x99, |
Anunnaki | 2:1c8f73aa2a16 | 6 | 0x88, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xB4, 0x13, 0x51, 0x35, 0x51, 0x51, 0x19, 0x01, 0x00 |
Anunnaki | 2:1c8f73aa2a16 | 7 | }; |
Anunnaki | 2:1c8f73aa2a16 | 8 | |
Anunnaki | 2:1c8f73aa2a16 | 9 | GDEP015OC1::GDEP015OC1(SPI& spi, PinName cs=p5, PinName dc=p6, PinName rst=p7, PinName busy=p8) : _spi(spi), _cs(cs), _dc(dc), _rst(rst), _busy(busy){ |
Anunnaki | 1:4fff95e316c9 | 10 | _init(); |
Anunnaki | 1:4fff95e316c9 | 11 | } |
Anunnaki | 1:4fff95e316c9 | 12 | |
Anunnaki | 2:1c8f73aa2a16 | 13 | void GDEP015OC1::_spiCommand(unsigned char command){ |
Anunnaki | 2:1c8f73aa2a16 | 14 | _cs = _dc = 0; |
Anunnaki | 2:1c8f73aa2a16 | 15 | wait_us(1); |
Anunnaki | 2:1c8f73aa2a16 | 16 | |
Anunnaki | 2:1c8f73aa2a16 | 17 | _spi.write(command); |
Anunnaki | 2:1c8f73aa2a16 | 18 | } |
Anunnaki | 2:1c8f73aa2a16 | 19 | |
Anunnaki | 2:1c8f73aa2a16 | 20 | void GDEP015OC1::_spiData(unsigned char data){ |
Anunnaki | 2:1c8f73aa2a16 | 21 | _cs = 0; |
Anunnaki | 2:1c8f73aa2a16 | 22 | _dc = 1; |
Anunnaki | 2:1c8f73aa2a16 | 23 | wait_us(1); |
Anunnaki | 2:1c8f73aa2a16 | 24 | |
Anunnaki | 2:1c8f73aa2a16 | 25 | _spi.write(data); |
Anunnaki | 2:1c8f73aa2a16 | 26 | } |
Anunnaki | 2:1c8f73aa2a16 | 27 | |
Anunnaki | 2:1c8f73aa2a16 | 28 | void GDEP015OC1::_init(void){ |
Anunnaki | 2:1c8f73aa2a16 | 29 | _rst = _cs = 1; |
Anunnaki | 2:1c8f73aa2a16 | 30 | //empty(); |
Anunnaki | 1:4fff95e316c9 | 31 | } |
Anunnaki | 1:4fff95e316c9 | 32 | |
Anunnaki | 2:1c8f73aa2a16 | 33 | void GDEP015OC1::_wakeUp(void){ |
Anunnaki | 2:1c8f73aa2a16 | 34 | _rst = 0; |
Anunnaki | 2:1c8f73aa2a16 | 35 | wait_ms(10); |
Anunnaki | 2:1c8f73aa2a16 | 36 | _rst = 1; |
Anunnaki | 2:1c8f73aa2a16 | 37 | wait_ms(10); |
Anunnaki | 1:4fff95e316c9 | 38 | |
Anunnaki | 2:1c8f73aa2a16 | 39 | //Stage 3 |
Anunnaki | 2:1c8f73aa2a16 | 40 | //Driver Output control |
Anunnaki | 2:1c8f73aa2a16 | 41 | _spiCommand(0x01); |
Anunnaki | 2:1c8f73aa2a16 | 42 | _spiData(0xC7); |
Anunnaki | 2:1c8f73aa2a16 | 43 | _spiData(0x00); |
Anunnaki | 2:1c8f73aa2a16 | 44 | _spiData(0x00); |
Anunnaki | 2:1c8f73aa2a16 | 45 | //Softstart |
Anunnaki | 2:1c8f73aa2a16 | 46 | _spiCommand(0x0C); |
Anunnaki | 2:1c8f73aa2a16 | 47 | _spiData(0xD7); |
Anunnaki | 2:1c8f73aa2a16 | 48 | _spiData(0xD6); |
Anunnaki | 2:1c8f73aa2a16 | 49 | _spiData(0x9D); |
Anunnaki | 2:1c8f73aa2a16 | 50 | |
Anunnaki | 2:1c8f73aa2a16 | 51 | _spiCommand(0x22); |
Anunnaki | 2:1c8f73aa2a16 | 52 | _spiData(0xF0); |
Anunnaki | 2:1c8f73aa2a16 | 53 | |
Anunnaki | 2:1c8f73aa2a16 | 54 | //VCOM setting |
Anunnaki | 2:1c8f73aa2a16 | 55 | _spiCommand(0x2C); |
Anunnaki | 2:1c8f73aa2a16 | 56 | _spiData(0xA8); |
Anunnaki | 2:1c8f73aa2a16 | 57 | //Dummy line period |
Anunnaki | 2:1c8f73aa2a16 | 58 | _spiCommand(0x3A); |
Anunnaki | 2:1c8f73aa2a16 | 59 | _spiData(0x1A); |
Anunnaki | 2:1c8f73aa2a16 | 60 | //Gate line width |
Anunnaki | 2:1c8f73aa2a16 | 61 | _spiCommand(0x3B); |
Anunnaki | 2:1c8f73aa2a16 | 62 | _spiData(0x08); |
Anunnaki | 2:1c8f73aa2a16 | 63 | //Set data entry mode |
Anunnaki | 2:1c8f73aa2a16 | 64 | _spiCommand(0x11); |
Anunnaki | 2:1c8f73aa2a16 | 65 | _spiData(0x01); //DO NOT TOUCH THIS! (MUST BE 0x01) |
Anunnaki | 2:1c8f73aa2a16 | 66 | |
Anunnaki | 2:1c8f73aa2a16 | 67 | //Gate driver start position |
Anunnaki | 2:1c8f73aa2a16 | 68 | _spiCommand(0x0F); |
Anunnaki | 2:1c8f73aa2a16 | 69 | _spiData(0x2B); |
Anunnaki | 2:1c8f73aa2a16 | 70 | _spiData(0x01); |
Anunnaki | 2:1c8f73aa2a16 | 71 | |
Anunnaki | 2:1c8f73aa2a16 | 72 | //VGH VGL |
Anunnaki | 2:1c8f73aa2a16 | 73 | _spiCommand(0x03); |
Anunnaki | 2:1c8f73aa2a16 | 74 | _spiData(0x00); |
Anunnaki | 2:1c8f73aa2a16 | 75 | //VSH, VSL |
Anunnaki | 2:1c8f73aa2a16 | 76 | _spiCommand(0x04); |
Anunnaki | 2:1c8f73aa2a16 | 77 | _spiData(0x0A); |
Anunnaki | 2:1c8f73aa2a16 | 78 | |
Anunnaki | 2:1c8f73aa2a16 | 79 | //Define X display size |
Anunnaki | 2:1c8f73aa2a16 | 80 | _spiCommand(0x44); |
Anunnaki | 2:1c8f73aa2a16 | 81 | _spiData(0x00); |
Anunnaki | 2:1c8f73aa2a16 | 82 | _spiData(0x18); |
Anunnaki | 2:1c8f73aa2a16 | 83 | //Define Y display size |
Anunnaki | 2:1c8f73aa2a16 | 84 | _spiCommand(0x45); |
Anunnaki | 2:1c8f73aa2a16 | 85 | _spiData(0xC7); |
Anunnaki | 2:1c8f73aa2a16 | 86 | _spiData(0x00); |
Anunnaki | 2:1c8f73aa2a16 | 87 | _spiData(0x2B); |
Anunnaki | 2:1c8f73aa2a16 | 88 | _spiData(0x01); |
Anunnaki | 2:1c8f73aa2a16 | 89 | |
Anunnaki | 2:1c8f73aa2a16 | 90 | //Define X RAM address |
Anunnaki | 2:1c8f73aa2a16 | 91 | _spiCommand(0x4E); |
Anunnaki | 2:1c8f73aa2a16 | 92 | _spiData(0x00); |
Anunnaki | 2:1c8f73aa2a16 | 93 | //Define Y RAM address |
Anunnaki | 2:1c8f73aa2a16 | 94 | _spiCommand(0x4F); |
Anunnaki | 2:1c8f73aa2a16 | 95 | _spiData(0xC7); |
Anunnaki | 2:1c8f73aa2a16 | 96 | _spiData(0x00); |
Anunnaki | 2:1c8f73aa2a16 | 97 | |
Anunnaki | 2:1c8f73aa2a16 | 98 | //Border waveform control |
Anunnaki | 2:1c8f73aa2a16 | 99 | _spiCommand(0x3C); |
Anunnaki | 2:1c8f73aa2a16 | 100 | _spiData(0x33); |
Anunnaki | 2:1c8f73aa2a16 | 101 | |
Anunnaki | 2:1c8f73aa2a16 | 102 | //Write LUT |
Anunnaki | 2:1c8f73aa2a16 | 103 | _spiCommand(0x32); |
Anunnaki | 2:1c8f73aa2a16 | 104 | for(uint8_t i = 0; i<30;i++) |
Anunnaki | 2:1c8f73aa2a16 | 105 | _spiData(_lutFull[i]); |
Anunnaki | 2:1c8f73aa2a16 | 106 | |
Anunnaki | 2:1c8f73aa2a16 | 107 | //Power on |
Anunnaki | 2:1c8f73aa2a16 | 108 | _spiCommand(0x22); |
Anunnaki | 2:1c8f73aa2a16 | 109 | _spiData(0xC0); |
Anunnaki | 2:1c8f73aa2a16 | 110 | |
Anunnaki | 2:1c8f73aa2a16 | 111 | //Define X RAM address |
Anunnaki | 2:1c8f73aa2a16 | 112 | _spiCommand(0x4E); |
Anunnaki | 2:1c8f73aa2a16 | 113 | _spiData(0x00); |
Anunnaki | 2:1c8f73aa2a16 | 114 | //Define Y RAM address |
Anunnaki | 2:1c8f73aa2a16 | 115 | _spiCommand(0x4F); |
Anunnaki | 2:1c8f73aa2a16 | 116 | _spiData(0xC7); |
Anunnaki | 2:1c8f73aa2a16 | 117 | _spiData(0x00); |
Anunnaki | 1:4fff95e316c9 | 118 | } |
Anunnaki | 1:4fff95e316c9 | 119 | |
Anunnaki | 2:1c8f73aa2a16 | 120 | void GDEP015OC1::_sleep(void){ |
Anunnaki | 2:1c8f73aa2a16 | 121 | while(_busy == BUSY_STATE); |
Anunnaki | 2:1c8f73aa2a16 | 122 | |
Anunnaki | 2:1c8f73aa2a16 | 123 | _spiCommand(0x10); |
Anunnaki | 2:1c8f73aa2a16 | 124 | _spiData(0x01); |
Anunnaki | 2:1c8f73aa2a16 | 125 | } |
Anunnaki | 2:1c8f73aa2a16 | 126 | |
Anunnaki | 2:1c8f73aa2a16 | 127 | //If you touch this function satan will feast on your soul for an eternity! |
Anunnaki | 2:1c8f73aa2a16 | 128 | unsigned char GDEP015OC1::_pixelConv(unsigned char *data, int i){ |
Anunnaki | 2:1c8f73aa2a16 | 129 | uint8_t pix = 0x00; |
Anunnaki | 2:1c8f73aa2a16 | 130 | for(uint8_t x = 0; x < 8; x++){ |
Anunnaki | 2:1c8f73aa2a16 | 131 | pix |= ((*(data + (i*200)%5000 + (24-i/200) + x*25)>>((i/25)%8))&(0x01))<<(7-x); |
Anunnaki | 2:1c8f73aa2a16 | 132 | } |
Anunnaki | 2:1c8f73aa2a16 | 133 | return pix; |
Anunnaki | 2:1c8f73aa2a16 | 134 | } |
Anunnaki | 2:1c8f73aa2a16 | 135 | |
Anunnaki | 2:1c8f73aa2a16 | 136 | uint8_t GDEP015OC1::_mirrorData(uint8_t data){ |
Anunnaki | 2:1c8f73aa2a16 | 137 | uint8_t mirror = 0x00; |
Anunnaki | 2:1c8f73aa2a16 | 138 | for(uint8_t i=0; i<8; i++) |
Anunnaki | 2:1c8f73aa2a16 | 139 | mirror |= ((data>>i) & 0x01) << (7 - i); |
Anunnaki | 2:1c8f73aa2a16 | 140 | |
Anunnaki | 2:1c8f73aa2a16 | 141 | return mirror; |
Anunnaki | 2:1c8f73aa2a16 | 142 | } |
Anunnaki | 2:1c8f73aa2a16 | 143 | |
Anunnaki | 2:1c8f73aa2a16 | 144 | void GDEP015OC1::empty(void){ |
Anunnaki | 2:1c8f73aa2a16 | 145 | for(uint16_t x=0; x<5000; x++) |
Anunnaki | 2:1c8f73aa2a16 | 146 | _buffer[x] = 0x00; |
Anunnaki | 2:1c8f73aa2a16 | 147 | } |
Anunnaki | 2:1c8f73aa2a16 | 148 | |
Anunnaki | 2:1c8f73aa2a16 | 149 | void GDEP015OC1::write(void){ |
Anunnaki | 2:1c8f73aa2a16 | 150 | _wakeUp(); |
Anunnaki | 2:1c8f73aa2a16 | 151 | |
Anunnaki | 2:1c8f73aa2a16 | 152 | _spiCommand(0x24); |
Anunnaki | 2:1c8f73aa2a16 | 153 | for(int16_t x=0; x>=0 && x<200; x++){ |
Anunnaki | 2:1c8f73aa2a16 | 154 | for(int16_t y=24; y>=0 && y<25; y--){ |
Anunnaki | 2:1c8f73aa2a16 | 155 | _spiData(_mirrorData(_pixelConv(_buffer, x*25+y))); |
Anunnaki | 2:1c8f73aa2a16 | 156 | wait_us(10); |
Anunnaki | 2:1c8f73aa2a16 | 157 | } |
Anunnaki | 2:1c8f73aa2a16 | 158 | } |
Anunnaki | 2:1c8f73aa2a16 | 159 | |
Anunnaki | 2:1c8f73aa2a16 | 160 | _spiCommand(0x22); |
Anunnaki | 2:1c8f73aa2a16 | 161 | _spiData(0x04); |
Anunnaki | 2:1c8f73aa2a16 | 162 | _spiCommand(0x22); |
Anunnaki | 2:1c8f73aa2a16 | 163 | _spiData(0x08); |
Anunnaki | 2:1c8f73aa2a16 | 164 | |
Anunnaki | 2:1c8f73aa2a16 | 165 | //Update |
Anunnaki | 2:1c8f73aa2a16 | 166 | _spiCommand(0x22); |
Anunnaki | 2:1c8f73aa2a16 | 167 | _spiData(0xC7); |
Anunnaki | 2:1c8f73aa2a16 | 168 | _spiCommand(0x20); |
Anunnaki | 2:1c8f73aa2a16 | 169 | |
Anunnaki | 2:1c8f73aa2a16 | 170 | _sleep(); |
Anunnaki | 2:1c8f73aa2a16 | 171 | } |
Anunnaki | 2:1c8f73aa2a16 | 172 | |
Anunnaki | 2:1c8f73aa2a16 | 173 | void GDEP015OC1::drawPixel(uint16_t startX, uint16_t startY, bool color=0){ |
Anunnaki | 2:1c8f73aa2a16 | 174 | if(startX>199 || startY>199) return; |
Anunnaki | 2:1c8f73aa2a16 | 175 | |
Anunnaki | 2:1c8f73aa2a16 | 176 | uint16_t i = startX/8 + startY*25; |
Anunnaki | 2:1c8f73aa2a16 | 177 | |
Anunnaki | 2:1c8f73aa2a16 | 178 | if(!color) |
Anunnaki | 2:1c8f73aa2a16 | 179 | _buffer[i] = (_buffer[i] | (1<<(7-startX%8))); |
Anunnaki | 2:1c8f73aa2a16 | 180 | else |
Anunnaki | 2:1c8f73aa2a16 | 181 | _buffer[i] = (_buffer[i] & (0xFF^(1<<(7-startX%8)))); |
Anunnaki | 2:1c8f73aa2a16 | 182 | } |
Anunnaki | 2:1c8f73aa2a16 | 183 | |
Anunnaki | 2:1c8f73aa2a16 | 184 | |
Anunnaki | 2:1c8f73aa2a16 | 185 | void GDEP015OC1::drawLine(uint16_t startX, uint16_t startY, uint16_t stopX, uint16_t stopY, bool color=0){ |
Anunnaki | 2:1c8f73aa2a16 | 186 | int dx = abs(stopX-startX), sx = startX<stopX ? 1 : -1; |
Anunnaki | 2:1c8f73aa2a16 | 187 | int dy = abs(stopY-startY), sy = startY<stopY ? 1 : -1; |
Anunnaki | 2:1c8f73aa2a16 | 188 | int err = (dx>dy ? dx : -dy)/2, e2; |
Anunnaki | 2:1c8f73aa2a16 | 189 | |
Anunnaki | 2:1c8f73aa2a16 | 190 | for(;;){ |
Anunnaki | 2:1c8f73aa2a16 | 191 | drawPixel(startX,startY,color); |
Anunnaki | 2:1c8f73aa2a16 | 192 | if (startX==stopX && startY==stopY) break; |
Anunnaki | 2:1c8f73aa2a16 | 193 | e2 = err; |
Anunnaki | 2:1c8f73aa2a16 | 194 | if (e2 >-dx) { err -= dy; startX += sx; } |
Anunnaki | 2:1c8f73aa2a16 | 195 | if (e2 < dy) { err += dx; startY += sy; } |
Anunnaki | 2:1c8f73aa2a16 | 196 | } |
Anunnaki | 2:1c8f73aa2a16 | 197 | } |