GDEP015OC1 ePaper display for ACD52832 Dev board

Dependents:   ACD52832_ePaper

Fork of ACD_ePaper by Filip Hormot

GDEP015OC1.cpp

Committer:
Anunnaki
Date:
2016-09-15
Revision:
2:1c8f73aa2a16
Parent:
1:4fff95e316c9
Child:
3:dc7b794b59b7

File content as of revision 2:1c8f73aa2a16:

#include "mbed.h"
#include "GDEP015OC1.h"

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::_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();    
}

void GDEP015OC1::_wakeUp(void){
    _rst = 0;
    wait_ms(10);
    _rst = 1;
    wait_ms(10);
    
    //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; }
    }
}