Subversion Repositories Code-Repo

Rev

Rev 215 | Blame | Last modification | View Log | RSS feed

// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
/* ------------------------------------------------------------ */
/* PIC32 Configuration Settings */
/* ------------------------------------------------------------ */
/* Oscillator Settings */
#pragma config FNOSC     = PRIPLL   // Oscillator Selection Bits
#pragma config POSCMOD   = EC       // Primary Oscillator Configuration
#pragma config FPLLIDIV  = DIV_2    // PLL Input Divider
#pragma config FPLLMUL   = MUL_20   // PLL Multiplier
#pragma config FPLLODIV  = DIV_1    // PLL Output Divider
#pragma config FPBDIV    = DIV_1    // Peripheral Clock Divisor (timers/UART/SPI/I2C)
#pragma config FSOSCEN   = OFF      // Secondary Oscillator Enable
/* Clock Control Settings */
#pragma config IESO      = OFF      // Internal/External Clock Switch Over
#pragma config FCKSM     = CSDCMD   // Clock Switching and Monitor Selection
#pragma config OSCIOFNC  = OFF      // CLKO Output Signal Active on the OSCO Pin
/* USB Settings */
#pragma config UPLLEN    = ON       // USB PLL Enable
#pragma config UPLLIDIV  = DIV_2    // USB PLL Input Divider
#pragma config FVBUSONIO = OFF      // USB VBUS ON Selection
#pragma config FUSBIDIO  = OFF      // USB USID Selection
/* Other Peripheral Device Settings */
#pragma config FWDTEN    = OFF      // Watchdog Timer Enable
#pragma config WDTPS     = PS1024   // Watchdog Timer Postscaler
#pragma config FSRSSEL   = PRIORITY_7   // SRS Interrupt Priority
#pragma config FCANIO    = OFF      // CAN I/O Pin Select (default/alternate)
#pragma config FETHIO    = ON       // Ethernet I/O Pin Select (default/alternate)
#pragma config FMIIEN    = OFF      // Ethernet MII/RMII select (OFF=RMII)
/* Code Protection Settings */
#pragma config CP        = OFF      // Code Protect
#pragma config BWP       = OFF      // Boot Flash Write Protect
#pragma config PWP       = OFF      // Program Flash Write Protect
/* Debug Settings */
#pragma config ICESEL = ICS_PGx1    // ICE/ICD Comm Channel Select (on-board debugger)
/* ------------------------------------------------------------ */
// </editor-fold>

#include <xc.h>
#include <plib.h>
#include <stdlib.h>
#include "defines.h"
#include "UART1.h"
#include "SPI1.h"
#include "TIMER4.h"
#include "TIMER5.h"
#include "CUBE.h"
#include "BTN.h"

void BTN1_Interrupt(void);
void BTN2_Interrupt(void);
void BTN3_Interrupt(void);
void Animation_Solid_Colors(int iterations, int delay_ms);
void Animation_Layer_Alternate(int iterations, int delay_ms);
void Animation_Pixel_Alternate(int iterations, int delay_ms);
void Animation_Full_Color_Sweep(int iterations, int delay_us);
void Animation_Row_Column_Sweep(int iterations, int delay_ms);
void Animation_Pixel_Sweep(int iterations, int delay_ms);
void Animation_Pseudo_Random_Colors(int iterations,int delay_ms);
void Animation_Random_Colors(int iterations, int delay_ms);
void Animation_Cube_In_Cube(int iterations, int delay_ms);
void Animation_Double_Rotation(int iterations, int delay_ms);

void Delay_MS(unsigned int delay_ms) {
    // Delays the CPU for the given amount of time.
    // Note: Watch out for integer overflow! (max delay_ms = 107374) ??
    unsigned int delay = delay_ms * MS_TO_CT_TICKS;
    unsigned int startTime = ReadCoreTimer();
    while ((unsigned int)(ReadCoreTimer() - startTime) < delay) {};
}

void Delay_US(unsigned int delay_us) {
    // Delays the CPU for the given amount of time.
    // Note: Watch out for integer overflow!
    unsigned int delay = delay_us * US_TO_CT_TICKS;
    unsigned int startTime = ReadCoreTimer();
    while ((unsigned int)(ReadCoreTimer() - startTime) < delay) {};
}

