#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "avr-hd44780/lcd.h"
#include "i2c_peter/i2cmaster.h"
#include "sdi_shield.h"

volatile uint8_t cam = 0;
volatile uint8_t pgm = 0;
volatile uint8_t pvw = 0;
volatile uint8_t valid = 0;

#define SDI_SHIELD_ADDR 0x84

#define VF_BACK_PVW_TALLY_PIN 0
#define VF_BACK_PGM_TALLY_PIN 1
#define LENS_PGM_TALLY_PIN 2
#define VF_FRONT_PGM_TALLY_PIN 3

volatile uint8_t pvw_enabled = 1;
volatile uint8_t front_enabled = 1;

void enc_init(){
    PORTC |= (1<<2) | (1 << 3);
    PCICR |= (1<<PCIE1);
    PCMSK1 |= (1<<PCINT10);
}

int main(void){
    DDRB |= 1<<VF_BACK_PVW_TALLY_PIN; //uln glueh rot
    DDRB |= 1<<VF_BACK_PGM_TALLY_PIN; //uln glueh rot
    DDRB |= 1<<LENS_PGM_TALLY_PIN; //uln glueh rot
    DDRB |= 1<<VF_FRONT_PGM_TALLY_PIN; //uln glueh rot

    DDRD |= 1<<7; //enable backlight

    //PullUp BTN
    PORTB |= 1<<6;

    lcd_init();
    lcd_on();
    lcd_clear();

    i2c_init();                             // initialize I2C library

    DDRC |= 1<<0;
    PORTB |= (1<<VF_BACK_PVW_TALLY_PIN);

    _delay_ms(100);

    PORTB |= (1<<VF_FRONT_PGM_TALLY_PIN);
    PORTB |= (1<<VF_BACK_PGM_TALLY_PIN);

    lcd_printf("id: ");
    i2c_start_wait(SDI_SHIELD_ADDR + I2C_WRITE);     // set device address and write mode
    i2c_write(0x00);
    i2c_write(0x00);
    i2c_rep_start(SDI_SHIELD_ADDR + I2C_READ);       // set device address and read mode
    for(uint8_t i = 0; i < 4; i++){
        unsigned char ret = i2c_readAck();                    // read one byte from EEPROM
        lcd_printf("%c", ret);
    }
    unsigned char hw0 = i2c_readAck();                    // read one byte from EEPROM
    unsigned char hw1 = i2c_readAck();                    // read one byte from EEPROM

    unsigned char fw0 = i2c_readAck();
    unsigned char fw1 = i2c_readNak();

    i2c_stop();

    lcd_set_cursor(0,1);
    lcd_printf("hw: %d.%d", hw0, hw1);
    lcd_printf(" fw: %d.%d", fw0, fw1);

    DDRC |= 1<<1;
    PORTB &= ~(1<<VF_BACK_PVW_TALLY_PIN);

    _delay_ms(1000);
    lcd_clear();

    PORTB &= ~(1<<VF_FRONT_PGM_TALLY_PIN);

    // Turn LEDs off
    DDRC &= ~(1<<0);
    DDRC &= ~(1<<1);

    /*
    for(uint8_t i=0; i<255; i++){
	uint8_t res = i2c_start(i);
	i2c_stop();
	if(!res)
	    lcd_printf("%x ", i);
    }
    */

    enc_init();
    sei();

    while(1){
	shield_set_flags(0x5000, 0x01);

	uint8_t data_length;
	shield_read_uint8(0x5001, &data_length);
	if(data_length){
	    uint8_t tally;
	    shield_read_uint8(0x5100+cam, &tally);
	    pgm = !!(tally & (1<<0));
	    pvw = !!(tally & (1<<1));
	}

	if(pgm){
	    DDRC |= 1<<1;
	    if(front_enabled){
		PORTB |= (1<<LENS_PGM_TALLY_PIN);
		PORTB |= (1<<VF_FRONT_PGM_TALLY_PIN);
	    }
	    PORTB |= (1<<VF_BACK_PGM_TALLY_PIN);
	}
	else
	{
	    DDRC &= ~(1<<1);
	    if(front_enabled){
		PORTB &= ~(1<<LENS_PGM_TALLY_PIN);
		PORTB &= ~(1<<VF_FRONT_PGM_TALLY_PIN);
	    }
	    PORTB &= ~(1<<VF_BACK_PGM_TALLY_PIN);
	}

	if(pvw & pvw_enabled){
	    DDRC |= 1<<0;
	    PORTB |= (1<<VF_BACK_PVW_TALLY_PIN);
	}
	else{
	    DDRC &= ~(1<<0);
	    PORTB &= ~(1<<VF_BACK_PVW_TALLY_PIN);
	}

	lcd_set_cursor(0,0);
	lcd_printf("pgm: %d pvw: %d", pgm, pvw);
	lcd_set_cursor(0,1);
	lcd_printf("cam: %d  ", cam+1);

	_delay_ms(10);
    }

}

ISR(PCINT1_vect){
    if(PINC & (1<<2)){
	if(PINC & (1<<3))
	    cam--;
	else
	    cam++;
    }else{
	if(PINC & (1<<3))
	    cam++;
	else
	    cam--;
    }
}
