EPD display library. Check this version (it should work)

Fork of GDEP015OC1 by Jurica Resetar

Files at this revision

API Documentation at this revision

Comitter:
Anunnaki
Date:
Thu Sep 15 09:19:30 2016 +0000
Parent:
1:4fff95e316c9
Child:
3:dc7b794b59b7
Commit message:
Written the basic functions and confirmed the library functionality.

Changed in this revision

GDEP015OC1.cpp Show annotated file Show diff for this revision Revisions of this file
GDEP015OC1.h Show annotated file Show diff for this revision Revisions of this file
--- a/GDEP015OC1.cpp	Wed Sep 14 14:57:23 2016 +0000
+++ b/GDEP015OC1.cpp	Thu Sep 15 09:19:30 2016 +0000
@@ -1,21 +1,197 @@
 #include "mbed.h"
-
 #include "GDEP015OC1.h"
 
-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){
+static const unsigned char _lutFull[] = { 
+    0x02, 0x02, 0x01, 0x11, 0x12, 0x12, 0x22, 0x22, 0x66, 0x69, 0x69, 0x59, 0x58, 0x99, 0x99, 
+    0x88, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xB4, 0x13, 0x51, 0x35, 0x51, 0x51, 0x19, 0x01, 0x00
+};
+
+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){
     _init();
 }
 