int main() {
    /* Configure the target for maximum performance at 80 MHz. */
    // Note: This overrides the peripheral clock to 80Mhz regardless of config
    SYSTEMConfigPerformance(CPU_CLOCK_HZ);

    // Configure the interrupts for multiple vectors
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);

    // Set all analog I/O pins to digital
    AD1PCFGSET = 0xFFFF;

    // Initialize the SPI1 module
    SPI1_DATA spi_data;
    SPI1_Init(&spi_data, NULL);

    // Initialize the UART1 module
    UART1_DATA uart_data;
    UART1_Init(&uart_data, &Cube_Data_In);

    // Initializs the PWM2 output to 20MHz
    PWM2_Init();
    PWM2_Start();

    // Initialize the cube variables
    CUBE_DATA cube_data;
    Cube_Init(&cube_data, 0x40);

    // Start the cube update layer interrupt
    // 2083 = 60Hz, 500 = 250Hz, 250 = 500Hz
    TIMER5_DATA timer_5_data;
    TIMER5_Init(&timer_5_data, &Cube_Timer_Interrupt, 500);
    TIMER5_Start();

    // Start the overlay rotation interrupt
    TIMER4_DATA timer_4_data;
    TIMER4_Init(&timer_4_data, &Cube_Text_Interrupt, 120000);

    // Process button inputs
    BTN_DATA btn_data;
    BTN_Init(&btn_data, &BTN1_Interrupt, &BTN2_Interrupt, &BTN3_Interrupt);
    
    // Begin display

//    Cube_Set_All(RED);
//    Delay_MS(6000);
//    Cube_Set_All(GREEN);
//    Delay_MS(6000);
//    Cube_Set_All(BLUE);
//    Delay_MS(6000);

//    char start_text[] = "Cube Initialized\r\n";
//    UART1_Write(start_text, 18);

    // Set the overlay text
    char text_string[] = "Welcome to the CCM Lab    ";
    Cube_Text_Init(text_string, 26, 0xFF, 0xFF, 0xFF);
    TIMER4_Start();

    // Loop through some preset animations
    while(1) {
//        Animation_Solid_Colors(2,300);
//        Animation_Layer_Alternate(2,300);
//        Animation_Pixel_Alternate(1,200);
//        Animation_Full_Color_Sweep(2,1000);
        Animation_Row_Column_Sweep(2,40);
        Animation_Cube_In_Cube(4,300);
        Animation_Double_Rotation(2,40);
        Animation_Pseudo_Random_Colors(10,300);
//        Animation_Random_Colors(10,300);
    }
}

// Function call on button 1 press to change refresh rate
void BTN1_Interrupt(void) {
    static char state;
    state = (state == 4) ? 0 : state + 1;
    TIMER5_Stop();
    switch (state) {
        case 0:
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 500); // 250Hz
            break;
        case 1:
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 2083); // 60Hz
            break;
        case 2:
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 4166); // 30Hz
            break;
        case 3:
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 12498); // 10Hz
            break;
        case 4:
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 24996); // 5Hz
            break;
    }
    TIMER5_Start();
}

// Function call on button 2 press to change brightness
void BTN2_Interrupt(void) {
    static char state;
    state = (state == 6) ? 0 : state + 1;
    TIMER5_Stop();
    Delay_MS(1); // Need to wait for all SPI writes to complete
    char BC;
    switch (state) {
        case 0:
            BC = 0x01;
            break;
        case 1:
            BC = 0x08;
            break;
        case 2:
            BC = 0x10;
            break;
        case 3:
            BC = 0x20;
            break;
        case 4:
            BC = 0x40;
            break;
        case 5:
            BC = 0x80;
            break;
        case 6:
            BC = 0xFF;
            break;
    }
    Cube_Write_DCS(BC);
    TIMER5_Start();
}

// Function call on button 3 press to change text scroll speed
void BTN3_Interrupt(void)  {
    static char state;
    state = (state == 4) ? 0 : state + 1;
    TIMER4_Stop();
    switch (state) {
        case 0:
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 209712);
            break;
        case 1:
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 180000);
            break;
        case 2:
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 150000);
            break;
        case 3:
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 120000);
            break;
        case 4:
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 90000);
            break;
    }
    TIMER4_Start();
}

