Subversion Repositories Code-Repo

Rev

Rev 238 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 238 Rev 240
Line 1... Line 1...
1
#include "defines.h"
1
#include "defines.h"
2
#include "CONTROLLERS.h"
2
#include "CONTROLLERS.h"
3
#include "I2C1.h"
3
#include "I2C1.h"
4
 
4
 
5
static CONTROLLER_DATA *ctrl_data_p;
5
static CONTROLLER_DATA *ctrl_data_p;
6
static BOARD_STATE *board_state_p;
-
 
7
 
6
 
8
void Controller_Init(CONTROLLER_DATA *data, BOARD_STATE *state,
7
void Controller_Init(CONTROLLER_DATA *data, 
9
        void (*btn_change_callback)(uint8_t, uint8_t)) {
8
        void (*btn_change_callback)(uint8_t, uint8_t)) {
10
    ctrl_data_p = data;
9
    ctrl_data_p = data;
11
    board_state_p = state;
-
 
12
 
10
 
13
    ctrl_data_p->btn_change_callback = btn_change_callback;
11
    ctrl_data_p->btn_change_callback = btn_change_callback;
14
    
12
    
15
    ctrl_data_p->ctrl_1_connected = 0;
13
    ctrl_data_p->ctrl_1_connected = 0;
16
    ctrl_data_p->ctrl_1_buttons_prev = CONTROLLER_BTN_DEFAULT;
14
    ctrl_data_p->ctrl_1_buttons_prev = CONTROLLER_BTN_DEFAULT;
Line 103... Line 101...
103
            result = I2C1_Get_Status();
101
            result = I2C1_Get_Status();
104
        } while (!result);
102
        } while (!result);
105
    }
103
    }
106
 
104
 
107
    // If board is in an idle state and a controller is connected, switch modes
105
    // If board is in an idle state and a controller is connected, switch modes
108
    if (board_state_p->cube_mode == BOARD_MODE_IDLE) {
106
    if (Get_Board_State() == BOARD_MODE_IDLE) {
109
        // If both controllers are active, go into game TRON mode
107
        // If both controllers are active, go into game TRON mode
110
        if (ctrl_data_p->ctrl_1_active && ctrl_data_p->ctrl_2_active) {
108
        if (ctrl_data_p->ctrl_1_active && ctrl_data_p->ctrl_2_active) {
111
            board_state_p->cube_mode = BOARD_MODE_TRON;
-
 
112
            Reset_Board();
109
            Reset_Board(BOARD_MODE_TRON);
113
        }
110
        }
114
        // Otherwise if only one controller is active, go into game SNAKE mode
111
        // Otherwise if only one controller is active, go into game SNAKE mode
115
        if (ctrl_data_p->ctrl_1_active || ctrl_data_p->ctrl_2_active) {
112
        if (ctrl_data_p->ctrl_1_active || ctrl_data_p->ctrl_2_active) {
116
            board_state_p->cube_mode = BOARD_MODE_SNAKE;
-
 
117
            Reset_Board();
113
            Reset_Board(BOARD_MODE_SNAKE);
118
        }
114
        }
119
    }
115
    }
120
    if (board_state_p->cube_mode == BOARD_MODE_SNAKE) {
116
    if (Get_Board_State() == BOARD_MODE_SNAKE) {
121
        // If both controllers are active, go into game TRON mode
117
        // If both controllers are active, go into game TRON mode
122
        if (ctrl_data_p->ctrl_1_active && ctrl_data_p->ctrl_2_active) {
118
        if (ctrl_data_p->ctrl_1_active && ctrl_data_p->ctrl_2_active) {
123
            board_state_p->cube_mode = BOARD_MODE_TRON;
-
 
124
            Reset_Board();
119
            Reset_Board(BOARD_MODE_TRON);
125
        }
120
        }
126
    }
121
    }
127
 
122
 
128
    // Call the callback function if any buttons have changed
123
    // Call the callback function if any buttons have changed
129
    if (ctrl_data_p->btn_change_callback != NULL) {
124
    if (ctrl_data_p->btn_change_callback != NULL) {
Line 134... Line 129...
134
}
129
}
135
 
130
 
136
void Controller_Set_Leds(uint8_t ctrl_1, uint8_t ctrl_2) {
131
void Controller_Set_Leds(uint8_t ctrl_1, uint8_t ctrl_2) {
137
    ctrl_data_p->ctrl_1_leds = ctrl_1;
132
    ctrl_data_p->ctrl_1_leds = ctrl_1;
138
    ctrl_data_p->ctrl_2_leds = ctrl_2;
133
    ctrl_data_p->ctrl_2_leds = ctrl_2;
-
 
134
}
-
 
135
 
-
 
136
uint8_t Controller_Query(void) {
-
 
137
    // Returns the active status of attached controllers
-
 
138
    if (ctrl_data_p->ctrl_1_active && ctrl_data_p->ctrl_2_active)
-
 
139
        return 0x3;
-
 
140
    else if (ctrl_data_p->ctrl_1_active)
-
 
141
        return 0x1;
-
 
142
    else if (ctrl_data_p->ctrl_2_active)
-
 
143
        return 0x2;
-
 
144
    return 0;
139
}
145
}
140
146