Subversion Repositories Code-Repo

Rev

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

Rev 275 Rev 277
Line 24... Line 24...
24
#include "defines.h"
24
#include "defines.h"
25
#include "INTERRUPTS.h"
25
#include "INTERRUPTS.h"
26
#include "IO.h"
26
#include "IO.h"
27
#include "I2C1.h"
27
#include "I2C1.h"
28
#include "I2C2.h"
28
#include "I2C2.h"
-
 
29
#include "CPS.h"
29
#include "TLC59116.h"
30
#include "TLC59116.h"
30
#include "MCP23009.h"
31
#include "MCP23009.h"
31
 
32
 
32
persistent uint8_t op_state;
33
persistent uint8_t op_state;
33
 
34
 
Line 115... Line 116...
115
    // Initialize I2C2 in master mode
116
    // Initialize I2C2 in master mode
116
    I2C2_DATA i2c2_data;
117
    I2C2_DATA i2c2_data;
117
    I2C2_Init(&i2c2_data);
118
    I2C2_Init(&i2c2_data);
118
    I2C2_Configure_Master(I2C_1MHZ);
119
    I2C2_Configure_Master(I2C_1MHZ);
119
 
120
 
-
 
121
    CPS_DATA cps_data;
-
 
122
    CPS_Init(&cps_data);
-
 
123
 
120
    // Initialize interrupts
124
    // Initialize interrupts
121
    Interrupt_Init();
125
    Interrupt_Init();
122
    Interrupt_Enable();
126
    Interrupt_Enable();
123
 
127
 
124
    MCP23009_Init(&btns);
128
    MCP23009_Init(&btns);
Line 129... Line 133...
129
 
133
 
