(WIP) RGB controller on Arduino w/ sync to main PC system
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.

breathing.cpp 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <string.h>
  2. #include "breathing.h"
  3. #include "eeprom.h"
  4. // ==== The Breathing Effect ====
  5. #define BREATHING_CYCLE 30
  6. #define HALF_BREATHING_CYCLE (BREATHING_CYCLE / 2)
  7. #define WRAP_OVERFLOW(x) \
  8. if (x < 0) { \
  9. x = 0; \
  10. } else if (x > 255) { \
  11. x = 255; \
  12. }
  13. void BreathingEffect::reset() {
  14. sign = -1;
  15. breathingCounter = 0;
  16. paletteIndex = 0;
  17. fullPalette = false;
  18. this->onParamUpdate();
  19. }
  20. // Called when parameter is changed via command line or doing a full reset
  21. // DO NOT reset the full animation status here (e.g. the counter)
  22. void BreathingEffect::onParamUpdate() {
  23. startColor = *getPaletteColor(paletteIndex);
  24. this->updateIncrement();
  25. // Keep the current progress while resetting to new color
  26. if (breathingCounter < HALF_BREATHING_CYCLE) {
  27. breathingColor = startColor;
  28. this->stepColor(breathingCounter);
  29. } else {
  30. breathingColor = CRGB(0, 0, 0);
  31. this->stepColor(breathingCounter - HALF_BREATHING_CYCLE);
  32. }
  33. }
  34. void BreathingEffect::updateIncrement() {
  35. breathingIncrement = CRGB(startColor.r / HALF_BREATHING_CYCLE,
  36. startColor.g / HALF_BREATHING_CYCLE,
  37. startColor.b / HALF_BREATHING_CYCLE);
  38. }
  39. void BreathingEffect::handleCommand(char *cmdBuf, int len) {
  40. if (strncmp("COLOR ", cmdBuf, 6) == 0) {
  41. // COLOR <index_in_palette> - Change breathing color
  42. paletteIndex = cmdBuf[6];
  43. this->onParamUpdate();
  44. } else if (strncmp("FULLPALETTE ", cmdBuf, 12) == 0) {
  45. // FULLPALETTE <0|1> - enable or disable full palette breathing effect
  46. // i.e. the breathing color loops through all palette colors
  47. fullPalette = cmdBuf[12];
  48. this->onParamUpdate();
  49. }
  50. }
  51. void BreathingEffect::writeToEEPROM() {
  52. EEPROM.update(EEPROM_BREATHING_COLOR, paletteIndex);
  53. EEPROM.update(EEPROM_BREATHING_FULLPALETTE, fullPalette);
  54. }
  55. void BreathingEffect::loadFromEEPROM() {
  56. paletteIndex = EEPROM.read(EEPROM_BREATHING_COLOR);
  57. fullPalette = EEPROM.read(EEPROM_BREATHING_FULLPALETTE);
  58. this->onParamUpdate();
  59. }
  60. void BreathingEffect::onUpdate() {
  61. if (breathingCounter == HALF_BREATHING_CYCLE) {
  62. sign = -sign;
  63. if (fullPalette) {
  64. startColor = *nextPaletteColor(&paletteIndex);
  65. this->updateIncrement();
  66. }
  67. } else if (breathingCounter == BREATHING_CYCLE) {
  68. sign = -sign;
  69. breathingCounter = 0;
  70. }
  71. breathingCounter++;
  72. this->stepColor(1);
  73. }
  74. void BreathingEffect::stepColor(unsigned int step) {
  75. int newR = (int)breathingColor.r + step * sign * breathingIncrement.r;
  76. int newG = (int)breathingColor.g + step * sign * breathingIncrement.g;
  77. int newB = (int)breathingColor.b + step * sign * breathingIncrement.b;
  78. WRAP_OVERFLOW(newR);
  79. WRAP_OVERFLOW(newG);
  80. WRAP_OVERFLOW(newB);
  81. breathingColor = CRGB(newR, newG, newB);
  82. for (int i = 0; i < NUM_LEDS; i++) {
  83. leds[i] = CRGB(breathingColor.r, breathingColor.g, breathingColor.b);
  84. }
  85. }