Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
238 Kevin 1
#include "defines.h"
2
#include "CONTROLLERS.h"
3
#include "I2C1.h"
4
 
5
static CONTROLLER_DATA *ctrl_data_p;
6
 
274 Kevin 7
void Controller_Init(CONTROLLER_DATA *data) {
238 Kevin 8
    ctrl_data_p = data;
9
 
274 Kevin 10
    // Variable initialization
11
    int i;
12
    for (i = 0; i < CONTROLLER_MAX_COUNT; i++) {
13
        ctrl_data_p->connected_controllers[i] = 0x0;
14
        ctrl_data_p->led_status[i][0] = 0x0;
15
        ctrl_data_p->led_status[i][1] = 0x0;
16
        ctrl_data_p->btn_prev[i] = 0x0;
17
        ctrl_data_p->btn_last[i] = 0x0;
18
    }
238 Kevin 19
 
274 Kevin 20
    ctrl_data_p->connected_count = 0x0;
21
 
22
    // Poll to see which controllers are connected
23
    Controller_Poll_Connected();
238 Kevin 24
}
25
 
274 Kevin 26
void Controller_Poll_Connected(void) {
27
    uint8_t buffer[2] = {CONTROLLER_CMD_RESET};
28
    uint8_t result, length, i;
29
    uint8_t address = CONTROLLER_PREFIX_ADDRESS + CONTROLLER_START_ADDRESS;
30
 
31
    // Attempt to contact each controller to see if its connected
32
    for (i = 0; i < CONTROLLER_MAX_COUNT; i++) {
33
//        I2C1_Master_Send(address + i, buffer, 1);
34
//        do {
35
//            result = I2C1_Get_Status();
36
//        } while (!result);
37
//        if (result == I2C1_SEND_OK) {
38
//            // If a controller is connected, save its address
39
//            ctrl_data_p->connected_controllers[ctrl_data_p->connected_count]
40
//                    = address + i;
41
//            ctrl_data_p->connected_count++;
42
//        }
43
        I2C1_Master_Restart(address + i, CONTROLLER_CMD_READ, 1);
44
        do {
45
            result = I2C1_Get_Status();
46
        } while (!result);
47
        if (result == I2C1_RECV_OK) {
48
            length = I2C1_Read_Buffer(buffer);
49
            // If a controller is connected, save its address
50
            ctrl_data_p->connected_controllers[ctrl_data_p->connected_count]
51
                    = address + i;
52
            ctrl_data_p->connected_count++;
53
        }
54
    }
55
}
56
 
