Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <FastLED.h>
- FASTLED_USING_NAMESPACE
- using namespace std;
- #define LED_TYPE WS2812B
- #define COLOR_ORDER GRB
- #define MIN_BRIGHTNESS 20
- #define MAX_BRIGHTNESS 170
- #define CURRENT_COLOR CRGB(225, 202, 0)
- #define CURRENT_HUE 54
- #define LED_PIN_SOCKET_HOOD 2
- #define LED_PIN_HOOD_KLE_AC 4
- #define LED_PIN_HOOD_KLE_DC 16
- #define LED_PIN_KLE_AC_KLE_DC 17
- #define LED_PIN_KLE_EME_DC 5
- #define LED_PIN_KLE_EME_AC 18
- #define LED_PIN_EME_BATTERY 19
- #define LED_PIN_EME_EDH 21
- #define LED_PIN_EME_EKK 3
- #define LED_PIN_EME_EM 22
- #define LED_PIN_CHASSIS 23
- const uint16_t NUM_LEDS_SOCKET_HOOD = 60; // 300
- const uint16_t NUM_LEDS_HOOD_KLE_AC = 60; // 60
- const uint16_t NUM_LEDS_HOOD_KLE_DC = 10; // 60
- const uint16_t NUM_LEDS_KLE_AC_KLE_DC = 10; // 48
- const uint16_t NUM_LEDS_KLE_EME_DC = 10; // 84
- const uint16_t NUM_LEDS_KLE_EME_AC = 10; // 29
- const uint16_t NUM_LEDS_EME_BATTERY = 10; // 60
- const uint16_t NUM_LEDS_EME_EDH = 10; // 96
- const uint16_t NUM_LEDS_EME_EKK = 10; // 66
- const uint16_t NUM_LEDS_EME_EM = 10; // 18
- const uint16_t NUM_LEDS_CHASSIS = 10; // 120
- #define BUTTON_PIN_CHARGE_AC1 13
- #define BUTTON_PIN_CHARGE_AC3 12
- #define BUTTON_PIN_CHARGE_DC 14
- #define BUTTON_PIN_ACC 27
- #define BUTTON_PIN_RECU_HIGH 26
- #define BUTTON_PIN_RECU_LOW 25
- #define BUTTON_PIN_COOL 33
- #define BUTTON_PIN_HEAT 32
- #define BUTTON_PIN_RESET 15
- CRGB socket_hood[NUM_LEDS_SOCKET_HOOD];
- CRGB hood_KLE_AC[NUM_LEDS_HOOD_KLE_AC];
- CRGB hood_KLE_DC[NUM_LEDS_HOOD_KLE_DC];
- CRGB KLE_AC_KLE_DC[NUM_LEDS_KLE_AC_KLE_DC];
- CRGB KLE_EME_DC[NUM_LEDS_KLE_EME_DC];
- CRGB KLE_EME_AC[NUM_LEDS_KLE_EME_AC];
- CRGB EME_Battery[NUM_LEDS_EME_BATTERY];
- CRGB EME_EDH[NUM_LEDS_EME_EDH];
- CRGB EME_EKK[NUM_LEDS_EME_EKK];
- CRGB EME_EM[NUM_LEDS_EME_EM];
- CRGB CHASSIS[NUM_LEDS_CHASSIS];
- void sinusWave(CRGB leds[], uint16_t num_leds, uint16_t current_led);
- void IRAM_ATTR registerButtonPress(void* arg);
- void clearLEDs(CRGB leds[], uint16_t num_leds);
- void clearAllLEDs();
- void chargeAC1(uint16_t wait_ms);
- void chargeAC3(uint16_t wait_ms);
- void chargeDC(uint16_t wait_ms);
- void accelerate(uint16_t wait_ms);
- void recu(uint16_t wait_ms);
- void heat(uint16_t wait_ms);
- void cool(uint16_t wait_ms);
- const float pi = 6.28;
- byte BRIGHTNESS = 30;
- uint16_t DEFAULT_INTERVAL_MS = 25;
- struct Button
- {
- const uint8_t Pin;
- bool IsPressed;
- };
- Button button_charge_AC1 = { BUTTON_PIN_CHARGE_AC1, false };
- Button button_charge_AC3 = { BUTTON_PIN_CHARGE_AC3, false };
- Button button_charge_DC = { BUTTON_PIN_CHARGE_DC, false };
- Button button_acc = { BUTTON_PIN_ACC, false };
- Button button_recu_high = { BUTTON_PIN_RECU_HIGH, false };
- Button button_recu_low = { BUTTON_PIN_RECU_LOW, false };
- Button button_heating = { BUTTON_PIN_HEAT, false };
- Button button_cooling = { BUTTON_PIN_COOL, false };
- Button button_reset = { BUTTON_PIN_RESET, false };
- enum class LEDState: int8_t
- {
- IDLE,
- AC_1,
- AC_3,
- DC,
- ACC,
- RECU_HIGH,
- RECU_LOW,
- HEATING,
- COOLING
- };
- LEDState currentLEDState = LEDState::IDLE;
- LEDState previousLEDState = LEDState::IDLE;
- uint16_t currentLED = 0;
- uint16_t currentLED_AC = 0;
- void setup() {
- Serial.begin(115200);
- delay(3000); // 3 second delay for recovery
- Serial.println("Starting Controller...");
- // tell FastLED about the LED strip configuration
- FastLED.addLeds<LED_TYPE, LED_PIN_SOCKET_HOOD, COLOR_ORDER>(socket_hood, NUM_LEDS_SOCKET_HOOD).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE, LED_PIN_HOOD_KLE_AC, COLOR_ORDER>(hood_KLE_AC, NUM_LEDS_HOOD_KLE_AC).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE, LED_PIN_HOOD_KLE_DC, COLOR_ORDER>(hood_KLE_DC, NUM_LEDS_HOOD_KLE_DC).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE, LED_PIN_KLE_AC_KLE_DC, COLOR_ORDER>(KLE_AC_KLE_DC, NUM_LEDS_KLE_AC_KLE_DC).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE, LED_PIN_KLE_EME_DC, COLOR_ORDER>(KLE_EME_DC, NUM_LEDS_KLE_EME_DC).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE, LED_PIN_KLE_EME_AC, COLOR_ORDER>(KLE_EME_AC, NUM_LEDS_KLE_EME_AC).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE, LED_PIN_EME_BATTERY, COLOR_ORDER>(EME_Battery, NUM_LEDS_EME_BATTERY).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE, LED_PIN_EME_EDH, COLOR_ORDER>(EME_EDH, NUM_LEDS_EME_EDH).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE, LED_PIN_EME_EKK, COLOR_ORDER>(EME_EKK, NUM_LEDS_EME_EKK).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE, LED_PIN_EME_EM, COLOR_ORDER>(EME_EM, NUM_LEDS_EME_EM).setCorrection(TypicalLEDStrip);
- FastLED.addLeds<LED_TYPE, LED_PIN_CHASSIS, COLOR_ORDER>(CHASSIS, NUM_LEDS_CHASSIS).setCorrection(TypicalLEDStrip);
- // clamp brightness between min and max
- BRIGHTNESS = constrain(BRIGHTNESS, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
- clearAllLEDs();
- // set master brightness control
- FastLED.setBrightness(BRIGHTNESS);
- FastLED.setDither( 0 );
- FastLED.show();
- pinMode(button_charge_AC1.Pin, INPUT_PULLUP);
- pinMode(button_charge_AC3.Pin, INPUT_PULLUP);
- pinMode(button_charge_DC.Pin, INPUT_PULLUP);
- pinMode(button_acc.Pin, INPUT_PULLUP);
- pinMode(button_recu_high.Pin, INPUT_PULLUP);
- pinMode(button_recu_low.Pin, INPUT_PULLUP);
- pinMode(button_heating.Pin, INPUT_PULLUP);
- pinMode(button_cooling.Pin, INPUT_PULLUP);
- pinMode(button_reset.Pin, INPUT_PULLUP);
- attachInterruptArg(digitalPinToInterrupt(button_charge_AC1.Pin), registerButtonPress, &button_charge_AC1, FALLING);
- attachInterruptArg(digitalPinToInterrupt(button_charge_AC3.Pin), registerButtonPress, &button_charge_AC3, FALLING);
- attachInterruptArg(digitalPinToInterrupt(button_charge_DC.Pin), registerButtonPress, &button_charge_DC, FALLING);
- attachInterruptArg(digitalPinToInterrupt(button_acc.Pin), registerButtonPress, &button_acc, FALLING);
- attachInterruptArg(digitalPinToInterrupt(button_recu_high.Pin), registerButtonPress, &button_recu_high, FALLING);
- attachInterruptArg(digitalPinToInterrupt(button_recu_low.Pin), registerButtonPress, &button_recu_low, FALLING);
- attachInterruptArg(digitalPinToInterrupt(button_heating.Pin), registerButtonPress, &button_heating, FALLING);
- attachInterruptArg(digitalPinToInterrupt(button_cooling.Pin), registerButtonPress, &button_cooling, FALLING);
- attachInterruptArg(button_reset.Pin, registerButtonPress, &button_reset, FALLING);
- Serial.println("Controller initialization successful.");
- }
- void loop() {
- updateState();
- executeState();
- FastLED.show();
- }
- void executeState()
- {
- switch(currentLEDState)
- {
- case LEDState::IDLE:
- //clearAllLEDs();
- break;
- case LEDState::AC_1:
- chargeAC1(DEFAULT_INTERVAL_MS);
- break;
- case LEDState::AC_3:
- break;
- case LEDState::DC:
- chargeDC(DEFAULT_INTERVAL_MS);
- break;
- case LEDState::ACC:
- accelerate(DEFAULT_INTERVAL_MS);
- break;
- case LEDState::RECU_HIGH:
- recu(DEFAULT_INTERVAL_MS);
- break;
- case LEDState::RECU_LOW:
- recu(DEFAULT_INTERVAL_MS * 4);
- break;
- case LEDState::HEATING:
- heat(DEFAULT_INTERVAL_MS);
- break;
- case LEDState::COOLING:
- cool(DEFAULT_INTERVAL_MS);
- break;
- default:
- break;
- }
- }
- void updateState()
- {
- if(button_charge_AC1.IsPressed)
- {
- Serial.println("AC_1");
- button_charge_AC1.IsPressed = false;
- currentLEDState = LEDState::AC_1;
- if(previousLEDState != currentLEDState)
- {
- clearAllLEDs();
- previousLEDState = currentLEDState;
- }
- }
- if(button_charge_AC3.IsPressed)
- {
- button_charge_AC3.IsPressed = false;
- Serial.println("AC_3");
- currentLEDState = LEDState::AC_3;
- if(previousLEDState != currentLEDState)
- {
- clearAllLEDs();
- previousLEDState = currentLEDState;
- }
- }
- if(button_charge_DC.IsPressed)
- {
- button_charge_DC.IsPressed = false;
- Serial.println("DC");
- currentLEDState = LEDState::DC;
- if(previousLEDState != currentLEDState)
- {
- clearAllLEDs();
- previousLEDState = currentLEDState;
- }
- }
- if(button_acc.IsPressed)
- {
- button_acc.IsPressed = false;
- Serial.println("Acc");
- currentLEDState = LEDState::ACC;
- if(previousLEDState != currentLEDState)
- {
- clearAllLEDs();
- previousLEDState = currentLEDState;
- }
- }
- if(button_recu_high.IsPressed)
- {
- button_recu_high.IsPressed = false;
- Serial.println("Recu High");
- currentLEDState = LEDState::RECU_HIGH;
- if(previousLEDState != currentLEDState)
- {
- clearAllLEDs();
- previousLEDState = currentLEDState;
- }
- }
- if(button_recu_low.IsPressed)
- {
- button_recu_low.IsPressed = false;
- Serial.println("Recu_Low");
- currentLEDState = LEDState::RECU_LOW;
- if(previousLEDState != currentLEDState)
- {
- clearAllLEDs();
- previousLEDState = currentLEDState;
- }
- }
- if(button_heating.IsPressed)
- {
- button_heating.IsPressed = false;
- Serial.println("Heating");
- currentLEDState = LEDState::HEATING;
- if(previousLEDState != currentLEDState)
- {
- clearAllLEDs();
- previousLEDState = currentLEDState;
- }
- }
- if(button_cooling.IsPressed)
- {
- button_cooling.IsPressed = false;
- Serial.println("Cooling");
- currentLEDState = LEDState::COOLING;
- if(previousLEDState != currentLEDState)
- {
- clearAllLEDs();
- previousLEDState = currentLEDState;
- }
- }
- if(button_reset.IsPressed)
- {
- button_reset.IsPressed = false;
- Serial.println("Reset");
- /*currentLEDState = LEDState::IDLE;
- if(previousLEDState != currentLEDState)
- {
- //clearAllLEDs();
- previousLEDState = currentLEDState;
- }*/
- }
- }
- void IRAM_ATTR registerButtonPress(void* arg) {
- Button* s = static_cast<Button*>(arg);
- s->IsPressed = true;
- }
- /*CRGB socket_hood[NUM_LEDS_SOCKET_HOOD];
- CRGB hood_KLE_AC[NUM_LEDS_HOOD_KLE_AC];
- CRGB hood_KLE_DC[NUM_LEDS_HOOD_KLE_DC];
- CRGB KLE_AC_KLE_DC[NUM_LEDS_KLE_AC_KLE_DC];
- CRGB KLE_EME_DC[NUM_LEDS_KLE_EME_DC];
- CRGB KLE_EME_AC[NUM_LEDS_KLE_EME_AC];
- CRGB EME_Battery[NUM_LEDS_EME_BATTERY];
- CRGB EME_EDH[NUM_LEDS_EME_EDH];
- CRGB EME_EKK[NUM_LEDS_EME_EKK];
- CRGB EME_EM[NUM_LEDS_EME_EM];
- CRGB CHASSIS[NUM_LEDS_CHASSIS];
- */
- // restart from last pos instead of first to avoid "jump" in animation
- void sinusWave(CRGB leds[], uint16_t num_leds, uint16_t current_led)
- {
- Serial.println(current_led);
- byte brightness;
- byte phase = 2;
- for(uint16_t i=0; i<num_leds; i++) {
- brightness = (pow(sin(((current_led + i) % num_leds) / (float)num_leds * pi * phase),8) * 255);
- if(brightness < MIN_BRIGHTNESS)
- {
- brightness = 0;
- }
- leds[i] = CHSV(CURRENT_HUE, 255, brightness);
- }
- }
- void chargeAC1(uint16_t wait_ms)
- {
- // sinus
- if(currentLED_AC < NUM_LEDS_SOCKET_HOOD + NUM_LEDS_HOOD_KLE_AC + NUM_LEDS_KLE_AC_KLE_DC)
- {
- sinusWave(socket_hood, NUM_LEDS_SOCKET_HOOD, currentLED_AC % NUM_LEDS_SOCKET_HOOD);
- sinusWave(hood_KLE_AC, NUM_LEDS_HOOD_KLE_AC, currentLED_AC % NUM_LEDS_HOOD_KLE_AC);
- sinusWave(KLE_AC_KLE_DC, NUM_LEDS_KLE_AC_KLE_DC, currentLED_AC % NUM_LEDS_KLE_AC_KLE_DC);
- }
- else
- {
- currentLED_AC = -1;
- }
- // gradual
- if(currentLED < NUM_LEDS_KLE_EME_DC)
- {
- KLE_EME_DC[currentLED] = CURRENT_COLOR;
- }
- else if(currentLED < NUM_LEDS_KLE_EME_DC + NUM_LEDS_EME_BATTERY)
- {
- EME_Battery[currentLED - NUM_LEDS_KLE_EME_DC] = CURRENT_COLOR;
- }
- else
- {
- currentLED = -1;
- clearLEDs(KLE_EME_DC, NUM_LEDS_KLE_EME_DC);
- clearLEDs(EME_Battery, NUM_LEDS_EME_BATTERY);
- }
- currentLED_AC++;
- currentLED++;
- delay(wait_ms);
- }
- void chargeDC(uint16_t wait_ms)
- {
- if(currentLED < NUM_LEDS_SOCKET_HOOD)
- {
- socket_hood[currentLED] = CURRENT_COLOR;
- }
- else if(currentLED < NUM_LEDS_SOCKET_HOOD + NUM_LEDS_HOOD_KLE_DC)
- {
- hood_KLE_DC[currentLED - NUM_LEDS_SOCKET_HOOD] = CURRENT_COLOR;
- }
- else if(currentLED < NUM_LEDS_SOCKET_HOOD + NUM_LEDS_HOOD_KLE_DC + NUM_LEDS_KLE_EME_DC)
- {
- KLE_EME_DC[currentLED - NUM_LEDS_SOCKET_HOOD - NUM_LEDS_HOOD_KLE_DC] = CURRENT_COLOR;
- }
- else if(currentLED < NUM_LEDS_SOCKET_HOOD + NUM_LEDS_HOOD_KLE_DC + NUM_LEDS_KLE_EME_DC + NUM_LEDS_EME_BATTERY)
- {
- EME_Battery[currentLED - NUM_LEDS_SOCKET_HOOD - NUM_LEDS_HOOD_KLE_DC - NUM_LEDS_KLE_EME_DC] = CURRENT_COLOR;
- }
- else
- {
- clearAllLEDs();
- currentLED = -1;
- }
- currentLED++;
- delay(wait_ms);
- }
- void accelerate(uint16_t wait_ms)
- {
- uint16_t actualLED = currentLED;
- // Bat -> EME -> EM
- if(currentLED < NUM_LEDS_EME_BATTERY)
- {
- //Serial.println("EME_Battery");
- actualLED = NUM_LEDS_EME_BATTERY - currentLED - 1;
- EME_Battery[NUM_LEDS_EME_BATTERY - currentLED - 1] = CURRENT_COLOR;
- }
- else if(currentLED < NUM_LEDS_EME_BATTERY + NUM_LEDS_EME_EM)
- {
- //Serial.println("EME_EM");
- actualLED = currentLED - NUM_LEDS_EME_BATTERY;
- EME_EM[currentLED - NUM_LEDS_EME_BATTERY] = CURRENT_COLOR;
- }
- else
- {
- clearAllLEDs();
- currentLED = -1;
- }
- currentLED++;
- delay(wait_ms);
- }
- void recu(uint16_t wait_ms)
- {
- if(currentLED < NUM_LEDS_EME_EM)
- {
- EME_EM[NUM_LEDS_EME_EM - currentLED - 1] = CURRENT_COLOR;
- }
- else if(currentLED < NUM_LEDS_EME_EM + NUM_LEDS_EME_BATTERY)
- {
- EME_Battery[currentLED - NUM_LEDS_EME_EM - 1] = CURRENT_COLOR;
- }
- else
- {
- clearAllLEDs();
- currentLED = -1;
- }
- currentLED++;
- delay(wait_ms);
- }
- void heat(uint16_t wait_ms)
- {
- // Bat -> EME -> EM
- if(currentLED < NUM_LEDS_EME_BATTERY)
- {
- EME_Battery[NUM_LEDS_EME_BATTERY - currentLED - 1] = CURRENT_COLOR;
- }
- else if(currentLED < NUM_LEDS_EME_BATTERY + NUM_LEDS_EME_EM)
- {
- EME_EM[currentLED - NUM_LEDS_EME_BATTERY] = CURRENT_COLOR;
- }
- else if(currentLED < NUM_LEDS_EME_BATTERY + NUM_LEDS_EME_EM + NUM_LEDS_EME_EDH)
- {
- EME_EDH[currentLED - NUM_LEDS_EME_BATTERY - NUM_LEDS_EME_EM] = CURRENT_COLOR;
- }
- else
- {
- clearAllLEDs();
- currentLED = -1;
- }
- currentLED++;
- delay(wait_ms);
- }
- void cool(uint16_t wait_ms)
- {
- // Bat -> EME -> EM
- if(currentLED < NUM_LEDS_EME_BATTERY)
- {
- EME_Battery[NUM_LEDS_EME_BATTERY - currentLED - 1] = CURRENT_COLOR;
- }
- else if(currentLED < NUM_LEDS_EME_BATTERY + NUM_LEDS_EME_EM)
- {
- EME_EM[currentLED - NUM_LEDS_EME_BATTERY] = CURRENT_COLOR;
- }
- else if(currentLED < NUM_LEDS_EME_BATTERY + NUM_LEDS_EME_EM + NUM_LEDS_EME_EKK)
- {
- EME_EKK[currentLED - NUM_LEDS_EME_BATTERY - NUM_LEDS_EME_EM] = CURRENT_COLOR;
- }
- else
- {
- clearAllLEDs();
- currentLED = -1;
- }
- currentLED++;
- delay(wait_ms);
- }
- void clearLEDs(CRGB leds[], uint16_t num_leds)
- {
- for(uint16_t i=0; i < num_leds; i++)
- {
- leds[i] = CRGB::Black;
- }
- }
- void clearAllLEDs()
- {
- clearLEDs(socket_hood, NUM_LEDS_SOCKET_HOOD);
- clearLEDs(hood_KLE_AC, NUM_LEDS_HOOD_KLE_AC);
- clearLEDs(KLE_AC_KLE_DC, NUM_LEDS_KLE_AC_KLE_DC);
- clearLEDs(hood_KLE_DC, NUM_LEDS_HOOD_KLE_DC);
- clearLEDs(KLE_EME_DC, NUM_LEDS_KLE_EME_DC);
- clearLEDs(KLE_EME_AC, NUM_LEDS_KLE_EME_AC);
- clearLEDs(EME_Battery, NUM_LEDS_EME_BATTERY);
- clearLEDs(EME_EDH, NUM_LEDS_EME_EDH);
- clearLEDs(EME_EKK, NUM_LEDS_EME_EKK);
- clearLEDs(EME_EM, NUM_LEDS_EME_EM);
- clearLEDs(CHASSIS, NUM_LEDS_CHASSIS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement