26,6 → 26,7 |
#include "IO.h" |
#include "I2C1.h" |
#include "I2C2.h" |
#include "CPS.h" |
#include "TLC59116.h" |
#include "MCP23009.h" |
|
117,6 → 118,9 |
I2C2_Init(&i2c2_data); |
I2C2_Configure_Master(I2C_1MHZ); |
|
CPS_DATA cps_data; |
CPS_Init(&cps_data); |
|
// Initialize interrupts |
Interrupt_Init(); |
Interrupt_Enable(); |
131,23 → 135,53 |
// IO_IOC_Enable(); |
Idle_Animation(); |
} else if (op_state == OP_STATE_ACTIVE) { |
uint8_t button_R = 0, button_L = 0; |
uint8_t dir_state = DIR_S; |
leds.single.LED_S = DIR_BRIGHTNESS; |
TLC59116_Write_All(&leds); |
while (1) { |
// Check if an I2C message was received |
result = I2C1_Get_Status(); |
if (result) { |
length = I2C1_Read_Buffer(buffer); |
if (length == 1 && buffer[0] == CMD_RESET) { |
Reset_Board(OP_STATE_IDLE); |
} else if (length == 1 && buffer[0] == CMD_ACTIVE) { |
Reset_Board(OP_STATE_ACTIVE); |
} else if (length == 17 && buffer[0] == CMD_SET_LEDS) { |
for (i = 0; i < 16; i++) { |
leds.w[i] = buffer[i + 1]; |
} |
TLC59116_Write_All(&leds); |
do { |
result = I2C1_Get_Status(); |
} while (!result); |
length = I2C1_Read_Buffer(buffer); |
if (length == 1 && buffer[0] == CMD_RESET) { |
Reset_Board(OP_STATE_IDLE); |
} else if (length == 1 && buffer[0] == CMD_ACTIVE) { |
Reset_Board(OP_STATE_ACTIVE); |
} else if (length == 17 && buffer[0] == CMD_SET_LEDS) { |
for (i = 0; i < 16; i++) { |
leds.w[i] = buffer[i + 1]; |
} |
// Reset the direction LEDs |
if (dir_state == DIR_S) |
leds.single.LED_S = DIR_BRIGHTNESS; |
else if (dir_state == DIR_W) |
leds.single.LED_W = DIR_BRIGHTNESS; |
else if (dir_state == DIR_N) |
leds.single.LED_N = DIR_BRIGHTNESS; |
else if (dir_state == DIR_E) |
leds.single.LED_E = DIR_BRIGHTNESS; |
TLC59116_Write_All(&leds); |
} |
|
|
// Check if either capacitive buttons were pressed |
if (cps_data.btn_pressed[0] && button_R == 0) { |
// Right button went from unpressed -> pressed |
dir_state = Direction_Rotate_CClockwise(dir_state); |
TLC59116_Write_All(&leds); |
} |
if (cps_data.btn_pressed[1] && button_L == 0) { |
// Left button went from unpressed -> pressed |
dir_state = Direction_Rotate_Clockwise(dir_state); |
TLC59116_Write_All(&leds); |
} |
// Save the previous button state |
button_R = cps_data.btn_pressed[0]; |
button_L = cps_data.btn_pressed[1]; |
|
// Poll the hardware buttons |
I2C2_Master_Restart(MCP23009_ADDR, MCP23009_GPIOA, 1); |
do { |
result = I2C2_Get_Status(); |
154,11 → 188,86 |
} while (!result); |
length = I2C2_Read_Buffer(buffer); |
btns.w = buffer[0]; |
// Change the direction according to the user's perspective |
if (dir_state == DIR_W) { |
uint8_t tmp = btns.BTN_R_S; |
btns.BTN_R_S = btns.BTN_R_W; |
btns.BTN_R_W = btns.BTN_R_N; |
btns.BTN_R_N = btns.BTN_R_E; |
btns.BTN_R_E = tmp; |
} else if (dir_state == DIR_E) { |
uint8_t tmp = btns.BTN_R_S; |
btns.BTN_R_S = btns.BTN_R_E; |
btns.BTN_R_E = btns.BTN_R_N; |
btns.BTN_R_N = btns.BTN_R_W; |
btns.BTN_R_W = tmp; |
} else if (dir_state == DIR_N) { |
uint8_t tmp = btns.BTN_R_S; |
btns.BTN_R_S = btns.BTN_R_N; |
btns.BTN_R_N = tmp; |
tmp = btns.BTN_R_E; |
btns.BTN_R_E = btns.BTN_R_W; |
btns.BTN_R_W = tmp; |
} |
} |
} |
} |
|
void Check_I2C(void) { |
uint8_t Direction_Rotate_Clockwise(uint8_t dir_state) { |
uint8_t ret; |
switch (dir_state) { |
case DIR_S: |
leds.single.LED_S = 0x00; |
leds.single.LED_W = DIR_BRIGHTNESS; |
ret = DIR_W; |
break; |
case DIR_W: |
leds.single.LED_W = 0x00; |
leds.single.LED_N = DIR_BRIGHTNESS; |
ret = DIR_N; |
break; |
case DIR_N: |
leds.single.LED_N = 0x00; |
leds.single.LED_E = DIR_BRIGHTNESS; |
ret = DIR_E; |
break; |
case DIR_E: |
leds.single.LED_E = 0x00; |
leds.single.LED_S = DIR_BRIGHTNESS; |
ret = DIR_S; |
break; |
} |
return ret; |
} |
|
uint8_t Direction_Rotate_CClockwise(uint8_t dir_state) { |
uint8_t ret; |
switch (dir_state) { |
case DIR_S: |
leds.single.LED_S = 0x00; |
leds.single.LED_E = DIR_BRIGHTNESS; |
ret = DIR_E; |
break; |
case DIR_E: |
leds.single.LED_E = 0x00; |
leds.single.LED_N = DIR_BRIGHTNESS; |
ret = DIR_N; |
break; |
case DIR_N: |
leds.single.LED_N = 0x00; |
leds.single.LED_W = DIR_BRIGHTNESS; |
ret = DIR_W; |
break; |
case DIR_W: |
leds.single.LED_W = 0x00; |
leds.single.LED_S = DIR_BRIGHTNESS; |
ret = DIR_S; |
break; |
} |
return ret; |
} |
|
void Check_I2C_Idle(void) { |
uint8_t buffer[32]; |
uint8_t result, length, i; |
|
170,11 → 279,6 |
Reset_Board(OP_STATE_IDLE); |
} else if (length == 1 && buffer[0] == CMD_ACTIVE) { |
Reset_Board(OP_STATE_ACTIVE); |
} else if (length == 17 && buffer[0] == CMD_SET_LEDS) { |
for (i = 0; i < 16; i++) { |
leds.w[i] = buffer[i + 1]; |
} |
TLC59116_Write_All(&leds); |
} |
} |
} |
195,7 → 299,7 |
while (1) { |
|
// Check to see if a new message was received |
Check_I2C(); |
Check_I2C_Idle(); |
|
// Update the LEDs in the middle section |
for (i = 0; i < 8; i++) { |