led_i2c.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * light weight WS2812 lib V2.0b
  3. *
  4. * Controls WS2811/WS2812/WS2812B RGB-LEDs
  5. * Author: Tim (cpldcpu@gmail.com)
  6. *
  7. * Jan 18th, 2014 v2.0b Initial Version
  8. * Nov 29th, 2015 v2.3 Added SK6812RGBW support
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #ifdef RGBLIGHT_ENABLE
  24. # include "ws2812.c"
  25. # include "ergodox_ez.h"
  26. extern rgblight_config_t rgblight_config;
  27. void rgblight_set(void) {
  28. if (!rgblight_config.enable) {
  29. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  30. led[i].r = 0;
  31. led[i].g = 0;
  32. led[i].b = 0;
  33. #ifdef RGBW
  34. led[i].w = 0;
  35. #endif
  36. }
  37. }
  38. #ifdef RGBW
  39. else {
  40. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  41. convert_rgb_to_rgbw(&led[i]);
  42. }
  43. }
  44. #endif
  45. uint8_t led_num = RGBLED_NUM;
  46. i2c_init();
  47. i2c_start(0x84, ERGODOX_EZ_I2C_TIMEOUT);
  48. int i = 0;
  49. # if defined(ERGODOX_LED_30)
  50. // prevent right-half code from trying to bitbang all 30
  51. // so with 30 LEDs, we count from 29 to 15 here, and the
  52. // other half does 0 to 14.
  53. led_num = RGBLED_NUM / 2;
  54. for (i = led_num + led_num - 1; i >= led_num; --i)
  55. # elif defined(ERGODOX_LED_15_MIRROR)
  56. for (i = 0; i < led_num; ++i)
  57. # else // ERGDOX_LED_15 non-mirrored
  58. for (i = led_num - 1; i >= 0; --i)
  59. # endif
  60. {
  61. uint8_t *data = (uint8_t *)(led + i);
  62. i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
  63. i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
  64. i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
  65. #ifdef RGBW
  66. i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
  67. #endif
  68. }
  69. i2c_stop();
  70. ws2812_setleds(led, RGBLED_NUM);
  71. }
  72. #endif // RGBLIGHT_ENABLE