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.

rgb.c 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright 2014 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/eeprom.h>
  15. #include "yc059.h"
  16. #include "rgb.h"
  17. static rgb_config_t rgb_config;
  18. void rgb_init(void)
  19. {
  20. DDRE |= (1<<PE2);
  21. yc059_init();
  22. rgb_read_config();
  23. if (rgb_config.raw == RGB_UNCONFIGURED) {
  24. rgb_config.enable = 0;
  25. rgb_config.mode = RGB_STATIC;
  26. rgb_config.id = RGB_STATIC_WHITE;
  27. rgb_write_config();
  28. }
  29. yc059_send(rgb_config.enable ? YC059_ON : YC059_OFF);
  30. rgb_resume();
  31. PORTE |= (1<<PE2);
  32. }
  33. void rgb_resume(void)
  34. {
  35. yc059_send(rgb_to_yc059(rgb_config.mode, rgb_config.id));
  36. }
  37. void rgb_read_config(void)
  38. {
  39. rgb_config.raw = eeprom_read_byte(EECONFIG_RGB);
  40. }
  41. void rgb_write_config(void)
  42. {
  43. eeprom_write_byte(EECONFIG_RGB, rgb_config.raw);
  44. }
  45. void rgb_on(void)
  46. {
  47. rgb_config.enable = 1;
  48. PORTE &= ~(1<<PE2);
  49. yc059_send(YC059_ON);
  50. rgb_resume();
  51. PORTE |= (1<<PE2);
  52. rgb_write_config();
  53. }
  54. void rgb_off(void)
  55. {
  56. rgb_config.enable = 0;
  57. yc059_send(YC059_OFF);
  58. rgb_write_config();
  59. }
  60. void rgb_toggle(void)
  61. {
  62. if (rgb_config.enable) {
  63. rgb_off();
  64. }
  65. else {
  66. rgb_on();
  67. }
  68. }
  69. void rgb_increase(void)
  70. {
  71. yc059_send(YC059_INCREASE);
  72. }
  73. void rgb_decrease(void)
  74. {
  75. yc059_send(YC059_DECREASE);
  76. }
  77. void rgb_set(uint8_t mode, uint8_t id)
  78. {
  79. if (rgb_config.enable) {
  80. rgb_config.mode = mode;
  81. rgb_config.id = id;
  82. rgb_resume();
  83. rgb_write_config();
  84. }
  85. }
  86. uint8_t rgb_to_yc059(uint8_t mode, uint8_t id)
  87. {
  88. switch (mode) {
  89. case RGB_STATIC:
  90. if (id == RGB_STATIC_WHITE) {
  91. return YC059_STATIC_WHITE;
  92. }
  93. else {
  94. return YC059_STATIC_RED + (id / 3) * 4 + id % 3;
  95. }
  96. break;
  97. case RGB_ANIMATE:
  98. return YC059_FLASH + id * 4;
  99. break;
  100. default:
  101. return YC059_OFF;
  102. break;
  103. }
  104. }