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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright 2015 Kai Ryu <[email protected]>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/pgmspace.h>
  15. #include <avr/eeprom.h>
  16. #include "rgb.h"
  17. #include "light_ws2812.h"
  18. static const uint8_t rgb_table[RGB_LEVELS][3] PROGMEM = {
  19. { 0, 0, 0 },
  20. { 255, 0, 0 },
  21. { 255, 127, 0 },
  22. { 255, 255, 0 },
  23. { 0, 255, 0 },
  24. { 0, 255, 255 },
  25. { 0, 0, 255 },
  26. { 143, 0, 255 },
  27. { 255, 255, 255 }
  28. };
  29. static rgb_config_t rgb_config;
  30. void rgb_write_config(void);
  31. void rgb_read_config(void);
  32. void rgb_set(uint8_t level);
  33. void rgb_init(void)
  34. {
  35. rgb_read_config();
  36. if (rgb_config.raw == RGB_UNCONFIGURED) {
  37. rgb_config.enable = 0;
  38. rgb_config.level = RGB_OFF;
  39. rgb_write_config();
  40. }
  41. if (rgb_config.enable) {
  42. rgb_set(rgb_config.level);
  43. }
  44. }
  45. void rgb_read_config(void)
  46. {
  47. rgb_config.raw = eeprom_read_byte(EECONFIG_RGB);
  48. }
  49. void rgb_write_config(void)
  50. {
  51. eeprom_write_byte(EECONFIG_RGB, rgb_config.raw);
  52. }
  53. void rgb_toggle(void)
  54. {
  55. if (rgb_config.enable) {
  56. rgb_off();
  57. }
  58. else {
  59. rgb_on();
  60. }
  61. }
  62. void rgb_on(void)
  63. {
  64. rgb_config.enable = 1;
  65. rgb_set(rgb_config.level);
  66. rgb_write_config();
  67. }
  68. void rgb_off(void)
  69. {
  70. rgb_config.enable = 0;
  71. rgb_set(RGB_OFF);
  72. rgb_write_config();
  73. }
  74. void rgb_decrease(void)
  75. {
  76. if(rgb_config.level > 0) {
  77. rgb_config.level--;
  78. rgb_config.enable = (rgb_config.level != 0);
  79. rgb_write_config();
  80. }
  81. rgb_set(rgb_config.level);
  82. }
  83. void rgb_increase(void)
  84. {
  85. if(rgb_config.level < RGB_LEVELS - 1) {
  86. rgb_config.level++;
  87. rgb_config.enable = 1;
  88. rgb_write_config();
  89. }
  90. rgb_set(rgb_config.level);
  91. }
  92. void rgb_step(void)
  93. {
  94. rgb_config.level++;
  95. if(rgb_config.level >= RGB_LEVELS)
  96. {
  97. rgb_config.level = 0;
  98. }
  99. rgb_config.enable = (rgb_config.level != 0);
  100. rgb_set(rgb_config.level);
  101. }
  102. void rgb_set(uint8_t level)
  103. {
  104. struct cRGB rgb[1];
  105. rgb[0].r = pgm_read_byte(&rgb_table[level][0]);
  106. rgb[0].g = pgm_read_byte(&rgb_table[level][1]);
  107. rgb[0].b = pgm_read_byte(&rgb_table[level][2]);
  108. ws2812_setleds(rgb, 1);
  109. }