/*
 * File:   main.c
 * Author: boos
 *
 * Created on August 12, 2023, 7:39 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 = ON       // Power-up Timer Enable (PWRT enabled)
#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 = NOCLKDIV// CPU System Clock Selection Bit (NO CPU system divide)
#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 = OFF     // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not 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 (Low-voltage programming disabled)

#include <xc.h>


// abbreviations for locations of external devices
// (this is not necessary, but useful)
#define WS2812_DATA  RC2
#define ENC_SW       !RA3
#define ENC_A        RA4
#define ENC_B        RA5

// clock frequency (for __delay_ms function)
#define _XTAL_FREQ 48000000

// functions to control the WS2812 NeoPixel LEDs
#define WS2812_send_bit(b) WS2812_DATA=1; NOP(); NOP(); NOP(); WS2812_DATA=b; NOP(); NOP(); NOP(); NOP(); WS2812_DATA=0; NOP(); NOP(); NOP(); NOP();
void WS2812_send_byte (unsigned char b);
void WS2812_send_RGB (unsigned char r, unsigned char g, unsigned char b);

// function that converts Gray code into a binary number (for rotary encoder)
int gray_to_binary (void);


// global variables used for the rotary encoder (we add "volatile" wherever they are used and updated in the ISR)
volatile int rotary_pos, rotary_pos_before, rotary_diff;
volatile int rotary_value;
int rotary_value_old, rotary_value_change;

// global debounce variable for the rotary encoder's pushbutton
// (is altered in the ISR, so we make it "volatile")
volatile int sw_buffer;


// *****
// main function
void main (void) {
    
    
    // *****
    // set up internal oscillator to run at 48 MHz
    
    // set the SCS settings (system clock select)
    // to use the frequency specified in configuration word (line 22)
    SCS0 = 0;
    SCS1 = 0;
    
    // set to 16 MHz configuration
    IRCF0 = 1;
    IRCF1 = 1;
    IRCF2 = 1;
    IRCF3 = 1;
    
    // enable PLL to result in 48MHz
    // (PLL is set to 3x in line 23)
    SPLLEN = 1;
    
    
    // *****
    // set up peripherals
    
    // WS2812_DATA is an output
    TRISC2 = 0;
    
    // turn off all analog to digital converters
    ANSA4 = 0;
    ANSC2 = 0;
    
    // the rotary encoder pins are all inputs
    // (RA3 can only be an input, so we don't have to set its tristate register)
    TRISA4 = 1;
    TRISA5 = 1;
    
    // enable the weak internal pullup resistors on the rotary encoder inputs RA3, RA4, RA5
    WPUA3 = 1;
    WPUA4 = 1;
    WPUA5 = 1;
    nWPUEN = 0;


    // *****
    // configure TIMER0 (we use it for reading out the rotary encoder)
	// (see Sec. 19 in the PIC16F1455 datasheet for specifications)
    TMR0CS = 0;                 // internal oscillator (Fosc/4)
    PSA = 0;                    // prescaler ON
    PS2 = 0; PS1 = 1; PS0 = 1;  // set to 1:16
    TMR0IE = 1;                 // enable interrupt on overflow
    GIE = 1;                    // enable global interrupts

    
    // *****
    // main loop
    while (1) {


		// get rotary encoder values
        unsigned char brt = (unsigned char) rotary_value;
        unsigned char pmr, pmg;
        if (ENC_SW) {
            pmr = 255; pmg = 128;
        } else {
            pmr = 0; pmg = 0;
        }
        
        
        // we need to turn interrupts OFF during data transmission because this part is time critical
        GIE = 0;
        
        // wait a bit, just in case
        __delay_ms(1);
        
        // send hours data
        WS2812_send_RGB(pmr, pmg, 0); WS2812_send_RGB(0, 0, 0); WS2812_send_RGB(0, 0, 0); WS2812_send_RGB(0, 0, 0); WS2812_send_RGB(0, 0, 0); WS2812_send_RGB(0, 0, brt);
        WS2812_send_RGB(0, 0, brt); WS2812_send_RGB(0, 0, brt); WS2812_send_RGB(0, 0, brt); WS2812_send_RGB(0, 0, 0); WS2812_send_RGB(0, 0, brt); WS2812_send_RGB(0, 0, 0);
        WS2812_send_RGB(0, 0, brt); WS2812_send_RGB(0, 0, 0); WS2812_send_RGB(0, 0, brt); WS2812_send_RGB(0, 0, brt); WS2812_send_RGB(0, 0, 0); WS2812_send_RGB(0, 0, brt);
        
        // wait a bit, just in case
        __delay_ms(1);
        
        // now it is safe to turn interrupts back on
        GIE = 1;
        
    }
    
    return;
    
}

// interrupt service routine (is called approximately 2930 times per second)
// (48MHz/4 clock speed, 8-bit counter, with 1:16 prescaler = 2930 Hz)
void __interrupt () isr (void) {  
 
    // timer overflow?
    if (TMR0IF) {
        
        // read out the current rotary encoder position
        rotary_pos = gray_to_binary();
        
        // calculate the difference to previous position
        rotary_diff = rotary_pos_before - rotary_pos;

        // turned clockwise
        if (((rotary_diff == -1) || (rotary_diff == 3))) {
            rotary_pos_before = rotary_pos;
            rotary_value++;
            
        // turned counter-clockwise
        } else if (((rotary_diff == 1) || (rotary_diff == -3))) {
            rotary_pos_before = rotary_pos;
            rotary_value--;
        
        // in this case, a step has been missed
        // (best practice: ignore it!)
        } else if ((rotary_diff == 2) || (rotary_diff == -2)) {
            
        }
        
        // clear the debounce buffer for the rotary encoder pushbutton,
        // but only if the pushbutton is not pressed
        if ((sw_buffer > 0) && !ENC_SW) {
            sw_buffer--;
        } 
        
        // clear timer flag
        TMR0IF = 0;
        
    }
    
}

// send out a byte b in WS2812 protocol
void WS2812_send_byte (unsigned char b) {

    if (b & 0b10000000) { WS2812_send_bit(1); } else { WS2812_send_bit(0); }
    if (b & 0b01000000) { WS2812_send_bit(1); } else { WS2812_send_bit(0); }
    if (b & 0b00100000) { WS2812_send_bit(1); } else { WS2812_send_bit(0); }
    if (b & 0b00010000) { WS2812_send_bit(1); } else { WS2812_send_bit(0); }
    if (b & 0b00001000) { WS2812_send_bit(1); } else { WS2812_send_bit(0); }
    if (b & 0b00000100) { WS2812_send_bit(1); } else { WS2812_send_bit(0); }
    if (b & 0b00000010) { WS2812_send_bit(1); } else { WS2812_send_bit(0); }
    if (b & 0b00000001) { WS2812_send_bit(1); } else { WS2812_send_bit(0); }
    
}

// send red, green, and blue values in WS2812 protocol
void WS2812_send_RGB (unsigned char r, unsigned char g, unsigned char b) {

    WS2812_send_byte(g);
    WS2812_send_byte(r);
    WS2812_send_byte(b);
    
}

// convert Gray code into a binary number
// 00 -> 0, 01 -> 1, 11 -> 2, 10 -> 3
int gray_to_binary () {
 
    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;
    }
    
}