/*
 * File:   main.c
 * Author: boos
 *
 * Created on January 28, 2023, 4:16 PM
 */

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (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 BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config CPUDIV = CLKDIV6 // CPU System Clock Selection Bit (CPU system clock divided by 6)
#pragma config USBLSCLK = 48MHz // USB Low SPeed Clock Selection bit (System clock expects 48 MHz, FS/LS USB CLKENs divide-by is set to 8.)
#pragma config PLLMULT = 3x     // PLL Multipler Selection Bit (3x Output Frequency Selected)
#pragma config PLLEN = ENABLED  // PLL Enable Bit (3x or 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 LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

#include <xc.h>


// where are our connections?
#define SW    RA3
#define ENC_A RA4
#define ENC_B RA5
#define LED1  RC2
#define LED2  RC1
#define LED3  RC0
#define LED4  RC3
#define LED5  RC4


// prototype for our function that converts Gray code into a binary number
int convertGrayToBinary (void);


// variables
volatile int pos = 0, pos_before = 0;
volatile int v = 0;
int value = 0;
int diff = 0;


// main function
void main (void) {
    
    
    // turn RA4 and RA5 into inputs
	// (RA3 is always an input by default)
    TRISA4 = 1;
    TRISA5 = 1;
    
	
	// turn on the weak internal pullup resistors on RA3, RA4, and RA5
	WPUA3 = 1;
    WPUA4 = 1;
    WPUA5 = 1;
    nWPUEN = 0;
    
	
	// turn RC0...RC4 into outputs
    TRISC0 = 0;
    TRISC1 = 0;
    TRISC2 = 0;
    TRISC3 = 0;
    TRISC4 = 0;
    
	// turn off analog features on all of our pins
	// (otherwise they won't work as digital inputs or outputs)
    ANSA4 = 0;
    ANSC0 = 0;
    ANSC1 = 0;
    ANSC2 = 0;
    ANSC3 = 0;
    
	
    // set the internal oscillator frequency to 4MHz
	// (see page 73 in the PIC16F1455 datasheet)
    IRCF3 = 1; IRCF2 = 1; IRCF1 = 0; IRCF0 = 1;
    
	
    // configure TIMER0
	// (see Sec. 19 in the PIC16F1455 datasheet)
    TMR0CS = 0;                 // internal oscillator (Fosc/4)
    PSA = 1;                    // prescaler OFF
    TMR0IE = 1;                 // enable interrupt on overflow
    GIE = 1;                    // enable global interrupts
    
	
    // main loop
    while (1) {
        
        
        // convert four steps of Gray code into one numerical step
        // (necessary for our rotary encoder, where one "step" is actually four steps in Gray code)
        value = v >> 2;
        
        
        // show the position on the five LEDs, and invert the pattern if the button SW is pressed
        // (because of the internal pullup resistor, SW=0 whenever it is pressed, and SW=1 when it is not pressed)
        if (value == 0) {
            LED1 = SW;  LED2 = !SW; LED3 = !SW; LED4 = !SW; LED5 = !SW;
        } else if (value == 1) {
            LED1 = !SW; LED2 = SW;  LED3 = !SW; LED4 = !SW; LED5 = !SW;
        } else if (value == 2) {
            LED1 = !SW; LED2 = !SW; LED3 = SW;  LED4 = !SW; LED5 = !SW;
        } else if (value == 3) {
            LED1 = !SW; LED2 = !SW; LED3 = !SW; LED4 = SW;  LED5 = !SW;
        } else if (value == 4) {
            LED1 = !SW; LED2 = !SW; LED3 = !SW; LED4 = !SW; LED5 = SW;
        }
        
    }
    
    // end of the main function
    return;
    
}

// interrupt service routine (is called approximately 3906 times per second)
void __interrupt () isr (void) {  
 
    // timer overflow?
    if (TMR0IF) {
        
        // read out the current position
        pos = convertGrayToBinary();
        
        // calculate the difference to previous position
        diff = pos_before - pos;
        
        // turned clockwise
        if (((diff == -1) || (diff == 3))) {
            pos_before = pos;
            if (v < 20) { v++; }
            
        // turned counter-clockwise
        } else if (((diff == 1) || (diff == -3))) {
            pos_before = pos;
            if (v > 0) { v--; }
        
        // in this case, an step has been missed
        // (best practice: ignore it!)
        } else if ((diff == 2) || (diff == -2)) {

        }
        
        // clear timer flag
        TMR0IF = 0;
        
    }
    
}

// this function converts Gray code into a binary number
// 00 -> 0, 01 -> 1, 11 -> 2, 10 -> 3
int convertGrayToBinary () {
 
    if (ENC_A == 0 && ENC_B == 0) {
        return 0;
    } else if (ENC_A == 0 && ENC_B == 1) {
        return 1;
    } else if (ENC_A == 1 && ENC_B == 1) {
        return 2;
    } else {
        return 3;
    }
    
}