238 Kevin 57
void Controller_Update(void) {
274 Kevin 58
    /*
238 Kevin 59
    uint8_t buffer[2];
60
    uint8_t result, length;
61
    uint8_t ctrl_1_btn = 0, ctrl_2_btn = 0;
62
 
63
    // Read button values from controllers
64
    I2C1_Master_Restart(CONTROLLER_1_ADDRESS, CONTROLLER_READ, 1);
65
    do {
66
        result = I2C1_Get_Status();
67
    } while (!result);
68
    if (result == I2C1_RECV_OK) {
69
        // Indicate that controller 1 is connected
70
        LED1_LAT = 1;
71
        ctrl_data_p->ctrl_1_connected = 1;
72
        length = I2C1_Read_Buffer(buffer);
73
        buffer[0] = ~buffer[0];
74
        // Button change detected
75
        if (ctrl_data_p->ctrl_1_buttons_prev != buffer[0]) {
76
            // Check if a button has been pressed since startup
77
            if (!ctrl_data_p->ctrl_1_active) {
78
                ctrl_data_p->ctrl_1_active = 1;
79
            }
80
            // Figure out which button has changed
81
            ctrl_1_btn = ctrl_data_p->ctrl_1_buttons_prev ^ buffer[0];
82
            // Save the button if it went from unpressed -> pressed
83
            ctrl_1_btn &= buffer[0];
84
        }
85
        ctrl_data_p->ctrl_1_buttons_prev = buffer[0];
86
    } else {
87
        LED1_LAT = 0;
88
        ctrl_data_p->ctrl_1_connected = 0;
89
        ctrl_data_p->ctrl_1_active = 0;
90
    }
91
 
92
    I2C1_Master_Restart(CONTROLLER_2_ADDRESS, CONTROLLER_READ, 1);
93
    do {
94
        result = I2C1_Get_Status();
95
    } while (!result);
96
    if (result == I2C1_RECV_OK) {
97
        // Indicate that controller 2 is connected
98
        LED2_LAT = 1;
99
        ctrl_data_p->ctrl_2_connected = 1;
100
        length = I2C1_Read_Buffer(buffer);
101
        buffer[0] = ~buffer[0];
102
        // Button change detected
103
        if (ctrl_data_p->ctrl_2_buttons_prev != buffer[0]) {
104
            // Check if a button has been pressed since startup
105
            if (!ctrl_data_p->ctrl_2_active) {
106
                ctrl_data_p->ctrl_2_active = 1;
107
            }
108
            // Figure out which button has changed
109
            ctrl_2_btn = ctrl_data_p->ctrl_2_buttons_prev ^ buffer[0];
110
            // Save the button if it went from unpressed -> pressed
111
            ctrl_2_btn &= buffer[0];
112
        }
113
        ctrl_data_p->ctrl_2_buttons_prev = buffer[0];
114
    } else {
115
        LED2_LAT = 0;
116
        ctrl_data_p->ctrl_2_connected = 0;
117
        ctrl_data_p->ctrl_2_active = 0;
118
    }
119
 
120
    // Write LED values to controllers
121
    if (ctrl_data_p->ctrl_1_connected) {
122
        buffer[0] = CONTROLLER_WRITE;
123
        buffer[1] = ctrl_data_p->ctrl_1_leds;
124
        I2C1_Master_Send(CONTROLLER_1_ADDRESS, buffer, 2);
125
        do {
126
            result = I2C1_Get_Status();
127
        } while (!result);
128
    }
129
 
130
    if (ctrl_data_p->ctrl_2_connected) {
131
        buffer[0] = CONTROLLER_WRITE;
132
        buffer[1] = ctrl_data_p->ctrl_2_leds;
133
        I2C1_Master_Send(CONTROLLER_2_ADDRESS, buffer, 2);
134
        do {
135
            result = I2C1_Get_Status();
136
        } while (!result);
137
    }
138
 
139
    // If board is in an idle state and a controller is connected, switch modes
240 Kevin 140
    if (Get_Board_State() == BOARD_MODE_IDLE) {
238 Kevin 141
        // If both controllers are active, go into game TRON mode
142
        if (ctrl_data_p->ctrl_1_active && ctrl_data_p->ctrl_2_active) {
240 Kevin 143
            Reset_Board(BOARD_MODE_TRON);
238 Kevin 144
        }
145
        // Otherwise if only one controller is active, go into game SNAKE mode
146
        if (ctrl_data_p->ctrl_1_active || ctrl_data_p->ctrl_2_active) {
240 Kevin 147
            Reset_Board(BOARD_MODE_SNAKE);
238 Kevin 148
        }
149
    }
240 Kevin 150
    if (Get_Board_State() == BOARD_MODE_SNAKE) {
238 Kevin 151
        // If both controllers are active, go into game TRON mode
152
        if (ctrl_data_p->ctrl_1_active && ctrl_data_p->ctrl_2_active) {
240 Kevin 153
            Reset_Board(BOARD_MODE_TRON);
238 Kevin 154
        }
155
    }
156
 
157
    // Call the callback function if any buttons have changed
158
    if (ctrl_data_p->btn_change_callback != NULL) {
159
        if (ctrl_1_btn || ctrl_2_btn) {
160
            (*ctrl_data_p->btn_change_callback)(ctrl_1_btn, ctrl_2_btn);
161
        }
162
    }
274 Kevin 163
    */
238 Kevin 164
}
165
 
274 Kevin 166
void Controller_Set_Leds(uint8_t controller, uint16_t value) {
167
//    ctrl_data_p->ctrl_1_leds = ctrl_1;
168
//    ctrl_data_p->ctrl_2_leds = ctrl_2;
240 Kevin 169
}
170
 
274 Kevin 171
uint8_t Controller_Query(uint8_t controller) {
172
//    // Returns the active status of attached controllers
173
//    if (ctrl_data_p->ctrl_1_active && ctrl_data_p->ctrl_2_active)
174
//        return 0x3;
175
//    else if (ctrl_data_p->ctrl_1_active)
176
//        return 0x1;
177
//    else if (ctrl_data_p->ctrl_2_active)
178
//        return 0x2;
179
//    return 0;
238 Kevin 180
}