Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.

rgb.c 2.4KB

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