-void GDEP015OC1::_spiCallback(int event){
-    if(event = SPI_EVENT_COMPLETE)
-        cs = 1;    
+void GDEP015OC1::_spiCommand(unsigned char command){
+    _cs = _dc = 0;
+    wait_us(1);
+    
+    _spi.write(command);
+}
+
+void GDEP015OC1::_spiData(unsigned char data){
+    _cs = 0;
+    _dc = 1;
+    wait_us(1);
+    
+    _spi.write(data);
+}
+
+void GDEP015OC1::_init(void){
+    _rst = _cs = 1;
+    //empty();    
 }
 
-bool GDEP015OC1::_spiCommand(unsigned char command){
-    unsigned char _package = command;
-    cs = dc = 0;
-    wait_us(1);
+void GDEP015OC1::_wakeUp(void){
+    _rst = 0;
+    wait_ms(10);
+    _rst = 1;
+    wait_ms(10);
     
-    while(transfer(&_package, 1, NULL, 0, NULL)!= 0);
+    //Stage 3
+    //Driver Output control
+    _spiCommand(0x01);
+    _spiData(0xC7);
+    _spiData(0x00);
+    _spiData(0x00);
+    //Softstart
+    _spiCommand(0x0C);
+    _spiData(0xD7);
+    _spiData(0xD6);
+    _spiData(0x9D);
+
+    _spiCommand(0x22);
+    _spiData(0xF0);
+
+    //VCOM setting
+    _spiCommand(0x2C);
+    _spiData(0xA8);
+    //Dummy line period
+    _spiCommand(0x3A);
+    _spiData(0x1A);
+    //Gate line width
+    _spiCommand(0x3B);
+    _spiData(0x08);
+    //Set data entry mode
+    _spiCommand(0x11);
+    _spiData(0x01); //DO NOT TOUCH THIS! (MUST BE 0x01)
+
+    //Gate driver start position
+    _spiCommand(0x0F);
+    _spiData(0x2B);
+    _spiData(0x01);
+
+    //VGH VGL
+    _spiCommand(0x03);
+    _spiData(0x00);
+    //VSH, VSL
+    _spiCommand(0x04);
+    _spiData(0x0A);
+
+    //Define X display size
+    _spiCommand(0x44);
+    _spiData(0x00);
+    _spiData(0x18);
+    //Define Y display size
+    _spiCommand(0x45);
+    _spiData(0xC7);
+    _spiData(0x00);
+    _spiData(0x2B);
+    _spiData(0x01);
+
+    //Define X RAM address
+    _spiCommand(0x4E);
+    _spiData(0x00);
+    //Define Y RAM address
+    _spiCommand(0x4F);
+    _spiData(0xC7);
+    _spiData(0x00);
+
+    //Border waveform control
+    _spiCommand(0x3C);
+    _spiData(0x33);
+
+    //Write LUT
+    _spiCommand(0x32);
+    for(uint8_t i = 0; i<30;i++)
+      _spiData(_lutFull[i]);
+
+    //Power on
+    _spiCommand(0x22);
+    _spiData(0xC0);
+
+    //Define X RAM address
+    _spiCommand(0x4E);
+    _spiData(0x00);
+    //Define Y RAM address
+    _spiCommand(0x4F);
+    _spiData(0xC7);
+    _spiData(0x00);
 }
 
+void GDEP015OC1::_sleep(void){
+    while(_busy == BUSY_STATE);
+
+    _spiCommand(0x10);
+    _spiData(0x01);
+}
+
+//If you touch this function satan will feast on your soul for an eternity!
+unsigned char GDEP015OC1::_pixelConv(unsigned char *data, int i){
+    uint8_t pix = 0x00;
+    for(uint8_t x = 0; x < 8; x++){
+        pix |= ((*(data + (i*200)%5000 + (24-i/200) + x*25)>>((i/25)%8))&(0x01))<<(7-x);
+    }
+    return pix;    
+}
+
+uint8_t GDEP015OC1::_mirrorData(uint8_t data){
+    uint8_t mirror = 0x00;
+    for(uint8_t i=0; i<8; i++)
+        mirror |= ((data>>i) & 0x01) << (7 - i);
+
+    return mirror;
+}
+
+void GDEP015OC1::empty(void){
+    for(uint16_t x=0; x<5000; x++)
+        _buffer[x] = 0x00;
+}
+
+void GDEP015OC1::write(void){
+    _wakeUp();
+
+    _spiCommand(0x24);
+    for(int16_t x=0; x>=0 && x<200; x++){
+        for(int16_t y=24; y>=0 && y<25; y--){
+            _spiData(_mirrorData(_pixelConv(_buffer, x*25+y)));
+            wait_us(10);
+        }
+    }
+
+    _spiCommand(0x22);
+    _spiData(0x04);
+    _spiCommand(0x22);
+    _spiData(0x08);
+
+    //Update
+    _spiCommand(0x22);
+    _spiData(0xC7);
+    _spiCommand(0x20);
+
+    _sleep();
+}
+
+void GDEP015OC1::drawPixel(uint16_t startX, uint16_t startY, bool color=0){
+    if(startX>199 || startY>199) return;
+    
+    uint16_t i = startX/8 + startY*25;
+
+    if(!color)
+        _buffer[i] = (_buffer[i] | (1<<(7-startX%8)));
+    else
+        _buffer[i] = (_buffer[i] & (0xFF^(1<<(7-startX%8))));
+}
+
+
+void GDEP015OC1::drawLine(uint16_t startX,  uint16_t startY, uint16_t stopX, uint16_t stopY, bool color=0){
+    int dx = abs(stopX-startX), sx = startX<stopX ? 1 : -1;
+    int dy = abs(stopY-startY), sy = startY<stopY ? 1 : -1;
+    int err = (dx>dy ? dx : -dy)/2, e2;
+    
+    for(;;){
+        drawPixel(startX,startY,color);
+        if (startX==stopX && startY==stopY) break;
+        e2 = err;
+        if (e2 >-dx) { err -= dy; startX += sx; }
+        if (e2 < dy) { err += dx; startY += sy; }
+    }
+}
\ No newline at end of file
--- a/GDEP015OC1.h	Wed Sep 14 14:57:23 2016 +0000
+++ b/GDEP015OC1.h	Thu Sep 15 09:19:30 2016 +0000
@@ -1,28 +1,34 @@
-namespace GDEP015OC1
-{
-    class GDEP015OC1
-    {
-        public:            
-            void fill(unsigned char data, uin16_t i);            
-            unsigned char get(uint16_t i);            
-            void empty(void);            
-            void invert(void);            
-            bool write(void);            
-            void drawPixel(uint16_t startX, uint16_t startY, bool color);            
-            void drawLine(uint16_t startX,  uint16_t startY, uint16_t stopX, uint16_t stopY, bool color);
-        private:
-            SPI& _spi;
-            DigitalOut _cs;
-            DigitalOut _dc;
-            DigitalOut _rst;
-            DigitalIn  _busy;
+#include "mbed.h"
+
+#define BUFFER_SIZE     5000
+#define BUSY_STATE         1
+
+class GDEP015OC1 {
+    public: 
+        GDEP015OC1(SPI& spi, PinName cs, PinName dc, PinName rst, PinName busy);
+                   
+        void fill(unsigned char data, uint16_t i);            
+        unsigned char get(uint16_t i);            
+        void empty(void);            
+        void invert(void);            
+        void write(void);            
+        void drawPixel(uint16_t startX, uint16_t startY, bool color);            
+        void drawLine(uint16_t startX,  uint16_t startY, uint16_t stopX, uint16_t stopY, bool color);
+    private:
+        SPI& _spi;
+        DigitalOut _cs;
+        DigitalOut _dc;
+        DigitalOut _rst;
+        DigitalIn  _busy;
+        
+        unsigned char _buffer[BUFFER_SIZE];
+         
     
-            void _spiCallback(int event);
-            bool _spiCommand(unsigned char command);            
-            bool _spiData(unsigned char data);            
-            void _init(void);
-            void _wakeUp(void);            
-            void _sleep(void);            
-            unsigned char _pixelConv(unsigned char *data, int i);
-    };    
-}
\ No newline at end of file
+        void _spiCommand(unsigned char command);            
+        void _spiData(unsigned char data);            
+        void _init(void);
+        void _wakeUp(void);            
+        void _sleep(void);            
+        unsigned char _pixelConv(unsigned char *data, int i);
+        uint8_t _mirrorData(uint8_t data);
+};
\ No newline at end of file