/*
 * File:   main.c
 * Author: boos
 *
 * Created on May 16, 2021, 10:27 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 = 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>


// location of the LED, DATA output, and pushbutton
#define LED RC2
#define DATA RC3
#define SW1 !RA5


// frequency is 48MHz
// (need this so that __delay_ms() works properly)
#define _XTAL_FREQ 48000000


// macro to send the bit 'b' (can be either 0 or 1)
#define send(b) DATA=1; NOP(); NOP(); NOP(); DATA=b; NOP(); NOP(); NOP(); NOP(); DATA=0; NOP(); NOP(); NOP(); NOP();


// auxiliary functions to control the WS2812 NeoPixel LEDs
void sendByte (unsigned char b);
void sendRGB (unsigned char r, unsigned char g, unsigned char b);


// 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 9)
    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
    
    // LED is an output
    TRISC2 = 0;
    
    // DATA is an output
    TRISC3 = 0;
    
    // turn off analog to digital converter
    ANSC2 = 0;
    ANSC3 = 0;
    
    // button input
    TRISA5 = 1;
    WPUA5 = 1;
    nWPUEN = 0;
    
    
    // declare variables used for animation
    unsigned char brt = 1, dir = 1;
    int timebase = 0, stage = 0;
    
    
    // main loop
    while (1) {
        
        
        // react to button press
        if (SW1) {
            LED = 1;
            brt += dir;
            if (brt >= 255) {
                dir = -1;
            } else if (brt == 0) {
                dir = 1;
            }
        } else {
            LED = 0;
        }
        
        
        // time the animation
        timebase++;
        if (timebase > 300) {
            timebase = 0;
            stage += 1;
            if (stage >= 8) {
                stage = 0;
            }
        }
        
        
        // send out animation pattern
        if (stage == 0) {
            sendRGB(0,0,0); sendRGB(0,0,0); sendRGB(0,0,0);
            sendRGB(0,0,0); sendRGB(brt,0,0); sendRGB(0,brt,0);sendRGB(0,0,brt); sendRGB(brt,brt,0); sendRGB(brt,0,brt); sendRGB(0,brt,brt); sendRGB(brt,brt,brt);
        } else if (stage == 1) {
            sendRGB(brt,0,0); sendRGB(brt,0,0); sendRGB(brt,0,0);
            sendRGB(brt,brt,brt); sendRGB(0,0,0); sendRGB(brt,0,0); sendRGB(0,brt,0);sendRGB(0,0,brt); sendRGB(brt,brt,0); sendRGB(brt,0,brt); sendRGB(0,brt,brt);
        } else if (stage == 2) {
            sendRGB(0,brt,0); sendRGB(0,brt,0); sendRGB(0,brt,0);
            sendRGB(0,brt,brt); sendRGB(brt,brt,brt); sendRGB(0,0,0); sendRGB(brt,0,0); sendRGB(0,brt,0);sendRGB(0,0,brt); sendRGB(brt,brt,0); sendRGB(brt,0,brt);
        } else if (stage == 3) {
            sendRGB(0,0,brt); sendRGB(0,0,brt); sendRGB(0,0,brt);
            sendRGB(brt,0,brt); sendRGB(0,brt,brt); sendRGB(brt,brt,brt); sendRGB(0,0,0); sendRGB(brt,0,0); sendRGB(0,brt,0);sendRGB(0,0,brt); sendRGB(brt,brt,0);
        } else if (stage == 4) {
            sendRGB(brt,brt,0); sendRGB(brt,brt,0); sendRGB(brt,brt,0);
            sendRGB(brt,brt,0); sendRGB(brt,0,brt); sendRGB(0,brt,brt); sendRGB(brt,brt,brt); sendRGB(0,0,0); sendRGB(brt,0,0); sendRGB(0,brt,0); sendRGB(0,0,brt);
        } else if (stage == 5) {
            sendRGB(brt,0,brt); sendRGB(brt,0,brt); sendRGB(brt,0,brt);
            sendRGB(0,0,brt); sendRGB(brt,brt,0); sendRGB(brt,0,brt); sendRGB(0,brt,brt); sendRGB(brt,brt,brt); sendRGB(0,0,0); sendRGB(brt,0,0); sendRGB(0,brt,0);
        } else if (stage == 6) {
            sendRGB(0,brt,brt); sendRGB(0,brt,brt); sendRGB(0,brt,brt);
            sendRGB(0,brt,0); sendRGB(0,0,brt); sendRGB(brt,brt,0); sendRGB(brt,0,brt); sendRGB(0,brt,brt); sendRGB(brt,brt,brt); sendRGB(0,0,0); sendRGB(brt,0,0);
        } else if (stage == 7) {
            sendRGB(brt,brt,brt); sendRGB(brt,brt,brt); sendRGB(brt,brt,brt);
            sendRGB(brt,0,0); sendRGB(0,brt,0); sendRGB(0,0,brt); sendRGB(brt,brt,0); sendRGB(brt,0,brt); sendRGB(0,brt,brt); sendRGB(brt,brt,brt); sendRGB(0,0,0);
        }
        
        
        // wait some time
        __delay_ms(1);
        
        
    }
    
    return;
    
}

// send out a byte b in WS2812 protocol
void sendByte (unsigned char b) {

    if (b & 0b10000000) { send(1); } else { send(0); }
    if (b & 0b01000000) { send(1); } else { send(0); }
    if (b & 0b00100000) { send(1); } else { send(0); }
    if (b & 0b00010000) { send(1); } else { send(0); }
    if (b & 0b00001000) { send(1); } else { send(0); }
    if (b & 0b00000100) { send(1); } else { send(0); }
    if (b & 0b00000010) { send(1); } else { send(0); }
    if (b & 0b00000001) { send(1); } else { send(0); }
    
}

// send red, green, and blue values in WS2812 protocol
void sendRGB (unsigned char r, unsigned char g, unsigned char b) {

    sendByte(g);
    sendByte(r);
    sendByte(b);
    
}