void Animation_Solid_Colors(int iterations, int delay_ms) {
    int i;
    for (i = 0; i < iterations; i++) {
        Cube_Set_All(RED);
        Delay_MS(delay_ms);
        Cube_Set_All(GREEN);
        Delay_MS(delay_ms);
        Cube_Set_All(BLUE);
        Delay_MS(delay_ms);
    }
}

void Animation_Layer_Alternate(int iterations, int delay_ms) {
    int i,z;
    for (z = 0; z < iterations; z++) {
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
            if (i % 3 == 0)
                Cube_Set_Layer(i,RED);
            else if (i % 3 == 1)
                Cube_Set_Layer(i,GREEN);
            else
                Cube_Set_Layer(i,BLUE);
        }
        Delay_MS(delay_ms);
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
            if (i % 3 == 0)
                Cube_Set_Layer(i,GREEN);
            else if (i % 3 == 1)
                Cube_Set_Layer(i,BLUE);
            else
                Cube_Set_Layer(i,RED);
        }
        Delay_MS(delay_ms);
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
            if (i % 3 == 0)
                Cube_Set_Layer(i,BLUE);
            else if (i % 3 == 1)
                Cube_Set_Layer(i,RED);
            else
                Cube_Set_Layer(i,GREEN);
        }
        Delay_MS(delay_ms);
    }
}

void Animation_Pixel_Alternate(int iterations, int delay_ms) {
    int i,j,k,z;
    for (z = 0; z < iterations; z++) {
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
            Cube_Clear();
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
                    int var = (j * 8) + k;
                    if (var % 3 == 0)
                        Cube_Set_Pixel(i,j,k,RED);
                    else if (var % 3 == 1)
                        Cube_Set_Pixel(i,j,k,GREEN);
                    else
                        Cube_Set_Pixel(i,j,k,BLUE);
                }
            }
            Delay_MS(delay_ms);
            Cube_Clear();
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
                    int var = (j * 8) + k;
                    if (var % 3 == 0)
                        Cube_Set_Pixel(i,j,k,GREEN);
                    else if (var % 3 == 1)
                        Cube_Set_Pixel(i,j,k,BLUE);
                    else
                        Cube_Set_Pixel(i,j,k,RED);
                }
            }
            Delay_MS(delay_ms);
            Cube_Clear();
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
                    int var = (j * 8) + k;
                    if (var % 3 == 0)
                        Cube_Set_Pixel(i,j,k,BLUE);
                    else if (var % 3 == 1)
                        Cube_Set_Pixel(i,j,k,RED);
                    else
                        Cube_Set_Pixel(i,j,k,GREEN);
                }
            }
            Delay_MS(delay_ms);
        }
    }
}

void Animation_Full_Color_Sweep(int iterations, int delay_us) {
    int i,z;
    for (z = 0; z < iterations; z++) {
        for (i = 0; i < 0x0FF; i+=2) {
            Cube_Set_All(i,0,0);
            Delay_US(delay_us);
        }
        for (i = 0; i < 0x0FF; i+=2) {
            Cube_Set_All(0x0FF,i,0);
            Delay_US(delay_us);
        }
        for (i = 0x0FF; i >= 0; i-=2) {
            Cube_Set_All(i,0x0FF,0);
            Delay_US(delay_us);
        }
        for (i = 0; i < 0x0FF; i+=2) {
            Cube_Set_All(0,0x0FF,i);
            Delay_US(delay_us);
        }
        for (i = 0; i < 0x0FF; i+=2) {
            Cube_Set_All(i,0x0FF,0x0FF);
            Delay_US(delay_us);
        }
        for (i = 0x0FF; i >= 0; i-=2) {
            Cube_Set_All(0x0FF,i,0x0FF);
            Delay_US(delay_us);
        }
        for (i = 0x0FF; i >= 0; i-=2) {
            Cube_Set_All(i,0,0x0FF);
            Delay_US(delay_us);
        }
        for (i = 0x100; i >= 0; i-=2) {
            Cube_Set_All(0,0,i);
            Delay_US(delay_us);
        }
    }
}

