diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ab3ad38..3b3b016 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,6 @@ # This file was automatically generated for projects # without default 'CMakeLists.txt' file. +add_compile_options(" -Wno-unused-variable -Wno-unused-function -Wno-error") FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*) diff --git a/src/MyBLEServer.cpp b/src/MyBLEServer.cpp new file mode 100644 index 0000000..c929805 --- /dev/null +++ b/src/MyBLEServer.cpp @@ -0,0 +1,80 @@ +#include "MyBLEServer.hpp" +#include "config.hpp" + +#include "tlc.hpp" +#include +#include + +MyServerCallbacks::MyServerCallbacks(bool * connectionState) { + _BLEClientConnected = connectionState; +} + +void MyServerCallbacks::onConnect(BLEServer * pServer) { + // Serial.println("Callback connected"); + + deviceConnected = true; + *_BLEClientConnected = true; +} + +void MyServerCallbacks::onDisconnect(BLEServer * pServer) { + *_BLEClientConnected = false; + deviceConnected = false; +} + +bool MyServerCallbacks::getConnectionState() { + return *_BLEClientConnected; +} + +MyAlarmCallback::MyAlarmCallback(enum Alarm * alarm, enum Dimmer * dimmer) { + this->alarmFired = alarm; + this->dimmerState = dimmer; +} +void MyAlarmCallback::onWrite(BLECharacteristic * pCharacteristic) { + String value = pCharacteristic->getValue().c_str(); + + if (value.compareTo("X") == 0) { + // Serial.println("found X"); + // Serial.println(deviceConnected); + *this->alarmFired = ALARM_1KM; + } + else if (value.compareTo("XX") == 0) { + // Serial.println("found XX"); + // alarmFired_500m = 1; + *alarmFired = ALARM_500M; + } + else if (value.compareTo("XXX") == 0) { + // Serial.println("found XXX"); + // alarmFired_300m = 1; + *alarmFired = ALARM_300M; + } + else if (value.compareTo("XXXX") == 0) { + // Serial.println("found XXXX"); + // alarmFired_100m = 1; + *alarmFired = ALARM_100M; + } + else if (value.compareTo("brighter") == 0) { + // Serial.println("found brighter"); + // alarmFired_100m = 1; + // alarmFired = ALARM_100M; + // state = 0; + // brightnessFired = BRIGHTER; + *dimmerState = BRIGHTNESS_UP; + } + else if (value.compareTo("darker") == 0) { + // Serial.println("found darker"); + // alarmFired_100m = 1; + // alarmFired = ALARM_100M; + // state = 0; + // brightnessFired = DARKER; + *dimmerState = BRIGHTNESS_DOWN; + } + else if (value.compareTo("U") == 0) { + // reboot in WifiMode + // Serial.println("Found U"); + EEPROM.write(MODE_ADDRESS, MODE_WIFI); + EEPROM.commit(); + + // reboot + ESP.restart(); + } +} diff --git a/src/MyBLEServer.hpp b/src/MyBLEServer.hpp new file mode 100644 index 0000000..6cf2241 --- /dev/null +++ b/src/MyBLEServer.hpp @@ -0,0 +1,29 @@ +#ifndef MYBLESERVER_H +#define MYBLESERVER_H +#include "BLEServer.h" +#include "datatypes.hpp" +#include "tlc.hpp" + +class MyServerCallbacks : public BLEServerCallbacks { +public: + MyServerCallbacks(bool *); + bool getConnectionState(); + void onConnect(BLEServer * pServer); + void onDisconnect(BLEServer * pServer); + +private: + bool * _BLEClientConnected; + bool deviceConnected = false; +}; + +class MyAlarmCallback : public BLECharacteristicCallbacks { +public: + MyAlarmCallback(enum Alarm *, enum Dimmer *); + +private: + void onWrite(BLECharacteristic * pCharacteristic); + enum Alarm * alarmFired; + enum Dimmer * dimmerState; +}; + +#endif \ No newline at end of file diff --git a/src/config.hpp b/src/config.hpp index 85bccad..94b2e97 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -1,7 +1,7 @@ #ifndef CONFIG_H #define CONFIG_H // uncommend for very first flash -// #define FIRST_BOOT +// #define FIRST_BOOT // #define ALARM_LED GPIO_NUM_2 #define ALARM_LED GPIO_NUM_3 @@ -15,5 +15,4 @@ #define NUM_LEDS 7 // do not touch - #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 70c6abf..1939fa0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,17 +1,17 @@ -#include -#include #include "HWCDC.h" #include "esp32-hal-gpio.h" #include "esp32-hal.h" #include "freertos/portmacro.h" #include "hal/gpio_types.h" #include "secrets.hpp" +#include #include #include +#include #include "freertos/FreeRTOS.h" -#include "freertos/task.h" #include "freertos/queue.h" +#include "freertos/task.h" #include #include @@ -23,19 +23,18 @@ #include #include -#include -#include "tlc.hpp" +#include "MyBLEServer.hpp" #include "config.hpp" #include "datatypes.hpp" +#include "tlc.hpp" +#include // both lines are useless, when using rmt mode int pTime = 3; // in useconds -int tCycle = pTime * 6; // time for one bit. Per datasheet tCycle is between 0.33 useconds and 10 useconds +int tCycle = + pTime * 6; // time for one bit. Per datasheet tCycle is between 0.33 useconds and 10 useconds -//#define TESTMODE 1 - -const char * ssid = SECRET_SSID; -const char * password = SECRET_PASS; +// #define TESTMODE 1 bool _BLEClientConnected = false; @@ -46,136 +45,39 @@ BLEDescriptor AlarmLevelDescriptor(BLEUUID((uint16_t)0x2901)); // BLEServer * pServer = NULL; // bool volatile deviceConnected = false; bool volatile oldDeviceConnected = false; -// uint32_t value = 0; -enum NET_MODE mode; - -void displayWarning( void * params); +void displayWarning(void * params); void dimmer(void * params); enum Alarm alarmFired = NO_ALARM; enum Dimmer dimmerState = NO_DIMMER; +// class MyServerCallbacks : public BLEServerCallbacks { +// bool deviceConnected = false; +// void onConnect(BLEServer * pServer) { +// Serial.println("Callback connected"); -class MyServerCallbacks : public BLEServerCallbacks { - bool deviceConnected = false; +// deviceConnected = true; +// _BLEClientConnected = true; +// }; - void onConnect(BLEServer * pServer) { - Serial.println("Callback connected"); - - - deviceConnected = true; - _BLEClientConnected = true; - }; +// void onDisconnect(BLEServer * pServer) { +// _BLEClientConnected = false; +// deviceConnected = false; +// } - void onDisconnect(BLEServer * pServer) { - _BLEClientConnected = false; - deviceConnected = false; - } - public: - bool getConnectionState(){ - return deviceConnected; - } -}; +// public: +// bool getConnectionState() { return deviceConnected; } +// }; -bool brighter = false; -bool darker = false; - -enum Alarm saveFiredAlarm; -uint8_t saveAlarmState; - -int state = 0; - -byte brightnessState = 0; - -Cockpit cockpit(ALARM_PIN, NUM_LEDS, pTime, tCycle, brightnessState); - -TaskHandle_t thDisplayWarning; - -struct taskParams pvParams = { - .cockpit = &cockpit, - .alarmFired = &alarmFired, - .dimmerState = &dimmerState, - .thDisplayWarning = &thDisplayWarning -}; - -// var to count to with state for different distances -int stateLimit = 50; - -// save state of buttons -bool brightnessUpPressed = false; -bool brightnessDownPressed = false; - -enum Brightness brightnessFired = NO_CHANGE; - - - -class MyAlarmCallback : public BLECharacteristicCallbacks { - void onWrite(BLECharacteristic * pCharacteristic) { - String value = pCharacteristic->getValue().c_str(); - - if (value.compareTo("X") == 0) { - Serial.println("found X"); - // Serial.println(deviceConnected); - alarmFired = ALARM_1KM; - state = 0; - } - else if (value.compareTo("XX") == 0) { - Serial.println("found XX"); - // alarmFired_500m = 1; - alarmFired = ALARM_500M; - state = 0; - } - else if (value.compareTo("XXX") == 0) { - Serial.println("found XXX"); - // alarmFired_300m = 1; - alarmFired = ALARM_300M; - state = 0; - } - else if (value.compareTo("XXXX") == 0) { - Serial.println("found XXXX"); - // alarmFired_100m = 1; - alarmFired = ALARM_100M; - state = 0; - } - else if (value.compareTo("brighter") == 0) { - Serial.println("found brighter"); - // alarmFired_100m = 1; - // alarmFired = ALARM_100M; - // state = 0; - // brightnessFired = BRIGHTER; - dimmerState = BRIGHTNESS_UP; - - } - else if (value.compareTo("darker") == 0) { - Serial.println("found darker"); - // alarmFired_100m = 1; - // alarmFired = ALARM_100M; - // state = 0; - // brightnessFired = DARKER; - dimmerState = BRIGHTNESS_DOWN; - - } - else if (value.compareTo("U") == 0) { - // reboot in WifiMode - Serial.println("Found U"); - EEPROM.write(MODE_ADDRESS, MODE_WIFI); - EEPROM.commit(); - - // reboot - ESP.restart(); - } - } -}; - -void InitBLE(BLEServer *pServer, MyServerCallbacks * serverCallback, MyAlarmCallback * myAlarmCallback) { +void InitBLE(BLEServer * pServer, MyServerCallbacks * serverCallback, + MyAlarmCallback * myAlarmCallback) { BLEDevice::init("OilCheck"); pServer = BLEDevice::createServer(); Serial.println("Start BLE"); - pServer->setCallbacks(serverCallback); // Create BLE Service @@ -196,32 +98,28 @@ void InitBLE(BLEServer *pServer, MyServerCallbacks * serverCallback, MyAlarmCa pAdvertising->addServiceUUID(AlarmService); pAdvertising->setScanResponse(true); BLEDevice::startAdvertising(); - } -volatile int ctr = 0; -hw_timer_t * timer = NULL; -portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; - -void IRAM_ATTR onTimer() { - portENTER_CRITICAL_ISR(&timerMux); - ctr++; - portEXIT_CRITICAL_ISR(&timerMux); -} - -// volatile int pc = 0; - // Create the BLE Server -BLEServer * pServer = NULL; -MyServerCallbacks *serverCallback = new MyServerCallbacks(); -MyAlarmCallback * myAlarmCallback = new MyAlarmCallback(); +BLEServer * pServer = NULL; +MyServerCallbacks * serverCallback = new MyServerCallbacks(&_BLEClientConnected); +MyAlarmCallback * myAlarmCallback = new MyAlarmCallback(&alarmFired, &dimmerState); +enum NET_MODE mode; +byte brightnessState = 0; +Cockpit cockpit(ALARM_PIN, NUM_LEDS, pTime, tCycle, brightnessState); + +TaskHandle_t thDisplayWarning; +struct taskParams pvParams = {.cockpit = &cockpit, + .alarmFired = &alarmFired, + .dimmerState = &dimmerState, + .thDisplayWarning = &thDisplayWarning}; void setup() { // //setup GPIO pinMode(BRIGHTER_PIN, INPUT_PULLUP); pinMode(DARKER_PIN, INPUT_PULLUP); - pinMode(GPIO_NUM_10, INPUT); // former Alarm pin + pinMode(GPIO_NUM_10, INPUT); // former Alarm pin Serial.begin(9600); delay(1000); @@ -232,25 +130,16 @@ void setup() { Serial.println("Hallo Welt"); EEPROM.begin(EEPROM_SIZE); - #if defined(FIRST_BOOT) +#if defined(FIRST_BOOT) // needs to be uncommend for very first boot - EEPROM.write(MODE_ADDRESS, MODE_BLE); - EEPROM.commit(); - #endif + EEPROM.write(MODE_ADDRESS, MODE_BLE); + EEPROM.commit(); +#endif mode = static_cast(EEPROM.read(MODE_ADDRESS)); - // mode = MODE_WIFI; if (mode == MODE_BLE) { - // upcounting timer 1MHz - timer = timerBegin(1, 80, true); - timerAttachInterrupt(timer, &onTimer, true); - timerAlarmWrite(timer, 100000, true); - timerAlarmEnable(timer); - - - rmt_config_t config = RMT_DEFAULT_CONFIG_TX(ALARM_PIN, RMT_CHANNEL_0); config.clk_div = 160; // 80 config.tx_config.carrier_en = false; @@ -259,21 +148,20 @@ void setup() { ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0)); InitBLE(pServer, serverCallback, myAlarmCallback); - - xTaskCreate(displayWarning, "Warning", 1000, &pvParams, tskIDLE_PRIORITY, &thDisplayWarning); + + xTaskCreate(displayWarning, "Warning", 1000, &pvParams, tskIDLE_PRIORITY, + &thDisplayWarning); xTaskCreate(dimmer, "Dimmer", 10000, &pvParams, tskIDLE_PRIORITY, NULL); - } else if (mode == MODE_WIFI) { // boot next time into ble mode - EEPROM.write(MODE_ADDRESS, MODE_BLE); EEPROM.commit(); - // delay(3000); - Serial.println("Booting in Wifimode"); WiFi.mode(WIFI_STA); + const char * ssid = SECRET_SSID; + const char * password = SECRET_PASS; WiFi.begin(ssid, password); while (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("Connection Failed! "); @@ -319,13 +207,6 @@ void setup() { Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); - // // WiFi.mode(WIFI_STA); - // WiFi.begin(ssid, password); - // while (WiFi.waitForConnectResult() != WL_CONNECTED) { - // delay(3000); - // break; - // } - // ArduinoOTA.begin(); } } @@ -336,57 +217,49 @@ void loop() { // Serial.print("WIFI"); } else { - - vTaskDelay(100/portTICK_PERIOD_MS); + + vTaskDelay(100 / portTICK_PERIOD_MS); if (serverCallback->getConnectionState()) { - } // disconnecting if (!serverCallback->getConnectionState() && oldDeviceConnected) { - delay(500); // give the bluetooth stack the chance to get things ready - pServer->startAdvertising(); // restart advertising - Serial.println("start advertising"); + delay(500); // give the bluetooth stack the chance to get thingsready + pServer->startAdvertising(); // restart advertising Serial.println("startadvertising "); oldDeviceConnected = serverCallback->getConnectionState(); } // connecting if (serverCallback->getConnectionState() && !oldDeviceConnected) { // do stuff here on connecting oldDeviceConnected = serverCallback->getConnectionState(); - - Color c[] = { - Cockpit::YELLOW, - Cockpit::RED, - Cockpit::PURPLE, - Cockpit::BLUE, - Cockpit::CYAN, - Cockpit::GREEN, - Cockpit::YELLOW, - Cockpit::RED, - Cockpit::PURPLE, - Cockpit::BLUE, - Cockpit::CYAN, - Cockpit::GREEN - }; + + Color c[] = {Cockpit::YELLOW, Cockpit::RED, Cockpit::PURPLE, Cockpit::BLUE, + Cockpit::CYAN, Cockpit::GREEN, Cockpit::YELLOW, Cockpit::RED, + Cockpit::PURPLE, Cockpit::BLUE, Cockpit::CYAN, Cockpit::GREEN}; // for(int i = 0; i < 36; i++){ // // cockpit.knightRiderAdvanced(red, i); // // cockpit.outToIn(red, i); // cockpit.outToInMulticolor(c, 6, i); // // cockpit.knightRiderColored(i, red); - // ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cockpit.led_data, sizeof(cockpit.led_data) / sizeof(rmt_item32_t), true)); - // delay(100); + // ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cockpit.led_data, + // sizeof(cockpit.led_data) / sizeof(rmt_item32_t), true)); delay(100); // } - for(int i = 0; i < 50; i++){ + for (int i = 0; i < 49; i++) { // cockpit.knightRiderAdvanced(red, i); // cockpit.outToIn(red, i); cockpit.inToOutMulticolor(c, 12, i); + // cockpit.speedUpBounce(c[0], i); // cockpit.knightRiderColored(i, red); - ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cockpit.led_data, sizeof(cockpit.led_data) / sizeof(rmt_item32_t), true)); + ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cockpit.led_data, + sizeof(cockpit.led_data) / sizeof(rmt_item32_t), + true)); + // uint8_t myDelay = uint8_t(7 / 250.0 * i - 14 / 5.0 * i + 100); delay(100); } delay(10); cockpit.turnOffLeds(); - ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cockpit.led_data, sizeof(cockpit.led_data) / sizeof(rmt_item32_t), true)); + ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cockpit.led_data, + sizeof(cockpit.led_data) / sizeof(rmt_item32_t), true)); } } } \ No newline at end of file diff --git a/src/tasks.cpp b/src/tasks.cpp index 5a5526d..76daa97 100644 --- a/src/tasks.cpp +++ b/src/tasks.cpp @@ -1,13 +1,13 @@ +#include "tasks.hpp" #include "Arduino.h" #include "config.hpp" -#include "tasks.hpp" -#include "tlc.hpp" #include "datatypes.hpp" +#include "tlc.hpp" -void displayWarning( void * params){ - struct taskParams *myParams = (struct taskParams*)params; - Cockpit *cp = myParams->cockpit; - enum Alarm *alarm = myParams->alarmFired; +void displayWarning(void * params) { + struct taskParams * myParams = (struct taskParams *)params; + Cockpit * cp = myParams->cockpit; + enum Alarm * alarm = myParams->alarmFired; enum Alarm alarmStatebefore = NO_ALARM; uint8_t alarmStateChanged = 0; @@ -16,135 +16,140 @@ void displayWarning( void * params){ alarmStatebefore = *alarm; while (true) { - if(*alarm != alarmStatebefore){ + if (*alarm != alarmStatebefore) { alarmStateChanged = 1; } - else{ + else { alarmStateChanged = 0; } switch (*alarm) { - case NO_ALARM: - vTaskDelay(100/portTICK_PERIOD_MS); + case NO_ALARM: + vTaskDelay(100 / portTICK_PERIOD_MS); break; - case ALARM_100M: - if(alarmStateChanged){ - alarmStatebefore = ALARM_100M; - statePreload = 30; - - } - cp->flashing(Cockpit::WHITE, statePreload); - ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); - statePreload--; - if(statePreload == 0){ - *alarm = NO_ALARM; - alarmStatebefore = NO_ALARM; - } - vTaskDelay(300/portTICK_PERIOD_MS); - break; - case ALARM_300M: - if(alarmStateChanged){ - alarmStatebefore = ALARM_300M; - statePreload = 20; - } - cp->flashing(Cockpit::ORANGE, statePreload); - ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); - statePreload--; - if(statePreload == 0){ - *alarm = NO_ALARM; - alarmStatebefore = NO_ALARM; - } - vTaskDelay(500/portTICK_PERIOD_MS); - break; - case ALARM_500M: - // Serial.print("Alarm 500"); - if(alarmStateChanged){ - alarmStatebefore = ALARM_500M; - statePreload = 20; - } - cp->flashing(Cockpit::GREEN, statePreload); - ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); - statePreload--; - if(statePreload == 0){ - *alarm = NO_ALARM; - alarmStatebefore = NO_ALARM; - } - vTaskDelay(500/portTICK_PERIOD_MS); - break; + case ALARM_100M: + if (alarmStateChanged) { + alarmStatebefore = ALARM_100M; + statePreload = 30; + } + cp->flashing(Cockpit::WHITE, statePreload); + ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, + sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); + statePreload--; + if (statePreload == 0) { + *alarm = NO_ALARM; + alarmStatebefore = NO_ALARM; + } + vTaskDelay(300 / portTICK_PERIOD_MS); break; - case ALARM_1KM: - if(alarmStateChanged){ - alarmStatebefore = ALARM_1KM; - statePreload = 20; - // statePreload = 120; - } - // cp->flashing(orange, statePreload); - // cp->colorfade(statePreload); - cp->flashing(Cockpit::BLUE, statePreload); - - ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); - statePreload--; - if(statePreload == 0){ - *alarm = NO_ALARM; - alarmStatebefore = NO_ALARM; - cp->turnOffLeds(); - ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); - } - vTaskDelay(500/portTICK_PERIOD_MS); + case ALARM_300M: + if (alarmStateChanged) { + alarmStatebefore = ALARM_300M; + statePreload = 20; + } + cp->flashing(Cockpit::ORANGE, statePreload); + ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, + sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); + statePreload--; + if (statePreload == 0) { + *alarm = NO_ALARM; + alarmStatebefore = NO_ALARM; + } + vTaskDelay(500 / portTICK_PERIOD_MS); break; - default: - vTaskDelay(1000/portTICK_PERIOD_MS); + case ALARM_500M: + // Serial.print("Alarm 500"); + if (alarmStateChanged) { + alarmStatebefore = ALARM_500M; + statePreload = 20; + } + cp->flashing(Cockpit::GREEN, statePreload); + ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, + sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); + statePreload--; + if (statePreload == 0) { + *alarm = NO_ALARM; + alarmStatebefore = NO_ALARM; + } + vTaskDelay(500 / portTICK_PERIOD_MS); + break; + break; + case ALARM_1KM: + if (alarmStateChanged) { + alarmStatebefore = ALARM_1KM; + statePreload = 20; + // statePreload = 120; + } + // cp->flashing(orange, statePreload); + // cp->colorfade(statePreload); + cp->flashing(Cockpit::BLUE, statePreload); + ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, + sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); + statePreload--; + if (statePreload == 0) { + *alarm = NO_ALARM; + alarmStatebefore = NO_ALARM; + cp->turnOffLeds(); + ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, + sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); + } + vTaskDelay(500 / portTICK_PERIOD_MS); + break; + default: + vTaskDelay(1000 / portTICK_PERIOD_MS); } // Serial.println(uxTaskGetStackHighWaterMark(NULL)); } } -void dimmer(void * params){ - struct taskParams *myParams = (struct taskParams*)params; - Cockpit *cp = myParams->cockpit; - enum Dimmer *dimmerState = myParams->dimmerState; +void dimmer(void * params) { + struct taskParams * myParams = (struct taskParams *)params; + Cockpit * cp = myParams->cockpit; + enum Dimmer * dimmerState = myParams->dimmerState; - TaskHandle_t *thDisplayWarning = myParams->thDisplayWarning; + TaskHandle_t * thDisplayWarning = myParams->thDisplayWarning; uint8_t brighterPressed = 0; uint8_t darkerPressed = 0; - uint8_t ctrBrightnessChanged = 0; + uint8_t ctrBrightnessChanged = 0; while (1) { - - if((gpio_get_level(BRIGHTER_PIN) == 0 && brighterPressed == 0) || *dimmerState == BRIGHTNESS_UP){ + + if ((gpio_get_level(BRIGHTER_PIN) == 0 && brighterPressed == 0) || + *dimmerState == BRIGHTNESS_UP) { // brighter brighterPressed = 1; ctrBrightnessChanged = 10; *dimmerState = NO_DIMMER; - if(eTaskGetState(*thDisplayWarning) != eTaskState::eDeleted){ + if (eTaskGetState(*thDisplayWarning) != eTaskState::eDeleted) { vTaskSuspend(*thDisplayWarning); - } cp->brightnessUp(); cp->displayBrightness(); - ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); + ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, + sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); } if (gpio_get_level(BRIGHTER_PIN) == 1 && brighterPressed == 1) { // brighter button released brighterPressed = 0; } - if((gpio_get_level(DARKER_PIN) == 0 && darkerPressed == 0) || *dimmerState == BRIGHTNESS_DOWN){ + if ((gpio_get_level(DARKER_PIN) == 0 && darkerPressed == 0) || + *dimmerState == BRIGHTNESS_DOWN) { // darker darkerPressed = 1; ctrBrightnessChanged = 10; *dimmerState = NO_DIMMER; - if(eTaskGetState(*thDisplayWarning) != eTaskState::eDeleted){ + if (eTaskGetState(*thDisplayWarning) != eTaskState::eDeleted) { vTaskSuspend(*thDisplayWarning); - } cp->brightnessDown(); cp->displayBrightness(); - ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); + ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, + sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); } if (gpio_get_level(DARKER_PIN) == 1 && darkerPressed == 1) { // darker button released @@ -152,20 +157,24 @@ void dimmer(void * params){ } vTaskDelay(100 / portTICK_PERIOD_MS); - - if (ctrBrightnessChanged > 0){ + + if (ctrBrightnessChanged > 0) { ctrBrightnessChanged--; - if(ctrBrightnessChanged == 0){ + if (ctrBrightnessChanged == 0) { // check if there are suspended tasks - if(eTaskGetState(*thDisplayWarning) == eTaskState::eSuspended){ + if (eTaskGetState(*thDisplayWarning) == eTaskState::eSuspended) { cp->turnOffLeds(); - ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); + ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, + sizeof(cp->led_data) / sizeof(rmt_item32_t), + true)); vTaskResume(*thDisplayWarning); } - else{ + else { cp->turnOffLeds(); - ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, sizeof(cp->led_data) / sizeof(rmt_item32_t), true)); + ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, + sizeof(cp->led_data) / sizeof(rmt_item32_t), + true)); } } } diff --git a/src/tlc.cpp b/src/tlc.cpp index ca4b3b7..35ea832 100644 --- a/src/tlc.cpp +++ b/src/tlc.cpp @@ -1,95 +1,118 @@ #include "tlc.hpp" -Color Cockpit::ORANGE = {.blue=0, .red=255, .green=0xA5}; -Color Cockpit::RED = {.blue=0, .red=255, .green=0}; -Color Cockpit::BLUE = {.blue=255, .red=0, .green=0}; -Color Cockpit::GREEN = {.blue=0, .red=0, .green=255}; -Color Cockpit::CYAN = {.blue=0xff, .red=0, .green=0xff}; -Color Cockpit::WHITE = {.blue=255, .red=255, .green=255}; -Color Cockpit::BLACK = {.blue=0, .red=0, .green=0}; -Color Cockpit::YELLOW = {.blue=0, .red=0xFF, .green=0xC0}; -Color Cockpit::PURPLE = {.blue=0xBF, .red=0xBF, .green=0x40}; +Color Cockpit::ORANGE = {.blue = 0, .red = 255, .green = 0xA5}; +Color Cockpit::RED = {.blue = 0, .red = 255, .green = 0}; +Color Cockpit::BLUE = {.blue = 255, .red = 0, .green = 0}; +Color Cockpit::GREEN = {.blue = 0, .red = 0, .green = 255}; +Color Cockpit::CYAN = {.blue = 0xff, .red = 0, .green = 0xff}; +Color Cockpit::WHITE = {.blue = 255, .red = 255, .green = 255}; +Color Cockpit::BLACK = {.blue = 0, .red = 0, .green = 0}; +Color Cockpit::YELLOW = {.blue = 0, .red = 0xFF, .green = 0xC0}; +Color Cockpit::PURPLE = {.blue = 0xBF, .red = 0xBF, .green = 0x40}; +Cockpit::Cockpit(gpio_num_t pin, int leds, int pTime, int tCycle, uint8_t brightnessState) { + this->pin = pin; + this->leds = leds; + this->pTime = pTime; + this->tCycle = tCycle; + // this->brightness = brightness; + this->brightnessState = brightnessState; -Cockpit::Cockpit(gpio_num_t pin, int leds, int pTime, int tCycle, uint8_t brightnessState){ - this->pin = pin; - this->leds = leds; - this->pTime = pTime; - this->tCycle = tCycle; - // this->brightness = brightness; - this->brightnessState = brightnessState; - - this->cp = 0; - + this->cp = 0; } -Color Cockpit::scaleBrightness(Color color, uint8_t dimmfactor){ +Color Cockpit::scaleBrightness(Color color, uint8_t dimmfactor) { // dimmfactor is an addition option to dimm led Color scaledColor = color; - scaledColor.blue = uint8_t(this->brightness[brightnessState] / 255.0 * color.blue * dimmfactor /100.0); - scaledColor.red = uint8_t(this->brightness[brightnessState] / 255.0 * color.red * dimmfactor /100.0); - scaledColor.green = uint8_t(this->brightness[brightnessState] / 255.0 * color.green * dimmfactor /100.0); + scaledColor.blue = + uint8_t(this->brightness[brightnessState] / 255.0 * color.blue * dimmfactor / 100.0); + scaledColor.red = + uint8_t(this->brightness[brightnessState] / 255.0 * color.red * dimmfactor / 100.0); + scaledColor.green = + uint8_t(this->brightness[brightnessState] / 255.0 * color.green * dimmfactor / 100.0); return scaledColor; } -void Cockpit::setColorLed(struct Color color, uint8_t led){ +void Cockpit::setColorLed(struct Color color, uint8_t led) { this->stateLEDs[led] = color; } -void Cockpit::fillFromLeft(Color color, int state){ +void Cockpit::fillFromLeft(Color color, int state) { // scale state to sufficient range state = state % NUM_LEDS; turnOffLeds(); - for(uint8_t led = 0; led <= state; led++){ + for (uint8_t led = 0; led <= state; led++) { this->stateLEDs[led] = scaleBrightness(color); } this->writeLEDs(); } -void Cockpit::outToIn(Color color, int state){ +void Cockpit::speedUpBounce(Color color, int state) { + // lights bounces from left to right with increasing speed + + turnOffLeds(); + + uint8_t position = state % 12; + if (position < NUM_LEDS) { + this->stateLEDs[position] = scaleBrightness(color); + } + else { + // 7 -> 5 + // 8 -> 4 + // 9 -> 3 + // 10-> 2 + // 11-> 1 + position = -1 * position + 12; + this->stateLEDs[position] = scaleBrightness(color); + } + + writeLEDs(); +} + +void Cockpit::outToIn(Color color, int state) { state = state % 5; turnOffLeds(); - for(int8_t i = -1; i <= state; i++){ - if(i < 0){ + for (int8_t i = -1; i <= state; i++) { + if (i < 0) { this->stateLEDs[i] = BLACK; } - else{ - this->stateLEDs[i-1] = scaleBrightness(color); - this->stateLEDs[NUM_LEDS-i] = scaleBrightness(color); + else { + this->stateLEDs[i - 1] = scaleBrightness(color); + this->stateLEDs[NUM_LEDS - i] = scaleBrightness(color); } } writeLEDs(); } -void Cockpit::outToInMulticolor(Color color[], uint8_t colors, int state){ +void Cockpit::outToInMulticolor(Color color[], uint8_t colors, int state) { uint8_t curColorIndex = state / 5; state = state % 5; turnOffLeds(); - if(curColorIndex == 0){ - for(int8_t i = -1; i <= state; i++){ - if(i < 0){ + if (curColorIndex == 0) { + for (int8_t i = -1; i <= state; i++) { + if (i < 0) { this->stateLEDs[i] = BLACK; } - else{ - if(curColorIndex < colors && i-1 < NUM_LEDS && NUM_LEDS-i < NUM_LEDS){ // just for safety - this->stateLEDs[i-1] = scaleBrightness(color[curColorIndex]); - this->stateLEDs[NUM_LEDS-i] = scaleBrightness(color[curColorIndex]); + else { + if (curColorIndex < colors && i - 1 < NUM_LEDS && + NUM_LEDS - i < NUM_LEDS) { // just for safety + this->stateLEDs[i - 1] = scaleBrightness(color[curColorIndex]); + this->stateLEDs[NUM_LEDS - i] = scaleBrightness(color[curColorIndex]); } } } } - else{ + else { // predraw last color - for(uint8_t i = 0; i < NUM_LEDS; i++){ - this->stateLEDs[i] = scaleBrightness(color[curColorIndex-1]); + for (uint8_t i = 0; i < NUM_LEDS; i++) { + this->stateLEDs[i] = scaleBrightness(color[curColorIndex - 1]); } - for(int8_t i = 0; i < state; i++){ + for (int8_t i = 0; i < state; i++) { this->stateLEDs[i] = scaleBrightness(color[curColorIndex]); - this->stateLEDs[NUM_LEDS-i-1] = scaleBrightness(color[curColorIndex]); + this->stateLEDs[NUM_LEDS - i - 1] = scaleBrightness(color[curColorIndex]); } - - } + } writeLEDs(); } @@ -98,53 +121,52 @@ void Cockpit::inToOutMulticolor(Color color[], uint8_t colors, int state) { state = state % 4; turnOffLeds(); - if(curColorIndex == 0){ - for(int8_t i = 0; i <= state; i++){ + if (curColorIndex == 0) { + for (int8_t i = 0; i <= state; i++) { // left part of leds - this->stateLEDs[3-i] = scaleBrightness(color[curColorIndex]); - //right part of leds - this->stateLEDs[3+i] = scaleBrightness(color[curColorIndex]); + this->stateLEDs[3 - i] = scaleBrightness(color[curColorIndex]); + // right part of leds + this->stateLEDs[3 + i] = scaleBrightness(color[curColorIndex]); } } - else{ + else { // predraw last color - for(uint8_t i = 0; i < NUM_LEDS; i++){ - this->stateLEDs[i] = scaleBrightness(color[curColorIndex-1]); + for (uint8_t i = 0; i < NUM_LEDS; i++) { + this->stateLEDs[i] = scaleBrightness(color[curColorIndex - 1]); } - for(int8_t i = 0; i < state; i++){ + for (int8_t i = 0; i < state; i++) { // left part of leds - this->stateLEDs[3-i] = scaleBrightness(color[curColorIndex]); - //right part of leds - this->stateLEDs[3+i] = scaleBrightness(color[curColorIndex]); + this->stateLEDs[3 - i] = scaleBrightness(color[curColorIndex]); + // right part of leds + this->stateLEDs[3 + i] = scaleBrightness(color[curColorIndex]); } - - } + } writeLEDs(); } -void Cockpit::colorfade(int state){ +void Cockpit::colorfade(int state) { // 60 steps to get around the color circle state = state % 60; - if(state < 20){ //red -> blue - for(uint8_t led = 0; led < NUM_LEDS; led++){ - this->stateLEDs[led].red = uint8_t(255 / 20.0 * (20-state)); + if (state < 20) { // red -> blue + for (uint8_t led = 0; led < NUM_LEDS; led++) { + this->stateLEDs[led].red = uint8_t(255 / 20.0 * (20 - state)); this->stateLEDs[led].blue = uint8_t(255 / 20.0 * state); this->stateLEDs[led].green = 0; this->stateLEDs[led] = scaleBrightness(this->stateLEDs[led]); } } - else if (state < 40){ //blue -> green - for(uint8_t led = 0; led < NUM_LEDS; led++){ - this->stateLEDs[led].blue = uint8_t(255 / 20 * (20-(state-20))); - this->stateLEDs[led].green = uint8_t(255 / 20 * (state-20)); + else if (state < 40) { // blue -> green + for (uint8_t led = 0; led < NUM_LEDS; led++) { + this->stateLEDs[led].blue = uint8_t(255 / 20 * (20 - (state - 20))); + this->stateLEDs[led].green = uint8_t(255 / 20 * (state - 20)); this->stateLEDs[led].red = 0; this->stateLEDs[led] = scaleBrightness(this->stateLEDs[led]); } } - else{//green -> red - for(uint8_t led = 0; led < NUM_LEDS; led++){ - this->stateLEDs[led].green = uint8_t(255 / 20 * (20-(state-40))); - this->stateLEDs[led].red = uint8_t(255 / 20 * (state-40)); + else { // green -> red + for (uint8_t led = 0; led < NUM_LEDS; led++) { + this->stateLEDs[led].green = uint8_t(255 / 20 * (20 - (state - 40))); + this->stateLEDs[led].red = uint8_t(255 / 20 * (state - 40)); this->stateLEDs[led].blue = 0; this->stateLEDs[led] = scaleBrightness(this->stateLEDs[led]); } @@ -174,146 +196,145 @@ void Cockpit::colorfade(int state){ // break; // case 2: // this->stateLEDs[0][color] = heavyDimmedIntensitiy; -// this->stateLEDs[1][color] = dimmedIntensitiy; +// this->stateLEDs[1][color] = dimmedIntensitiy; // this->stateLEDs[2][color] = brightness; // // step++; // break; // case 3: // this->stateLEDs[0][color] = 0; -// this->stateLEDs[1][color] = heavyDimmedIntensitiy; -// this->stateLEDs[2][color] = dimmedIntensitiy; +// this->stateLEDs[1][color] = heavyDimmedIntensitiy; +// this->stateLEDs[2][color] = dimmedIntensitiy; // this->stateLEDs[3][color] = brightness; // // step++; // break; // case 4: // this->stateLEDs[1][color] = 0; -// this->stateLEDs[2][color] = heavyDimmedIntensitiy; -// this->stateLEDs[3][color] = dimmedIntensitiy; +// this->stateLEDs[2][color] = heavyDimmedIntensitiy; +// this->stateLEDs[3][color] = dimmedIntensitiy; // this->stateLEDs[4][color] = brightness; // // step++; // break; // case 5: // this->stateLEDs[2][color] = 0; -// this->stateLEDs[3][color] = heavyDimmedIntensitiy; -// this->stateLEDs[4][color] = dimmedIntensitiy; +// this->stateLEDs[3][color] = heavyDimmedIntensitiy; +// this->stateLEDs[4][color] = dimmedIntensitiy; // this->stateLEDs[5][color] = brightness; // // step++; // break; // case 6: // this->stateLEDs[3][color] = 0; -// this->stateLEDs[4][color] = heavyDimmedIntensitiy; -// this->stateLEDs[5][color] = dimmedIntensitiy; +// this->stateLEDs[4][color] = heavyDimmedIntensitiy; +// this->stateLEDs[5][color] = dimmedIntensitiy; // this->stateLEDs[6][color] = brightness; // // step++; // break; // case 7: -// this->stateLEDs[4][color] = 0; -// this->stateLEDs[5][color] = brightness; +// this->stateLEDs[4][color] = 0; +// this->stateLEDs[5][color] = brightness; // this->stateLEDs[6][color] = dimmedIntensitiy; // // step++; // break; // case 8: -// this->stateLEDs[4][color] = brightness; -// this->stateLEDs[5][color] = dimmedIntensitiy; +// this->stateLEDs[4][color] = brightness; +// this->stateLEDs[5][color] = dimmedIntensitiy; // this->stateLEDs[6][color] = heavyDimmedIntensitiy; // // step++; // break; // case 9: -// this->stateLEDs[3][color] = brightness; -// this->stateLEDs[4][color] = dimmedIntensitiy; -// this->stateLEDs[5][color] = heavyDimmedIntensitiy; +// this->stateLEDs[3][color] = brightness; +// this->stateLEDs[4][color] = dimmedIntensitiy; +// this->stateLEDs[5][color] = heavyDimmedIntensitiy; // this->stateLEDs[6][color] = 0; // // step++; // break; // case 10: -// this->stateLEDs[2][color] = brightness; -// this->stateLEDs[3][color] = dimmedIntensitiy; -// this->stateLEDs[4][color] = heavyDimmedIntensitiy; +// this->stateLEDs[2][color] = brightness; +// this->stateLEDs[3][color] = dimmedIntensitiy; +// this->stateLEDs[4][color] = heavyDimmedIntensitiy; // this->stateLEDs[5][color] = 0; // // step++; -// break; +// break; // case 11: -// this->stateLEDs[1][color] = brightness; -// this->stateLEDs[2][color] = dimmedIntensitiy; -// this->stateLEDs[3][color] = heavyDimmedIntensitiy; +// this->stateLEDs[1][color] = brightness; +// this->stateLEDs[2][color] = dimmedIntensitiy; +// this->stateLEDs[3][color] = heavyDimmedIntensitiy; // this->stateLEDs[4][color] = 0; // // step++; // break; // case 12: -// this->stateLEDs[0][color] = brightness; -// this->stateLEDs[1][color] = dimmedIntensitiy; -// this->stateLEDs[2][color] = heavyDimmedIntensitiy; +// this->stateLEDs[0][color] = brightness; +// this->stateLEDs[1][color] = dimmedIntensitiy; +// this->stateLEDs[2][color] = heavyDimmedIntensitiy; // this->stateLEDs[3][color] = 0; // // step++; // break; - + // default: // break; // } -// this->writeLEDs(); +// this->writeLEDs(); // } -void Cockpit::knightRiderAdvanced(Color color, int state){ +void Cockpit::knightRiderAdvanced(Color color, int state) { this->turnOffLeds(); // 15 states state = state % 15; - if(state < 7){ + if (state < 7) { this->stateLEDs[state] = scaleBrightness(color); - if(state > 0){ - this->stateLEDs[state-1] = scaleBrightness(color, 70); + if (state > 0) { + this->stateLEDs[state - 1] = scaleBrightness(color, 70); } - if(state > 1){ - this->stateLEDs[state-2] = scaleBrightness(color, 30); + if (state > 1) { + this->stateLEDs[state - 2] = scaleBrightness(color, 30); } } - else if(state < 14){ - this->stateLEDs[2*NUM_LEDS-(state)-1] = scaleBrightness(color); - if(state > 8){ - this->stateLEDs[2*NUM_LEDS-(state)-1+1] = scaleBrightness(color, 70); + else if (state < 14) { + this->stateLEDs[2 * NUM_LEDS - (state)-1] = scaleBrightness(color); + if (state > 8) { + this->stateLEDs[2 * NUM_LEDS - (state)-1 + 1] = scaleBrightness(color, 70); } - if(state > 9){ - this->stateLEDs[2*NUM_LEDS-(state)-1+2] = scaleBrightness(color, 30); + if (state > 9) { + this->stateLEDs[2 * NUM_LEDS - (state)-1 + 2] = scaleBrightness(color, 30); } } - else{ + else { this->turnOffLeds(); } this->writeLEDs(); } -uint8_t scaleColor(uint8_t color, int scaleFactor){ - return uint8_t(color * (float)scaleFactor / 255); +uint8_t scaleColor(uint8_t color, int scaleFactor) { + return uint8_t(color * (float)scaleFactor / 255); } - -void Cockpit::turnOffLeds(){ - for(uint8_t led = 0; led < NUM_LEDS; led++){ +void Cockpit::turnOffLeds() { + for (uint8_t led = 0; led < NUM_LEDS; led++) { this->stateLEDs[led] = BLACK; - } - this->writeLEDs(); + } + this->writeLEDs(); } -void Cockpit::brightnessUp(){ - if(this->brightnessState < 6){ +void Cockpit::brightnessUp() { + if (this->brightnessState < 6) { this->brightnessState++; } } -void Cockpit::brightnessDown(){ - if(this->brightnessState > 0){ +void Cockpit::brightnessDown() { + if (this->brightnessState > 0) { this->brightnessState--; } } -void Cockpit::displayBrightness(Color color){ - for(int i = 0; i < this->leds; i++){ - if(i <= brightnessState){ +void Cockpit::displayBrightness(Color color) { + for (int i = 0; i < this->leds; i++) { + if (i <= brightnessState) { this->stateLEDs[i] = scaleBrightness(color); // this->stateLEDs[i][0] = this->brightness[this->brightnessState]; // this->stateLEDs[i][1] = 0; // this->stateLEDs[i][2] = 0; } - else{ + else { this->stateLEDs[i] = BLACK; // this->stateLEDs[i][0] = 0; // this->stateLEDs[i][1] = 0; @@ -350,382 +371,373 @@ void Cockpit::displayBrightness(Color color){ // } // } - -void Cockpit::alternating(Color color, int state, int frequencyDivider){ - if(state % (frequencyDivider*2) < frequencyDivider){ - state = 0; - } - else{ - state = 1; - } - if(state % 2 == 0){ - for(uint8_t led = 0; led < this->leds; led++){ - if(led % 2 == 0){ +void Cockpit::alternating(Color color, int state, int frequencyDivider) { + if (state % (frequencyDivider * 2) < frequencyDivider) { + state = 0; + } + else { + state = 1; + } + if (state % 2 == 0) { + for (uint8_t led = 0; led < this->leds; led++) { + if (led % 2 == 0) { this->stateLEDs[led] = scaleBrightness(color); - } - else{ - this->stateLEDs[led] = BLACK; - } - - } - this->writeLEDs(); - } - else{ - for(uint8_t led = 0; led < this->leds; led++){ - if(led % 2 == 1){ + } + else { + this->stateLEDs[led] = BLACK; + } + } + this->writeLEDs(); + } + else { + for (uint8_t led = 0; led < this->leds; led++) { + if (led % 2 == 1) { this->stateLEDs[led] = scaleBrightness(color); - } - else{ - this->stateLEDs[led] = BLACK; - } - - } - this->writeLEDs(); - } + } + else { + this->stateLEDs[led] = BLACK; + } + } + this->writeLEDs(); + } } -void Cockpit::flashing(Color color, int state){ - - if(state % 2 == 0){ - for(uint8_t led = 0; led < this->leds; led++){ +void Cockpit::flashing(Color color, int state) { + + if (state % 2 == 0) { + for (uint8_t led = 0; led < this->leds; led++) { this->stateLEDs[led] = scaleBrightness(color); - } - this->writeLEDs(); - } - else{ - for(uint8_t led = 0; led < this->leds; led++){ - this->stateLEDs[led] = BLACK; - } - this->writeLEDs(); - } + } + this->writeLEDs(); + } + else { + for (uint8_t led = 0; led < this->leds; led++) { + this->stateLEDs[led] = BLACK; + } + this->writeLEDs(); + } } -void Cockpit::writeLEDs(){ - this->writeCommTimer(); - for (int i = 0; i < this->leds; i++) { - this->writeCommand(); +void Cockpit::writeLEDs() { + this->writeCommTimer(); + for (int i = 0; i < this->leds; i++) { + this->writeCommand(); - this->writeData(this->stateLEDs[i].blue); - this->writeData(this->stateLEDs[i].red); - this->writeData(this->stateLEDs[i].green); - - this->waitEOS(); - } - this->waitGSLAT(); + this->writeData(this->stateLEDs[i].blue); + this->writeData(this->stateLEDs[i].red); + this->writeData(this->stateLEDs[i].green); + + this->waitEOS(); + } + this->waitGSLAT(); } - - void Cockpit::writeLED(uint8_t led) { - this->writeCommTimer(); - for (int i = 0; i < this->leds; i++) { - this->writeCommand(); + this->writeCommTimer(); + for (int i = 0; i < this->leds; i++) { + this->writeCommand(); - if (i == this->leds){ - // this->writeData(redValue); - // this->writeData(greenValue); - // this->writeData(blueValue); - this->writeData(this->stateLEDs[i].blue); - this->writeData(this->stateLEDs[i].red); - this->writeData(this->stateLEDs[i].green); - } - else{ - this->writeData(0); - this->writeData(0); - this->writeData(0); - } - this->waitEOS(); - } - this->waitGSLAT(); + if (i == this->leds) { + // this->writeData(redValue); + // this->writeData(greenValue); + // this->writeData(blueValue); + this->writeData(this->stateLEDs[i].blue); + this->writeData(this->stateLEDs[i].red); + this->writeData(this->stateLEDs[i].green); + } + else { + this->writeData(0); + this->writeData(0); + this->writeData(0); + } + this->waitEOS(); + } + this->waitGSLAT(); } void Cockpit::writeZero() { - // PORTB |= B1; //uno - #if defined(INVERSE_MODE) +// PORTB |= B1; //uno +#if defined(INVERSE_MODE) - #if defined(MODE_RMT) - this->led_data[this->cp].level0 = 0; - this->led_data[this->cp].duration0 = 1; - this->led_data[this->cp].level1 = 0; - this->led_data[this->cp].duration1 = 1; - this->cp++; - this->led_data[this->cp].level0 = 1; - this->led_data[this->cp].duration0 = 1; - this->led_data[this->cp].level1 = 1; - this->led_data[this->cp].duration1 = 1; - this->cp++; - - for(int i = 0; i < 5; i++){ - this->led_data[this->cp].level0 = this->led_data[this->cp-1].level0; - this->led_data[this->cp].duration0 = 1; - this->led_data[this->cp].level1 = this->led_data[this->cp-1].level1; - this->led_data[this->cp].duration1 = 1; - this->cp++; - } - +#if defined(MODE_RMT) + this->led_data[this->cp].level0 = 0; + this->led_data[this->cp].duration0 = 1; + this->led_data[this->cp].level1 = 0; + this->led_data[this->cp].duration1 = 1; + this->cp++; + this->led_data[this->cp].level0 = 1; + this->led_data[this->cp].duration0 = 1; + this->led_data[this->cp].level1 = 1; + this->led_data[this->cp].duration1 = 1; + this->cp++; - #else - - #if defined(MODE_INTERRUPT) - this->buffer[this->cp] = 0; - this->cp++; + for (int i = 0; i < 5; i++) { + this->led_data[this->cp].level0 = this->led_data[this->cp - 1].level0; + this->led_data[this->cp].duration0 = 1; + this->led_data[this->cp].level1 = this->led_data[this->cp - 1].level1; + this->led_data[this->cp].duration1 = 1; + this->cp++; + } - this->buffer[this->cp] = 1; - this->cp++; +#else - this->buffer[this->cp] = this->buffer[this->cp-1]; - this->cp++; - this->buffer[this->cp] = this->buffer[this->cp-1]; - this->cp++; - this->buffer[this->cp] = this->buffer[this->cp-1]; - this->cp++; - this->buffer[this->cp] = this->buffer[this->cp-1]; - this->cp++; - this->buffer[this->cp] = this->buffer[this->cp-1]; - this->cp++; - #else - - gpio_set_level(this->pin, 0); - delayMicroseconds(pTime); - gpio_set_level(this->pin, 1); - delayMicroseconds(pTime); - delayMicroseconds(pTime); - delayMicroseconds(pTime); - delayMicroseconds(pTime); - delayMicroseconds(pTime); - #endif - #endif - - #else - digitalWrite(this->pin, HIGH); - delayMicroseconds(pTime); - // PORTB &= B111110; //uno - digitalWrite(this->pin, LOW); - delayMicroseconds(pTime); - delayMicroseconds(pTime); - delayMicroseconds(pTime); - delayMicroseconds(pTime); - delayMicroseconds(pTime); - #endif - +#if defined(MODE_INTERRUPT) + this->buffer[this->cp] = 0; + this->cp++; + + this->buffer[this->cp] = 1; + this->cp++; + + this->buffer[this->cp] = this->buffer[this->cp - 1]; + this->cp++; + this->buffer[this->cp] = this->buffer[this->cp - 1]; + this->cp++; + this->buffer[this->cp] = this->buffer[this->cp - 1]; + this->cp++; + this->buffer[this->cp] = this->buffer[this->cp - 1]; + this->cp++; + this->buffer[this->cp] = this->buffer[this->cp - 1]; + this->cp++; +#else + + gpio_set_level(this->pin, 0); + delayMicroseconds(pTime); + gpio_set_level(this->pin, 1); + delayMicroseconds(pTime); + delayMicroseconds(pTime); + delayMicroseconds(pTime); + delayMicroseconds(pTime); + delayMicroseconds(pTime); +#endif +#endif + +#else + digitalWrite(this->pin, HIGH); + delayMicroseconds(pTime); + // PORTB &= B111110; //uno + digitalWrite(this->pin, LOW); + delayMicroseconds(pTime); + delayMicroseconds(pTime); + delayMicroseconds(pTime); + delayMicroseconds(pTime); + delayMicroseconds(pTime); +#endif } void Cockpit::writeOne() { - // PORTB |= B1; //uno - #if defined(INVERSE_MODE) - #if defined(MODE_RMT) - this->led_data[this->cp].level0 = 0; - this->led_data[this->cp].duration0 = 1; - this->led_data[this->cp].level1 = 0; - this->led_data[this->cp].duration1 = 1; - this->cp++; - this->led_data[this->cp].level0 = 1; - this->led_data[this->cp].duration0 = 1; - this->led_data[this->cp].level1 = 1; - this->led_data[this->cp].duration1 = 1; - this->cp++; - this->led_data[this->cp].level0 = 0; - this->led_data[this->cp].duration0 = 1; - this->led_data[this->cp].level1 = 0; - this->led_data[this->cp].duration1 = 1; - this->cp++; - this->led_data[this->cp].level0 = 1; - this->led_data[this->cp].duration0 = 1; - this->led_data[this->cp].level1 = 1; - this->led_data[this->cp].duration1 = 1; - this->cp++; +// PORTB |= B1; //uno +#if defined(INVERSE_MODE) +#if defined(MODE_RMT) + this->led_data[this->cp].level0 = 0; + this->led_data[this->cp].duration0 = 1; + this->led_data[this->cp].level1 = 0; + this->led_data[this->cp].duration1 = 1; + this->cp++; + this->led_data[this->cp].level0 = 1; + this->led_data[this->cp].duration0 = 1; + this->led_data[this->cp].level1 = 1; + this->led_data[this->cp].duration1 = 1; + this->cp++; + this->led_data[this->cp].level0 = 0; + this->led_data[this->cp].duration0 = 1; + this->led_data[this->cp].level1 = 0; + this->led_data[this->cp].duration1 = 1; + this->cp++; + this->led_data[this->cp].level0 = 1; + this->led_data[this->cp].duration0 = 1; + this->led_data[this->cp].level1 = 1; + this->led_data[this->cp].duration1 = 1; + this->cp++; - this->led_data[this->cp].level0 = this->led_data[this->cp-1].level0; - this->led_data[this->cp].duration0 = 1; - this->led_data[this->cp].level1 = this->led_data[this->cp-1].level1; - this->led_data[this->cp].duration1 = 1; - this->cp++; - this->led_data[this->cp].level0 = this->led_data[this->cp-1].level0; - this->led_data[this->cp].duration0 = 1; - this->led_data[this->cp].level1 = this->led_data[this->cp-1].level1; - this->led_data[this->cp].duration1 = 1; - this->cp++; - this->led_data[this->cp].level0 = this->led_data[this->cp-1].level0; - this->led_data[this->cp].duration0 = 1; - this->led_data[this->cp].level1 = this->led_data[this->cp-1].level1; - this->led_data[this->cp].duration1 = 1; - this->cp++; - - - #else + this->led_data[this->cp].level0 = this->led_data[this->cp - 1].level0; + this->led_data[this->cp].duration0 = 1; + this->led_data[this->cp].level1 = this->led_data[this->cp - 1].level1; + this->led_data[this->cp].duration1 = 1; + this->cp++; + this->led_data[this->cp].level0 = this->led_data[this->cp - 1].level0; + this->led_data[this->cp].duration0 = 1; + this->led_data[this->cp].level1 = this->led_data[this->cp - 1].level1; + this->led_data[this->cp].duration1 = 1; + this->cp++; + this->led_data[this->cp].level0 = this->led_data[this->cp - 1].level0; + this->led_data[this->cp].duration0 = 1; + this->led_data[this->cp].level1 = this->led_data[this->cp - 1].level1; + this->led_data[this->cp].duration1 = 1; + this->cp++; - #if defined(MODE_INTERRUPT) - this->buffer[this->cp] = 0; - this->cp++; - this->buffer[this->cp] = 1; - this->cp++; - this->buffer[this->cp] = 0; - this->cp++; - this->buffer[this->cp] = 1; - this->cp++; - this->buffer[this->cp] = this->buffer[this->cp-1]; - this->cp++; - this->buffer[this->cp] = this->buffer[this->cp-1]; - this->cp++; - this->buffer[this->cp] = this->buffer[this->cp-1]; - this->cp++; +#else - #else - gpio_set_level(this->pin, 0); - delayMicroseconds(pTime); - // PORTB &= B111110; //uno - gpio_set_level(this->pin, 1); - delayMicroseconds(pTime); +#if defined(MODE_INTERRUPT) + this->buffer[this->cp] = 0; + this->cp++; + this->buffer[this->cp] = 1; + this->cp++; + this->buffer[this->cp] = 0; + this->cp++; + this->buffer[this->cp] = 1; + this->cp++; + this->buffer[this->cp] = this->buffer[this->cp - 1]; + this->cp++; + this->buffer[this->cp] = this->buffer[this->cp - 1]; + this->cp++; + this->buffer[this->cp] = this->buffer[this->cp - 1]; + this->cp++; - // PORTB |= B1; //rising edge of second pulse has to be within 0.5 * tCycle //uno - gpio_set_level(this->pin, 0); - delayMicroseconds(pTime); +#else + gpio_set_level(this->pin, 0); + delayMicroseconds(pTime); + // PORTB &= B111110; //uno + gpio_set_level(this->pin, 1); + delayMicroseconds(pTime); - // PORTB &= B111110; //uno - gpio_set_level(this->pin, 1); - delayMicroseconds(pTime); - delayMicroseconds(pTime); - delayMicroseconds(pTime); + // PORTB |= B1; //rising edge of second pulse has to be within 0.5 * tCycle //uno + gpio_set_level(this->pin, 0); + delayMicroseconds(pTime); - #endif - #endif - #else - digitalWrite(this->pin, HIGH); - delayMicroseconds(pTime); - // PORTB &= B111110; //uno - digitalWrite(this->pin, LOW); - delayMicroseconds(pTime); - // PORTB |= B1; //rising edge of second pulse has to be within 0.5 * tCycle //uno - digitalWrite(this->pin, HIGH); - delayMicroseconds(pTime); - // PORTB &= B111110; //uno - digitalWrite(this->pin, LOW); - delayMicroseconds(pTime); - delayMicroseconds(pTime); - delayMicroseconds(pTime); - #endif + // PORTB &= B111110; //uno + gpio_set_level(this->pin, 1); + delayMicroseconds(pTime); + delayMicroseconds(pTime); + delayMicroseconds(pTime); +#endif +#endif +#else + digitalWrite(this->pin, HIGH); + delayMicroseconds(pTime); + // PORTB &= B111110; //uno + digitalWrite(this->pin, LOW); + delayMicroseconds(pTime); + // PORTB |= B1; //rising edge of second pulse has to be within 0.5 * tCycle //uno + digitalWrite(this->pin, HIGH); + delayMicroseconds(pTime); + // PORTB &= B111110; //uno + digitalWrite(this->pin, LOW); + delayMicroseconds(pTime); + delayMicroseconds(pTime); + delayMicroseconds(pTime); +#endif } void Cockpit::writeCommTimer() { - //first two zeroes determine the timing (tcycle) after device is powered up or after a GSLAT - this->writeZero(); - this->writeZero(); + // first two zeroes determine the timing (tcycle) after device is powered up or after a GSLAT + this->writeZero(); + this->writeZero(); } void Cockpit::writeCommand() { - this->writeZero(); - this->writeZero(); - this->writeOne(); - this->writeOne(); - - this->writeOne(); - this->writeZero(); - this->writeOne(); - this->writeZero(); + this->writeZero(); + this->writeZero(); + this->writeOne(); + this->writeOne(); + + this->writeOne(); + this->writeZero(); + this->writeOne(); + this->writeZero(); } -//end of sequence (for a single driver chip) +// end of sequence (for a single driver chip) void Cockpit::waitEOS() { - - #if defined(INVERSE_MODE) - #if defined(MODE_RMT) - this->led_data[this->cp].level0 = 1; - this->led_data[this->cp].duration0 = 1; - this->led_data[this->cp].level1 = 1; - this->led_data[this->cp].duration1 = 1; - this->cp++; - #else - #if defined(MODE_INTERRUPT) - this->buffer[this->cp] = 1; - this->cp++; - #else - digitalWrite(this->pin, HIGH); - #endif - #endif - #else - digitalWrite(this->pin, LOW); - #endif +#if defined(INVERSE_MODE) - #if defined(MODE_RMT) - for(int i = this->cp; i < 24+this->cp; i++){ - this->led_data[i].level0 = this->led_data[i-1].level0; - this->led_data[i].duration0 = 1; - this->led_data[i].level1 = this->led_data[i-1].level1; - this->led_data[i].duration1 = 1; - } - this->cp += 24; // tCycle is 6 * pTime - #else +#if defined(MODE_RMT) + this->led_data[this->cp].level0 = 1; + this->led_data[this->cp].duration0 = 1; + this->led_data[this->cp].level1 = 1; + this->led_data[this->cp].duration1 = 1; + this->cp++; +#else +#if defined(MODE_INTERRUPT) + this->buffer[this->cp] = 1; + this->cp++; +#else + digitalWrite(this->pin, HIGH); +#endif +#endif +#else + digitalWrite(this->pin, LOW); +#endif - #if defined(MODE_INTERRUPT) - for(int i = this->cp; i < 24+this->cp; i++){ - this->buffer[i] = this->buffer[i-1]; - } - this->cp += 24; // tCycle is 6 * pTime - #else - delayMicroseconds(this->tCycle*4); // min 3.5 to max 5.5 times tCycle - #endif - #endif +#if defined(MODE_RMT) + for (int i = this->cp; i < 24 + this->cp; i++) { + this->led_data[i].level0 = this->led_data[i - 1].level0; + this->led_data[i].duration0 = 1; + this->led_data[i].level1 = this->led_data[i - 1].level1; + this->led_data[i].duration1 = 1; + } + this->cp += 24; // tCycle is 6 * pTime +#else + +#if defined(MODE_INTERRUPT) + for (int i = this->cp; i < 24 + this->cp; i++) { + this->buffer[i] = this->buffer[i - 1]; + } + this->cp += 24; // tCycle is 6 * pTime +#else + delayMicroseconds(this->tCycle * 4); // min 3.5 to max 5.5 times tCycle +#endif +#endif } -//grayscale latch (for the end of a chain of driver chips) +// grayscale latch (for the end of a chain of driver chips) void Cockpit::waitGSLAT() { - // PORTB &= B111110; //uno + // PORTB &= B111110; //uno - #if defined(INVERSE_MODE) +#if defined(INVERSE_MODE) - #if defined(MODE_RMT) +#if defined(MODE_RMT) - this->led_data[this->cp].level0 = 1; - this->led_data[this->cp].duration0 = 1; - this->led_data[this->cp].level1 = 1; - this->led_data[this->cp].duration1 = 1; - this->cp += 1; - #else + this->led_data[this->cp].level0 = 1; + this->led_data[this->cp].duration0 = 1; + this->led_data[this->cp].level1 = 1; + this->led_data[this->cp].duration1 = 1; + this->cp += 1; +#else - #if defined(MODE_INTERRUPT) - this->buffer[this->cp] = 1; - this->cp++; - #else - digitalWrite(this->pin, HIGH); - #endif - #endif - #else - digitalWrite(this->pin, LOW); - - #endif +#if defined(MODE_INTERRUPT) + this->buffer[this->cp] = 1; + this->cp++; +#else + digitalWrite(this->pin, HIGH); +#endif +#endif +#else + digitalWrite(this->pin, LOW); - #if defined(MODE_RMT) - for(int i = this->cp; i < 60+this->cp; i++){ - this->led_data[i].level0 = this->led_data[i-1].level0; - this->led_data[i].duration0 = 1; - this->led_data[i].level1 = this->led_data[i-1].level1; - this->led_data[i].duration1 = 1; - } - this->cp = 0; // minimum 8 time tCycle - #else +#endif - #if defined(MODE_INTERRUPT) - for(int i = this->cp; i < 60+this->cp; i++){ - this->buffer[i] = this->buffer[i-1]; - } - this->cp += 60; // tCycle is 6 * pTime - this->cp = 0; //reset cp - #else - delayMicroseconds(this->tCycle*10); // minimum 8 time tCycle - #endif - #endif +#if defined(MODE_RMT) + for (int i = this->cp; i < 60 + this->cp; i++) { + this->led_data[i].level0 = this->led_data[i - 1].level0; + this->led_data[i].duration0 = 1; + this->led_data[i].level1 = this->led_data[i - 1].level1; + this->led_data[i].duration1 = 1; + } + this->cp = 0; // minimum 8 time tCycle +#else + +#if defined(MODE_INTERRUPT) + for (int i = this->cp; i < 60 + this->cp; i++) { + this->buffer[i] = this->buffer[i - 1]; + } + this->cp += 60; // tCycle is 6 * pTime + this->cp = 0; // reset cp +#else + delayMicroseconds(this->tCycle * 10); // minimum 8 time tCycle +#endif +#endif } void Cockpit::writeData(uint8_t data) { - for (uint8_t i = 0; i<8; i++) { - if(data & 0b10000000) { - this->writeOne(); - } - else{ - this->writeZero(); - } - data <<= 1; - } + for (uint8_t i = 0; i < 8; i++) { + if (data & 0b10000000) { + this->writeOne(); + } + else { + this->writeZero(); + } + data <<= 1; + } } \ No newline at end of file diff --git a/src/tlc.hpp b/src/tlc.hpp index 91d2f31..1c984ee 100644 --- a/src/tlc.hpp +++ b/src/tlc.hpp @@ -5,8 +5,9 @@ #include "Arduino.h" #include #define INVERSE_MODE -#include #include "driver/rmt.h" +#include + #define BUFFER_SIZE 1818 // #include "config.h" @@ -17,95 +18,80 @@ class Cockpit; -enum Colors { - BLUE, - RED, - GREEN -}; -typedef struct Color{ +enum Colors { BLUE, RED, GREEN }; +typedef struct Color { uint8_t blue; uint8_t red; uint8_t green; } Color; -enum Alarm { - ALARM_1KM, - ALARM_500M, - ALARM_300M, - ALARM_100M, - NO_ALARM -}; +enum Alarm { ALARM_1KM, ALARM_500M, ALARM_300M, ALARM_100M, NO_ALARM }; -enum Dimmer { - BRIGHTNESS_UP, - BRIGHTNESS_DOWN, - NO_DIMMER -}; +enum Dimmer { BRIGHTNESS_UP, BRIGHTNESS_DOWN, NO_DIMMER }; -enum Brightness{ - BRIGHTER, - DARKER, - NO_CHANGE -}; +enum Brightness { BRIGHTER, DARKER, NO_CHANGE }; -class Cockpit{ - private: +class Cockpit { +private: // byte brightness[7] = {5, 10, 20, 50, 70, 90, 100}; // min value 0, max value 255 uint8_t brightness[NUM_LEDS] = {6, 25, 50, 127, 178, 229, 255}; uint8_t brightnessState; - uint8_t pTime; + uint8_t pTime; uint8_t tCycle; uint8_t leds; // amout of leds // int pin; // pin connected to uC gpio_num_t pin; - // uint8_t stateLEDs[LEDS][COLOR_CHANNELS] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; - Color stateLEDs[NUM_LEDS] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; + // uint8_t stateLEDs[LEDS][COLOR_CHANNELS] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, + // 0}, {0, 0, 0}, {0, 0, 0}}; + Color stateLEDs[NUM_LEDS] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, + {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; Color scaleBrightness(Color color, uint8_t additionDimm = 100); void setColorLed(Color color, uint8_t led); - - public: - static Color ORANGE; - static Color RED; - static Color BLUE; - static Color GREEN; - static Color CYAN; - static Color WHITE; - static Color BLACK; - static Color YELLOW; - static Color PURPLE; - - int cp; - bool buffer[BUFFER_SIZE]; - rmt_item32_t led_data[BUFFER_SIZE]; - Cockpit(gpio_num_t pin, int leds, int pTime, int tCycle, uint8_t brightness); - void setBrightnessState(int brightness, int state); - void brightnessUp(); - void brightnessDown(); - void displayBrightness(Color color = {.blue=255, .red=0, .green=0}); - void turnOffLeds(); - // void knightRider(int state); - void knightRiderAdvanced(Color color, int state); - void colorfade(int state); - void fillFromLeft(Color color, int state); - // void fillFromRight(Color color, int state); - void flashing(Color color, int state); - void alternating(Color color, int state, int frequencyDivider); - void outToIn(Color, int state); - void outToInMulticolor(Color color[], uint8_t colors, int state); - void inToOutMulticolor(Color color[], uint8_t colors, int state); +public: + static Color ORANGE; + static Color RED; + static Color BLUE; + static Color GREEN; + static Color CYAN; + static Color WHITE; + static Color BLACK; + static Color YELLOW; + static Color PURPLE; - void writeLEDs(); - void writeLED(uint8_t ledNum); - void writeZero(); - void writeOne(); - void writeCommTimer(); - void writeCommand(); - //end of sequence (for a single driver chip) - void waitEOS(); - //grayscale latch (for the end of a chain of driver chips) - void waitGSLAT(); - void writeData(uint8_t data); + int cp; + bool buffer[BUFFER_SIZE]; + rmt_item32_t led_data[BUFFER_SIZE]; + Cockpit(gpio_num_t pin, int leds, int pTime, int tCycle, uint8_t brightness); + void setBrightnessState(int brightness, int state); + void brightnessUp(); + void brightnessDown(); + void displayBrightness(Color color = {.blue = 255, .red = 0, .green = 0}); + void turnOffLeds(); + // void knightRider(int state); + void knightRiderAdvanced(Color color, int state); + void colorfade(int state); + void fillFromLeft(Color color, int state); + // void fillFromRight(Color color, int state); + void speedUpBounce(Color color, int state); + + void flashing(Color color, int state); + void alternating(Color color, int state, int frequencyDivider); + void outToIn(Color, int state); + void outToInMulticolor(Color color[], uint8_t colors, int state); + void inToOutMulticolor(Color color[], uint8_t colors, int state); + + void writeLEDs(); + void writeLED(uint8_t ledNum); + void writeZero(); + void writeOne(); + void writeCommTimer(); + void writeCommand(); + // end of sequence (for a single driver chip) + void waitEOS(); + // grayscale latch (for the end of a chain of driver chips) + void waitGSLAT(); + void writeData(uint8_t data); }; #endif \ No newline at end of file