led_i2c.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /*
  28. * Forward declare internal functions
  29. *
  30. * The functions take a byte-array and send to the data output as WS2812 bitstream.
  31. * The length is the number of bytes to send - three per LED.
  32. */
  33. void ws2812_sendarray(uint8_t *array, uint16_t length);
  34. void ws2812_sendarray_mask(uint8_t *array, uint16_t length, uint8_t pinmask);
  35. void rgblight_set(void) {
  36. if (!rgblight_config.enable) {
  37. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  38. led[i].r = 0;
  39. led[i].g = 0;
  40. led[i].b = 0;
  41. #ifdef RGBW
  42. led[i].w = 0;
  43. #endif
  44. }
  45. }
  46. #ifdef RGBW
  47. else {
  48. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  49. convert_rgb_to_rgbw(&led[i]);
  50. }
  51. }
  52. #endif
  53. uint8_t led_num = RGBLED_NUM;
  54. i2c_init();
  55. i2c_start(0x84, ERGODOX_EZ_I2C_TIMEOUT);
  56. int i = 0;
  57. # if defined(ERGODOX_LED_30)
  58. // prevent right-half code from trying to bitbang all 30
  59. // so with 30 LEDs, we count from 29 to 15 here, and the
  60. // other half does 0 to 14.
  61. led_num = RGBLED_NUM / 2;
  62. for (i = led_num + led_num - 1; i >= led_num; --i)
  63. # elif defined(ERGODOX_LED_15_MIRROR)
  64. for (i = 0; i < led_num; ++i)
  65. # else // ERGDOX_LED_15 non-mirrored
  66. for (i = led_num - 1; i >= 0; --i)
  67. # endif
  68. {
  69. uint8_t *data = (uint8_t *)(led + i);
  70. i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
  71. i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
  72. i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
  73. #ifdef RGBW
  74. i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
  75. #endif
  76. }
  77. i2c_stop();
  78. ws2812_setleds(led, RGBLED_NUM);
  79. }
  80. #endif // RGBLIGHT_ENABLE