aboutsummaryrefslogtreecommitdiff
path: root/keyboards/infinity60/led_controller.c
diff options
context:
space:
mode:
authorjpetermans <tibcmhhm@gmail.com>2017-05-10 15:53:59 -0700
committerjpetermans <tibcmhhm@gmail.com>2017-05-10 15:53:59 -0700
commit164a6c994b797b66634f7a1c12fcaffd0ffd7506 (patch)
tree067b2b81488a218b87c1d1c73cb1c6e93704b394 /keyboards/infinity60/led_controller.c
parenta144968807f5bf98b2c41cfe153f3df780ed4eaa (diff)
downloadqmk_firmware-164a6c994b797b66634f7a1c12fcaffd0ffd7506.tar.gz
qmk_firmware-164a6c994b797b66634f7a1c12fcaffd0ffd7506.zip
reorder mailbox msg data; formatting
Diffstat (limited to 'keyboards/infinity60/led_controller.c')
-rw-r--r--keyboards/infinity60/led_controller.c138
1 files changed, 68 insertions, 70 deletions
diff --git a/keyboards/infinity60/led_controller.c b/keyboards/infinity60/led_controller.c
index d776b4fcf..a66edb927 100644
--- a/keyboards/infinity60/led_controller.c
+++ b/keyboards/infinity60/led_controller.c
@@ -143,9 +143,6 @@ void is31_init(void) {
143 palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL); 143 palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
144 palSetPad(GPIOB, 16); 144 palSetPad(GPIOB, 16);
145 chThdSleepMilliseconds(10); 145 chThdSleepMilliseconds(10);
146 // software shutdown
147 is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, 0);
148 chThdSleepMilliseconds(10);
149 // software shutdown disable (i.e. turn stuff on) 146 // software shutdown disable (i.e. turn stuff on)
150 is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON); 147 is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
151 chThdSleepMilliseconds(10); 148 chThdSleepMilliseconds(10);
@@ -153,7 +150,7 @@ void is31_init(void) {
153 uint8_t i; 150 uint8_t i;
154 for(i=0; i<8; i++) { 151 for(i=0; i<8; i++) {
155 is31_write_data(i, full_page, 0xB4 + 1); 152 is31_write_data(i, full_page, 0xB4 + 1);
156 chThdSleepMilliseconds(1); 153 chThdSleepMilliseconds(5);
157 } 154 }
158} 155}
159 156
@@ -176,64 +173,65 @@ static THD_FUNCTION(LEDthread, arg) {
176 uint8_t pwm_step_status, page_status; 173 uint8_t pwm_step_status, page_status;
177 174
178 //mailbox variables 175 //mailbox variables
179 uint8_t temp, msg_type, msg_pin, msg_col, msg_led; 176 uint8_t temp, msg_type;
177 uint8_t msg_args[3];
180 msg_t msg; 178 msg_t msg;
181 179
182// initialize persistent variables 180 // initialize persistent variables
183pwm_step_status = 4; //full brightness 181 pwm_step_status = 4; //full brightness
184page_status = 0; //start frame 0 (all off/on) 182 page_status = 0; //start frame 0 (all off/on)
185 183
186 while(true) { 184 while(true) {
187 // wait for a message (asynchronous) 185 // wait for a message (asynchronous)
188 // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't 186 // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
189 // be processed right away 187 // be processed right away
190 chMBFetch(&led_mailbox, &msg, TIME_INFINITE); 188 chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
191 msg_col = (msg >> 24) & 0xFF;//if needed 189 msg_type = msg & 0xFF; //first byte is action information
192 msg_pin = (msg >> 16) & 0XFF;//if needed (e.g. SET_FULL_ROW) 190 msg_args[0] = (msg >> 8) & 0xFF;
193 msg_type = (msg >> 8) & 0xFF; //second byte is msg type 191 msg_args[1] = (msg >> 16) & 0XFF;
194 msg_led = (msg) & 0xFF; //first byte is action information 192 msg_args[2] = (msg >> 24) & 0xFF;
195 193
196 switch (msg_type){ 194 switch (msg_type){
197 case SET_FULL_ROW: 195 case SET_FULL_ROW:
198 //write full byte to pin address, msg_pin = pin #, msg_led = byte to write 196 //write full byte to pin address, msg_args[1] = pin #, msg_args[0] = 8 bits to write
199 //writes only to current page 197 //writes only to currently displayed page
200 write_led_byte(page_status,msg_pin,msg_led); 198 write_led_byte(page_status, msg_args[1], msg_args[0]);
201 break; 199 break;
202 200
203 case OFF_LED: 201 case OFF_LED:
204 //on/off/toggle single led, msg_led = row/col of led 202 //on/off/toggle single led, msg_args[0] = row/col of led
205 set_led_bit(7, control_register_word, msg_led, 0); 203 set_led_bit(msg_args[1], control_register_word, msg_args[0], 0);
206 is31_write_data (7, control_register_word, 0x02); 204 is31_write_data (msg_args[1], control_register_word, 0x02);
207 break; 205 break;
208 case ON_LED: 206 case ON_LED:
209 set_led_bit(7, control_register_word, msg_led, 1); 207 set_led_bit(msg_args[1], control_register_word, msg_args[0], 1);
210 is31_write_data (7, control_register_word, 0x02); 208 is31_write_data (msg_args[1], control_register_word, 0x02);
211 break; 209 break;
212 case TOGGLE_LED: 210 case TOGGLE_LED:
213 set_led_bit(7, control_register_word, msg_led, 2); 211 set_led_bit(msg_args[1], control_register_word, msg_args[0], 2);
214 is31_write_data (7, control_register_word, 0x02); 212 is31_write_data (msg_args[1], control_register_word, 0x02);
215 break; 213 break;
216 214
217 case BLINK_OFF_LED: 215 case BLINK_OFF_LED:
218 //on/off/toggle single led, msg_led = row/col of led 216 //on/off/toggle single led, msg_args[0] = row/col of led
219 set_led_bit(7, control_register_word, msg_led, 4); 217 set_led_bit(msg_args[1], control_register_word, msg_args[0], 4);
220 is31_write_data (7, control_register_word, 0x02); 218 is31_write_data (msg_args[1], control_register_word, 0x02);
221 break; 219 break;
222 case BLINK_ON_LED: 220 case BLINK_ON_LED:
223 set_led_bit(7, control_register_word, msg_led, 5); 221 set_led_bit(msg_args[1], control_register_word, msg_args[0], 5);
224 is31_write_data (7, control_register_word, 0x02); 222 is31_write_data (msg_args[1], control_register_word, 0x02);
225 break; 223 break;
226 case BLINK_TOGGLE_LED: 224 case BLINK_TOGGLE_LED:
227 set_led_bit(7, control_register_word, msg_led, 6); 225 set_led_bit(msg_args[1], control_register_word, msg_args[0], 6);
228 is31_write_data (7, control_register_word, 0x02); 226 is31_write_data (msg_args[1], control_register_word, 0x02);
229 break; 227 break;
230 228
231 case TOGGLE_ALL: 229 case TOGGLE_ALL:
232 //msg_led = unused 230 //turn on/off all leds, msg_args = unused
233 is31_read_register(0, 0x00, &temp); 231 is31_read_register(0, 0x00, &temp);
234 led_control_reg[0] = 0; 232 led_control_reg[0] = 0;
235 233
236 //if first byte is on, then toggle frame 0 off 234 //if first leds are already on, toggle frame 0 off
237 if (temp==0 || page_status > 0) { 235 if (temp==0 || page_status > 0) {
238 __builtin_memcpy(led_control_reg+1, all_on_leds_mask, 0x12); 236 __builtin_memcpy(led_control_reg+1, all_on_leds_mask, 0x12);
239 } else { 237 } else {
@@ -252,27 +250,27 @@ page_status = 0; //start frame 0 (all off/on)
252 break; 250 break;
253 251
254 case TOGGLE_BACKLIGHT: 252 case TOGGLE_BACKLIGHT:
255 //msg_led = on/off 253 //msg_args[0] = on/off
256 254
257 //populate the 9 byte rows to be written to each pin, first byte is register (pin) address 255 //populate 9 byte rows to be written to each pin, first byte is register (pin) address
258 if (msg_led == 1) { 256 if (msg_args[0] == 1) {
259 __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8); 257 __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
260 } else { 258 } else {
261 __builtin_memset(pwm_register_array+1, 0, 8); 259 __builtin_memset(pwm_register_array+1, 0, 8);
262 } 260 }
263 261
264 for(i=0; i<8; i++) { 262 for(i=0; i<8; i++) {
265 //first byte is register address, every 0x10 9 bytes is A-register pwm pins 263 //first byte is register address, every 0x10 9 bytes is A-matrix pwm pins
266 pwm_register_array[0] = 0x24 + (i * 0x10); 264 pwm_register_array[0] = 0x24 + (i * 0x10);
267 is31_write_data(0,pwm_register_array,9); 265 is31_write_data(0,pwm_register_array,9);
268 } 266 }
269 break; 267 break;
270 268
271 case DISPLAY_PAGE: 269 case DISPLAY_PAGE:
272 //msg_led = page to toggle on 270 //msg_args[0] = page to toggle on
273 if (page_status != msg_led) { 271 if (page_status != msg_args[0]) {
274 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_led); 272 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_args[0]);
275 page_status = msg_led; 273 page_status = msg_args[0];
276 274
277 //maintain lock leds 275 //maintain lock leds
278 led_set(host_keyboard_leds()); 276 led_set(host_keyboard_leds());
@@ -280,24 +278,24 @@ page_status = 0; //start frame 0 (all off/on)
280 break; 278 break;
281 279
282 case RESET_PAGE: 280 case RESET_PAGE:
283 //led_msg = page to reset 281 //led_args[0] = page to reset
284 led_control_reg[0] = 0; 282 led_control_reg[0] = 0;
285 __builtin_memset(led_control_reg+1, 0, 0x12); 283 __builtin_memset(led_control_reg+1, 0, 0x12);
286 is31_write_data(msg_led, led_control_reg, 0x13); 284 is31_write_data(msg_args[0], led_control_reg, 0x13);
287 break; 285 break;
288 286
289 case TOGGLE_NUM_LOCK: 287 case TOGGLE_NUM_LOCK:
290 //msg_led = 0 or 1, off/on 288 //msg_args[0] = 0 or 1, off/on
291 set_lock_leds(NUM_LOCK_LED_ADDRESS, msg_led, page_status); 289 set_lock_leds(NUM_LOCK_LED_ADDRESS, msg_args[0], page_status);
292 break; 290 break;
293 case TOGGLE_CAPS_LOCK: 291 case TOGGLE_CAPS_LOCK:
294 //msg_led = 0 or 1, off/on 292 //msg_args[0] = 0 or 1, off/on
295 set_lock_leds(CAPS_LOCK_LED_ADDRESS, msg_led, page_status); 293 set_lock_leds(CAPS_LOCK_LED_ADDRESS, msg_args[0], page_status);
296 break; 294 break;
297 295
298 case STEP_BRIGHTNESS: 296 case STEP_BRIGHTNESS:
299 //led_msg = step pwm up or down 297 //led_args[0] = step up (1) or down (0)
300 switch (msg_led) { 298 switch (msg_args[0]) {
301 case 0: 299 case 0:
302 if (pwm_step_status == 0) { 300 if (pwm_step_status == 0) {
303 pwm_step_status = 4; 301 pwm_step_status = 4;
@@ -305,7 +303,7 @@ page_status = 0; //start frame 0 (all off/on)
305 pwm_step_status--; 303 pwm_step_status--;
306 } 304 }
307 break; 305 break;
308 306
309 case 1: 307 case 1:
310 if (pwm_step_status == 4) { 308 if (pwm_step_status == 4) {
311 pwm_step_status = 0; 309 pwm_step_status = 0;
@@ -316,7 +314,7 @@ page_status = 0; //start frame 0 (all off/on)
316 } 314 }
317 315
318 //populate 8 byte arrays to write on each pin 316 //populate 8 byte arrays to write on each pin
319 //first byte is register address, every 0x10 9 bytes are A-register pwm pins 317 //first byte is register address, every 0x10 9 bytes are A-matrix pwm pins
320 __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8); 318 __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
321 319
322 for(i=0; i<8; i++) { 320 for(i=0; i<8; i++) {
@@ -334,9 +332,9 @@ page_status = 0; //start frame 0 (all off/on)
334 332
335void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint8_t action) { 333void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint8_t action) {
336 //returns 2 bytes: led control register address and byte to write 334 //returns 2 bytes: led control register address and byte to write
337 //0 - bit off, 1 - bit on, 2 - toggle bit 335 //action: 0 - off, 1 - on, 2 - toggle, 4 - blink on, 5 - blink off, 6 - toggle blink
338 336
339 uint8_t control_reg_addr, column_bit, column_byte, bit_temp, blink_on; 337 uint8_t control_reg_addr, column_bit, column_byte, temp, blink_bit;
340 338
341 //check for valid led address 339 //check for valid led address
342 if (led_addr < 0 || led_addr > 87 || led_addr % 10 > 8) { 340 if (led_addr < 0 || led_addr > 87 || led_addr % 10 > 8) {
@@ -344,17 +342,17 @@ void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint
344 } 342 }
345 343
346 //check for blink bit 344 //check for blink bit
347 blink_on = action>>2; 345 blink_bit = action>>2;
348 action &= ~(1<<2); //strip blink bit 346 action &= ~(1<<2); //strip blink bit
349 347
350 //first byte is led control register address 0x00 348 //first byte is led control register address 0x00
351 //msg_led tens column is pin#, ones column is bit position in 8-bit mask 349 //led_addr tens column is pin#, ones column is bit position in 8-bit mask
352 control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-register is every other byte 350 control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-matrix is every other byte
353 control_reg_addr += blink_on == 1 ? 0x12 : 0x00;//shift 12 bytes to blink register 351 control_reg_addr += blink_bit == 1 ? 0x12 : 0x00;//if blink_bit, shift 12 bytes to blink register
354 352
355 is31_read_register(page, control_reg_addr, &bit_temp);//maintain status of leds on this byte 353 is31_read_register(page, control_reg_addr, &temp);//maintain status of leds on this byte
356 column_bit = 1<<(led_addr % 10 - 1); 354 column_bit = 1<<(led_addr % 10 - 1);
357 column_byte = bit_temp; 355 column_byte = temp;
358 356
359 switch(action) { 357 switch(action) {
360 case 0: 358 case 0:
@@ -376,7 +374,7 @@ void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint
376void write_led_byte (uint8_t page, uint8_t row, uint8_t led_byte) { 374void write_led_byte (uint8_t page, uint8_t row, uint8_t led_byte) {
377 uint8_t led_control_word[2] = {0};//register address and on/off byte 375 uint8_t led_control_word[2] = {0};//register address and on/off byte
378 376
379 led_control_word[0] = (row - 1 ) * 0x02;// A-register is every other byte 377 led_control_word[0] = (row - 1 ) * 0x02;// A-matrix is every other byte
380 led_control_word[1] = led_byte; 378 led_control_word[1] = led_byte;
381 is31_write_data(page, led_control_word, 0x02); 379 is31_write_data(page, led_control_word, 0x02);
382} 380}
@@ -389,7 +387,7 @@ void write_led_page (uint8_t page, uint8_t *user_led_array, uint8_t led_count) {
389 __builtin_memset(led_control_register,0,13); 387 __builtin_memset(led_control_register,0,13);
390 388
391 for(i=0;i<led_count;i++){ 389 for(i=0;i<led_count;i++){
392 // 1 byte shift for led register 0x00 address 390 //shift pin by 1 for led register 0x00 address
393 pin = ((user_led_array[i] / 10) % 10 - 1 ) * 2 + 1; 391 pin = ((user_led_array[i] / 10) % 10 - 1 ) * 2 + 1;
394 col = user_led_array[i] % 10 - 1; 392 col = user_led_array[i] % 10 - 1;
395 led_control_register[pin] |= 1<<(col); 393 led_control_register[pin] |= 1<<(col);
@@ -399,13 +397,13 @@ void write_led_page (uint8_t page, uint8_t *user_led_array, uint8_t led_count) {
399} 397}
400 398
401void set_lock_leds(uint8_t led_addr, uint8_t led_action, uint8_t page) { 399void set_lock_leds(uint8_t led_addr, uint8_t led_action, uint8_t page) {
402 uint8_t lock_temp; 400 uint8_t temp;
403 uint8_t led_control_word[2] = {0}; 401 uint8_t led_control_word[2] = {0};
404 402
405 //blink if all leds are on 403 //blink if all leds are on
406 if (page == 0) { 404 if (page == 0) {
407 is31_read_register(0, 0x00, &lock_temp); 405 is31_read_register(0, 0x00, &temp);
408 if (lock_temp == 0xFF) { 406 if (temp == 0xFF) {
409 led_action |= (1<<2); //set blink bit 407 led_action |= (1<<2); //set blink bit
410 } 408 }
411 } 409 }