#include <xc.h>
//#include <pic16f1823.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <math.h>

#define _XTAL_FREQ 4000000 //define crystal frequency to 4MHz

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = OFF       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

//Bemenetek
#define SW          PORTAbits.RA5   //RA5 - input
//


//Kimenetek
#define RELE1    LATCbits.LATC1  //RC1
#define RELE2    LATCbits.LATC0  //RC0

volatile unsigned int  SW_counter = 0;
volatile unsigned char SW_changed = 0;
volatile unsigned char SW_rising_edge = 0;
volatile unsigned char SW_debounce_state = 0;       //debounce state for shifting

//Switch states
volatile unsigned char SW_click = 0;

void EEPROM_Write(char address, char data) //20ms after
{
    EEADR = address;
    EEDATA = data;
    EECON1bits.EEPGD = 0; // Point to EEPROM Memory
    EECON1bits.CFGS = 0; // Access to EEPROM
    EECON1bits.WREN = 1; // Enable Write
    GIE = 0;
    EECON2 = 0x55;
    EECON2 = 0xAA;
    EECON1bits.WR = 1; // Begin Write
    GIE = 1;
    while (PIR2bits.EEIF == 0); // Wait for Write to get Complete
    PIR2bits.EEIF = 0; // Clear Flag
    EECON1bits.WREN = 0; // Disable Write
}

char EEPROM_Read(char address) {
    EEADR = address;
    EECON1bits.EEPGD = 0; // Point to EEPROM
    EECON1bits.CFGS = 0; // Access to EEPROM
    EECON1bits.RD = 1; // Enable Read
    return EEDATA;
}

void main(void) {
    //CPU clock selection
    //Clock determined by FOSC in configuration bits (4 MHz))
    SCS0 = 0;
    SCS1 = 0;
    //Frequency select bits (4 MHz)
    IRCF0 = 1;
    IRCF1 = 0;
    IRCF2 = 1;
    IRCF3 = 1;

    OSCTUNE = 0;

    //SET PLLx4 OFF
    SPLLEN = 0;

    ANSELA = 0x00; // Set all ports as digital
    ANSELC = 0x00; // Set all ports as digital
   
    TRISA = 0xFF; // Set all A pin as input
    TRISC = 0x10; // Set all C pin as output, C5 as input
 
    PORTA = 0x00; // Clear all ports
    PORTC = 0x00;

    ADCON0 = 0x00;
    PIE2 = 0b00000000;
    PIR2 = 0b00000000;
    CM1CON0 = 0b00000000;
    CM2CON0 = 0b00000000;

    //Timer1 Registers
    T1CONbits.T1CKPS1 = 1; // bits 5-4  Prescaler Rate Select bits = 4x
    T1CONbits.T1CKPS0 = 0; // bit 4
    T1CONbits.T1OSCEN = 1; // bit 3 Timer1 Oscillator Enable Control bit 1 = on
    T1CONbits.TMR1CS = 0; // bit 1 Timer1 Clock Source Select bit...0 = Internal clock (FOSC/4)
    T1CONbits.TMR1ON = 1; // bit 0 enables timer
    TMR1H = 246; // preset for timer1 MSB register - TMR1 63036 100Hz/ 4MHz
    TMR1L = 60; // preset for timer1 LSB register

    // Interrupt Registers
    INTCON = 0; // clear the interrpt control register
    TMR0IE = 0; // bit5 TMR0 Overflow Interrupt Enable bit...0 = Disables the TMR0 interrupt
    TMR1IF = 0; // clear timer1 interupt flag TMR1IF
    TMR1IE = 1; // enable Timer1 interrupts
    TMR0IF = 0; // bit2 clear timer 0 interrupt flag

    __delay_ms(1000);

    GIE = 1;  // bit7 global interrupt enable
    PEIE = 1; // bit6 Peripheral Interrupt Enable bit...1 = Enables all unmasked peripheral interrupts

    RELE1 = EEPROM_Read(0);
    RELE2 = EEPROM_Read(1);
        
    while (1) {
        
        if (SW_click) {
            SW_click = 0;
            SW_changed = 1;
            RELE1 = RELE1^1;
            RELE2 = RELE2^1;
        }
        
        if (SW_changed) {
            EEPROM_Write(0, RELE1);
            EEPROM_Write(1, RELE2);
            SW_changed = 0;
        }     
                       
    } //Loop forever

}

void interrupt Timer1_ISR(void) {
    if (TMR1IF) {                              // timer 1 interrupt flag
        SW_debounce_state = (SW_debounce_state<<1) | (SW == 0) | 0b11100000;
        
        if (SW_debounce_state == 0b11101111) { //rising edge 4x10ms debounce interval
            SW_click = 0;  
            SW_counter = 0;
            SW_rising_edge = 1;
        }
        
        if (SW_debounce_state == 0b11110000) { //falling edge 4x10ms debounce interval
           
            if ((SW_counter <= 100) & (SW_counter >= 5)) {     //click
                SW_click = 1;
                SW_counter = 0;
                SW_rising_edge = 0;
            }
        }      
               
        TMR1IF = 0; // interrupt must be cleared by software
        TMR1IE = 1; // reenable the interrupt
        TMR1H = 246; // preset for timer1 MSB register
        TMR1L = 60; // preset for timer1 LSB register
    }
}