130
    if (op_state == OP_STATE_IDLE) {
134
    if (op_state == OP_STATE_IDLE) {
131
//        IO_IOC_Enable();
135
//        IO_IOC_Enable();
132
        Idle_Animation();
136
        Idle_Animation();
133
    } else if (op_state == OP_STATE_ACTIVE) {
137
    } else if (op_state == OP_STATE_ACTIVE) {
-
 
138
        uint8_t button_R = 0, button_L = 0;
-
 
139
        uint8_t dir_state = DIR_S;
-
 
140
        leds.single.LED_S = DIR_BRIGHTNESS;
-
 
141
        TLC59116_Write_All(&leds);
134
        while (1) {
142
        while (1) {
135
            // Check if an I2C message was received
143
            // Check if an I2C message was received
-
 
144
            do {
136
            result = I2C1_Get_Status();
145
                result = I2C1_Get_Status();
137
            if (result) {
146
            } while (!result);
138
                length = I2C1_Read_Buffer(buffer);
147
            length = I2C1_Read_Buffer(buffer);
139
                if (length == 1 && buffer[0] == CMD_RESET) {
148
            if (length == 1 && buffer[0] == CMD_RESET) {
140
                    Reset_Board(OP_STATE_IDLE);
149
                Reset_Board(OP_STATE_IDLE);
141
                } else if (length == 1 && buffer[0] == CMD_ACTIVE) {
150
            } else if (length == 1 && buffer[0] == CMD_ACTIVE) {
142
                    Reset_Board(OP_STATE_ACTIVE);
151
                Reset_Board(OP_STATE_ACTIVE);
143
                } else if (length == 17 && buffer[0] == CMD_SET_LEDS) {
152
            } else if (length == 17 && buffer[0] == CMD_SET_LEDS) {
144
                    for (i = 0; i < 16; i++) {
153
                for (i = 0; i < 16; i++) {
145
                        leds.w[i] = buffer[i + 1];
154
                    leds.w[i] = buffer[i + 1];
146
                    }
-
 
147
                    TLC59116_Write_All(&leds);
-
 
148
                }
155
                }
-
 
156
                // Reset the direction LEDs
-
 
157
                if (dir_state == DIR_S)
-
 
158
                    leds.single.LED_S = DIR_BRIGHTNESS;
-
 
159
                else if (dir_state == DIR_W)
-
 
160
                    leds.single.LED_W = DIR_BRIGHTNESS;
-
 
161
                else if (dir_state == DIR_N)
-
 
162
                    leds.single.LED_N = DIR_BRIGHTNESS;
-
 
163
                else if (dir_state == DIR_E)
-
 
164
                    leds.single.LED_E = DIR_BRIGHTNESS;
-
 
165
                TLC59116_Write_All(&leds);
149
            }
166
            }
-
 
167
            
150
 
168
 
-
 
169
            // Check if either capacitive buttons were pressed
-
 
170
            if (cps_data.btn_pressed[0] && button_R == 0) {
-
 
171
                // Right button went from unpressed -> pressed
-
 
172
                dir_state = Direction_Rotate_CClockwise(dir_state);
-
 
173
                TLC59116_Write_All(&leds);
-
 
174
            }
-
 
175
            if (cps_data.btn_pressed[1] && button_L == 0) {
-
 
176
                // Left button went from unpressed -> pressed
-
 
177
                dir_state = Direction_Rotate_Clockwise(dir_state);
-
 
178
                TLC59116_Write_All(&leds);
-
 
179
            }
-
 
180
            // Save the previous button state
-
 
181
            button_R = cps_data.btn_pressed[0];
-
 
182
            button_L = cps_data.btn_pressed[1];
-
 
183
 
-
 
184
            // Poll the hardware buttons
151
            I2C2_Master_Restart(MCP23009_ADDR, MCP23009_GPIOA, 1);
185
            I2C2_Master_Restart(MCP23009_ADDR, MCP23009_GPIOA, 1);
152
            do {
186
            do {
153
                result = I2C2_Get_Status();
187
                result = I2C2_Get_Status();
154
            } while (!result);
188
            } while (!result);
155
            length = I2C2_Read_Buffer(buffer);
189
            length = I2C2_Read_Buffer(buffer);
156
            btns.w = buffer[0];
190
            btns.w = buffer[0];
-
 
191
            // Change the direction according to the user's perspective
-
 
192
            if (dir_state == DIR_W) {
-
 
193
                uint8_t tmp = btns.BTN_R_S;
-
 
194
                btns.BTN_R_S = btns.BTN_R_W;
-
 
195
                btns.BTN_R_W = btns.BTN_R_N;
-
 
196
                btns.BTN_R_N = btns.BTN_R_E;
-
 
197
                btns.BTN_R_E = tmp;
-
 
198
            } else if (dir_state == DIR_E) {
-
 
199
                uint8_t tmp = btns.BTN_R_S;
-
 
200
                btns.BTN_R_S = btns.BTN_R_E;
-
 
201
                btns.BTN_R_E = btns.BTN_R_N;
-
 
202
                btns.BTN_R_N = btns.BTN_R_W;
-
 
203
                btns.BTN_R_W = tmp;
-
 
204
            } else if (dir_state == DIR_N) {
-
 
205
                uint8_t tmp = btns.BTN_R_S;
-
 
206
                btns.BTN_R_S = btns.BTN_R_N;
-
 
207
                btns.BTN_R_N = tmp;
-
 
208
                tmp = btns.BTN_R_E;
-
 
209
                btns.BTN_R_E = btns.BTN_R_W;
-
 
210
                btns.BTN_R_W = tmp;
-
 
211
            }
157
        }
212
        }
158
    }
213
    }
159
}
214
}
160
 
215
 
-
 
216
uint8_t Direction_Rotate_Clockwise(uint8_t dir_state) {
-
 
217
    uint8_t ret;
-
 
218
    switch (dir_state) {
-
 
219
        case DIR_S:
-
 
220
            leds.single.LED_S = 0x00;
-
 
221
            leds.single.LED_W = DIR_BRIGHTNESS;
-
 
222
            ret = DIR_W;
-
 
223
            break;
-
 
224
        case DIR_W:
-
 
225
            leds.single.LED_W = 0x00;
-
 
226
            leds.single.LED_N = DIR_BRIGHTNESS;
-
 
227
            ret = DIR_N;
-
 
228
            break;
-
 
229
        case DIR_N:
-
 
230
            leds.single.LED_N = 0x00;
-
 
231
            leds.single.LED_E = DIR_BRIGHTNESS;
-
 
232
            ret = DIR_E;
-
 
233
            break;
-
 
234
        case DIR_E:
-
 
235
            leds.single.LED_E = 0x00;
-
 
236
            leds.single.LED_S = DIR_BRIGHTNESS;
-
 
237
            ret = DIR_S;
-
 
238
            break;
-
 
239
    }
-
 
240
    return ret;
-
 
241
}
-
 
