leds.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "quantum.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. __attribute__ ((weak))
  44. led_config_t g_led_config = { {
  45. { 27, 26, 20, 19, 12, 11, 4, 3 },
  46. { 28, 25, 21, 18, 13, 10, 5, 2 },
  47. { 29, 24, 22, 17, 14, 9, 6, 1 },
  48. { 30, 31, 23, 16, 15, 8, 7, 0 },
  49. { 60, 59, 52, 51, 44, 43, 37, 36 },
  50. { 61, 58, 53, 50, 45, 42, 38, 35 },
  51. { 62, 57, 54, 49, 46, 41, 39, 34 },
  52. { 63, 56, 55, 48, 47, 40, 32, 33 }
  53. }, {
  54. { 3, 35 }, { 0, 26 }, { 0, 17 }, { 0, 6 }, { 14, 5 }, { 15, 16 }, { 16, 25 }, { 17, 34 },
  55. { 31, 29 }, { 31, 19 }, { 30, 11 }, { 30, 1 }, { 45, 0 }, { 45, 8 }, { 46, 17 }, { 46, 27 },
  56. { 60, 27 }, { 60, 18 }, { 60, 9 }, { 60, 0 }, { 74, 2 }, { 74, 11 }, { 75, 20 }, { 74, 28 },
  57. { 89, 30 }, { 89, 19 }, { 89, 7 }, { 70, 38 }, { 82, 41 }, { 93, 45 }, { 104, 50 }, { 74, 64 },
  58. { 149, 64 }, { 119, 50 }, { 130, 45 }, { 141, 41 }, { 153, 38 }, { 134, 7 }, { 134, 19 }, { 134, 30 },
  59. { 149, 28 }, { 148, 20 }, { 149, 11 }, { 149, 2 }, { 163, 0 }, { 163, 9 }, { 163, 18 }, { 163, 27 },
  60. { 177, 27 }, { 177, 17 }, { 178, 8 }, { 178, 0 }, { 193, 1 }, { 193, 11 }, { 192, 19 }, { 192, 29 },
  61. { 206, 34 }, { 207, 25 }, { 208, 16 }, { 209, 5 }, { 224, 6 }, { 223, 17 }, { 223, 26 }, { 220, 35 }
  62. }, {
  63. 4, 4, 4, 4, 4, 4, 4, 4,
  64. 4, 4, 4, 4, 4, 4, 4, 4,
  65. 4, 4, 4, 4, 4, 4, 4, 4,
  66. 4, 4, 4, 1, 1, 1, 1, 1,
  67. 1, 1, 1, 1, 1, 4, 4, 4,
  68. 4, 4, 4, 4, 4, 4, 4, 4,
  69. 4, 4, 4, 4, 4, 4, 4, 4,
  70. 4, 4, 4, 4, 4, 4, 4, 4
  71. } };
  72. static struct {
  73. uint8_t b;
  74. uint8_t g;
  75. uint8_t r;
  76. } __attribute__((packed)) led_state[64];
  77. static void set_color(int index, uint8_t r, uint8_t g, uint8_t b) {
  78. led_state[index].r = r;
  79. led_state[index].g = g;
  80. led_state[index].b = b;
  81. }
  82. static void set_color_all(uint8_t r, uint8_t g, uint8_t b) {
  83. for (int i=0; i<RGB_MATRIX_LED_COUNT; i++)
  84. set_color(i, r, g, b);
  85. }
  86. static void init(void) {
  87. // Enable high current pathway to LEDs - this does violate the USB spec though! (1.6 amps...)
  88. DDRE |= _BV(6);
  89. PORTE &= ~_BV(6);
  90. // Overcurrent check input
  91. DDRB &= ~_BV(4);
  92. PORTB &= ~_BV(4);
  93. }
  94. static void flush(void) {
  95. uint8_t *bank_data = (uint8_t*)&led_state[0];
  96. uint8_t command[1 + 8*3];
  97. for (int hand=0; hand<2; hand++) {
  98. int addr = I2C_ADDR(hand);
  99. for (int bank=0; bank<4; bank++) {
  100. command[0] = TWI_CMD_LED_BASE + bank;
  101. memcpy(&command[1], bank_data, 8*3);
  102. i2c_transmit(addr, command, sizeof(command), I2C_TIMEOUT);
  103. _delay_us(100);
  104. bank_data += 8*3;
  105. }
  106. }
  107. }
  108. const rgb_matrix_driver_t rgb_matrix_driver = {
  109. .init = init,
  110. .flush = flush,
  111. .set_color = set_color,
  112. .set_color_all = set_color_all
  113. };
  114. #endif
  115. /* vim: set ts=2 sw=2 et: */