void Animation_Row_Column_Sweep(int iterations, int delay_ms) {
    int i,j,k,a,z;
    for (z = 0; z < iterations; z++) {
        for (i = 0; i < 3; i++) {
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
                Cube_Clear();
                for (k = 0; k < CUBE_COLUMN_COUNT; k++)
                    if (i % 3 == 0)
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
                            Cube_Set_Pixel(a,j,k,RED);
                    else if (i % 3 == 1)
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
                            Cube_Set_Pixel(a,j,k,GREEN);
                    else
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
                            Cube_Set_Pixel(a,j,k,BLUE);
                Delay_MS(delay_ms);
            }
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
                Cube_Clear();
                for (k = 0; k < CUBE_COLUMN_COUNT; k++)
                    if (i % 3 == 0)
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
                            Cube_Set_Pixel(a,k,j,RED);
                    else if (i % 3 == 1)
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
                            Cube_Set_Pixel(a,k,j,GREEN);
                    else
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
                            Cube_Set_Pixel(a,k,j,BLUE);
                Delay_MS(delay_ms);
            }
            for (j = CUBE_LAYER_COUNT-1; j >= 0; j--) {
                Cube_Clear();
                if (i % 3 == 0) {
                    for (k = 0; k < CUBE_LAYER_COUNT; k++)
                        if (k == j)
                            Cube_Set_Layer(k,RED);
                } else if (i % 3 == 1) {
                    for (k = 0; k < CUBE_LAYER_COUNT; k++)
                        if (k == j)
                            Cube_Set_Layer(k,GREEN);
                } else {
                    for (k = 0; k < CUBE_LAYER_COUNT; k++)
                        if (k == j)
                            Cube_Set_Layer(k,BLUE);
                }
                Delay_MS(delay_ms);
            }
        }
    }
}

void Animation_Pixel_Sweep(int iterations, int delay_ms) {
    int i,j,k,z,a;
    for (z = 0; z < iterations; z++) {
        for (a = 0; a < 3; a++) {
            for (i = 0; i < CUBE_LAYER_COUNT; i++) {
                for (j = 0; j < CUBE_ROW_COUNT; j++) {
                    for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
                        Cube_Clear();
                        if (a % 3 == 0) {
                            Cube_Set_Pixel(i,j,k,RED);
                        } else if (a % 3 == 1) {
                            Cube_Set_Pixel(i,j,k,GREEN);
                        } else {
                            Cube_Set_Pixel(i,j,k,BLUE);
                        }
                        Delay_MS(delay_ms);
                    }
                }
            }
        }
    }
}

void Animation_Pseudo_Random_Colors(int iterations, int delay_ms) {
    int i,j,k,z;
    for (z = 0; z < iterations; z++) {
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
                    unsigned int a = rand();
                    if (a % 5 == 0)
                        Cube_Set_Pixel(i,j,k,RED);
                    else if (a % 5 == 1)
                        Cube_Set_Pixel(i,j,k,GREEN);
                    else if (a % 5 == 2)
                        Cube_Set_Pixel(i,j,k,BLUE);
                    else if (a % 5 == 3)
                        Cube_Set_Pixel(i,j,k,PURPLE);
                    else if (a % 5 == 4)
                        Cube_Set_Pixel(i,j,k,YELLOW);
                    else
                        Cube_Set_Pixel(i,j,k,ORANGE);
                }
            }
        }
        Delay_MS(delay_ms);
    }
}

void Animation_Random_Colors(int iterations, int delay_ms) {
    int i,j,k,z;
    for (z = 0; z < iterations; z++) {
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
                    Cube_Set_Pixel(i,j,k,rand()&0x0FF,rand()&0x0FF,rand()&0x0FF);
                }
            }
        }
        Delay_MS(delay_ms);
    }
}

