logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

leds.c (2628B)


  1. /* Copyright 2018 James Laird-Wah
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "leds.h"
  17. #include "i2c_master.h"
  18. #include "led_tables.h"
  19. #include "rgb_matrix.h"
  20. #include <string.h>
  21. #include "model01.h"
  22. #define I2C_TIMEOUT 1000
  23. void set_all_leds_to(uint8_t r, uint8_t g, uint8_t b) {
  24. uint8_t buf[] = {
  25. TWI_CMD_LED_SET_ALL_TO,
  26. b, g, r
  27. };
  28. i2c_transmit(I2C_ADDR(LEFT), buf, sizeof(buf), I2C_TIMEOUT);
  29. i2c_transmit(I2C_ADDR(RIGHT), buf, sizeof(buf), I2C_TIMEOUT);
  30. _delay_us(10);
  31. }
  32. void set_led_to(int led, uint8_t r, uint8_t g, uint8_t b) {
  33. uint8_t buf[] = {
  34. TWI_CMD_LED_SET_ONE_TO,
  35. led & 0x1f,
  36. b, g, r
  37. };
  38. int hand = (led >= 32) ? RIGHT : LEFT;
  39. i2c_transmit(I2C_ADDR(hand), buf, sizeof(buf), I2C_TIMEOUT);
  40. _delay_us(10);
  41. }
  42. #ifdef RGB_MATRIX_ENABLE
  43. static struct {
  44. uint8_t b;
  45. uint8_t g;
  46. uint8_t r;
  47. } __attribute__((packed)) led_state[64];
  48. static void set_color(int index, uint8_t r, uint8_t g, uint8_t b) {
  49. led_state[index].r = r;
  50. led_state[index].g = g;
  51. led_state[index].b = b;
  52. }
  53. static void set_color_all(uint8_t r, uint8_t g, uint8_t b) {
  54. for (int i=0; i<RGB_MATRIX_LED_COUNT; i++)
  55. set_color(i, r, g, b);
  56. }
  57. static void init(void) {
  58. // Enable high current pathway to LEDs - this does violate the USB spec though! (1.6 amps...)
  59. gpio_set_pin_output(E6);
  60. gpio_write_pin_low(E6);
  61. // Overcurrent check input
  62. gpio_set_pin_input(B4);
  63. }
  64. static void flush(void) {
  65. uint8_t *bank_data = (uint8_t*)&led_state[0];
  66. uint8_t command[1 + 8*3];
  67. for (int hand=0; hand<2; hand++) {
  68. int addr = I2C_ADDR(hand);
  69. for (int bank=0; bank<4; bank++) {
  70. command[0] = TWI_CMD_LED_BASE + bank;
  71. memcpy(&command[1], bank_data, 8*3);
  72. i2c_transmit(addr, command, sizeof(command), I2C_TIMEOUT);
  73. _delay_us(100);
  74. bank_data += 8*3;
  75. }
  76. }
  77. }
  78. const rgb_matrix_driver_t rgb_matrix_driver = {
  79. .init = init,
  80. .flush = flush,
  81. .set_color = set_color,
  82. .set_color_all = set_color_all
  83. };
  84. #endif
  85. /* vim: set ts=2 sw=2 et: */