Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <BluetoothSerial.h>
- #include <FastLED.h>
- #include <Ticker.h>
- #include <Preferences.h>
- Preferences prefs;
- Ticker tickerBluetooth, tickerEffects;
- #define UPDATES_PER_SECOND 120
- // Control Defines
- #define DEBUG_MESSAGES // выводить отладочные сообщения
- #define BTN_PIN 2 // пин-вход для подключения кнопки
- #define LED_PIN 15 // пин-выход для подключения ленты
- #ifdef DEBUG_MESSAGES
- #define SERIAL_BAUDRATE 115200 // скорость работы последовательного порта
- #endif
- // defines for LEDs strip
- #define NUM_LEDS 300 // кол-во светодиодов
- #define LED_TYPE WS2813 // тип светодиодов в ленте
- #define COLOR_ORDER GRB // порядок передачи цветов для чипов светодиодов
- /* Bluetooth Serial Operation Codes */
- // Opcodes Read
- #define GET_SOLID_COLOR 0x11 // 17
- #define GET_EFFECT_CHANGE_TIMEOUT 0x12 // 18
- #define GET_EFFECT_CHANGE_MODE 0x13 // 19
- #define GET_CURRENT_EFFECT 0x14 // 20
- #define GET_BRIGHTNESS 0x15 // 21
- #define GET_EFFECTS_COUNT 0x16 // 22
- #define GET_EFFECTS_LIST 0x17 // 23
- // Opcodes Write
- #define SET_SOLID_COLOR 0x21 // 33
- #define SET_EFFECT_CHANGE_TIMEOUT 0x22 // 34
- #define SET_EFFECT_CHANGE_MODE 0x23 // 35
- #define SET_CURRENT_EFFECT 0x24 // 36
- #define SET_BRIGHTNESS 0x25 // 37
- #define SET_DEFAULTS 0x26 // 38
- #define SET_EFFECT_DISABLED 0x27 // 39
- #define SET_EFFECT_ENABLED 0x28 // 40
- #define TORSE_OFF 0x29 // 41
- #define TORSE_ON 0x2A // 42
- /**/
- enum {
- MODE_SOLID_COLOR = 0x01,
- MODE_ONE_EFFECT = 0x02,
- MODE_SEQUENTIAL_FORWARD = 0x03,
- MODE_SEQUENTIAL_BACKWARD = 0x04,
- MODE_RANDOM_EFFECT = 0x05
- } modes_t;
- // system lightning variables
- uint8_t effectChangeTimeout = 0; // 15-3600 with step 15 s
- uint8_t effectChangeMode = 0; // effect change modes: 0 - static color, 1 - sequential, 2 - random
- uint8_t effectsCount = 0; // count of non empty effects
- uint8_t brightness = 0; // brightness of LEDS on strip: 10 - 255
- bool isTorseOn = true; //
- CRGB solidColor = 0xFF55FF55; // color for static mode
- volatile uint8_t currentEffect = 0; // number of current displaying effect
- volatile uint8_t lastEffect = 255; // last diplayed effect
- uint8_t effectsStates[] = { // 11 эффектов // states of effects in memory: 0 - empty, 1 - non empty, disabled, 3 - enabled
- 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0 - 10, 11 - 15
- 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16, 17 - 20
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- };
- struct CRGB leds[NUM_LEDS]; // массив светодиодов в подключенной ленте
- BluetoothSerial SerialBT; // объект последовательного Bluetooth-порта
- bool isConnected = false; // показывает, подключено ли устройство по Bluetooth
- // variables for touch button
- //#define NORMAL_OPEN_BTN // используется нормально открытая кнопка
- #define NEXT_TAP_TIMEOUT 400 // время ожидания следующего нажатия кнопки для обработки нескольких нажатий
- #define CLICK_TIME 200 // максимальное время удержания кнопки нажатой для распознавания нажатия
- #define HOLD_TIME 2000 // максимальное время удержания кнопки нажатой для распознавания короткого удержания
- uint8_t numberOfClicks = 0; // кол-во нажатий на кнопку
- uint64_t lastTapTime = 0; // время последнего нажатия
- uint64_t startHoldTime = 0; // время начала удержания
- uint64_t endHoldTime = 0; // время отпускания кнопки
- bool isClicked = false; // кнопка была нажата
- bool isHold = false; // кнопка удерживалась
- /**************** Для эффектов 11-16 ***************************/
- uint8_t gHue = 0; // rotating "base color" used by many of the patterns
- /**************** Для эффектов 0-10 ****************************/
- CRGBPalette16 currentPalette;
- TBlendType currentBlending;
- extern CRGBPalette16 redWhiteBluePalette;
- extern const TProgmemPalette16 redWhiteBluePalette_p PROGMEM;
- /***************************************************************/
- void btnCallback() { // обработчик прерывания нажатия и отпускания кнопки
- /* #ifdef NORMAL_OPEN_BTN
- if (digitalRead(BTN_PIN))
- #else
- if (!digitalRead(BTN_PIN))
- #endif
- startHoldTime = millis();
- else endHoldTime = millis();*/
- if (startHoldTime && endHoldTime) {
- if (endHoldTime - startHoldTime <= CLICK_TIME) {
- isClicked = true;
- if (millis() - lastTapTime > NEXT_TAP_TIMEOUT) {
- numberOfClicks = 1;
- lastTapTime = millis();
- }
- else {
- numberOfClicks++;
- lastTapTime = millis();
- }
- }
- if (endHoldTime - startHoldTime >= HOLD_TIME) isHold = true;
- startHoldTime = 0;
- endHoldTime = 0;
- }
- else if (!startHoldTime && endHoldTime) {
- startHoldTime = 0;
- endHoldTime = 0;
- }
- }
- void printData() { // вывод хранимых данных в памяти
- Serial.printf("Effects count: %d\n", effectsCount);
- Serial.printf("Current effect: %d\n", currentEffect);
- Serial.printf("Brightness: %d\n", brightness);
- Serial.printf("Effect change mode: %d\n", effectChangeMode);
- Serial.printf("Effect change timeout: %d\n", effectChangeTimeout);
- Serial.printf("Solid color: R:0x%02X G:0x%02X B:0x%02X\n", solidColor.r, solidColor.g, solidColor.b);
- String buff = "";
- for (uint8_t i = 0; i < sizeof(effectsStates); i++) { buff += effectsStates[i]; buff += "|"; }
- Serial.println("Effects list: " + buff);
- }
- void loadEffectsFromMem() {
- //prefs.putBytes("es", effectsStates, sizeof(effectsStates));
- size_t length = prefs.getBytesLength("es");
- prefs.getBytes("es", effectsStates, length);
- effectsCount = getEffectsCount();
- }
- void loadPropsFromMem() {
- currentEffect = prefs.getUChar("ce", 0);
- effectChangeMode = prefs.getUChar("ecm", 3);
- brightness = prefs.getUChar("br", 128);
- effectChangeTimeout = prefs.getUChar("ect", 1);
- solidColor = prefs.getInt("color", 0xFFFFFFFF);
- if (effectsStates[currentEffect] != 3) changeEffect();
- }
- uint8_t getActiveEffectsCount() {
- uint8_t address = 0, count = 0;
- while (address < sizeof(effectsStates))
- if (effectsStates[address++] == 3) count++;
- return count;
- }
- uint8_t getEffectsCount() {
- uint8_t count = 0;
- for (uint8_t address = 0; address < sizeof(effectsStates); address++)
- if (effectsStates[address] > 0 && effectsStates[address] < 4) count++;
- return count;
- }
- void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
- if (event == ESP_SPP_SRV_OPEN_EVT) {
- isConnected = true;
- #ifdef DEBUG_MESSAGES
- Serial.println("Client сonnected.");
- #endif
- tickerBluetooth.attach_ms(50, readBluetooth);
- // отправлем все параметры подключенному устройству для синхронизации
- if (isConnected) {
- String buff = "";
- buff += GET_EFFECTS_LIST; buff += "|";
- for (uint8_t i = 0; i < sizeof(effectsStates); i++) { buff += effectsStates[i]; buff += "|"; }
- SerialBT.printf("%d|%d\n", GET_SOLID_COLOR, (int)0xFF << 24 + (int)solidColor.r << 16 + (int)solidColor.g << 8 + (int)solidColor.b);
- SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_TIMEOUT, effectChangeTimeout);
- SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_MODE, effectChangeMode);
- SerialBT.printf("%d|%d\n", GET_CURRENT_EFFECT, currentEffect);
- SerialBT.printf("%d|%d\n", GET_BRIGHTNESS, brightness);
- SerialBT.println(buff);
- }
- }
- else if (event == ESP_SPP_CLOSE_EVT) {
- isConnected = false;
- #ifdef DEBUG_MESSAGES
- Serial.println("Client disconnected.");
- #endif
- tickerBluetooth.detach();
- }
- }
- void changeEffect() {
- if (effectChangeMode == MODE_SEQUENTIAL_FORWARD) nextEffect();
- else if (effectChangeMode == MODE_SEQUENTIAL_BACKWARD) previousEffect();
- else if (effectChangeMode == MODE_RANDOM_EFFECT) randomEffect();
- }
- void nextEffect() {
- currentEffect++;
- while (effectsStates[currentEffect] != 3) {
- currentEffect++;
- if (currentEffect == effectsCount) currentEffect = 0;
- }
- }
- void previousEffect() {
- currentEffect--;
- while (effectsStates[currentEffect] != 3) {
- currentEffect--;
- if (currentEffect == 0xFF) currentEffect = sizeof(effectsStates) - 1;
- }
- }
- void randomEffect() {
- currentEffect = random8(sizeof(effectsStates));
- while (effectsStates[currentEffect] != 3) currentEffect = random8(sizeof(effectsStates));
- }
- void readBluetooth() {
- String buffer = "";
- uint8_t cmd = 0xFF;
- int data = -1;
- if (isConnected && SerialBT.available()) {
- buffer = SerialBT.readString();
- int splitterIndex = buffer.indexOf('|');
- if (splitterIndex > -1) {
- cmd = (uint8_t)buffer.substring(0, splitterIndex).toInt();
- if ((buffer.length() - 1) > splitterIndex) data = buffer.substring(splitterIndex + 1).toInt();
- #ifdef DEBUG_MESSAGES
- Serial.printf("Command: 0x%02X\n", cmd);
- Serial.printf("Data: %d\n", data);
- #endif
- switch (cmd) {
- case SET_SOLID_COLOR:
- if (data >= 0xFF000000 && data <= 0xFFFFFFFF) {
- solidColor = data;
- effectChangeMode = MODE_SOLID_COLOR;
- prefs.putInt("color", data);
- prefs.putUChar("ecm", effectChangeMode);
- if (isConnected) SerialBT.printf("%d|%d\n", GET_SOLID_COLOR, (int)0xFF << 24 + (int)solidColor.r << 16 + (int)solidColor.g << 8 + (int)solidColor.b);
- #ifdef DEBUG_MESSAGES
- Serial.printf("Solid color: R:0x%02X G:0x%02X B:0x%02X\n", solidColor.r, solidColor.g, solidColor.b);
- if (isConnected) SerialBT.printf("Solid color: R:0x%02X G:0x%02X B:0x%02X\n", solidColor.r, solidColor.g, solidColor.b);
- #endif
- }
- else {
- #ifdef DEBUG_MESSAGES
- Serial.println(F("Wrong new solid color"));
- if (isConnected) SerialBT.println(F("Wrong new solid color"));
- #endif
- }
- break;
- case SET_EFFECT_CHANGE_TIMEOUT:
- if ((uint8_t)data >= 1 && (uint8_t)data <= 240) {
- effectChangeTimeout = (uint8_t)data;
- prefs.putUChar("ect", effectChangeTimeout);
- tickerEffects.detach();
- tickerEffects.attach_ms((uint64_t)effectChangeTimeout * 15000, changeEffect);
- if (isConnected) SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_TIMEOUT, effectChangeTimeout);
- #ifdef DEBUG_MESSAGES
- Serial.printf("Effect change timeout: %d s\n", effectChangeTimeout * 15);
- if (isConnected) SerialBT.printf("Effect change timeout: %d s\n", effectChangeTimeout * 15);
- #endif
- }
- else {
- #ifdef DEBUG_MESSAGES
- Serial.println(F("Wrong new effect change timeout, must be 15-3600 s"));
- if (isConnected) SerialBT.println(F("Wrong new effect change timeout, must be 15-3600 s"));
- #endif
- }
- break;
- case SET_EFFECT_CHANGE_MODE:
- if (data >= 1 && data <= 5) {
- effectChangeMode = (uint8_t)data;
- prefs.putUChar("ecm", effectChangeMode);
- if (isConnected) SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_MODE, effectChangeMode);
- #ifdef DEBUG_MESSAGES
- Serial.printf("Effect change mode: %d\n", effectChangeMode);
- if (isConnected) SerialBT.printf("Effect change mode: %d\n", effectChangeMode);
- #endif
- }
- else {
- //if (isConnected) SerialBT.write(ACK_FAILTURE);
- #ifdef DEBUG_MESSAGES
- Serial.println(F("Wrong new effect change mode, must be 1-5"));
- if (isConnected) SerialBT.println(F("Wrong new effect change mode, must be 1-5"));
- #endif
- }
- break;
- case SET_CURRENT_EFFECT:
- if ((uint8_t)data >= 0 && (uint8_t)data < sizeof(effectsStates)) {
- currentEffect = (uint8_t)data;
- prefs.putUChar("ce", currentEffect);
- if (isConnected) SerialBT.printf("%d|%d\n", GET_CURRENT_EFFECT, currentEffect);
- }
- else {
- #ifdef DEBUG_MESSAGES
- Serial.printf("Wrong new current effect, must be 0-%d\n", sizeof(effectsStates) - 1);
- if (isConnected) SerialBT.printf("Wrong new current effect, must be 0-%d\n", sizeof(effectsStates) - 1);
- #endif
- }
- break;
- case SET_BRIGHTNESS:
- if (data >= 0 && data <= 255) {
- brightness = constrain((uint8_t)data, 10, 255);
- FastLED.setBrightness(brightness);
- prefs.putUChar("br", brightness);
- if (isConnected) SerialBT.printf("%d|%d\n", GET_CURRENT_EFFECT, currentEffect);
- #ifdef DEBUG_MESSAGES
- Serial.printf("Brightness: %d\n", brightness);
- if (isConnected) SerialBT.printf("Brightness: %d\n", brightness);
- #endif
- }
- else {
- #ifdef DEBUG_MESSAGES
- Serial.println(F("Wrong new brigtness, must be 0-255"));
- if (isConnected) SerialBT.println(F("Wrong new brigtness, must be 0-255"));
- #endif
- }
- break;
- case SET_DEFAULTS:
- prefs.putUChar("ce", 0);
- prefs.putUChar("ecm", 3);
- prefs.putUChar("br", 128);
- prefs.putUChar("ect", 1);
- prefs.putInt("color", 0xFFFFFFFF);
- prefs.putBytes("es", effectsStates, sizeof(effectsStates));
- Serial.println("Defaults set.");
- // отправлем все параметры подключенному устройству для синхронизации
- if (isConnected) {
- String buff = "";
- buff += GET_EFFECTS_LIST; buff += "|";
- for (uint8_t i = 0; i < sizeof(effectsStates); i++) { buff += effectsStates[i]; buff += "|"; }
- SerialBT.printf("%d|%d\n", GET_SOLID_COLOR, (int)0xFF << 24 + (int)solidColor.r << 16 + (int)solidColor.g << 8 + (int)solidColor.b);
- SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_TIMEOUT, effectChangeTimeout);
- SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_MODE, effectChangeMode);
- SerialBT.printf("%d|%d\n", GET_CURRENT_EFFECT, currentEffect);
- SerialBT.printf("%d|%d\n", GET_BRIGHTNESS, brightness);
- SerialBT.println(buff);
- }
- break;
- case SET_EFFECT_DISABLED:
- if ((uint8_t)data < sizeof(effectsStates)) {
- effectsStates[(uint8_t)data] = 1;
- prefs.putBytes("es", effectsStates, sizeof(effectsStates));
- if (currentEffect == (uint8_t)data) {
- if (effectChangeMode == MODE_ONE_EFFECT) nextEffect();
- else if (effectChangeMode == MODE_SEQUENTIAL_FORWARD || effectChangeMode == MODE_SEQUENTIAL_FORWARD || effectChangeMode == MODE_RANDOM_EFFECT) changeEffect();
- }
- }
- else {
- #ifdef DEBUG_MESSAGES
- Serial.printf("Wrong effect number, must be 0-%d\n", sizeof(effectsStates) - 1);
- if (isConnected) SerialBT.printf("Wrong effect number, must be 0-%d\n", sizeof(effectsStates) - 1);
- #endif
- }
- break;
- case SET_EFFECT_ENABLED:
- if ((uint8_t)data < sizeof(effectsStates)) {
- effectsStates[(uint8_t)data] = 3;
- prefs.putBytes("es", effectsStates, sizeof(effectsStates));
- }
- else {
- #ifdef DEBUG_MESSAGES
- Serial.printf("Wrong effect number, must be 0-%d\n", sizeof(effectsStates) - 1);
- if (isConnected) SerialBT.printf("Wrong effect number, must be 0-%d\n", sizeof(effectsStates) - 1);
- #endif
- }
- break;
- case GET_SOLID_COLOR:
- if (isConnected) SerialBT.printf("%d|%d\n", GET_SOLID_COLOR, (int)0xFF << 24 + (int)solidColor.r << 16 + (int)solidColor.g << 8 + (int)solidColor.b);
- #ifdef DEBUG_MESSAGES
- Serial.printf("Solid color: R:0x%02X G:0x%02X B:0x%02X\n", solidColor.r, solidColor.g, solidColor.b);
- if (isConnected) SerialBT.printf("Solid color: R:0x%02X G:0x%02X B:0x%02X\n", solidColor.r, solidColor.g, solidColor.b);
- #endif
- break;
- case GET_EFFECT_CHANGE_TIMEOUT:
- if (isConnected) SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_TIMEOUT, effectChangeTimeout);
- #ifdef DEBUG_MESSAGES
- Serial.printf("Effect change timeout: %d s\n", effectChangeTimeout);
- if (isConnected) SerialBT.printf("Effect change timeout: %d s\n", effectChangeTimeout);
- #endif
- break;
- case GET_EFFECT_CHANGE_MODE:
- if (isConnected) SerialBT.printf("%d|%d\n", GET_EFFECT_CHANGE_MODE, effectChangeMode);
- #ifdef DEBUG_MESSAGES
- Serial.printf("Effect change mode: %d\n", effectChangeMode);
- if (isConnected) SerialBT.printf("Effect change mode: %d\n", effectChangeMode);
- #endif
- break;
- case GET_CURRENT_EFFECT:
- if (isConnected) SerialBT.printf("%d|%d\n", GET_CURRENT_EFFECT, currentEffect);
- #ifdef DEBUG_MESSAGES
- Serial.printf("Current effect: %d\n", currentEffect);
- if (isConnected) SerialBT.printf("Current effect: %d\n", currentEffect);
- #endif
- break;
- case GET_BRIGHTNESS:
- if (isConnected) SerialBT.printf("%d|%d\n", GET_BRIGHTNESS, brightness);
- #ifdef DEBUG_MESSAGES
- Serial.printf("Brightness: %d\n", brightness);
- if (isConnected) SerialBT.printf("Brightness: %d\n", brightness);
- #endif
- break;
- case GET_EFFECTS_LIST:
- String buff = "";
- buff += GET_EFFECTS_LIST; buff += "|";
- for (uint8_t i = 0; i < effectsCount; i++) { buff += effectsStates[i]; buff += "|"; }
- if (isConnected) SerialBT.println(buff);
- #ifdef DEBUG_MESSAGES
- Serial.println("Effects list: " + buff);
- //if (isConnected) SerialBT.printf("Effects list: " + buffer);
- #endif
- break;
- }
- }
- }
- }
- void setup() {
- #ifdef DEBUG_MESSAGES
- Serial.begin(SERIAL_BAUDRATE);
- uint64_t chipid = ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes).
- Serial.printf("ESP32 Chip ID = 0x%04X%08X\n",(uint16_t)(chipid >> 32), (uint32_t)chipid);
- #endif
- prefs.begin("duralight", false);
- loadPropsFromMem();
- loadEffectsFromMem();
- #ifdef DEBUG_MESSAGES
- printData();
- #endif
- tickerEffects.attach_ms((uint64_t)effectChangeTimeout * 15000, changeEffect);
- SerialBT.register_callback(callback);
- if (!SerialBT.begin("Duralight")) {
- #ifdef DEBUG_MESSAGES
- Serial.println(F("An error occurred initializing Bluetooth"));
- #endif
- ESP.restart();
- }
- else {
- #ifdef DEBUG_MESSAGES
- Serial.println(F("Bluetooth initialized."));
- Serial.println(F("The device started, now you can pair it with bluetooth."));
- #endif
- LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- LEDS.setBrightness(brightness);
- //attachInterrupt(digitalPinToInterrupt(BTN_PIN), btnCallback, CHANGE);
- }
- }
- void loop() {
- if (lastEffect != currentEffect) {
- prefs.putUChar("ce", currentEffect);
- if (isConnected) SerialBT.printf("%d|%d\n", GET_CURRENT_EFFECT, currentEffect);
- #ifdef DEBUG_MESSAGES
- Serial.printf("Current effect: %d\n", currentEffect);
- if (isConnected) SerialBT.printf("Current effect: %d\n", currentEffect);
- #endif
- lastEffect = currentEffect;
- // перезапуск таймера
- tickerEffects.detach();
- tickerEffects.attach_ms((uint64_t)effectChangeTimeout * 15000, changeEffect);
- // настройки палитры при смене режимов
- switch (currentEffect) {
- case 0: currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; break; // 0
- case 1: currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; break; // 1
- case 2: currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; break; // 2
- case 3: setupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; break; // 3
- case 4: setupTotallyRandomPalette(); currentBlending = LINEARBLEND; break; // 4
- case 5: setupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; break; // 5
- case 6: setupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; break; // 6
- case 7: currentPalette = CloudColors_p; currentBlending = LINEARBLEND; break; // 7
- case 8: currentPalette = PartyColors_p; currentBlending = LINEARBLEND; break; // 8
- case 9: currentPalette = redWhiteBluePalette_p; currentBlending = NOBLEND; break; // 9
- case 10: currentPalette = redWhiteBluePalette_p; currentBlending = LINEARBLEND; break; // 10
- }
- }
- if (startHoldTime)
- if (millis() - startHoldTime >= HOLD_TIME) {
- isHold = true;
- startHoldTime = 0;
- }
- if (isClicked) {
- #ifdef DEBUG_MESSAGES
- Serial.println("Click");
- #endif
- isClicked = false;
- }
- if (isHold) {
- #ifdef DEBUG_MESSAGES
- Serial.println("Hold");
- #endif
- isHold = false;
- }
- if (numberOfClicks > 0 && millis() - lastTapTime > NEXT_TAP_TIMEOUT) {
- #ifdef DEBUG_MESSAGES
- Serial.print(numberOfClicks);
- Serial.println(" clicks");
- #endif
- switch (numberOfClicks) {
- case 1:
- if (effectChangeMode == MODE_SEQUENTIAL_FORWARD || effectChangeMode == MODE_ONE_EFFECT) nextEffect();
- else if (effectChangeMode == MODE_SEQUENTIAL_BACKWARD) previousEffect();
- else if (effectChangeMode == MODE_RANDOM_EFFECT) randomEffect();
- break;
- case 2:
- if (effectChangeMode == MODE_SEQUENTIAL_FORWARD || effectChangeMode == MODE_ONE_EFFECT) previousEffect();
- else if (effectChangeMode == MODE_SEQUENTIAL_BACKWARD) nextEffect();
- else if (effectChangeMode == MODE_RANDOM_EFFECT) randomEffect();
- break;
- case 3:
- if (effectChangeMode == MODE_ONE_EFFECT) effectChangeMode = MODE_SEQUENTIAL_FORWARD;
- else if (effectChangeMode == MODE_SEQUENTIAL_FORWARD) effectChangeMode = MODE_SEQUENTIAL_BACKWARD;
- else if (effectChangeMode == MODE_SEQUENTIAL_BACKWARD) effectChangeMode = MODE_RANDOM_EFFECT;
- else if (effectChangeMode == MODE_RANDOM_EFFECT) effectChangeMode = MODE_ONE_EFFECT;
- break;
- case 4:
- if (effectChangeMode == MODE_ONE_EFFECT) effectChangeMode = MODE_RANDOM_EFFECT;
- else if (effectChangeMode == MODE_SEQUENTIAL_FORWARD) effectChangeMode = MODE_ONE_EFFECT;
- else if (effectChangeMode == MODE_SEQUENTIAL_BACKWARD) effectChangeMode = MODE_SEQUENTIAL_FORWARD;
- else if (effectChangeMode == MODE_RANDOM_EFFECT) effectChangeMode = MODE_SEQUENTIAL_BACKWARD;
- break;
- }
- numberOfClicks = 0;
- }
- if (effectChangeMode == MODE_SOLID_COLOR) fill_solid(leds, NUM_LEDS, solidColor);
- else {
- if (currentEffect >= 0 && currentEffect <= 10) {
- static uint8_t startIndex = 0;
- startIndex = startIndex + 1;
- fillLEDsFromPaletteColors(startIndex);
- }
- else if (currentEffect >= 11 && currentEffect <= 16) {
- switch (currentEffect) {
- case 11: rainbow(); break;
- case 12: rainbowWithGlitter(); break;
- case 13: confetti(); break;
- case 14: sinelon(); break;
- case 15: juggle(); break;
- case 16: bpm(); break;
- }
- EVERY_N_MILLISECONDS(20) { gHue++; }
- }
- else if (currentEffect >= 17 && currentEffect <= 20) {
- switch(currentEffect) {
- case 17: rainbow_center(150, 5); break;
- case 18: rainbow_center(200, 5); break;
- case 19: rainbow_center(150, 10); break;
- case 20: rainbow_center(200, 10); break;
- }
- }
- }
- FastLED.show();
- FastLED.delay(1000 / UPDATES_PER_SECOND);
- }
- /***************************** Для эффектов 0-10 ********************************/
- void fillLEDsFromPaletteColors(uint8_t colorIndex) {
- uint8_t _brightness = 255;
- for (uint16_t i = 0; i < NUM_LEDS; i++) {
- leds[i] = ColorFromPalette(currentPalette, colorIndex, _brightness, currentBlending);
- colorIndex += 3;
- }
- }
- void setupTotallyRandomPalette() {
- for(uint8_t i = 0; i < 16; i++) currentPalette[i] = CHSV(random8(), 0xFF, random8());
- }
- void setupBlackAndWhiteStripedPalette() {
- fill_solid(currentPalette, 16, CRGB::Black);
- currentPalette[0] = CRGB::White;
- currentPalette[4] = CRGB::White;
- currentPalette[8] = CRGB::White;
- currentPalette[12] = CRGB::White;
- }
- void setupPurpleAndGreenPalette() {
- CRGB purple = CHSV(HUE_PURPLE, 0xFF, 0xFF);
- CRGB green = CHSV(HUE_GREEN, 0xFF, 0xFF);
- CRGB black = CRGB::Black;
- currentPalette = CRGBPalette16(green, green, black, black, purple, purple,
- black, black, green, green, black, black, purple, purple, black, black);
- }
- const TProgmemPalette16 redWhiteBluePalette_p PROGMEM = {
- CRGB::Red, CRGB::Gray, CRGB::Blue, CRGB::Black,
- CRGB::Red, CRGB::Gray, CRGB::Blue, CRGB::Black,
- CRGB::Red, CRGB::Red, CRGB::Gray, CRGB::Gray,
- CRGB::Blue, CRGB::Blue, CRGB::Black, CRGB::Black
- };
- /******************************** Для эффектов 11-16 ****************************/
- void rainbow() {
- // FastLED's built-in rainbow generator
- fill_rainbow(leds, NUM_LEDS, gHue, 7);
- }
- void rainbowWithGlitter() {
- // built-in FastLED rainbow, plus some random sparkly glitter
- rainbow();
- addGlitter(80);
- }
- void addGlitter(fract8 chanceOfGlitter) {
- if (random8() < chanceOfGlitter) leds[random16(NUM_LEDS)] += CRGB::White;
- }
- void confetti() {
- // random colored speckles that blink in and fade smoothly
- fadeToBlackBy(leds, NUM_LEDS, 10);
- int pos = random16(NUM_LEDS);
- leds[pos] += CHSV(gHue + random8(64), 200, 255);
- }
- void sinelon() {
- // a colored dot sweeping back and forth, with fading trails
- fadeToBlackBy(leds, NUM_LEDS, 20);
- int pos = beatsin16(13, 0, NUM_LEDS - 1);
- leds[pos] += CHSV(gHue, 255, 192);
- }
- void bpm()
- {
- // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
- uint8_t BeatsPerMinute = 62;
- CRGBPalette16 palette = PartyColors_p;
- uint8_t beat = beatsin8(BeatsPerMinute, 64, 255);
- for (uint16_t i = 0; i < NUM_LEDS; i++)
- leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 110));
- }
- void juggle() {
- // eight colored dots, weaving in and out of sync with each other
- fadeToBlackBy(leds, NUM_LEDS, 20);
- byte dothue = 0;
- for (uint16_t i = 0; i < 8; i++) {
- leds[beatsin16(i + 7, 0, NUM_LEDS - 1)] |= CHSV(dothue, 200, 255);
- dothue += 32;
- }
- }
- /******************************************************/
- void rainbow_center(uint8_t thisdelay, uint8_t deltahue) { // The fill_rainbow call doesn't support brightness levels.
- 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.
- uint8_t huemod = 175;
- fill_rainbow(leds, NUM_LEDS / 2, thishue, deltahue); // Use FastLED's fill_rainbow routine from the start to the center
- fill_rainbow(leds + NUM_LEDS / 2, NUM_LEDS / 2, thishue + huemod, deltahue * -1); // And from the center to the far end
- }
- /*************************************************************/
- void simplemover(uint64_t speedval, uint8_t faderate, uint16_t count) {
- //int speedval = 100; // Higher = slower speed.
- //uint8_t faderate = 2; // Higher = quicker fade.
- 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
- for (uint16_t i = 0; i < NUM_LEDS; i += count) // Turn on every 4th LED up to NUM_LEDS
- leds[(millis() / speedval + i) % NUM_LEDS] = CRGB::White; // Use millis() to count up.
- } // simplemover()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement