You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.4 KiB

  1. #include "keyboard.h"
  2. #include "ws2812.h"
  3. #include "rgbsps.h"
  4. rgb_led_t led[RGBSPS_NUM];
  5. void keyboard_pre_init_kb(void) {
  6. ws2812_init();
  7. keyboard_pre_init_user();
  8. }
  9. void rgbsps_set(uint8_t index, uint8_t r, uint8_t g, uint8_t b) {
  10. led[index].r = r;
  11. led[index].g = g;
  12. led[index].b = b;
  13. }
  14. void rgbsps_setall(uint8_t r, uint8_t g, uint8_t b) {
  15. for (uint16_t i = 0; i < RGBSPS_NUM; i++) {
  16. rgbsps_set(i, r, g, b);
  17. }
  18. }
  19. void rgbsps_turnoff(void) {
  20. rgbsps_setall(0, 0, 0);
  21. }
  22. void rgbsps_send(void) {
  23. ws2812_setleds(led, RGBSPS_NUM);
  24. }
  25. void rgbsps_sethsv(uint8_t index, uint16_t hue, uint8_t sat, uint8_t val) {
  26. uint8_t r = 0, g = 0, b = 0, base, color;
  27. if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
  28. r = val;
  29. g = val;
  30. b = val;
  31. } else {
  32. base = ((255 - sat) * val) >> 8;
  33. color = (val - base) * (hue % 60) / 60;
  34. switch (hue / 60) {
  35. case 0:
  36. r = val;
  37. g = base + color;
  38. b = base;
  39. break;
  40. case 1:
  41. r = val - color;
  42. g = val;
  43. b = base;
  44. break;
  45. case 2:
  46. r = base;
  47. g = val;
  48. b = base + color;
  49. break;
  50. case 3:
  51. r = base;
  52. g = val - color;
  53. b = val;
  54. break;
  55. case 4:
  56. r = base + color;
  57. g = base;
  58. b = val;
  59. break;
  60. case 5:
  61. r = val;
  62. g = base;
  63. b = val - color;
  64. break;
  65. }
  66. }
  67. rgbsps_set(index, r, g, b);
  68. }