Subversion Repositories Code-Repo

Rev

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

#include "defines.h"
#include "CONTROLLERS.h"
#include "I2C1.h"

static CONTROLLER_DATA *ctrl_data_p;

void Controller_Init(CONTROLLER_DATA *data, 
        void (*btn_change_callback)(uint8_t, uint8_t)) {
    ctrl_data_p = data;

    ctrl_data_p->btn_change_callback = btn_change_callback;
    
    ctrl_data_p->ctrl_1_connected = 0;
    ctrl_data_p->ctrl_1_buttons_prev = CONTROLLER_BTN_DEFAULT;
    ctrl_data_p->ctrl_1_leds = CONTROLLER_LED_DEFAULT;
    ctrl_data_p->ctrl_1_active = 0;

    ctrl_data_p->ctrl_2_connected = 0;
    ctrl_data_p->ctrl_2_buttons_prev = CONTROLLER_BTN_DEFAULT;
    ctrl_data_p->ctrl_2_leds = CONTROLLER_LED_DEFAULT;
    ctrl_data_p->ctrl_2_active = 0;
}

void Controller_Update(void) {
    uint8_t buffer[2];
    uint8_t result, length;
    uint8_t ctrl_1_btn = 0, ctrl_2_btn = 0;

    // Read button values from controllers
    I2C1_Master_Restart(CONTROLLER_1_ADDRESS, CONTROLLER_READ, 1);
    do {
        result = I2C1_Get_Status();
    } while (!result);
    if (result == I2C1_RECV_OK) {
        // Indicate that controller 1 is connected
        LED1_LAT = 1;
        ctrl_data_p->ctrl_1_connected = 1;
        length = I2C1_Read_Buffer(buffer);
        buffer[0] = ~buffer[0];
        // Button change detected
        if (ctrl_data_p->ctrl_1_buttons_prev != buffer[0]) {
            // Check if a button has been pressed since startup
            if (!ctrl_data_p->ctrl_1_active) {
                ctrl_data_p->ctrl_1_active = 1;
            }
            // Figure out which button has changed
            ctrl_1_btn = ctrl_data_p->ctrl_1_buttons_prev ^ buffer[0];
            // Save the button if it went from unpressed -> pressed
            ctrl_1_btn &= buffer[0];
        }
        ctrl_data_p->ctrl_1_buttons_prev = buffer[0];
    } else {
        LED1_LAT = 0;
        ctrl_data_p->ctrl_1_connected = 0;
        ctrl_data_p->ctrl_1_active = 0;
    }

    I2C1_Master_Restart(CONTROLLER_2_ADDRESS, CONTROLLER_READ, 1);
    do {
        result = I2C1_Get_Status();
    } while (!result);
    if (result == I2C1_RECV_OK) {
        // Indicate that controller 2 is connected
        LED2_LAT = 1;
        ctrl_data_p->ctrl_2_connected = 1;
        length = I2C1_Read_Buffer(buffer);
        buffer[0] = ~buffer[0];
        // Button change detected
        if (ctrl_data_p->ctrl_2_buttons_prev != buffer[0]) {
            // Check if a button has been pressed since startup
            if (!ctrl_data_p->ctrl_2_active) {
                ctrl_data_p->ctrl_2_active = 1;
            }
            // Figure out which button has changed
            ctrl_2_btn = ctrl_data_p->ctrl_2_buttons_prev ^ buffer[0];
            // Save the button if it went from unpressed -> pressed
            ctrl_2_btn &= buffer[0];
        }
        ctrl_data_p->ctrl_2_buttons_prev = buffer[0];
    } else {
        LED2_LAT = 0;
        ctrl_data_p->ctrl_2_connected = 0;
        ctrl_data_p->ctrl_2_active = 0;
    }

    // Write LED values to controllers
    if (ctrl_data_p->ctrl_1_connected) {
        buffer[0] = CONTROLLER_WRITE;
        buffer[1] = ctrl_data_p->ctrl_1_leds;
        I2C1_Master_Send(CONTROLLER_1_ADDRESS, buffer, 2);
        do {
            result = I2C1_Get_Status();
        } while (!result);
    }

    if (ctrl_data_p->ctrl_2_connected) {
        buffer[0] = CONTROLLER_WRITE;
        buffer[1] = ctrl_data_p->ctrl_2_leds;
        I2C1_Master_Send(CONTROLLER_2_ADDRESS, buffer, 2);
        do {
            result = I2C1_Get_Status();
        } while (!result);
    }

    // If board is in an idle state and a controller is connected, switch modes
    if (Get_Board_State() == BOARD_MODE_IDLE) {
        // If both controllers are active, go into game TRON mode
        if (ctrl_data_p->ctrl_1_active && ctrl_data_p->ctrl_2_active) {
            Reset_Board(BOARD_MODE_TRON);
        }
        // Otherwise if only one controller is active, go into game SNAKE mode
        if (ctrl_data_p->ctrl_1_active || ctrl_data_p->ctrl_2_active) {
            Reset_Board(BOARD_MODE_SNAKE);
        }
    }
    if (Get_Board_State() == BOARD_MODE_SNAKE) {
        // If both controllers are active, go into game TRON mode
        if (ctrl_data_p->ctrl_1_active && ctrl_data_p->ctrl_2_active) {
            Reset_Board(BOARD_MODE_TRON);
        }
    }

    // Call the callback function if any buttons have changed
    if (ctrl_data_p->btn_change_callback != NULL) {
        if (ctrl_1_btn || ctrl_2_btn) {
            (*ctrl_data_p->btn_change_callback)(ctrl_1_btn, ctrl_2_btn);
        }
    }
}

void Controller_Set_Leds(uint8_t ctrl_1, uint8_t ctrl_2) {
    ctrl_data_p->ctrl_1_leds = ctrl_1;
    ctrl_data_p->ctrl_2_leds = ctrl_2;
}

uint8_t Controller_Query(void) {
    // Returns the active status of attached controllers
    if (ctrl_data_p->ctrl_1_active && ctrl_data_p->ctrl_2_active)
        return 0x3;
    else if (ctrl_data_p->ctrl_1_active)
        return 0x1;
    else if (ctrl_data_p->ctrl_2_active)
        return 0x2;
    return 0;
}