242
 
-
 
243
uint8_t Direction_Rotate_CClockwise(uint8_t dir_state) {
-
 
244
    uint8_t ret;
-
 
245
    switch (dir_state) {
-
 
246
        case DIR_S:
-
 
247
            leds.single.LED_S = 0x00;
-
 
248
            leds.single.LED_E = DIR_BRIGHTNESS;
-
 
249
            ret = DIR_E;
-
 
250
            break;
-
 
251
        case DIR_E:
-
 
252
            leds.single.LED_E = 0x00;
-
 
253
            leds.single.LED_N = DIR_BRIGHTNESS;
-
 
254
            ret = DIR_N;
-
 
255
            break;
-
 
256
        case DIR_N:
-
 
257
            leds.single.LED_N = 0x00;
-
 
258
            leds.single.LED_W = DIR_BRIGHTNESS;
-
 
259
            ret = DIR_W;
-
 
260
            break;
-
 
261
        case DIR_W:
-
 
262
            leds.single.LED_W = 0x00;
-
 
263
            leds.single.LED_S = DIR_BRIGHTNESS;
-
 
264
            ret = DIR_S;
-
 
265
            break;
-
 
266
    }
-
 
267
    return ret;
-
 
268
}
-
 
269
 
161
void Check_I2C(void) {
270
void Check_I2C_Idle(void) {
162
    uint8_t buffer[32];
271
    uint8_t buffer[32];
163
    uint8_t result, length, i;
272
    uint8_t result, length, i;
164
 
273
 
165
    // Check if an I2C message was received
274
    // Check if an I2C message was received
166
    result = I2C1_Get_Status();
275
    result = I2C1_Get_Status();
Line 168... Line 277...
168
        length = I2C1_Read_Buffer(buffer);
277
        length = I2C1_Read_Buffer(buffer);
169
        if (length == 1 && buffer[0] == CMD_RESET) {
278
        if (length == 1 && buffer[0] == CMD_RESET) {
170
            Reset_Board(OP_STATE_IDLE);
279
            Reset_Board(OP_STATE_IDLE);
171
        } else if (length == 1 && buffer[0] == CMD_ACTIVE) {
280
        } else if (length == 1 && buffer[0] == CMD_ACTIVE) {
172
            Reset_Board(OP_STATE_ACTIVE);
281
            Reset_Board(OP_STATE_ACTIVE);
173
        } else if (length == 17 && buffer[0] == CMD_SET_LEDS) {
-
 
174
            for (i = 0; i < 16; i++) {
-
 
175
                leds.w[i] = buffer[i + 1];
-
 
176
            }
-
 
177
            TLC59116_Write_All(&leds);
-
 
178
        }
282
        }
179
    }
283
    }
180
}
284
}
181
 
285
 
182
void Idle_Animation(void) {
286
void Idle_Animation(void) {
Line 193... Line 297...
193
    for (i = 0; i < 16; i++) leds.w[i] = 0x00;
297
    for (i = 0; i < 16; i++) leds.w[i] = 0x00;
194
    
298
    
195
    while (1) {
299
    while (1) {
196
 
300
 
197
        // Check to see if a new message was received
301
        // Check to see if a new message was received
198
        Check_I2C();
302
        Check_I2C_Idle();
199
 
303
 
200
        // Update the LEDs in the middle section
304
        // Update the LEDs in the middle section
201
        for (i = 0; i < 8; i++) {
305
        for (i = 0; i < 8; i++) {
202
            // Change the LED brightness depending on its direction
306
            // Change the LED brightness depending on its direction
203
            if (led_direction_bar[i] == 1) {
307
            if (led_direction_bar[i] == 1) {