led_i2c.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. uint8_t led_num = RGBLED_NUM;
  47. i2c_init();
  48. i2c_start(0x84, ERGODOX_EZ_I2C_TIMEOUT);
  49. int i = 0;
  50. # if defined(ERGODOX_LED_30)
  51. // prevent right-half code from trying to bitbang all 30
  52. // so with 30 LEDs, we count from 29 to 15 here, and the
  53. // other half does 0 to 14.
  54. led_num = RGBLED_NUM / 2;
  55. for (i = led_num + led_num - 1; i >= led_num; --i)
  56. # elif defined(ERGODOX_LED_15_MIRROR)
  57. for (i = 0; i < led_num; ++i)
  58. # else // ERGDOX_LED_15 non-mirrored
  59. for (i = led_num - 1; i >= 0; --i)
  60. # endif
  61. {
  62. uint8_t *data = (uint8_t *)(led + i);
  63. i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
  64. i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
  65. i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
  66. #ifdef RGBW
  67. i2c_write(*data++, ERGODOX_EZ_I2C_TIMEOUT);
  68. #endif
  69. }
  70. i2c_stop();
  71. ws2812_setleds(led, RGBLED_NUM);
  72. }
  73. #endif // RGBLIGHT_ENABLE