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