Subversion Repositories Code-Repo

Rev

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

Rev 260 Rev 270
Line 72... Line 72...
72
    ret |= I2C_ADDR_0_LAT;
72
    ret |= I2C_ADDR_0_LAT;
73
    
73
    
74
    return ret;
74
    return ret;
75
}
75
}
76
 
76
 
77
// Function to read button status into a data structure
-
 
78
void Pins_Read(BTN_STATUS *btns) {
-
 
79
    
-
 
80
}
-
 
81
 
-
 
82
// Function to write led values from the data structure
-
 
83
void Leds_Write(LED_STATUS *leds) {
-
 
84
    
-
 
85
}
-
 
86
 
-
 
87
int main(void) {
77
int main(void) {
88
    uint8_t buffer[32];
78
    uint8_t buffer[32];
89
    uint8_t result, length;
79
    uint8_t result, length;
90
    uint8_t i2c_slave_addr;
80
    uint8_t i2c_slave_addr;
91
 
81
 
Line 116... Line 106...
116
    // Initialize interrupts
106
    // Initialize interrupts
117
    Interrupt_Init();
107
    Interrupt_Init();
118
    Interrupt_Enable();
108
    Interrupt_Enable();
119
 
109
 
120
    TLC59116_Init();
110
    TLC59116_Init();
-
 
111
    MCP23009_Init();
121
 
112
 
-
 
113
    BTN_STATUS btns;
122
    uint8_t leds[16] = {0x00, 0x00, 0x00, 0x00,
114
    LED_VALUES leds = {0x00, 0x00, 0x00, 0x00,
123
                        0x00, 0x00, 0x00, 0x00,
115
                       0x00, 0x00, 0x00, 0x00,
124
                        0x10, 0x10, 0x10, 0x10,
116
                       0x00, 0x00, 0x00, 0x00,
125
                        0x10, 0x10, 0x10, 0x10};
117
                       0x00, 0x00, 0x00, 0x00};
126
    TLC59116_Write_All(leds);
-
 
127
 
118
 
128
    MCP23009_Init();
119
    TLC59116_Write_All(&leds);
129
 
120
 
-
 
121
    Idle_Animation();
-
 
122
 
130
    // Check for received data over I2C
123
    // Check for received data over I2C1
131
    while (1) {
124
    while (1) {
132
        uint8_t btn_value = MCP23009_Query();
125
        MCP23009_Query(&btns);
133
        uint8_t i;
126
        uint8_t i;
134
        for (i = 0; i < 8; i++) {
127
        for (i = 0; i < 8; i++) {
135
            if ((btn_value >> i) & 0x1) {
128
            if ((btns.w >> i) & 0x1) {
136
                leds[i] = 0x00;
129
                leds.w[i] = 0x00;
137
            } else {
130
            } else {
138
                leds[i] = 0x10;
131
                leds.w[i] = 0x20;
-
 
132
            }
-
 
133
        }
-
 
134
        TLC59116_Write_All(&leds);
-
 
135
    }
-
 
136
}
-
 
137
 
-
 
138
void Idle_Animation(void) {
-
 
139
    LED_VALUES leds = {0};
-
 
140
    uint8_t led_direction_bar[8] = {1,0,0,0,0,0,0,0};
-
 
141
    uint8_t led_direction_dir[8] = {1,0,0,0};
-
 
142
    uint8_t led_direction_ind[8] = {1,0,0,0};
-
 
143
    uint8_t led_8_high_thres = 0x80;    // Max brightness of the middle section
-
 
144
    uint8_t led_8_next_thresh = 0x40;   // Threshold to start the next LED
-
 
145
    uint8_t led_4_high_thres = 0x80;    // Max brightness of the side sections
-
 
146
    uint8_t led_4_next_thresh = 0x74;   // Threshold to start the next LED
-
 
147
    uint8_t i, next_led;
-
 
148
 
-
 
149
    for (i = 0; i < 16; i++) leds.w[i] = 0x00;
-
 
150
    
-
 
151
    while (1) {
-
 
152
 
-
 
153
        // Update the LEDs in the middle section
-
 
154
        for (i = 0; i < 8; i++) {
-
 
155
            // Change the LED brightness depending on its direction
-
 
156
            if (led_direction_bar[i] == 1) {
-
 
157
                leds.w[i]++;
-
 
158
            } else if (led_direction_bar[i] == 2) {
-
 
159
                leds.w[i]--;
-
 
160
            }
-
 
161
 
-
 
162
            // Change direction if peak brightness is reached
-
 
163
            // When the brightness reaches a middle threshold, start
-
 
164
            //   increasing the brightness of the next LED
-
 
165
            if (led_direction_bar[i] == 1 && leds.w[i] == led_8_high_thres) {
-
 
166
                led_direction_bar[i] = 2;
-
 
167
            } else if (led_direction_bar[i] == 1 && leds.w[i] == led_8_next_thresh) {
-
 
168
                next_led = (i == 7) ? 0 : i + 1;
-
 
169
                led_direction_bar[next_led] = 1;
-
 
170
            } else if (led_direction_bar[i] == 2 && leds.w[i] == 0x00) {
-
 
171
                led_direction_bar[i] = 0;
-
 
172
            }
-
 
173
        }
-
 
174
 
-
 
175
        // Update the LEDs in the right section
-
 
176
        for (i = 0; i < 4; i++) {
-
 
177
            // Change the LED brightness depending on its direction
-
 
178
            if (led_direction_dir[i] == 1) {
-
 
179
                leds.w[i+8]++;
-
 
180
            } else if (led_direction_dir[i] == 2) {
-
 
181
                leds.w[i+8]--;
-
 
182
            }
-
 
183
 
-
 
184
            // Change direction if peak brightness is reached
-
 
185
            // When the brightness reaches a middle threshold, start
-
 
186
            //   increasing the brightness of the next LED
-
 
187
            if (led_direction_dir[i] == 1 && leds.w[i+8] == led_4_high_thres) {
-
 
188
                led_direction_dir[i] = 2;
-
 
189
            } else if (led_direction_dir[i] == 1 && leds.w[i+8] == led_4_next_thresh) {
-
 
190
                next_led = (i == 3) ? 0 : i + 1;
-
 
191
                led_direction_dir[next_led] = 1;
-
 
192
            } else if (led_direction_dir[i] == 2 && leds.w[i+8] == 0x00) {
-
 
193
                led_direction_dir[i] = 0;
-
 
194
            }
-
 
195
        }
-
 
196
 
-
 
197
        // Update the LEDs in the left section
-
 
198
        for (i = 0; i < 4; i++) {
-
 
199
            // Change the LED brightness depending on its direction
-
 
200
            if (led_direction_ind[i] == 1) {
-
 
201
                leds.w[i+12]++;
-
 
202
            } else if (led_direction_ind[i] == 2) {
-
 
203
                leds.w[i+12]--;
-
 
204
            }
-
 
205
 
-
 
206
            // Change direction if peak brightness is reached
-
 
207
            // When the brightness reaches a middle threshold, start
-
 
208
            //   increasing the brightness of the next LED
-
 
209
            if (led_direction_ind[i] == 1 && leds.w[i+12] == led_4_high_thres) {
-
 
210
                led_direction_ind[i] = 2;
-
 
211
            } else if (led_direction_ind[i] == 1 && leds.w[i+12] == led_4_next_thresh) {
-
 
212
                next_led = (i == 3) ? 0 : i + 1;
-
 
213
                led_direction_ind[next_led] = 1;
-
 
214
            } else if (led_direction_ind[i] == 2 && leds.w[i+12] == 0x00) {
-
 
215
                led_direction_ind[i] = 0;
139
            }
216
            }
140
        }
217
        }
141
        TLC59116_Write_All(leds);
-
 
142
 
218
 
-
 
219
        // Write the LED values to the controller
-
 
220
        TLC59116_Write_All(&leds);
143
 
221
 
-
 
222
        // Delay a bit to slow down the animation
-
 
223
        __delay_ms(1);
144
    }
224
    }
145
}
-
 
146
 
225
 
-
 
226
}
147
227