Advertisement
alextudiodev

MyDuralight6Effects21

Jan 24th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 31.00 KB | None | 0 0
  1. #include <BluetoothSerial.h>
  2. #include <FastLED.h>
  3. #include <Ticker.h>
  4. #include <Preferences.h>
  5.  
  6. Preferences prefs;
  7. Ticker tickerBluetooth, tickerEffects;
  8.  
  9. #define UPDATES_PER_SECOND 120
  10.  
  11. // Control Defines
  12. #define DEBUG_MESSAGES                                                      // выводить отладочные сообщения
  13.  
  14. #define BTN_PIN                                                          2  // пин-вход для подключения кнопки
  15. #define LED_PIN                                                         15  // пин-выход для подключения ленты
  16.  
  17. #ifdef DEBUG_MESSAGES
  18.   #define SERIAL_BAUDRATE                                           115200  // скорость работы последовательного порта
  19. #endif
  20.  
  21. // defines for LEDs strip
  22. #define NUM_LEDS                                                       300  // кол-во светодиодов    
  23. #define LED_TYPE                                                    WS2813  // тип светодиодов в ленте
  24. #define COLOR_ORDER                                                    GRB  // порядок передачи цветов для чипов светодиодов
  25.  
  26. /* Bluetooth Serial Operation Codes */
  27. // Opcodes Read
  28. #define GET_SOLID_COLOR                                               0x11  // 17
  29. #define GET_EFFECT_CHANGE_TIMEOUT                                     0x12  // 18
  30. #define GET_EFFECT_CHANGE_MODE                                        0x13  // 19
  31. #define GET_CURRENT_EFFECT                                            0x14  // 20
  32. #define GET_BRIGHTNESS                                                0x15  // 21
  33. #define GET_EFFECTS_COUNT                                             0x16  // 22
  34. #define GET_EFFECTS_LIST                                              0x17  // 23
  35.  
  36. // Opcodes Write
  37. #define SET_SOLID_COLOR                                               0x21  // 33
  38. #define SET_EFFECT_CHANGE_TIMEOUT                                     0x22  // 34
  39. #define SET_EFFECT_CHANGE_MODE                                        0x23  // 35
  40. #define SET_CURRENT_EFFECT                                            0x24  // 36
  41. #define SET_BRIGHTNESS                                                0x25  // 37
  42. #define SET_DEFAULTS                                                  0x26  // 38
  43. #define SET_EFFECT_DISABLED                                           0x27  // 39
  44. #define SET_EFFECT_ENABLED                                            0x28  // 40
  45.  
  46. #define TORSE_OFF                                                     0x29  // 41
  47. #define TORSE_ON                                                      0x2A  // 42
  48. /**/
  49.  
  50. enum {
  51.   MODE_SOLID_COLOR          = 0x01,
  52.   MODE_ONE_EFFECT           = 0x02,
  53.   MODE_SEQUENTIAL_FORWARD   = 0x03,
  54.   MODE_SEQUENTIAL_BACKWARD  = 0x04,
  55.   MODE_RANDOM_EFFECT        = 0x05
  56. } modes_t;
  57.  
  58. // system lightning variables
  59. uint8_t  effectChangeTimeout = 0;                                           // 15-3600 with step 15 s
  60. uint8_t  effectChangeMode = 0;                                              // effect change modes: 0 - static color, 1 - sequential, 2 - random
  61. uint8_t  effectsCount = 0;                                                  // count of non empty effects
  62. uint8_t  brightness = 0;                                                    // brightness of LEDS on strip: 10 - 255
  63. bool     isTorseOn = true;                                                  //
  64. CRGB     solidColor = 0xFF55FF55;                                           // color for static mode
  65.  
  66. volatile uint8_t currentEffect = 0;                                         // number of current displaying effect
  67. volatile uint8_t lastEffect    = 255;                                       // last diplayed effect
  68.  
  69. uint8_t effectsStates[] = { // 11 эффектов // states of effects in memory: 0 - empty, 1 - non empty, disabled, 3 - enabled
  70.   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0 - 10, 11 - 15
  71.   3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16, 17 - 20
  72.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  73.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  74. };
  75.  
  76. struct CRGB leds[NUM_LEDS];             // массив светодиодов в подключенной ленте
  77. BluetoothSerial SerialBT;               // объект последовательного Bluetooth-порта
  78. bool isConnected = false;               // показывает, подключено ли устройство по Bluetooth
  79.  
  80. // variables for touch button
  81. //#define NORMAL_OPEN_BTN               // используется нормально открытая кнопка
  82. #define NEXT_TAP_TIMEOUT   400          // время ожидания следующего нажатия кнопки для обработки нескольких нажатий
  83. #define CLICK_TIME         200          // максимальное время удержания кнопки нажатой для распознавания нажатия
  84. #define HOLD_TIME         2000          // максимальное время удержания кнопки нажатой для распознавания короткого удержания
  85.  
  86. uint8_t  numberOfClicks = 0;            // кол-во нажатий на кнопку
  87. uint64_t lastTapTime    = 0;            // время последнего нажатия
  88. uint64_t startHoldTime  = 0;            // время начала удержания
  89. uint64_t endHoldTime    = 0;            // время отпускания кнопки
  90. bool     isClicked      = false;        // кнопка была нажата
  91. bool     isHold         = false;        // кнопка удерживалась
  92.  
  93. /**************** Для эффектов 11-16 ***************************/
  94. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  95. /**************** Для эффектов 0-10 ****************************/
  96. CRGBPalette16                   currentPalette;
  97. TBlendType                      currentBlending;
  98. extern CRGBPalette16            redWhiteBluePalette;
  99. extern const TProgmemPalette16  redWhiteBluePalette_p PROGMEM;
  100. /***************************************************************/
  101.  
  102. void btnCallback() {                    // обработчик прерывания нажатия и отпускания кнопки
  103. /*  #ifdef NORMAL_OPEN_BTN
  104.     if (digitalRead(BTN_PIN))
  105.   #else
  106.     if (!digitalRead(BTN_PIN))
  107.   #endif
  108.        startHoldTime = millis();
  109.   else endHoldTime = millis();*/
  110.  
  111.   if (startHoldTime && endHoldTime) {
  112.     if (endHoldTime - startHoldTime <= CLICK_TIME) {
  113.       isClicked = true;
  114.       if (millis() - lastTapTime > NEXT_TAP_TIMEOUT) {
  115.         numberOfClicks = 1;
  116.         lastTapTime = millis();
  117.       }
  118.       else {
  119.         numberOfClicks++;
  120.         lastTapTime = millis();
  121.       }
  122.     }
  123.     if (endHoldTime - startHoldTime >= HOLD_TIME) isHold = true;
  124.     startHoldTime = 0;
  125.     endHoldTime = 0;
  126.   }
  127.   else if (!startHoldTime && endHoldTime) {
  128.     startHoldTime = 0;
  129.     endHoldTime = 0;
  130.   }
  131. }
  132.  
  133. void printData() {                      // вывод хранимых данных в памяти
  134.   Serial.printf("Effects count:         %d\n",   effectsCount);
  135.   Serial.printf("Current effect:        %d\n",   currentEffect);
  136.   Serial.printf("Brightness:            %d\n",   brightness);
  137.   Serial.printf("Effect change mode:    %d\n",   effectChangeMode);
  138.   Serial.printf("Effect change timeout: %d\n",   effectChangeTimeout);
  139.   Serial.printf("Solid color:           R:0x%02X G:0x%02X B:0x%02X\n", solidColor.r, solidColor.g, solidColor.b);
  140.  
  141.   String buff = "";
  142.   for (uint8_t i = 0; i < sizeof(effectsStates); i++) { buff += effectsStates[i]; buff += "|"; }
  143.   Serial.println("Effects list: " + buff);
  144. }
  145.  
  146. void loadEffectsFromMem() {
  147.   //prefs.putBytes("es", effectsStates, sizeof(effectsStates));
  148.   size_t length = prefs.getBytesLength("es");
  149.   prefs.getBytes("es", effectsStates, length);
  150.   effectsCount = getEffectsCount();
  151. }
  152.  
  153. void loadPropsFromMem() {
  154.   currentEffect       = prefs.getUChar("ce", 0);
  155.   effectChangeMode    = prefs.getUChar("ecm", 3);
  156.   brightness          = prefs.getUChar("br", 128);
  157.   effectChangeTimeout = prefs.getUChar("ect", 1);
  158.   solidColor          = prefs.getInt("color", 0xFFFFFFFF);
  159.   if (effectsStates[currentEffect] != 3) changeEffect();
  160. }
  161.  
  162. uint8_t getActiveEffectsCount() {
  163.   uint8_t address = 0, count = 0;
  164.   while (address < sizeof(effectsStates))
  165.     if (effectsStates[address++] == 3) count++;
  166.   return count;
  167. }
  168.  
  169. uint8_t getEffectsCount() {
  170.   uint8_t count = 0;
  171.   for (uint8_t address = 0; address < sizeof(effectsStates); address++)
  172.     if (effectsStates[address] > 0 && effectsStates[address] < 4) count++;
  173.   return count;
  174. }
  175.  
  176. void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
  177.   if (event == ESP_SPP_SRV_OPEN_EVT) {
  178.     isConnected = true;
  179.     #ifdef DEBUG_MESSAGES
  180.       Serial.println("Client сonnected.");
  181.     #endif
  182.     tickerBluetooth.attach_ms(50, readBluetooth);
  183.     // отправлем все параметры подключенному устройству для синхронизации
  184.     if (isConnected) {
  185.       String buff = "";
  186.       buff += GET_EFFECTS_LIST; buff += "|";
  187.       for (uint8_t i = 0; i < sizeof(effectsStates); i++) { buff += effectsStates[i]; buff += "|"; }
  188.       SerialBT.printf("%d|%d\n", GET_SOLID_COLOR,           (int)0xFF << 24 + (int)solidColor.r << 16 + (int)solidColor.g << 8 + (int)solidColor.b);        
  189.       SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_TIMEOUT, effectChangeTimeout);        
  190.       SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_MODE,    effectChangeMode);        
  191.       SerialBT.printf("%d|%d\n", GET_CURRENT_EFFECT,        currentEffect);      
  192.       SerialBT.printf("%d|%d\n", GET_BRIGHTNESS,            brightness);          
  193.       SerialBT.println(buff);
  194.     }
  195.   }
  196.   else if (event == ESP_SPP_CLOSE_EVT) {
  197.     isConnected = false;
  198.     #ifdef DEBUG_MESSAGES
  199.       Serial.println("Client disconnected.");
  200.     #endif
  201.     tickerBluetooth.detach();
  202.   }
  203. }
  204.  
  205. void changeEffect() {
  206.   if      (effectChangeMode == MODE_SEQUENTIAL_FORWARD)  nextEffect();
  207.   else if (effectChangeMode == MODE_SEQUENTIAL_BACKWARD) previousEffect();
  208.   else if (effectChangeMode == MODE_RANDOM_EFFECT)       randomEffect();
  209. }
  210.  
  211. void nextEffect() {
  212.   currentEffect++;
  213.   while (effectsStates[currentEffect] != 3) {
  214.      currentEffect++;
  215.      if (currentEffect == effectsCount) currentEffect = 0;
  216.   }
  217. }
  218.  
  219. void previousEffect() {
  220.   currentEffect--;
  221.   while (effectsStates[currentEffect] != 3) {
  222.      currentEffect--;
  223.      if (currentEffect == 0xFF) currentEffect = sizeof(effectsStates) - 1;
  224.   }
  225. }
  226.  
  227. void randomEffect() {
  228.   currentEffect = random8(sizeof(effectsStates));
  229.   while (effectsStates[currentEffect] != 3) currentEffect = random8(sizeof(effectsStates));
  230. }
  231.  
  232. void readBluetooth() {
  233.   String  buffer = "";
  234.   uint8_t cmd    = 0xFF;
  235.   int     data   = -1;
  236.   if (isConnected && SerialBT.available()) {
  237.     buffer = SerialBT.readString();
  238.     int splitterIndex = buffer.indexOf('|');
  239.     if (splitterIndex > -1) {
  240.       cmd = (uint8_t)buffer.substring(0, splitterIndex).toInt();
  241.       if ((buffer.length() - 1) > splitterIndex) data = buffer.substring(splitterIndex + 1).toInt();
  242.       #ifdef DEBUG_MESSAGES
  243.         Serial.printf("Command: 0x%02X\n", cmd);
  244.         Serial.printf("Data:    %d\n", data);
  245.       #endif
  246.       switch (cmd) {
  247.         case SET_SOLID_COLOR:
  248.           if (data >= 0xFF000000 && data <= 0xFFFFFFFF) {
  249.             solidColor = data;
  250.             effectChangeMode = MODE_SOLID_COLOR;
  251.             prefs.putInt("color", data);
  252.             prefs.putUChar("ecm", effectChangeMode);
  253.             if (isConnected) SerialBT.printf("%d|%d\n", GET_SOLID_COLOR, (int)0xFF << 24 + (int)solidColor.r << 16 + (int)solidColor.g << 8 + (int)solidColor.b);        
  254.             #ifdef DEBUG_MESSAGES
  255.               Serial.printf("Solid color: R:0x%02X G:0x%02X B:0x%02X\n", solidColor.r, solidColor.g, solidColor.b);
  256.               if (isConnected) SerialBT.printf("Solid color: R:0x%02X G:0x%02X B:0x%02X\n", solidColor.r, solidColor.g, solidColor.b);
  257.             #endif
  258.           }
  259.           else {
  260.             #ifdef DEBUG_MESSAGES
  261.               Serial.println(F("Wrong new solid color"));
  262.               if (isConnected) SerialBT.println(F("Wrong new solid color"));
  263.             #endif
  264.           }
  265.           break;
  266.          
  267.         case SET_EFFECT_CHANGE_TIMEOUT:
  268.           if ((uint8_t)data >= 1 && (uint8_t)data <= 240) {
  269.             effectChangeTimeout = (uint8_t)data;        
  270.             prefs.putUChar("ect", effectChangeTimeout);  
  271.             tickerEffects.detach();
  272.             tickerEffects.attach_ms((uint64_t)effectChangeTimeout * 15000, changeEffect);      
  273.             if (isConnected) SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_TIMEOUT, effectChangeTimeout);      
  274.             #ifdef DEBUG_MESSAGES
  275.               Serial.printf("Effect change timeout: %d s\n", effectChangeTimeout * 15);
  276.               if (isConnected) SerialBT.printf("Effect change timeout: %d s\n", effectChangeTimeout * 15);
  277.             #endif
  278.           }
  279.           else {            
  280.             #ifdef DEBUG_MESSAGES
  281.               Serial.println(F("Wrong new effect change timeout, must be 15-3600 s"));
  282.               if (isConnected) SerialBT.println(F("Wrong new effect change timeout, must be 15-3600 s"));
  283.             #endif
  284.           }
  285.           break;
  286.          
  287.         case SET_EFFECT_CHANGE_MODE:
  288.           if (data >= 1 && data <= 5) {
  289.             effectChangeMode = (uint8_t)data;
  290.             prefs.putUChar("ecm", effectChangeMode);  
  291.             if (isConnected) SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_MODE, effectChangeMode);            
  292.             #ifdef DEBUG_MESSAGES
  293.               Serial.printf("Effect change mode: %d\n", effectChangeMode);
  294.               if (isConnected) SerialBT.printf("Effect change mode: %d\n", effectChangeMode);
  295.             #endif
  296.           }
  297.           else {
  298.             //if (isConnected) SerialBT.write(ACK_FAILTURE);          
  299.             #ifdef DEBUG_MESSAGES
  300.               Serial.println(F("Wrong new effect change mode, must be 1-5"));
  301.               if (isConnected) SerialBT.println(F("Wrong new effect change mode, must be 1-5"));
  302.             #endif
  303.           }
  304.           break;
  305.          
  306.         case SET_CURRENT_EFFECT:
  307.           if ((uint8_t)data >= 0 && (uint8_t)data < sizeof(effectsStates)) {
  308.             currentEffect = (uint8_t)data;
  309.             prefs.putUChar("ce", currentEffect);
  310.             if (isConnected) SerialBT.printf("%d|%d\n", GET_CURRENT_EFFECT, currentEffect);
  311.           }
  312.           else {          
  313.             #ifdef DEBUG_MESSAGES
  314.               Serial.printf("Wrong new current effect, must be 0-%d\n", sizeof(effectsStates) - 1);
  315.               if (isConnected) SerialBT.printf("Wrong new current effect, must be 0-%d\n", sizeof(effectsStates) - 1);
  316.             #endif
  317.           }
  318.           break;
  319.          
  320.         case SET_BRIGHTNESS:
  321.           if (data >= 0 && data <= 255) {
  322.             brightness = constrain((uint8_t)data, 10, 255);
  323.             FastLED.setBrightness(brightness);
  324.             prefs.putUChar("br", brightness);  
  325.             if (isConnected) SerialBT.printf("%d|%d\n", GET_CURRENT_EFFECT, currentEffect);        
  326.             #ifdef DEBUG_MESSAGES
  327.               Serial.printf("Brightness: %d\n", brightness);
  328.               if (isConnected) SerialBT.printf("Brightness: %d\n", brightness);
  329.             #endif
  330.           }
  331.           else {
  332.             #ifdef DEBUG_MESSAGES
  333.               Serial.println(F("Wrong new brigtness, must be 0-255"));
  334.               if (isConnected) SerialBT.println(F("Wrong new brigtness, must be 0-255"));
  335.             #endif
  336.           }
  337.           break;
  338.          
  339.         case SET_DEFAULTS:
  340.           prefs.putUChar("ce", 0);
  341.           prefs.putUChar("ecm", 3);
  342.           prefs.putUChar("br", 128);
  343.           prefs.putUChar("ect", 1);
  344.           prefs.putInt("color", 0xFFFFFFFF);
  345.           prefs.putBytes("es", effectsStates, sizeof(effectsStates));
  346.           Serial.println("Defaults set.");
  347.           // отправлем все параметры подключенному устройству для синхронизации
  348.           if (isConnected) {
  349.             String buff = "";
  350.             buff += GET_EFFECTS_LIST; buff += "|";
  351.             for (uint8_t i = 0; i < sizeof(effectsStates); i++) { buff += effectsStates[i]; buff += "|"; }
  352.             SerialBT.printf("%d|%d\n", GET_SOLID_COLOR,           (int)0xFF << 24 + (int)solidColor.r << 16 + (int)solidColor.g << 8 + (int)solidColor.b);        
  353.             SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_TIMEOUT, effectChangeTimeout);        
  354.             SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_MODE,    effectChangeMode);        
  355.             SerialBT.printf("%d|%d\n", GET_CURRENT_EFFECT,        currentEffect);      
  356.             SerialBT.printf("%d|%d\n", GET_BRIGHTNESS,            brightness);          
  357.             SerialBT.println(buff);
  358.           }
  359.           break;
  360.  
  361.         case SET_EFFECT_DISABLED:
  362.           if ((uint8_t)data < sizeof(effectsStates)) {
  363.             effectsStates[(uint8_t)data] = 1;
  364.             prefs.putBytes("es", effectsStates, sizeof(effectsStates));
  365.             if (currentEffect == (uint8_t)data) {
  366.               if (effectChangeMode == MODE_ONE_EFFECT) nextEffect();
  367.               else if (effectChangeMode == MODE_SEQUENTIAL_FORWARD || effectChangeMode == MODE_SEQUENTIAL_FORWARD || effectChangeMode == MODE_RANDOM_EFFECT) changeEffect();
  368.             }
  369.           }
  370.           else {
  371.             #ifdef DEBUG_MESSAGES
  372.               Serial.printf("Wrong effect number, must be 0-%d\n", sizeof(effectsStates) - 1);
  373.               if (isConnected) SerialBT.printf("Wrong effect number, must be 0-%d\n", sizeof(effectsStates) - 1);
  374.             #endif
  375.           }
  376.           break;
  377.          
  378.         case SET_EFFECT_ENABLED:
  379.           if ((uint8_t)data < sizeof(effectsStates)) {
  380.             effectsStates[(uint8_t)data] = 3;
  381.             prefs.putBytes("es", effectsStates, sizeof(effectsStates));
  382.           }
  383.           else {
  384.             #ifdef DEBUG_MESSAGES
  385.               Serial.printf("Wrong effect number, must be 0-%d\n", sizeof(effectsStates) - 1);
  386.               if (isConnected) SerialBT.printf("Wrong effect number, must be 0-%d\n", sizeof(effectsStates) - 1);
  387.             #endif
  388.           }
  389.           break;
  390.          
  391.         case GET_SOLID_COLOR:
  392.           if (isConnected) SerialBT.printf("%d|%d\n", GET_SOLID_COLOR, (int)0xFF << 24 + (int)solidColor.r << 16 + (int)solidColor.g << 8 + (int)solidColor.b);        
  393.           #ifdef DEBUG_MESSAGES
  394.             Serial.printf("Solid color: R:0x%02X G:0x%02X B:0x%02X\n", solidColor.r, solidColor.g, solidColor.b);
  395.             if (isConnected) SerialBT.printf("Solid color: R:0x%02X G:0x%02X B:0x%02X\n", solidColor.r, solidColor.g, solidColor.b);
  396.           #endif        
  397.           break;
  398.          
  399.         case GET_EFFECT_CHANGE_TIMEOUT:
  400.           if (isConnected) SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_TIMEOUT, effectChangeTimeout);        
  401.           #ifdef DEBUG_MESSAGES
  402.             Serial.printf("Effect change timeout: %d s\n", effectChangeTimeout);
  403.             if (isConnected) SerialBT.printf("Effect change timeout: %d s\n", effectChangeTimeout);
  404.           #endif        
  405.           break;
  406.          
  407.         case GET_EFFECT_CHANGE_MODE:
  408.           if (isConnected) SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_MODE, effectChangeMode);        
  409.           #ifdef DEBUG_MESSAGES
  410.             Serial.printf("Effect change mode: %d\n", effectChangeMode);
  411.             if (isConnected) SerialBT.printf("Effect change mode: %d\n", effectChangeMode);
  412.           #endif        
  413.           break;
  414.          
  415.         case GET_CURRENT_EFFECT:
  416.           if (isConnected) SerialBT.printf("%d|%d\n", GET_CURRENT_EFFECT, currentEffect);      
  417.           #ifdef DEBUG_MESSAGES
  418.             Serial.printf("Current effect: %d\n", currentEffect);
  419.             if (isConnected) SerialBT.printf("Current effect: %d\n", currentEffect);
  420.           #endif        
  421.           break;
  422.          
  423.         case GET_BRIGHTNESS:
  424.           if (isConnected) SerialBT.printf("%d|%d\n", GET_BRIGHTNESS, brightness);        
  425.           #ifdef DEBUG_MESSAGES
  426.             Serial.printf("Brightness: %d\n", brightness);
  427.             if (isConnected) SerialBT.printf("Brightness: %d\n", brightness);
  428.           #endif    
  429.           break;
  430.          
  431.         case GET_EFFECTS_LIST:
  432.           String buff = "";
  433.           buff += GET_EFFECTS_LIST; buff += "|";
  434.           for (uint8_t i = 0; i < effectsCount; i++) { buff += effectsStates[i]; buff += "|"; }
  435.           if (isConnected) SerialBT.println(buff);
  436.           #ifdef DEBUG_MESSAGES
  437.             Serial.println("Effects list: " + buff);
  438.             //if (isConnected) SerialBT.printf("Effects list: " + buffer);
  439.           #endif
  440.           break;
  441.       }
  442.     }
  443.   }
  444. }
  445.  
  446. void setup() {
  447.   #ifdef DEBUG_MESSAGES
  448.     Serial.begin(SERIAL_BAUDRATE);
  449.     uint64_t chipid = ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes).
  450.     Serial.printf("ESP32 Chip ID = 0x%04X%08X\n",(uint16_t)(chipid >> 32), (uint32_t)chipid);
  451.   #endif
  452.  
  453.   prefs.begin("duralight", false);
  454.   loadPropsFromMem();
  455.   loadEffectsFromMem();
  456.  
  457.   #ifdef DEBUG_MESSAGES
  458.     printData();
  459.   #endif
  460.  
  461.   tickerEffects.attach_ms((uint64_t)effectChangeTimeout * 15000, changeEffect);
  462.  
  463.   SerialBT.register_callback(callback);
  464.   if (!SerialBT.begin("Duralight")) {
  465.     #ifdef DEBUG_MESSAGES
  466.       Serial.println(F("An error occurred initializing Bluetooth"));
  467.     #endif
  468.     ESP.restart();
  469.   }
  470.   else {
  471.     #ifdef DEBUG_MESSAGES
  472.       Serial.println(F("Bluetooth initialized."));
  473.       Serial.println(F("The device started, now you can pair it with bluetooth."));
  474.     #endif
  475.    
  476.     LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  477.     LEDS.setBrightness(brightness);
  478.        
  479.     //attachInterrupt(digitalPinToInterrupt(BTN_PIN), btnCallback, CHANGE);
  480.   }
  481. }
  482.  
  483. void loop() {
  484.   if (lastEffect != currentEffect) {
  485.     prefs.putUChar("ce", currentEffect);
  486.     if (isConnected) SerialBT.printf("%d|%d\n", GET_CURRENT_EFFECT, currentEffect);        
  487.     #ifdef DEBUG_MESSAGES
  488.       Serial.printf("Current effect: %d\n", currentEffect);
  489.       if (isConnected) SerialBT.printf("Current effect: %d\n", currentEffect);
  490.     #endif
  491.     lastEffect = currentEffect;
  492.     // перезапуск таймера
  493.     tickerEffects.detach();
  494.     tickerEffects.attach_ms((uint64_t)effectChangeTimeout * 15000, changeEffect);
  495.     // настройки палитры при смене режимов
  496.     switch (currentEffect) {
  497.       case  0: currentPalette = RainbowColors_p;       currentBlending = LINEARBLEND; break; //  0
  498.       case  1: currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND;     break; //  1
  499.       case  2: currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; break; //  2
  500.       case  3: setupPurpleAndGreenPalette();           currentBlending = LINEARBLEND; break; //  3
  501.       case  4: setupTotallyRandomPalette();            currentBlending = LINEARBLEND; break; //  4
  502.       case  5: setupBlackAndWhiteStripedPalette();     currentBlending = NOBLEND;     break; //  5
  503.       case  6: setupBlackAndWhiteStripedPalette();     currentBlending = LINEARBLEND; break; //  6
  504.       case  7: currentPalette = CloudColors_p;         currentBlending = LINEARBLEND; break; //  7
  505.       case  8: currentPalette = PartyColors_p;         currentBlending = LINEARBLEND; break; //  8
  506.       case  9: currentPalette = redWhiteBluePalette_p; currentBlending = NOBLEND;     break; //  9
  507.       case 10: currentPalette = redWhiteBluePalette_p; currentBlending = LINEARBLEND; break; // 10
  508.     }
  509.   }
  510.   if (startHoldTime)
  511.     if (millis() - startHoldTime >= HOLD_TIME) {
  512.       isHold = true;
  513.       startHoldTime = 0;
  514.     }
  515.  
  516.   if (isClicked) {
  517.     #ifdef DEBUG_MESSAGES
  518.       Serial.println("Click");
  519.     #endif
  520.     isClicked = false;
  521.   }
  522.   if (isHold) {
  523.     #ifdef DEBUG_MESSAGES
  524.       Serial.println("Hold");
  525.     #endif
  526.     isHold = false;
  527.   }
  528.  
  529.   if (numberOfClicks > 0 && millis() - lastTapTime > NEXT_TAP_TIMEOUT) {
  530.     #ifdef DEBUG_MESSAGES
  531.       Serial.print(numberOfClicks);
  532.       Serial.println(" clicks");
  533.     #endif
  534.     switch (numberOfClicks) {
  535.       case 1:
  536.         if      (effectChangeMode == MODE_SEQUENTIAL_FORWARD || effectChangeMode == MODE_ONE_EFFECT) nextEffect();
  537.         else if (effectChangeMode == MODE_SEQUENTIAL_BACKWARD) previousEffect();
  538.         else if (effectChangeMode == MODE_RANDOM_EFFECT) randomEffect();
  539.         break;
  540.       case 2:
  541.         if      (effectChangeMode == MODE_SEQUENTIAL_FORWARD || effectChangeMode == MODE_ONE_EFFECT) previousEffect();
  542.         else if (effectChangeMode == MODE_SEQUENTIAL_BACKWARD) nextEffect();
  543.         else if (effectChangeMode == MODE_RANDOM_EFFECT) randomEffect();
  544.         break;
  545.       case 3:
  546.         if      (effectChangeMode == MODE_ONE_EFFECT)           effectChangeMode = MODE_SEQUENTIAL_FORWARD;
  547.         else if (effectChangeMode == MODE_SEQUENTIAL_FORWARD)   effectChangeMode = MODE_SEQUENTIAL_BACKWARD;
  548.         else if (effectChangeMode == MODE_SEQUENTIAL_BACKWARD)  effectChangeMode = MODE_RANDOM_EFFECT;
  549.         else if (effectChangeMode == MODE_RANDOM_EFFECT)        effectChangeMode = MODE_ONE_EFFECT;
  550.         break;
  551.       case 4:
  552.         if      (effectChangeMode == MODE_ONE_EFFECT)           effectChangeMode = MODE_RANDOM_EFFECT;
  553.         else if (effectChangeMode == MODE_SEQUENTIAL_FORWARD)   effectChangeMode = MODE_ONE_EFFECT;
  554.         else if (effectChangeMode == MODE_SEQUENTIAL_BACKWARD)  effectChangeMode = MODE_SEQUENTIAL_FORWARD;
  555.         else if (effectChangeMode == MODE_RANDOM_EFFECT)        effectChangeMode = MODE_SEQUENTIAL_BACKWARD;
  556.         break;
  557.     }
  558.     numberOfClicks = 0;
  559.   }
  560.  
  561.   if (effectChangeMode == MODE_SOLID_COLOR) fill_solid(leds, NUM_LEDS, solidColor);
  562.   else {
  563.     if (currentEffect >= 0 && currentEffect <= 10) {
  564.       static uint8_t startIndex = 0;
  565.       startIndex = startIndex + 1;
  566.       fillLEDsFromPaletteColors(startIndex);
  567.     }
  568.     else if (currentEffect >= 11 && currentEffect <= 16) {
  569.       switch (currentEffect) {
  570.         case 11: rainbow(); break;
  571.         case 12: rainbowWithGlitter(); break;
  572.         case 13: confetti(); break;
  573.         case 14: sinelon(); break;
  574.         case 15: juggle(); break;
  575.         case 16: bpm(); break;
  576.       }
  577.       EVERY_N_MILLISECONDS(20) { gHue++; }
  578.     }
  579.     else if (currentEffect >= 17 && currentEffect <= 20) {
  580.       switch(currentEffect) {
  581.         case 17: rainbow_center(150, 5); break;
  582.         case 18: rainbow_center(200, 5); break;
  583.         case 19: rainbow_center(150, 10); break;
  584.         case 20: rainbow_center(200, 10); break;
  585.       }
  586.     }
  587.   }
  588.   FastLED.show();
  589.   FastLED.delay(1000 / UPDATES_PER_SECOND);
  590. }
  591.  
  592. /***************************** Для эффектов 0-10 ********************************/
  593. void fillLEDsFromPaletteColors(uint8_t colorIndex) {
  594.   uint8_t _brightness = 255;
  595.   for (uint16_t i = 0; i < NUM_LEDS; i++) {
  596.     leds[i] = ColorFromPalette(currentPalette, colorIndex, _brightness, currentBlending);
  597.     colorIndex += 3;
  598.   }
  599. }
  600.  
  601. void setupTotallyRandomPalette() {
  602.   for(uint8_t i = 0; i < 16; i++) currentPalette[i] = CHSV(random8(), 0xFF, random8());
  603. }
  604.  
  605. void setupBlackAndWhiteStripedPalette() {
  606.   fill_solid(currentPalette, 16, CRGB::Black);
  607.   currentPalette[0] = CRGB::White;
  608.   currentPalette[4] = CRGB::White;
  609.   currentPalette[8] = CRGB::White;
  610.   currentPalette[12] = CRGB::White;
  611. }
  612.  
  613. void setupPurpleAndGreenPalette() {
  614.   CRGB purple = CHSV(HUE_PURPLE, 0xFF, 0xFF);
  615.   CRGB green  = CHSV(HUE_GREEN, 0xFF, 0xFF);
  616.   CRGB black  = CRGB::Black;
  617.   currentPalette = CRGBPalette16(green, green, black, black, purple, purple,
  618.     black, black, green, green, black, black, purple, purple, black, black);
  619. }
  620.  
  621. const TProgmemPalette16 redWhiteBluePalette_p PROGMEM = {
  622.   CRGB::Red,  CRGB::Gray, CRGB::Blue,  CRGB::Black,
  623.   CRGB::Red,  CRGB::Gray, CRGB::Blue,  CRGB::Black,
  624.   CRGB::Red,  CRGB::Red,  CRGB::Gray,  CRGB::Gray,
  625.   CRGB::Blue, CRGB::Blue, CRGB::Black, CRGB::Black
  626. };
  627.  
  628. /******************************** Для эффектов 11-16 ****************************/
  629. void rainbow() {
  630.   // FastLED's built-in rainbow generator
  631.   fill_rainbow(leds, NUM_LEDS, gHue, 7);
  632. }
  633.  
  634. void rainbowWithGlitter() {
  635.   // built-in FastLED rainbow, plus some random sparkly glitter
  636.   rainbow();
  637.   addGlitter(80);
  638. }
  639.  
  640. void addGlitter(fract8 chanceOfGlitter) {
  641.   if (random8() < chanceOfGlitter) leds[random16(NUM_LEDS)] += CRGB::White;
  642. }
  643.  
  644. void confetti() {
  645.   // random colored speckles that blink in and fade smoothly
  646.   fadeToBlackBy(leds, NUM_LEDS, 10);
  647.   int pos = random16(NUM_LEDS);
  648.   leds[pos] += CHSV(gHue + random8(64), 200, 255);
  649. }
  650.  
  651. void sinelon() {
  652.   // a colored dot sweeping back and forth, with fading trails
  653.   fadeToBlackBy(leds, NUM_LEDS, 20);
  654.   int pos = beatsin16(13, 0, NUM_LEDS - 1);
  655.   leds[pos] += CHSV(gHue, 255, 192);
  656. }
  657.  
  658. void bpm()
  659. {
  660.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  661.   uint8_t BeatsPerMinute = 62;
  662.   CRGBPalette16 palette = PartyColors_p;
  663.   uint8_t beat = beatsin8(BeatsPerMinute, 64, 255);
  664.   for (uint16_t i = 0; i < NUM_LEDS; i++)
  665.     leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 110));
  666. }
  667.  
  668. void juggle() {
  669.   // eight colored dots, weaving in and out of sync with each other
  670.   fadeToBlackBy(leds, NUM_LEDS, 20);
  671.   byte dothue = 0;
  672.   for (uint16_t i = 0; i < 8; i++) {
  673.     leds[beatsin16(i + 7, 0, NUM_LEDS - 1)] |= CHSV(dothue, 200, 255);
  674.     dothue += 32;
  675.   }
  676. }
  677.  
  678. /******************************************************/
  679. void rainbow_center(uint8_t thisdelay, uint8_t deltahue) {                     // The fill_rainbow call doesn't support brightness levels.
  680.   uint8_t thishue = millis() * (255 - thisdelay) / 255;                             // To change the rate, add a beat or something to the result. 'thisdelay' must be a fixed value.
  681.   uint8_t huemod = 175;
  682.   fill_rainbow(leds, NUM_LEDS / 2, thishue, deltahue);                          // Use FastLED's fill_rainbow routine from the start to the center
  683.   fill_rainbow(leds + NUM_LEDS / 2, NUM_LEDS / 2, thishue + huemod, deltahue * -1);     // And from the center to the far end
  684. }
  685.  
  686. /*************************************************************/
  687. void simplemover(uint64_t speedval, uint8_t faderate, uint16_t count) {
  688.   //int speedval = 100;                                         // Higher = slower speed.
  689.   //uint8_t faderate = 2;                                       // Higher = quicker fade.
  690.   fadeToBlackBy(leds, NUM_LEDS, faderate);                    // 8 bit, 1 = slow, 255 = fast. You will need to adjust the fade based on the length of your strip
  691.   for (uint16_t i = 0; i < NUM_LEDS; i += count)                      // Turn on every 4th LED up to NUM_LEDS
  692.     leds[(millis() / speedval + i) % NUM_LEDS] =  CRGB::White;    // Use millis() to count up.
  693. } // simplemover()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement