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
 
276 Kevin 7
void Controller_Init(CONTROLLER_DATA *data,
8
        void (*change_callback)(uint8_t controller, CTRL_BTN_STATUS values)) {
238 Kevin 9
 
276 Kevin 10
    if (data != NULL)
11
        ctrl_data_p = data;
12
    ctrl_data_p->change_callback = change_callback;
13
 
274 Kevin 14
    // Variable initialization
276 Kevin 15
    int i, j;
274 Kevin 16
    for (i = 0; i < CONTROLLER_MAX_COUNT; i++) {
17
        ctrl_data_p->connected_controllers[i] = 0x0;
276 Kevin 18
        ctrl_data_p->led_status[i].w[i] = 0x0;
19
        ctrl_data_p->btn_prev[i].w = 0x0;
20
        ctrl_data_p->btn_curr[i].w = 0x0;
21
        for (j = 0; j < 16; j++) {
22
            ctrl_data_p->led_status[i].w[j] = 0x0;
23
        }
274 Kevin 24
    }
238 Kevin 25
 
274 Kevin 26
    ctrl_data_p->connected_count = 0x0;
27
 
28
    // Poll to see which controllers are connected
29
    Controller_Poll_Connected();
238 Kevin 30
}
31
 
274 Kevin 32
void Controller_Poll_Connected(void) {
276 Kevin 33
    uint8_t buffer[2];
34
    uint8_t result, i;
274 Kevin 35
    uint8_t address = CONTROLLER_PREFIX_ADDRESS + CONTROLLER_START_ADDRESS;
36
 
37
    // Attempt to contact each controller to see if its connected
38
    for (i = 0; i < CONTROLLER_MAX_COUNT; i++) {
39
        I2C1_Master_Restart(address + i, CONTROLLER_CMD_READ, 1);
40
        do {
41
            result = I2C1_Get_Status();
42
        } while (!result);
43
        if (result == I2C1_RECV_OK) {
276 Kevin 44
            uint8_t length = I2C1_Read_Buffer(buffer);
274 Kevin 45
            // If a controller is connected, save its address
276 Kevin 46
            ctrl_data_p->connected_controllers[ctrl_data_p->connected_count] = address + i;
274 Kevin 47
            ctrl_data_p->connected_count++;
48
        }
276 Kevin 49
 
50
        // A small delay is needed between polls
51
        Delay_MS(1);
274 Kevin 52
    }
276 Kevin 53
 
54
    // Show the number of controllers connected
55
    if (ctrl_data_p->connected_count & 0x1)
56
        LED1_LAT = 1;
57
    if (ctrl_data_p->connected_count & 0x2)
58
        LED2_LAT = 1;
59
    if (ctrl_data_p->connected_count & 0x4)
60
        LED3_LAT = 1;
61
    if (ctrl_data_p->connected_count & 0x8)
62
        LED4_LAT = 1;
274 Kevin 63
}
64
 
238 Kevin 65
void Controller_Update(void) {
66
    uint8_t buffer[2];
276 Kevin 67
    uint8_t result, length, i, j;
68
    CTRL_BTN_STATUS btn_change;
69
 
70
    for (i = 0; i < ctrl_data_p->connected_count; i++) {
71
        // Store the last read values
72
        ctrl_data_p->btn_prev[i] = ctrl_data_p->btn_curr[i];
238 Kevin 73
 
276 Kevin 74
        // Read in the button values for each controller
75
        I2C1_Master_Restart(ctrl_data_p->connected_controllers[i], CONTROLLER_CMD_READ, 1);
76
        do {
77
            result = I2C1_Get_Status();
78
        } while (!result);
79
        // Save the read values
238 Kevin 80
        length = I2C1_Read_Buffer(buffer);
276 Kevin 81
        ctrl_data_p->btn_curr[i].w = buffer[0];
82
 
83
        // Determine if a change was made between the current and previous values
84
        if (ctrl_data_p->btn_curr[i].w != ctrl_data_p->btn_prev[i].w) {
85
            // Determine if a button was pressed (unpressed->pressed)
86
            btn_change.w = ctrl_data_p->btn_prev[i].w ^ ctrl_data_p->btn_curr[i].w;
87
            btn_change.w &= ctrl_data_p->btn_curr[i].w;
88
 
89
            // If a button was pressed, call the saved callback
90
            if (btn_change.w) {
91
                if (ctrl_data_p->change_callback != NULL) {
92
                    ctrl_data_p->change_callback(i, btn_change);
93
                }
238 Kevin 94
            }
95
        }
96
    }
276 Kevin 97
}
238 Kevin 98
 
276 Kevin 99
void Controller_Set_Left_Leds(uint8_t controller, uint8_t value) {
100
    uint8_t result, i;
101
    uint8_t buffer[18];
102
 
103
    for (i = 0; i < 4; i++) {
104
        if (value & (0x01 << i))
105
            ctrl_data_p->led_status[controller].w[i+12] = CONTROLLER_BRIGHTNESS_HIGH;
106
        else
107
            ctrl_data_p->led_status[controller].w[i+12] = 0x00;
108
    }
109
 
110
    // Write the LED value to the controller
111
    buffer[0] = CONTROLLER_CMD_WRITE;
112
    for (i = 0; i < 16; i++)
113
        buffer[i+1] = ctrl_data_p->led_status[controller].w[i];
114
    I2C1_Master_Send(ctrl_data_p->connected_controllers[controller], buffer, 17);
238 Kevin 115
    do {
116
        result = I2C1_Get_Status();
117
    } while (!result);
118
 
276 Kevin 119
    Delay_MS(1);
120
}
238 Kevin 121
 
276 Kevin 122
void Controller_Set_Middle_Leds(uint8_t controller, uint8_t value) {
123
    uint8_t result, i;
124
    uint8_t buffer[18];
125
 
126
    for (i = 0; i < 8; i++) {
127
        if (value & (0x01 << i))
128
            ctrl_data_p->led_status[controller].w[i] = CONTROLLER_BRIGHTNESS_HIGH;
129
        else
130
            ctrl_data_p->led_status[controller].w[i] = 0x00;
238 Kevin 131
    }
132
 
276 Kevin 133
    // Write the LED value to the controller
134
    buffer[0] = CONTROLLER_CMD_WRITE;
135
    for (i = 0; i < 16; i++)
136
        buffer[i+1] = ctrl_data_p->led_status[controller].w[i];
137
    I2C1_Master_Send(ctrl_data_p->connected_controllers[controller], buffer, 17);
138
    do {
139
        result = I2C1_Get_Status();
140
    } while (!result);
141
 
142
    Delay_MS(1);
143
}
144
 
145
void Controller_Set_Right_Leds(uint8_t controller, uint8_t value) {
146
    uint8_t result, i;
147
    uint8_t buffer[18];
148
 
149
    for (i = 0; i < 4; i++) {
150
        if (value & (0x01 << i))
151
            ctrl_data_p->led_status[controller].w[i+8] = CONTROLLER_BRIGHTNESS_HIGH;
152
        else
153
            ctrl_data_p->led_status[controller].w[i+8] = 0x00;
238 Kevin 154
    }
155
 
276 Kevin 156
    // Write the LED value to the controller
157
    buffer[0] = CONTROLLER_CMD_WRITE;
158
    for (i = 0; i < 16; i++)
159
        buffer[i+1] = ctrl_data_p->led_status[controller].w[i];
160
    I2C1_Master_Send(ctrl_data_p->connected_controllers[controller], buffer, 17);
161
    do {
162
        result = I2C1_Get_Status();
163
    } while (!result);
164
 
165
    Delay_MS(1);
238 Kevin 166
}
167
 
276 Kevin 168
uint8_t Controller_Get_Connected(void) {
169
    return ctrl_data_p->connected_count;
240 Kevin 170
}
171
 
276 Kevin 172
void Controller_Set_Active(uint8_t controller) {
173
    uint8_t buffer[2];
174
    uint8_t result;
175
 
176
    buffer[0] = CONTROLLER_CMD_ACTIVE;
177
    I2C1_Master_Send(ctrl_data_p->connected_controllers[controller], buffer, 1);
178
    do {
179
        result = I2C1_Get_Status();
180
    } while (!result);
181
 
182
    Delay_MS(1);
183
}
184
 
185
void Controller_Set_Idle(uint8_t controller) {
186
    uint8_t buffer[2];
187
    uint8_t result;
188
 
189
    buffer[0] = CONTROLLER_CMD_RESET;
190
    I2C1_Master_Send(ctrl_data_p->connected_controllers[controller], buffer, 1);
191
    do {
192
        result = I2C1_Get_Status();
193
    } while (!result);
194
 
195
    Delay_MS(1);
238 Kevin 196
}