// -----------------------------------------------------------------------
//   LCD.c -> LCD acesse
//   Autor:  Rodrigo Maximiano Antunes de Almeida
//          rodrigomax at unifei.edu.br
// -----------------------------------------------------------------------
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; version 2 of the License.
// 
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
// -----------------------------------------------------------------------


#include "drvLcd.h"
#include "basico.h"
static driver thisDriver;
static driver_functions this_functions[LCD_END];

//pin definition
#define RS  6
#define EN  7

void delayMicroseconds(int ms) {
    int i;
    for (; ms > 0; ms--) {
        for (i = 0; i < 30; i++);
    }
}
//pulse the Enable pin high
void pulseEnablePin() {
    BitClr(PORTC, EN);
    delayMicroseconds(1);
    BitSet(PORTC, EN);
    delayMicroseconds(1);
    BitClr(PORTC, EN);
}

//push a nibble of data through the the LCD's DB4~7 pins
void pushNibble(int value, int rs) {
    PORTD = (value) << 4;
    if (rs) {
        BitSet(PORTC, RS);
    } else {
        BitClr(PORTC, RS);
    }
    pulseEnablePin();
}
//push a byte of data through the LCD's DB4~7 pins, in two steps, clocking each with the enable pin.
void pushByte(int value, int rs) {
    int val_lower = value & 0x0F;
    int val_upper = value >> 4;
    pushNibble(val_upper, rs);
    pushNibble(val_lower, rs);
}

void commandWriteNibble(int nibble) {
    pushNibble(nibble, 0);
}

char sendCommand(void *parameters) {
    pushByte((int) parameters, 0);
    delayMicroseconds(40);
    return SUCCESS;
}

char changeLine(void *parameters) {
    if ((unsigned char) parameters == 1) {
        sendCommand(0x80);
    }
    if ((unsigned char) parameters == 2) {
        sendCommand(0xC0);
    }
    return SUCCESS;
}

char sendData(void *parameters) {
    pushByte((int) parameters, 1);
    delayMicroseconds(2);
    return SUCCESS;
}

char intWrite(void *parameters) {
    sendData(((unsigned int) parameters / 10000) % 10 + 48);
    sendData(((unsigned int) parameters / 1000) % 10 + 48);
    sendData(((unsigned int) parameters / 100) % 10 + 48);
    sendData(((unsigned int) parameters / 10) % 10 + 48);
    sendData(((unsigned int) parameters) % 10 + 48);
    return SUCCESS;
}

void LCD_build(void) {
    int i;
    unsigned char defcon[] = {
        0x0, 0x1, 0x3, 0x3, 0x3, 0x3, 0x1, 0x4,
        0xe, 0x1f, 0x4, 0x4, 0x1f, 0xe, 0x11, 0x1f,
        0x0, 0x10, 0x18, 0x18, 0x18, 0x18, 0x10, 0x4,
        0xc, 0x3, 0x0, 0x0, 0x0, 0x3, 0xc, 0x4,
        0x0, 0x0, 0x1b, 0x4, 0x1b, 0x0, 0x0, 0x0,
        0x6, 0x18, 0x0, 0x0, 0x0, 0x18, 0x6, 0x2
    };
    sendCommand(0x40);
    for (i = 0; i < 8 * 6; i++) {
        sendData(~defcon[i]);
    }
}

char initLCD(void *parameters) {

    thisDriver.drv_id = *(char*) parameters;
    this_functions[LCD_COMMAND].func_ptr = sendCommand;
    this_functions[LCD_CHAR].func_ptr = sendData;
    this_functions[LCD_INTEGER].func_ptr = intWrite;
    this_functions[LCD_LINE].func_ptr = changeLine;

    BitClr(TRISC, EN);
    BitClr(TRISC, RS);
    TRISD = 0x0f;

    delayMicroseconds(50);

    commandWriteNibble(0x03);
    delayMicroseconds(5);
    commandWriteNibble(0x03);
    delayMicroseconds(100);
    commandWriteNibble(0x03);
    delayMicroseconds(5);

    // needed by the LCDs controller
    //this being 2 sets up 4-bit mode.
    commandWriteNibble(0x02);
    delayMicroseconds(10);

    //dysplay configuration
    sendCommand(0x28); //4bits, 2 lines, 5x8
    sendCommand(0x06); //incremental mode
    sendCommand(0x0c); //display on, cursor and blink off
    sendCommand(0x03); //clean data
    sendCommand(0x80); //initial position
    sendCommand(0x01); //display clear
    delayMicroseconds(2);

    LCD_build();
    return SUCCESS;
}

driver* getLCDDriver(void) {
    //to ensure that at least the init function will be known
    thisDriver.drv_init = initLCD;
    thisDriver.func_ptr = (driver_functions*) & this_functions;
    return &thisDriver;
}
