Subversion Repositories Code-Repo

Rev

Rev 240 | 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) {
    ctrl_data_p = data;

    // Variable initialization
    int i;
    for (i = 0; i < CONTROLLER_MAX_COUNT; i++) {
        ctrl_data_p->connected_controllers[i] = 0x0;
        ctrl_data_p->led_status[i][0] = 0x0;
        ctrl_data_p->led_status[i][1] = 0x0;
        ctrl_data_p->btn_prev[i] = 0x0;
        ctrl_data_p->btn_last[i] = 0x0;
    }

    ctrl_data_p->connected_count = 0x0;

    // Poll to see which controllers are connected
    Controller_Poll_Connected();
}

void Controller_Poll_Connected(void) {
    uint8_t buffer[2] = {CONTROLLER_CMD_RESET};
    uint8_t result, length, i;
    uint8_t address = CONTROLLER_PREFIX_ADDRESS + CONTROLLER_START_ADDRESS;

    // Attempt to contact each controller to see if its connected
    for (i = 0; i < CONTROLLER_MAX_COUNT; i++) {
//        I2C1_Master_Send(address + i, buffer, 1);
//        do {
//            result = I2C1_Get_Status();
//        } while (!result);
//        if (result == I2C1_SEND_OK) {
//            // If a controller is connected, save its address
//            ctrl_data_p->connected_controllers[ctrl_data_p->connected_count]
//                    = address + i;
//            ctrl_data_p->connected_count++;
//        }
        I2C1_Master_Restart(address + i, CONTROLLER_CMD_READ, 1);
        do {
            result = I2C1_Get_Status();
        } while (!result);
        if (result == I2C1_RECV_OK) {
            length = I2C1_Read_Buffer(buffer);
            // If a controller is connected, save its address
            ctrl_data_p->connected_controllers[ctrl_data_p->connected_count]
                    = address + i;
            ctrl_data_p->connected_count++;
        }
    }
}

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 controller, uint16_t value) {
//    ctrl_data_p->ctrl_1_leds = ctrl_1;
//    ctrl_data_p->ctrl_2_leds = ctrl_2;
}

uint8_t Controller_Query(uint8_t controller) {
//    // 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;
}