leds.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright 2018 James Laird-Wah
  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 <quantum.h>
  17. #include <i2c_master.h>
  18. #include <led_tables.h>
  19. #include "model01.h"
  20. #define I2C_TIMEOUT 1000
  21. #define LINCOR(i) pgm_read_byte(&CIE1931_CURVE[i])
  22. int set_all_leds_to_raw(uint8_t r, uint8_t g, uint8_t b) {
  23. uint8_t buf[] = {
  24. TWI_CMD_LED_SET_ALL_TO,
  25. b, g, r
  26. };
  27. int ret = 0;
  28. ret |= i2c_transmit(I2C_ADDR(LEFT), buf, sizeof(buf), I2C_TIMEOUT);
  29. ret |= i2c_transmit(I2C_ADDR(RIGHT), buf, sizeof(buf), I2C_TIMEOUT);
  30. _delay_us(10);
  31. return ret;
  32. }
  33. int set_all_leds_to(uint8_t r, uint8_t g, uint8_t b) {
  34. return set_all_leds_to_raw(LINCOR(r), LINCOR(g), LINCOR(b));
  35. }
  36. int set_led_to_raw(uint8_t led, uint8_t r, uint8_t g, uint8_t b) {
  37. uint8_t buf[] = {
  38. TWI_CMD_LED_SET_ONE_TO,
  39. led & 0x1f,
  40. b, g, r
  41. };
  42. int hand = (led >= 32) ? RIGHT : LEFT;
  43. int ret = i2c_transmit(I2C_ADDR(hand), buf, sizeof(buf), I2C_TIMEOUT);
  44. _delay_us(10);
  45. return ret;
  46. }
  47. int set_led_to(uint8_t led, uint8_t r, uint8_t g, uint8_t b) {
  48. return set_led_to_raw(led, LINCOR(r), LINCOR(g), LINCOR(b));
  49. }
  50. /* vim: set ts=2 sw=2 et: */