Subversion Repositories Code-Repo

Rev

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

Rev 273 Rev 275
Line 73... Line 73...
73
 
73
 
74
    return ret;
74
    return ret;
75
}
75
}
76
 
76
 
77
BTN_STATUS btns;
77
BTN_STATUS btns;
-
 
78
LED_VALUES leds = {0x00};
78
 
79
 
79
int main(void) {
80
int main(void) {
80
    uint8_t buffer[32];
81
    uint8_t buffer[32];
81
    uint8_t result, length;
82
    uint8_t result, length;
82
    uint8_t i2c_slave_addr;
83
    uint8_t i2c_slave_addr;
Line 102... Line 103...
102
//    }
103
//    }
103
 
104
 
104
    i2c_slave_addr = Read_Address();
105
    i2c_slave_addr = Read_Address();
105
 
106
 
106
    // Delay a bit to allow I2C lines to stabilize
107
    // Delay a bit to allow I2C lines to stabilize
107
    __delay_ms(1);
108
    __delay_ms(10);
108
 
109
 
109
    // Initialize I2C1 in slave mode
110
    // Initialize I2C1 in slave mode
110
    I2C1_DATA i2c1_data;
111
    I2C1_DATA i2c1_data;
111
    I2C1_Init(&i2c1_data);
112
    I2C1_Init(&i2c1_data);
112
    I2C1_Configure_Slave(i2c_slave_addr);
113
    I2C1_Configure_Slave(i2c_slave_addr);
113
 
114
 
114
    // Initialize I2C2 in master mode
115
    // Initialize I2C2 in master mode
115
    I2C2_DATA i2c2_data;
116
    I2C2_DATA i2c2_data;
116
    I2C2_Init(&i2c2_data);
117
    I2C2_Init(&i2c2_data);
117
    I2C2_Configure_Master(I2C_400KHZ);
118
    I2C2_Configure_Master(I2C_1MHZ);
118
 
119
 
119
    // Initialize interrupts
120
    // Initialize interrupts
120
    Interrupt_Init();
121
    Interrupt_Init();
121
    Interrupt_Enable();
122
    Interrupt_Enable();
122
 
123
 
123
    MCP23009_Init(&btns);
124
    MCP23009_Init(&btns);
124
    MCP23009_Query();
125
    MCP23009_Query();
125
 
126
 
126
    LED_VALUES leds = {0x00};
-
 
127
    TLC59116_Init();
127
    TLC59116_Init();
128
//    TLC59116_Write_All(&leds);
128
//    TLC59116_Write_All(&leds);
129
 
129
 
130
    if (op_state == OP_STATE_IDLE) {
130
    if (op_state == OP_STATE_IDLE) {
131
        IO_IOC_Enable();
131
//        IO_IOC_Enable();
132
        Idle_Animation();
132
        Idle_Animation();
133
    } else if (op_state == OP_STATE_ACTIVE) {
133
    } else if (op_state == OP_STATE_ACTIVE) {
134
        while (1) {
134
        while (1) {
135
            __delay_ms(1);
-
 
136
            // Read value of buttons
-
 
137
            MCP23009_Query();
-
 
138
 
-
 
139
            // Check if an I2C message was received
135
            // Check if an I2C message was received
140
            result = I2C1_Get_Status();
136
            result = I2C1_Get_Status();
141
            if (result) {
137
            if (result) {
142
                length = I2C1_Read_Buffer(buffer);
138
                length = I2C1_Read_Buffer(buffer);
143
                if (length == 1 && buffer[0] == CMD_RESET) {
139
                if (length == 1 && buffer[0] == CMD_RESET) {
144
                    Reset_Board(OP_STATE_IDLE);
140
                    Reset_Board(OP_STATE_IDLE);
-
 
141
                } else if (length == 1 && buffer[0] == CMD_ACTIVE) {
-
 
142
                    Reset_Board(OP_STATE_ACTIVE);
145
                } else if (length == 17 && buffer[0] == CMD_SET_LEDS) {
143
                } else if (length == 17 && buffer[0] == CMD_SET_LEDS) {
146
                    for (i = 0; i < 16; i++) {
144
                    for (i = 0; i < 16; i++) {
147
                        leds.w[i] = buffer[i+1];
145
                        leds.w[i] = buffer[i + 1];
148
                    }
146
                    }
149
                    TLC59116_Write_All(&leds);
147
                    TLC59116_Write_All(&leds);
150
                }
148
                }
151
            }
149
            }
-
 
150
 
-
 
151
            I2C2_Master_Restart(MCP23009_ADDR, MCP23009_GPIOA, 1);
-
 
152
            do {
-
 
153
                result = I2C2_Get_Status();
-
 
154
            } while (!result);
-
 
155
            length = I2C2_Read_Buffer(buffer);
-
 
156
            btns.w = buffer[0];
-
 
157
        }
-
 
158
    }
-
 
159
}
-
 
160
 
-
 
161
void Check_I2C(void) {
-
 
162
    uint8_t buffer[32];
-
 
163
    uint8_t result, length, i;
-
 
164
 
-
 
165
    // Check if an I2C message was received
-
 
166
    result = I2C1_Get_Status();
-
 
167
    if (result) {
-
 
168
        length = I2C1_Read_Buffer(buffer);
-
 
169
        if (length == 1 && buffer[0] == CMD_RESET) {
-
 
170
            Reset_Board(OP_STATE_IDLE);
-
 
171
        } else if (length == 1 && buffer[0] == CMD_ACTIVE) {
-
 
172
            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);
152
        }
178
        }
153
    }
179
    }
154
}
180
}
155
 
181
 
156
void Idle_Animation(void) {
182
void Idle_Animation(void) {
Line 166... Line 192...
166
 
192
 
167
    for (i = 0; i < 16; i++) leds.w[i] = 0x00;
193
    for (i = 0; i < 16; i++) leds.w[i] = 0x00;
168
    
194
    
169
    while (1) {
195
    while (1) {
170
 
196
 
-
 
197
        // Check to see if a new message was received
-
 
198
        Check_I2C();
-
 
199
 
171
        // Update the LEDs in the middle section
200
        // Update the LEDs in the middle section
172
        for (i = 0; i < 8; i++) {
201
        for (i = 0; i < 8; i++) {
173
            // Change the LED brightness depending on its direction
202
            // Change the LED brightness depending on its direction
174
            if (led_direction_bar[i] == 1) {
203
            if (led_direction_bar[i] == 1) {
175
                leds.w[i]++;
204
                leds.w[i]++;
Line 238... Line 267...
238
        TLC59116_Write_All(&leds);
267
        TLC59116_Write_All(&leds);
239
 
268
 
240
        // Delay a bit to slow down the animation
269
        // Delay a bit to slow down the animation
241
        __delay_ms(1);
270
        __delay_ms(1);
242
    }
271
    }
243
 
-
 
244
}
272
}
245
273