logo

qmk_firmware

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

leds.c (3423B)


  1. /* Copyright 2018-2021 James Laird-Wah, Islam Sharabash
  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 <string.h>
  18. #include "i2c_master.h"
  19. #include "led_tables.h"
  20. #include "rgb_matrix.h"
  21. #include "wait.h"
  22. #include "raise.h"
  23. #include "wire-protocol-constants.h"
  24. #include "print.h"
  25. // Color order of LEDs is Green, Red, Blue.
  26. typedef struct PACKED {
  27. uint8_t r;
  28. uint8_t g;
  29. uint8_t b;
  30. } raiseRGB;
  31. #define LEDS_PER_HAND 72
  32. #define LED_BANKS 9
  33. #define LEDS_PER_BANK 8
  34. #define LED_BYTES_PER_BANK (sizeof(raiseRGB) * LEDS_PER_BANK)
  35. // shifting << 1 is because drivers/chibios/i2c_master.h expects the address
  36. // shifted.
  37. // 0x58 and 0x59 are the addresses defined in dygma/raise/Hand.h
  38. #define I2C_ADDR_LEFT (0x58 << 1)
  39. #define I2C_ADDR_RIGHT (0x59 << 1)
  40. #define I2C_ADDR(hand) ((hand) ? I2C_ADDR_RIGHT : I2C_ADDR_LEFT)
  41. #define LEFT 0
  42. #define RIGHT 1
  43. static raiseRGB led_pending[2 * LEDS_PER_HAND];
  44. static raiseRGB led_state[2 * LEDS_PER_HAND];
  45. static void set_color(int index, uint8_t r, uint8_t g, uint8_t b) {
  46. int sled = led_map[index];
  47. // The red component of the LED is apparently stronger than the others.
  48. // From: https://github.com/keyboardio/Kaleidoscope/blob/aba8c9ee66bbb5ded15135618d2b2964ee82b2cc/plugins/Kaleidoscope-Hardware-Dygma-Raise/src/kaleidoscope/device/dygma/raise/RaiseSide.cpp#L235-L242
  49. if (r >= 26) {
  50. r -= 26;
  51. }
  52. led_pending[sled].r = r;
  53. led_pending[sled].g = g;
  54. led_pending[sled].b = b;
  55. }
  56. static void set_color_all(uint8_t r, uint8_t g, uint8_t b) {
  57. for (int i = 0; i < RGB_MATRIX_LED_COUNT; i++) set_color(i, r, g, b);
  58. }
  59. static void init(void) {
  60. set_color_all(0,0,0);
  61. }
  62. static void flush(void) {
  63. uint8_t command[1 + LED_BYTES_PER_BANK];
  64. // SUBTLE(ibash) alternate hands when transmitting led data, otherwise the
  65. // mcu in the hand seems to have trouble keeping up with the i2c
  66. // transmission
  67. for (int bank = 0; bank < LED_BANKS; bank++) {
  68. for (int hand = 0; hand < 2; hand++) {
  69. int addr = I2C_ADDR(hand);
  70. int i = (hand * LEDS_PER_HAND) + (bank * LEDS_PER_BANK);
  71. if (memcmp(&led_state[i], &led_pending[i], LED_BYTES_PER_BANK) == 0) {
  72. // No change.
  73. continue;
  74. }
  75. // Update LED state
  76. memcpy(&led_state[i], &led_pending[i], LED_BYTES_PER_BANK);
  77. command[0] = TWI_CMD_LED_BASE + bank;
  78. memcpy(&command[1], &led_pending[i], LED_BYTES_PER_BANK);
  79. i2c_transmit(addr, command, sizeof(command), I2C_TIMEOUT);
  80. // delay to prevent issues with the i2c bus
  81. wait_us(10);
  82. }
  83. }
  84. }
  85. const rgb_matrix_driver_t rgb_matrix_driver = {.init = init, .flush = flush, .set_color = set_color, .set_color_all = set_color_all};