/* 
 * File:   newmain.c
 * Author: mmanno
 *
 * Created on September 30, 2013, 7:23 PM
 */

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>

#define SPIBRG              SPI3BRG
#define SPIBUF              SPI3BUF
#define SPICLOCK            TRISDbits.TRISD1
#define SPIOUT              TRISDbits.TRISD3
#define CS_TRIS             TRISDbits.TRISD4
#define SPIIN               TRISDbits.TRISD2
#define CS                  LATDbits.LATD4
#define SPILCD_INT          IFS0bits.SPI3RXIF

unsigned char Velocity[] = {'V','e','l',':'};
unsigned char Mode[] = {'M','o','d','e',':'};
//unsigned char Test[] = {'T','e','s','t'};
//unsigned char Rest[] = {'R','e','s','t'};
//unsigned char Race[] = {'R','a','c','e'};
unsigned char Fix[] = {'F','i','x'};
unsigned char Lap[] = {'L','a','p'};
//unsigned char No[] = {'N','o'};
//unsigned char NA[] = {'N','/','A'};
//unsigned char Space[] = {' ',' '};
unsigned char Current_Lap[] = {'C','u','r','r','e','n','t',':'};
unsigned char Previous_Lap[] = {'P','r','e','v','i','o','u','s',':'};
unsigned char rps[] = {'R','P','S',':'};

void LCD_SPI_init()
{
    SPIBRG = 85;                                                         //SCK to 59kHz
    //SPIOUT = 0;                                                          //MOSI output
    //SPIIN = 1;                                                           //MISO input
    //SPICLOCK = 0;                                                        //CLK output
    CS_TRIS = 0;                                                         //CS output

    int rData = SPI3BUF;

    SPI3CONbits.CKP = 1;
    SPI3CONbits.CKE = 0;
    SPI3CONbits.SMP=0;
    SPI3CONbits.MSTEN=1;

    SPI3CONbits.ON=1;

    SPILCD_INT=0;
    CS=1;
}



unsigned char LCD_snd(unsigned char info)
{
    CS=0;
    SPI3BUF = info;
    while(!SPI3STATbits.SPIRBF);
    return SPI3BUF;
    CS=1;
}

delay_millisec(int input)
{
    int i=0;
    while (i<input*1000)
    {
        i=i+1;
    }
}

string_snd(unsigned char info[],int length)
{
    int i;
    for(i=0;i<length;i++)
    {
       LCD_snd(info[i]);
       delay_millisec(1);
    }

    delay_millisec(10);
}

void snd_cmd(unsigned char cmd, unsigned char location)
{

     LCD_snd(0xFE);
     delay_millisec(8);
     LCD_snd(cmd);

     if(cmd == 0x51)
     {
     delay_millisec(4);
     }


     if(cmd == 0x45)
     {
     LCD_snd(location);

     }

     else
     {
     delay_millisec(4);
     }

}


void LCD_init()
{

//    int test = 0;
//    int mode = 0;
//    int fix = 3;
//
//    unsigned long num;
//    char Vel[10];
//    num = 42424242;
//    sprintf(Vel, "%ld", num);

    //initialize SPI
    LCD_SPI_init();
    delay_millisec(100);

    snd_cmd(0x51,0x00);
    string_snd(Velocity,4);

    snd_cmd(0x45,0x0D);
    string_snd(rps,4);

    snd_cmd(0x45,0x40);
    string_snd(Mode,5);

    snd_cmd(0x45,0x25);
    string_snd(Fix,3);

    snd_cmd(0x45,0x14);
    string_snd(Current_Lap,8);

    snd_cmd(0x45,0x54);
    string_snd(Previous_Lap,9);

//    snd_cmd(0x45,0x0A);
//    string_snd(Vel,2);  //Will eventually be velocity
//
//    if(test == 1){
//        snd_cmd(0x45,0x46);
//        string_snd(Test,4);
//    }
//
//    if(test == 0){
//        if(mode == 0){
//        snd_cmd(0x45,0x46);
//        string_snd(Rest,4);
//        }
//        if(mode == 1){
//        snd_cmd(0x45,0x46);
//        string_snd(Race,4);
//        }
//    }
//
//
//
//    if(fix == 3){
//        snd_cmd(0x45,0x4E);
//        string_snd(Space,2);
//    }
//    if(fix != 3){
//        snd_cmd(0x45,0x4E);
//        string_snd(No,2);
//    }
//
//    snd_cmd(0x45,0x1D);
//    string_snd(NA,3); // Will eventually be current lap time
//
//    snd_cmd(0x45,0x5E);
//    string_snd(NA,3);  //Will eventually be previous lap time
}