void Animation_Cube_In_Cube(int iterations, int delay_ms) {
    int z,x,i,j,k;
    for (z = 0; z < iterations; z++) {
        for (x = 0; x < 5; x++) {
            Cube_Clear();
            for (i = 0; i < CUBE_LAYER_COUNT; i++) {
                if ((x == 0 || x == 4)&&(i == 0 || i == 7)) {
                    Cube_Set_Layer(i,RED);
                } else if ((x == 1 || x == 4)&&(i == 1 || i == 6)) {
                    for (j = 1; j < CUBE_ROW_COUNT-1; j++)
                        for (k = 1; k < CUBE_COLUMN_COUNT-1; k++)
                            Cube_Set_Pixel(i,j,k,YELLOW);
                } else if ((x == 2 || x == 4)&&(i == 2 || i == 5)) {
                    for (j = 2; j < CUBE_ROW_COUNT-2; j++)
                        for (k = 2; k < CUBE_COLUMN_COUNT-2; k++)
                            Cube_Set_Pixel(i,j,k,GREEN);
                } else if ((x == 3 || x == 4)&&(i == 3 || i == 4)) {
                    for (j = 3; j < CUBE_ROW_COUNT-3; j++)
                        for (k = 3; k < CUBE_COLUMN_COUNT-3; k++)
                            Cube_Set_Pixel(i,j,k,BLUE);
                }

                if ((x == 0 || x == 4)&&(i > 0 && i < 8)) {
                    for (j = 0; j < 8; j++) {
                        Cube_Set_Pixel(i,j,0,RED);
                        Cube_Set_Pixel(i,j,7,RED);
                        Cube_Set_Pixel(i,0,j,RED);
                        Cube_Set_Pixel(i,7,j,RED);
                    }
                }
                if ((x == 1 || x == 4)&&(i > 1 && i < 7)) {
                    for (j = 1; j < 7; j++) {
                        Cube_Set_Pixel(i,j,1,YELLOW);
                        Cube_Set_Pixel(i,j,6,YELLOW);
                        Cube_Set_Pixel(i,1,j,YELLOW);
                        Cube_Set_Pixel(i,6,j,YELLOW);
                    }
                }
                if ((x == 2 || x == 4)&&(i > 2 && i < 6)) {
                    for (j = 2; j < 6; j++) {
                        Cube_Set_Pixel(i,j,2,GREEN);
                        Cube_Set_Pixel(i,j,5,GREEN);
                        Cube_Set_Pixel(i,2,j,GREEN);
                        Cube_Set_Pixel(i,5,j,GREEN);
                    }
                }
            }
            Delay_MS(delay_ms);
        }
    }
}

void Animation_Double_Rotation(int iterations, int delay_ms) {
    Cube_Clear();
    int i,x,y,z;
    for (z = 0; z < 3; z++) {
        switch (z % 3) {
            case 0:
                for (y = 0; y < CUBE_LAYER_COUNT; y++) {
                    Cube_Set_Pixel(y,0,0,RED);
                    Cube_Set_Pixel(y,1,1,RED);
                    Cube_Set_Pixel(y,2,2,RED);
                    Cube_Set_Pixel(y,3,3,RED);
                    Cube_Set_Pixel(y,4,4,RED);
                    Cube_Set_Pixel(y,5,5,RED);
                    Cube_Set_Pixel(y,6,6,RED);
                    Cube_Set_Pixel(y,7,7,RED);
                }
                break;
            case 1:
                for (y = 0; y < CUBE_LAYER_COUNT; y++) {
                    Cube_Set_Pixel(y,0,0,GREEN);
                    Cube_Set_Pixel(y,1,1,GREEN);
                    Cube_Set_Pixel(y,2,2,GREEN);
                    Cube_Set_Pixel(y,3,3,GREEN);
                    Cube_Set_Pixel(y,4,4,GREEN);
                    Cube_Set_Pixel(y,5,5,GREEN);
                    Cube_Set_Pixel(y,6,6,GREEN);
                    Cube_Set_Pixel(y,7,7,GREEN);
                }
                break;
            case 2:
                for (y = 0; y < CUBE_LAYER_COUNT; y++) {
                    Cube_Set_Pixel(y,0,0,BLUE);
                    Cube_Set_Pixel(y,1,1,BLUE);
                    Cube_Set_Pixel(y,2,2,BLUE);
                    Cube_Set_Pixel(y,3,3,BLUE);
                    Cube_Set_Pixel(y,4,4,BLUE);
                    Cube_Set_Pixel(y,5,5,BLUE);
                    Cube_Set_Pixel(y,6,6,BLUE);
                    Cube_Set_Pixel(y,7,7,BLUE);
                }
                break;
        }

        for (i = 0; i < iterations; i++) {
            for (x = 0; x < 28; x++) {
                Delay_MS(delay_ms);
                Cube_Rotate(0);
            }
        }
    }
}