use of freertos
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -21,7 +21,8 @@ build_flags = -DARDUINO_USB_CDC_ON_BOOT=1 -DARDUINO_USB_MODE=1
|
||||
; upload_protocol = espota
|
||||
; upload_port = 192.168.2.136
|
||||
; upload_port = 192.168.2.128
|
||||
upload_port = 192.168.2.145
|
||||
; upload_port = 192.168.2.145
|
||||
; upload_port = 192.168.2.177
|
||||
; upload_port = 192.168.2.33
|
||||
;board_build.partitions = huge_app.csv
|
||||
board_build.partitions = min_spiffs.csv
|
||||
|
||||
19
src/config.hpp
Normal file
19
src/config.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
// uncommend for very first flash
|
||||
// #define FIRST_BOOT
|
||||
|
||||
// #define ALARM_LED GPIO_NUM_2
|
||||
#define ALARM_LED GPIO_NUM_3
|
||||
#define ALARM_PIN GPIO_NUM_3
|
||||
|
||||
#define BRIGHTER_PIN GPIO_NUM_7
|
||||
#define DARKER_PIN GPIO_NUM_8
|
||||
|
||||
#define EEPROM_SIZE 1
|
||||
#define MODE_ADDRESS 0
|
||||
|
||||
#define NUM_LEDS 7 // do not touch
|
||||
|
||||
|
||||
#endif
|
||||
17
src/datatypes.hpp
Normal file
17
src/datatypes.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef DATATYPES_H
|
||||
#define DATATYPES_H
|
||||
|
||||
#include "tlc.hpp"
|
||||
|
||||
enum NET_MODE{
|
||||
MODE_BLE = 0,
|
||||
MODE_WIFI = 1
|
||||
};
|
||||
|
||||
struct taskParams{
|
||||
Cockpit *cockpit;
|
||||
enum Alarm *alarmFired;
|
||||
enum Dimmer *dimmerState;
|
||||
TaskHandle_t *thDisplayWarning;
|
||||
};
|
||||
#endif
|
||||
256
src/main.cpp
256
src/main.cpp
@@ -3,11 +3,15 @@
|
||||
#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 <EEPROM.h>
|
||||
#include <cstdint>
|
||||
#define EEPROM_SIZE 1
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
|
||||
#include <BLE2902.h>
|
||||
#include <BLEDevice.h>
|
||||
@@ -21,31 +25,13 @@
|
||||
|
||||
#include <driver/rmt.h>
|
||||
#include "tlc.hpp"
|
||||
|
||||
// uncommend for very first flash
|
||||
// #define FIRST_BOOT
|
||||
|
||||
// #define ALARM_LED GPIO_NUM_2
|
||||
#define ALARM_LED GPIO_NUM_3
|
||||
#define ALARM_PIN GPIO_NUM_3
|
||||
|
||||
#define BRIGHTER_PIN GPIO_NUM_7
|
||||
#define DARKER_PIN GPIO_NUM_8
|
||||
#include "config.hpp"
|
||||
#include "datatypes.hpp"
|
||||
|
||||
// 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
|
||||
|
||||
#define NUM_LEDS 7
|
||||
#define MODE_ADDRESS 0
|
||||
// #define MODE_BLE 0
|
||||
// #define MODE_WIFI 1
|
||||
|
||||
enum MODE{
|
||||
MODE_BLE = 0,
|
||||
MODE_WIFI = 1
|
||||
};
|
||||
|
||||
//#define TESTMODE 1
|
||||
|
||||
const char * ssid = SECRET_SSID;
|
||||
@@ -60,9 +46,16 @@ BLEDescriptor AlarmLevelDescriptor(BLEUUID((uint16_t)0x2901));
|
||||
// BLEServer * pServer = NULL;
|
||||
// bool volatile deviceConnected = false;
|
||||
bool volatile oldDeviceConnected = false;
|
||||
uint32_t value = 0;
|
||||
// uint8_t mode;
|
||||
enum MODE mode;
|
||||
// uint32_t value = 0;
|
||||
|
||||
enum NET_MODE mode;
|
||||
|
||||
void displayWarning( void * params);
|
||||
void dimmer(void * params);
|
||||
|
||||
enum Alarm alarmFired = NO_ALARM;
|
||||
enum Dimmer dimmerState = NO_DIMMER;
|
||||
|
||||
|
||||
|
||||
class MyServerCallbacks : public BLEServerCallbacks {
|
||||
@@ -86,16 +79,6 @@ class MyServerCallbacks : public BLEServerCallbacks {
|
||||
}
|
||||
};
|
||||
|
||||
enum Alarm {
|
||||
ALARM_1KM,
|
||||
ALARM_500M,
|
||||
ALARM_300M,
|
||||
ALARM_100M,
|
||||
NO_ALARM
|
||||
};
|
||||
|
||||
enum Alarm alarmFired = NO_ALARM;
|
||||
|
||||
bool brighter = false;
|
||||
bool darker = false;
|
||||
|
||||
@@ -104,22 +87,19 @@ uint8_t saveAlarmState;
|
||||
|
||||
int state = 0;
|
||||
|
||||
// std::string x;
|
||||
// uint8_t stateLED = 0;
|
||||
// byte stateLEDs[7][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
|
||||
byte orange[3] = {0, 255, 0xA5};
|
||||
byte red[3] = {0, 255, 0};
|
||||
byte blue[3] = {255, 0, 0};
|
||||
byte green[3] = {0, 0, 255};
|
||||
byte color[3] = {0, 0, 0};
|
||||
byte cyan[3] = {255, 115, 230};
|
||||
|
||||
byte brightnessState = 6;
|
||||
|
||||
|
||||
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;
|
||||
|
||||
@@ -127,6 +107,8 @@ int stateLimit = 50;
|
||||
bool brightnessUpPressed = false;
|
||||
bool brightnessDownPressed = false;
|
||||
|
||||
enum Brightness brightnessFired = NO_CHANGE;
|
||||
|
||||
|
||||
|
||||
class MyAlarmCallback : public BLECharacteristicCallbacks {
|
||||
@@ -157,6 +139,24 @@ class MyAlarmCallback : public BLECharacteristicCallbacks {
|
||||
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");
|
||||
@@ -209,7 +209,7 @@ void IRAM_ATTR onTimer() {
|
||||
portEXIT_CRITICAL_ISR(&timerMux);
|
||||
}
|
||||
|
||||
volatile int pc = 0;
|
||||
// volatile int pc = 0;
|
||||
|
||||
// Create the BLE Server
|
||||
BLEServer * pServer = NULL;
|
||||
@@ -226,6 +226,10 @@ void setup() {
|
||||
Serial.begin(9600);
|
||||
delay(1000);
|
||||
Serial.println("Hallo Welt");
|
||||
delay(1000);
|
||||
Serial.println("Hallo Welt");
|
||||
delay(1000);
|
||||
Serial.println("Hallo Welt");
|
||||
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
#if defined(FIRST_BOOT)
|
||||
@@ -234,7 +238,7 @@ void setup() {
|
||||
EEPROM.commit();
|
||||
#endif
|
||||
|
||||
mode = static_cast<enum MODE>(EEPROM.read(MODE_ADDRESS));
|
||||
mode = static_cast<enum NET_MODE>(EEPROM.read(MODE_ADDRESS));
|
||||
// mode = MODE_WIFI;
|
||||
|
||||
if (mode == MODE_BLE) {
|
||||
@@ -245,13 +249,7 @@ void setup() {
|
||||
timerAlarmWrite(timer, 100000, true);
|
||||
timerAlarmEnable(timer);
|
||||
|
||||
// bitbanging timer
|
||||
// timerBits = timerBegin(1, 80, true);
|
||||
// rmt_reserve_memsize_t memsize = 1818;
|
||||
|
||||
// if (!rmtInit(ALARM_PIN, RMT_TX_MODE, RMT_MEM_512)) {
|
||||
// Serial.println("init sender failed\n");
|
||||
// }
|
||||
|
||||
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(ALARM_PIN, RMT_CHANNEL_0);
|
||||
config.clk_div = 160; // 80
|
||||
@@ -260,8 +258,11 @@ void setup() {
|
||||
ESP_ERROR_CHECK(rmt_config(&config));
|
||||
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
|
||||
|
||||
|
||||
InitBLE(pServer, serverCallback, myAlarmCallback);
|
||||
|
||||
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
|
||||
@@ -328,7 +329,6 @@ void setup() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
if (mode == MODE_WIFI) {
|
||||
// ota update mode
|
||||
@@ -336,119 +336,8 @@ void loop() {
|
||||
// Serial.print("WIFI");
|
||||
}
|
||||
else {
|
||||
// BLE mode
|
||||
// occures every 100ms
|
||||
if (ctr > 0) {
|
||||
portENTER_CRITICAL(&timerMux);
|
||||
ctr--;
|
||||
portEXIT_CRITICAL(&timerMux);
|
||||
|
||||
if(digitalRead(BRIGHTER_PIN) == LOW && !brightnessUpPressed){
|
||||
// save current alarmstate
|
||||
saveAlarmState = state;
|
||||
saveFiredAlarm = alarmFired;
|
||||
|
||||
brighter = true;
|
||||
// brighter button pressed
|
||||
if (brightnessState < 6){
|
||||
brightnessState++;
|
||||
|
||||
}
|
||||
stateLimit = 11; //
|
||||
state = 0;
|
||||
// cockpit.setBrightnessState(brightnessState, state);
|
||||
Serial.println("++");
|
||||
brightnessUpPressed = true;
|
||||
}
|
||||
else if (digitalRead(BRIGHTER_PIN) == HIGH){
|
||||
brightnessUpPressed = false;
|
||||
}
|
||||
if(digitalRead(DARKER_PIN) == LOW && !brightnessDownPressed){
|
||||
// save current alarmstate
|
||||
saveAlarmState = state;
|
||||
saveFiredAlarm = alarmFired;
|
||||
|
||||
darker = true;
|
||||
// brighter button pressed
|
||||
if (brightnessState > 0){
|
||||
brightnessState--;
|
||||
Serial.println("--");
|
||||
}
|
||||
stateLimit = 11;
|
||||
state = 0;
|
||||
brightnessDownPressed = true;
|
||||
}
|
||||
else if (digitalRead(DARKER_PIN) == HIGH){
|
||||
brightnessDownPressed = false;
|
||||
}
|
||||
|
||||
if(brighter){
|
||||
cockpit.setBrightnessState(brightnessState, state);
|
||||
state++;
|
||||
stateLimit = 11;
|
||||
ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cockpit.led_data, sizeof(cockpit.led_data) / sizeof(rmt_item32_t), true));
|
||||
}
|
||||
else if(darker){
|
||||
cockpit.setBrightnessState(brightnessState, state);
|
||||
state++;
|
||||
stateLimit = 11;
|
||||
ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cockpit.led_data, sizeof(cockpit.led_data) / sizeof(rmt_item32_t), true));
|
||||
}
|
||||
|
||||
else if (alarmFired == ALARM_100M) {
|
||||
|
||||
cockpit.flashing(red, state, 2);
|
||||
|
||||
ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cockpit.led_data, sizeof(cockpit.led_data) / sizeof(rmt_item32_t), true));
|
||||
|
||||
state++;
|
||||
stateLimit = 54; // 12*5
|
||||
|
||||
}
|
||||
else if (alarmFired == ALARM_300M) {
|
||||
cockpit.flashing(blue, state, 4);
|
||||
ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cockpit.led_data, sizeof(cockpit.led_data) / sizeof(rmt_item32_t), true));
|
||||
state++;
|
||||
stateLimit = 54;
|
||||
}
|
||||
else if (alarmFired == ALARM_500M) {
|
||||
|
||||
cockpit.flashing(green, state, 6);
|
||||
ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cockpit.led_data, sizeof(cockpit.led_data) / sizeof(rmt_item32_t), true));
|
||||
state++;
|
||||
stateLimit = 54;
|
||||
}
|
||||
else if (alarmFired == ALARM_1KM) {
|
||||
|
||||
cockpit.getColor(state, color);
|
||||
|
||||
cockpit.knightRiderColored(state, red);
|
||||
ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cockpit.led_data, sizeof(cockpit.led_data) / sizeof(rmt_item32_t), true));
|
||||
state++;
|
||||
stateLimit = 80;
|
||||
}
|
||||
|
||||
if (state >= stateLimit) {
|
||||
state = 0;
|
||||
// noInterrupts();
|
||||
cockpit.turnOffLeds();
|
||||
ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cockpit.led_data, sizeof(cockpit.led_data) / sizeof(rmt_item32_t), true));
|
||||
|
||||
alarmFired = NO_ALARM;
|
||||
|
||||
if(brighter || darker){ // repair ongoing alarm if brightness was adjusted while alarm
|
||||
// restore alarm + state
|
||||
state = saveAlarmState;
|
||||
alarmFired = saveFiredAlarm;
|
||||
}
|
||||
|
||||
#if defined TESTMODE
|
||||
ctr = -20;
|
||||
#endif
|
||||
brighter = false;
|
||||
darker = false;
|
||||
}
|
||||
}
|
||||
vTaskDelay(100/portTICK_PERIOD_MS);
|
||||
|
||||
if (serverCallback->getConnectionState()) {
|
||||
|
||||
@@ -465,8 +354,33 @@ void loop() {
|
||||
// do stuff here on connecting
|
||||
oldDeviceConnected = serverCallback->getConnectionState();
|
||||
|
||||
for(int i = 0; i < 27; i++){
|
||||
cockpit.knightRiderColored(i, red);
|
||||
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);
|
||||
// }
|
||||
for(int i = 0; i < 50; i++){
|
||||
// cockpit.knightRiderAdvanced(red, i);
|
||||
// cockpit.outToIn(red, i);
|
||||
cockpit.inToOutMulticolor(c, 12, 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);
|
||||
}
|
||||
|
||||
174
src/tasks.cpp
Normal file
174
src/tasks.cpp
Normal file
@@ -0,0 +1,174 @@
|
||||
#include "Arduino.h"
|
||||
#include "config.hpp"
|
||||
#include "tasks.hpp"
|
||||
#include "tlc.hpp"
|
||||
#include "datatypes.hpp"
|
||||
|
||||
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;
|
||||
|
||||
uint8_t statePreload = 0;
|
||||
|
||||
alarmStatebefore = *alarm;
|
||||
while (true) {
|
||||
|
||||
if(*alarm != alarmStatebefore){
|
||||
alarmStateChanged = 1;
|
||||
}
|
||||
else{
|
||||
alarmStateChanged = 0;
|
||||
}
|
||||
|
||||
switch (*alarm) {
|
||||
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;
|
||||
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;
|
||||
|
||||
TaskHandle_t *thDisplayWarning = myParams->thDisplayWarning;
|
||||
|
||||
uint8_t brighterPressed = 0;
|
||||
uint8_t darkerPressed = 0;
|
||||
uint8_t ctrBrightnessChanged = 0;
|
||||
|
||||
while (1) {
|
||||
|
||||
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){
|
||||
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));
|
||||
}
|
||||
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){
|
||||
// darker
|
||||
darkerPressed = 1;
|
||||
ctrBrightnessChanged = 10;
|
||||
*dimmerState = NO_DIMMER;
|
||||
|
||||
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));
|
||||
}
|
||||
if (gpio_get_level(DARKER_PIN) == 1 && darkerPressed == 1) {
|
||||
// darker button released
|
||||
darkerPressed = 0;
|
||||
}
|
||||
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
|
||||
if (ctrBrightnessChanged > 0){
|
||||
ctrBrightnessChanged--;
|
||||
|
||||
if(ctrBrightnessChanged == 0){
|
||||
// check if there are suspended tasks
|
||||
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));
|
||||
vTaskResume(*thDisplayWarning);
|
||||
}
|
||||
else{
|
||||
cp->turnOffLeds();
|
||||
ESP_ERROR_CHECK(rmt_write_items(RMT_CHANNEL_0, cp->led_data, sizeof(cp->led_data) / sizeof(rmt_item32_t), true));
|
||||
}
|
||||
}
|
||||
}
|
||||
// Serial.println(uxTaskGetStackHighWaterMark(NULL));
|
||||
}
|
||||
}
|
||||
7
src/tasks.hpp
Normal file
7
src/tasks.hpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef TASKS_H
|
||||
#define TASKS_H
|
||||
|
||||
void displayWarning( void * params);
|
||||
void dimmer( void * params);
|
||||
|
||||
#endif
|
||||
548
src/tlc.cpp
548
src/tlc.cpp
@@ -1,6 +1,17 @@
|
||||
#include "tlc.hpp"
|
||||
|
||||
Cockpit::Cockpit(gpio_num_t pin, int leds, int pTime, int tCycle, byte brightnessState){
|
||||
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;
|
||||
@@ -9,217 +20,338 @@ Cockpit::Cockpit(gpio_num_t pin, int leds, int pTime, int tCycle, byte brightnes
|
||||
this->brightnessState = brightnessState;
|
||||
|
||||
this->cp = 0;
|
||||
|
||||
}
|
||||
|
||||
void Cockpit::knightRider(int state){
|
||||
byte color = 1;
|
||||
this->turnOffLeds();
|
||||
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);
|
||||
return scaledColor;
|
||||
}
|
||||
|
||||
byte brightness = this->brightnessState;
|
||||
byte dimmedIntensitiy = brightness / 2;
|
||||
byte heavyDimmedIntensitiy = brightness / 4;
|
||||
void Cockpit::setColorLed(struct Color color, uint8_t led){
|
||||
this->stateLEDs[led] = color;
|
||||
}
|
||||
|
||||
int step = state % 13;
|
||||
switch (step)
|
||||
{
|
||||
case 0:
|
||||
this->stateLEDs[0][color] = brightness;
|
||||
// step += 1;
|
||||
break;
|
||||
case 1:
|
||||
this->stateLEDs[0][color] = dimmedIntensitiy;
|
||||
this->stateLEDs[1][color] = brightness;
|
||||
// step++;
|
||||
break;
|
||||
case 2:
|
||||
this->stateLEDs[0][color] = heavyDimmedIntensitiy;
|
||||
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[3][color] = brightness;
|
||||
// step++;
|
||||
break;
|
||||
case 4:
|
||||
this->stateLEDs[1][color] = 0;
|
||||
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[5][color] = brightness;
|
||||
// step++;
|
||||
break;
|
||||
case 6:
|
||||
this->stateLEDs[3][color] = 0;
|
||||
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[6][color] = dimmedIntensitiy;
|
||||
// step++;
|
||||
break;
|
||||
case 8:
|
||||
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[6][color] = 0;
|
||||
// step++;
|
||||
break;
|
||||
case 10:
|
||||
this->stateLEDs[2][color] = brightness;
|
||||
this->stateLEDs[3][color] = dimmedIntensitiy;
|
||||
this->stateLEDs[4][color] = heavyDimmedIntensitiy;
|
||||
this->stateLEDs[5][color] = 0;
|
||||
// step++;
|
||||
break;
|
||||
case 11:
|
||||
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[3][color] = 0;
|
||||
// step++;
|
||||
break;
|
||||
void Cockpit::fillFromLeft(Color color, int state){
|
||||
// scale state to sufficient range
|
||||
state = state % NUM_LEDS;
|
||||
turnOffLeds();
|
||||
|
||||
default:
|
||||
break;
|
||||
for(uint8_t led = 0; led <= state; led++){
|
||||
this->stateLEDs[led] = scaleBrightness(color);
|
||||
}
|
||||
|
||||
this->writeLEDs();
|
||||
}
|
||||
|
||||
byte scaleColor(byte color, int scaleFactor){
|
||||
return byte(color * (float)scaleFactor / 255);
|
||||
void Cockpit::outToIn(Color color, int state){
|
||||
state = state % 5;
|
||||
turnOffLeds();
|
||||
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);
|
||||
}
|
||||
}
|
||||
writeLEDs();
|
||||
}
|
||||
void Cockpit::outToInMulticolor(Color color[], uint8_t colors, int state){
|
||||
uint8_t curColorIndex = state / 5;
|
||||
state = state % 5;
|
||||
turnOffLeds();
|
||||
|
||||
void Cockpit::knightRiderColored(int state, byte color[]){
|
||||
int step = state % (this->leds*2-1);
|
||||
if (step < this->leds){
|
||||
this->stateLEDs[step][0] = scaleColor(color[0], this->brightness[this->brightnessState]);
|
||||
this->stateLEDs[step][1] = scaleColor(color[1], this->brightness[this->brightnessState]);
|
||||
this->stateLEDs[step][2] = scaleColor(color[2], this->brightness[this->brightnessState]);
|
||||
if(step < 1){
|
||||
this->stateLEDs[1][0] = 0;
|
||||
this->stateLEDs[1][1] = 0;
|
||||
this->stateLEDs[1][2] = 0;
|
||||
this->stateLEDs[2][0] = 0;
|
||||
this->stateLEDs[2][1] = 0;
|
||||
this->stateLEDs[2][2] = 0;
|
||||
if(curColorIndex == 0){
|
||||
for(int8_t i = -1; i <= state; i++){
|
||||
if(i < 0){
|
||||
this->stateLEDs[i] = BLACK;
|
||||
}
|
||||
if (step > 0){
|
||||
this->stateLEDs[step-1][0] = scaleColor(color[0], this->brightness[this->brightnessState]/2);
|
||||
this->stateLEDs[step-1][1] = scaleColor(color[1], this->brightness[this->brightnessState]/2);
|
||||
this->stateLEDs[step-1][2] = scaleColor(color[2], this->brightness[this->brightnessState]/2);
|
||||
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]);
|
||||
}
|
||||
if (step > 1){
|
||||
this->stateLEDs[step-2][0] = scaleColor(color[0], this->brightness[this->brightnessState]/4);
|
||||
this->stateLEDs[step-2][1] = scaleColor(color[1], this->brightness[this->brightnessState]/4);
|
||||
this->stateLEDs[step-2][2] = scaleColor(color[2], this->brightness[this->brightnessState]/4);
|
||||
}
|
||||
if (step > 2){
|
||||
this->stateLEDs[step-3][0] = 0;
|
||||
this->stateLEDs[step-3][1] = 0;
|
||||
this->stateLEDs[step-3][2] = 0;
|
||||
}
|
||||
}
|
||||
else{
|
||||
int curLed = this->leds-2 - (step-this->leds);
|
||||
// int curLed = step - (step%(this->leds-1))*2;
|
||||
this->stateLEDs[curLed][0] = scaleColor(color[0], this->brightness[this->brightnessState]);
|
||||
this->stateLEDs[curLed][1] = scaleColor(color[1], this->brightness[this->brightnessState]);
|
||||
this->stateLEDs[curLed][2] = scaleColor(color[2], this->brightness[this->brightnessState]);
|
||||
if(curLed < this->leds-1){
|
||||
this->stateLEDs[curLed+1][0] = scaleColor(color[0], this->brightness[this->brightnessState]/2);
|
||||
this->stateLEDs[curLed+1][1] = scaleColor(color[1], this->brightness[this->brightnessState]/2);
|
||||
this->stateLEDs[curLed+1][2] = scaleColor(color[2], this->brightness[this->brightnessState]/2);
|
||||
// predraw last color
|
||||
for(uint8_t i = 0; i < NUM_LEDS; i++){
|
||||
this->stateLEDs[i] = scaleBrightness(color[curColorIndex-1]);
|
||||
}
|
||||
if(curLed < this->leds-2){
|
||||
this->stateLEDs[curLed+2][0] = scaleColor(color[0], this->brightness[this->brightnessState]/4);
|
||||
this->stateLEDs[curLed+2][1] = scaleColor(color[1], this->brightness[this->brightnessState]/4);
|
||||
this->stateLEDs[curLed+2][2] = scaleColor(color[2], this->brightness[this->brightnessState]/4);
|
||||
for(int8_t i = 0; i < state; i++){
|
||||
this->stateLEDs[i] = scaleBrightness(color[curColorIndex]);
|
||||
this->stateLEDs[NUM_LEDS-i-1] = scaleBrightness(color[curColorIndex]);
|
||||
}
|
||||
if(curLed < this->leds-3){
|
||||
this->stateLEDs[curLed+3][0] = 0;
|
||||
this->stateLEDs[curLed+3][1] = 0;
|
||||
this->stateLEDs[curLed+3][2] = 0;
|
||||
|
||||
}
|
||||
writeLEDs();
|
||||
}
|
||||
|
||||
void Cockpit::inToOutMulticolor(Color color[], uint8_t colors, int state) {
|
||||
uint8_t curColorIndex = state / 4;
|
||||
state = state % 4;
|
||||
turnOffLeds();
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
else{
|
||||
// predraw last color
|
||||
for(uint8_t i = 0; i < NUM_LEDS; i++){
|
||||
this->stateLEDs[i] = scaleBrightness(color[curColorIndex-1]);
|
||||
}
|
||||
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]);
|
||||
}
|
||||
|
||||
}
|
||||
writeLEDs();
|
||||
}
|
||||
|
||||
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));
|
||||
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));
|
||||
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));
|
||||
this->stateLEDs[led].blue = 0;
|
||||
this->stateLEDs[led] = scaleBrightness(this->stateLEDs[led]);
|
||||
}
|
||||
}
|
||||
this->writeLEDs();
|
||||
}
|
||||
|
||||
// void Cockpit::knightRider(int state){
|
||||
// uint8_t color = 1;
|
||||
// this->turnOffLeds();
|
||||
|
||||
// uint8_t brightness = this->brightnessState;
|
||||
// uint8_t dimmedIntensitiy = brightness / 2;
|
||||
// uint8_t heavyDimmedIntensitiy = brightness / 4;
|
||||
|
||||
// int step = state % 13;
|
||||
// switch (step)
|
||||
// {
|
||||
// case 0:
|
||||
// this->stateLEDs[0][color] = brightness;
|
||||
// // step += 1;
|
||||
// break;
|
||||
// case 1:
|
||||
// this->stateLEDs[0][color] = dimmedIntensitiy;
|
||||
// this->stateLEDs[1][color] = brightness;
|
||||
// // step++;
|
||||
// break;
|
||||
// case 2:
|
||||
// this->stateLEDs[0][color] = heavyDimmedIntensitiy;
|
||||
// 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[3][color] = brightness;
|
||||
// // step++;
|
||||
// break;
|
||||
// case 4:
|
||||
// this->stateLEDs[1][color] = 0;
|
||||
// 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[5][color] = brightness;
|
||||
// // step++;
|
||||
// break;
|
||||
// case 6:
|
||||
// this->stateLEDs[3][color] = 0;
|
||||
// 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[6][color] = dimmedIntensitiy;
|
||||
// // step++;
|
||||
// break;
|
||||
// case 8:
|
||||
// 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[6][color] = 0;
|
||||
// // step++;
|
||||
// break;
|
||||
// case 10:
|
||||
// this->stateLEDs[2][color] = brightness;
|
||||
// this->stateLEDs[3][color] = dimmedIntensitiy;
|
||||
// this->stateLEDs[4][color] = heavyDimmedIntensitiy;
|
||||
// this->stateLEDs[5][color] = 0;
|
||||
// // step++;
|
||||
// break;
|
||||
// case 11:
|
||||
// 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[3][color] = 0;
|
||||
// // step++;
|
||||
// break;
|
||||
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
|
||||
// this->writeLEDs();
|
||||
// }
|
||||
|
||||
void Cockpit::knightRiderAdvanced(Color color, int state){
|
||||
this->turnOffLeds();
|
||||
// 15 states
|
||||
state = state % 15;
|
||||
if(state < 7){
|
||||
this->stateLEDs[state] = scaleBrightness(color);
|
||||
if(state > 0){
|
||||
this->stateLEDs[state-1] = scaleBrightness(color, 70);
|
||||
}
|
||||
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);
|
||||
}
|
||||
if(state > 9){
|
||||
this->stateLEDs[2*NUM_LEDS-(state)-1+2] = scaleBrightness(color, 30);
|
||||
}
|
||||
}
|
||||
else{
|
||||
this->turnOffLeds();
|
||||
}
|
||||
this->writeLEDs();
|
||||
}
|
||||
|
||||
uint8_t scaleColor(uint8_t color, int scaleFactor){
|
||||
return uint8_t(color * (float)scaleFactor / 255);
|
||||
}
|
||||
|
||||
|
||||
void Cockpit::turnOffLeds(){
|
||||
for(byte led = 0; led < 7; led++){
|
||||
for(byte color = 0; color < 3; color++){
|
||||
this->stateLEDs[led][color] = 0;
|
||||
}
|
||||
for(uint8_t led = 0; led < NUM_LEDS; led++){
|
||||
this->stateLEDs[led] = BLACK;
|
||||
}
|
||||
this->writeLEDs();
|
||||
}
|
||||
|
||||
void Cockpit::runningLights(byte color){
|
||||
this->turnOffLeds();
|
||||
for(byte led = 0; led < 7; led++){
|
||||
this->stateLEDs[led][0] = 10;
|
||||
writeLEDs();
|
||||
delay(30);
|
||||
void Cockpit::brightnessUp(){
|
||||
if(this->brightnessState < 6){
|
||||
this->brightnessState++;
|
||||
}
|
||||
}
|
||||
void Cockpit::brightnessDown(){
|
||||
if(this->brightnessState > 0){
|
||||
this->brightnessState--;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Cockpit::setBrightnessState(int brightnessState, int state){
|
||||
this->brightnessState = brightnessState;
|
||||
if (state < 10){
|
||||
void Cockpit::displayBrightness(Color color){
|
||||
for(int i = 0; i < this->leds; i++){
|
||||
if(i <= brightnessState){
|
||||
this->stateLEDs[i][0] = this->brightness[this->brightnessState];
|
||||
this->stateLEDs[i][1] = 0;
|
||||
this->stateLEDs[i][2] = 0;
|
||||
this->stateLEDs[i] = scaleBrightness(color);
|
||||
// this->stateLEDs[i][0] = this->brightness[this->brightnessState];
|
||||
// this->stateLEDs[i][1] = 0;
|
||||
// this->stateLEDs[i][2] = 0;
|
||||
}
|
||||
else{
|
||||
this->stateLEDs[i][0] = 0;
|
||||
this->stateLEDs[i][1] = 0;
|
||||
this->stateLEDs[i][2] = 0;
|
||||
this->stateLEDs[i] = BLACK;
|
||||
// this->stateLEDs[i][0] = 0;
|
||||
// this->stateLEDs[i][1] = 0;
|
||||
// this->stateLEDs[i][2] = 0;
|
||||
}
|
||||
}
|
||||
writeLEDs();
|
||||
}
|
||||
else{
|
||||
for(int i = 0; i < this->leds; i++){
|
||||
this->stateLEDs[i][0] = 0;
|
||||
this->stateLEDs[i][1] = 0;
|
||||
this->stateLEDs[i][2] = 0;
|
||||
}
|
||||
writeLEDs();
|
||||
}
|
||||
}
|
||||
|
||||
// void Cockpit::setBrightnessState(int brightnessState, int state){
|
||||
// this->brightnessState = brightnessState;
|
||||
// if (state < 10){
|
||||
// for(int i = 0; i < this->leds; i++){
|
||||
// if(i <= brightnessState){
|
||||
// this->stateLEDs[i][0] = this->brightness[this->brightnessState];
|
||||
// this->stateLEDs[i][1] = 0;
|
||||
// this->stateLEDs[i][2] = 0;
|
||||
// }
|
||||
// else{
|
||||
// this->stateLEDs[i][0] = 0;
|
||||
// this->stateLEDs[i][1] = 0;
|
||||
// this->stateLEDs[i][2] = 0;
|
||||
// }
|
||||
// }
|
||||
// writeLEDs();
|
||||
// }
|
||||
// else{
|
||||
// for(int i = 0; i < this->leds; i++){
|
||||
// this->stateLEDs[i][0] = 0;
|
||||
// this->stateLEDs[i][1] = 0;
|
||||
// this->stateLEDs[i][2] = 0;
|
||||
// }
|
||||
// writeLEDs();
|
||||
// }
|
||||
// }
|
||||
|
||||
void Cockpit::alternating(byte color[3], int state, int frequencyDivider){
|
||||
|
||||
void Cockpit::alternating(Color color, int state, int frequencyDivider){
|
||||
if(state % (frequencyDivider*2) < frequencyDivider){
|
||||
state = 0;
|
||||
}
|
||||
@@ -227,32 +359,24 @@ void Cockpit::alternating(byte color[3], int state, int frequencyDivider){
|
||||
state = 1;
|
||||
}
|
||||
if(state % 2 == 0){
|
||||
for(byte led = 0; led < this->leds; led++){
|
||||
for(uint8_t led = 0; led < this->leds; led++){
|
||||
if(led % 2 == 0){
|
||||
this->stateLEDs[led][0] = int(color[0] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led][1] = int(color[1] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led][2] = int(color[2] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led] = scaleBrightness(color);
|
||||
}
|
||||
else{
|
||||
this->stateLEDs[led][0] = 0;
|
||||
this->stateLEDs[led][1] = 0;
|
||||
this->stateLEDs[led][2] = 0;
|
||||
this->stateLEDs[led] = BLACK;
|
||||
}
|
||||
|
||||
}
|
||||
this->writeLEDs();
|
||||
}
|
||||
else{
|
||||
for(byte led = 0; led < this->leds; led++){
|
||||
for(uint8_t led = 0; led < this->leds; led++){
|
||||
if(led % 2 == 1){
|
||||
this->stateLEDs[led][0] = int(color[0] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led][1] = int(color[1] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led][2] = int(color[2] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led] = scaleBrightness(color);
|
||||
}
|
||||
else{
|
||||
this->stateLEDs[led][0] = 0;
|
||||
this->stateLEDs[led][1] = 0;
|
||||
this->stateLEDs[led][2] = 0;
|
||||
this->stateLEDs[led] = BLACK;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -260,35 +384,17 @@ void Cockpit::alternating(byte color[3], int state, int frequencyDivider){
|
||||
}
|
||||
}
|
||||
|
||||
void Cockpit::getColor(int state, byte color[]){
|
||||
color[0] = (state * 10 ) % 255;
|
||||
color[1] = (state * 10 +100) % 255;
|
||||
color[2] = (state * 10 +200) % 255;
|
||||
}
|
||||
|
||||
void Cockpit::flashing(byte color[3], int state, uint8_t frequencyDivider){
|
||||
|
||||
if(state % frequencyDivider < frequencyDivider/2){
|
||||
for(byte led = 0; led < this->leds; led++){
|
||||
|
||||
this->stateLEDs[led][0] = int(color[0] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led][1] = int(color[1] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led][2] = int(color[2] * (float)this->brightness[this->brightnessState] / 255);
|
||||
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(byte led = 0; led < this->leds; led++){
|
||||
// for(byte colorCtr = 0; colorCtr < 3; colorCtr++){
|
||||
// stateled[led][colorCtr] = 0;
|
||||
// }
|
||||
this->stateLEDs[led][0] = 0;
|
||||
this->stateLEDs[led][1] = 0;
|
||||
this->stateLEDs[led][2] = 0;
|
||||
// stateled[led][color[1]] = 10;
|
||||
// stateled[led][color[2]] = 10;
|
||||
// stateled[led][1] = 10;
|
||||
for(uint8_t led = 0; led < this->leds; led++){
|
||||
this->stateLEDs[led] = BLACK;
|
||||
}
|
||||
this->writeLEDs();
|
||||
}
|
||||
@@ -299,9 +405,9 @@ void Cockpit::writeLEDs(){
|
||||
for (int i = 0; i < this->leds; i++) {
|
||||
this->writeCommand();
|
||||
|
||||
this->writeData(this->stateLEDs[i][0]);
|
||||
this->writeData(this->stateLEDs[i][1]);
|
||||
this->writeData(this->stateLEDs[i][2]);
|
||||
this->writeData(this->stateLEDs[i].blue);
|
||||
this->writeData(this->stateLEDs[i].red);
|
||||
this->writeData(this->stateLEDs[i].green);
|
||||
|
||||
this->waitEOS();
|
||||
}
|
||||
@@ -310,7 +416,7 @@ void Cockpit::writeLEDs(){
|
||||
|
||||
|
||||
|
||||
void Cockpit::writeLED(byte led) {
|
||||
void Cockpit::writeLED(uint8_t led) {
|
||||
this->writeCommTimer();
|
||||
for (int i = 0; i < this->leds; i++) {
|
||||
this->writeCommand();
|
||||
@@ -319,14 +425,14 @@ void Cockpit::writeLED(byte led) {
|
||||
// this->writeData(redValue);
|
||||
// this->writeData(greenValue);
|
||||
// this->writeData(blueValue);
|
||||
this->writeData(this->stateLEDs[i][0]);
|
||||
this->writeData(this->stateLEDs[i][1]);
|
||||
this->writeData(this->stateLEDs[i][2]);
|
||||
this->writeData(this->stateLEDs[i].blue);
|
||||
this->writeData(this->stateLEDs[i].red);
|
||||
this->writeData(this->stateLEDs[i].green);
|
||||
}
|
||||
else{
|
||||
this->writeData(byte(0));
|
||||
this->writeData(byte(0));
|
||||
this->writeData(byte(0));
|
||||
this->writeData(0);
|
||||
this->writeData(0);
|
||||
this->writeData(0);
|
||||
}
|
||||
this->waitEOS();
|
||||
}
|
||||
@@ -612,9 +718,9 @@ void Cockpit::waitGSLAT() {
|
||||
#endif
|
||||
}
|
||||
|
||||
void Cockpit::writeData(byte data) {
|
||||
for (byte i = 0; i<8; i++) {
|
||||
if(data & B10000000) {
|
||||
void Cockpit::writeData(uint8_t data) {
|
||||
for (uint8_t i = 0; i<8; i++) {
|
||||
if(data & 0b10000000) {
|
||||
this->writeOne();
|
||||
}
|
||||
else{
|
||||
|
||||
681
src/tlc.cpp.old
Normal file
681
src/tlc.cpp.old
Normal file
@@ -0,0 +1,681 @@
|
||||
#include "tlc.hpp"
|
||||
|
||||
Cockpit::Cockpit(gpio_num_t pin, int leds, int pTime, int tCycle, byte brightnessState){
|
||||
this->pin = pin;
|
||||
this->leds = leds;
|
||||
this->pTime = pTime;
|
||||
this->tCycle = tCycle;
|
||||
// this->brightness = brightness;
|
||||
this->brightnessState = brightnessState;
|
||||
|
||||
this->cp = 0;
|
||||
}
|
||||
|
||||
void Cockpit::knightRider(int state){
|
||||
byte color = 1;
|
||||
this->turnOffLeds();
|
||||
|
||||
byte brightness = this->brightnessState;
|
||||
byte dimmedIntensitiy = brightness / 2;
|
||||
byte heavyDimmedIntensitiy = brightness / 4;
|
||||
|
||||
int step = state % 13;
|
||||
switch (step)
|
||||
{
|
||||
case 0:
|
||||
this->stateLEDs[0][color] = brightness;
|
||||
// step += 1;
|
||||
break;
|
||||
case 1:
|
||||
this->stateLEDs[0][color] = dimmedIntensitiy;
|
||||
this->stateLEDs[1][color] = brightness;
|
||||
// step++;
|
||||
break;
|
||||
case 2:
|
||||
this->stateLEDs[0][color] = heavyDimmedIntensitiy;
|
||||
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[3][color] = brightness;
|
||||
// step++;
|
||||
break;
|
||||
case 4:
|
||||
this->stateLEDs[1][color] = 0;
|
||||
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[5][color] = brightness;
|
||||
// step++;
|
||||
break;
|
||||
case 6:
|
||||
this->stateLEDs[3][color] = 0;
|
||||
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[6][color] = dimmedIntensitiy;
|
||||
// step++;
|
||||
break;
|
||||
case 8:
|
||||
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[6][color] = 0;
|
||||
// step++;
|
||||
break;
|
||||
case 10:
|
||||
this->stateLEDs[2][color] = brightness;
|
||||
this->stateLEDs[3][color] = dimmedIntensitiy;
|
||||
this->stateLEDs[4][color] = heavyDimmedIntensitiy;
|
||||
this->stateLEDs[5][color] = 0;
|
||||
// step++;
|
||||
break;
|
||||
case 11:
|
||||
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[3][color] = 0;
|
||||
// step++;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
this->writeLEDs();
|
||||
}
|
||||
|
||||
void Cockpit::knightRiderAdvanced(int state){
|
||||
byte color = 1;
|
||||
this->turnOffLeds();
|
||||
|
||||
byte rampUpSteps = 0;
|
||||
byte highSteps = 4;
|
||||
byte rampDownSteps = 4;
|
||||
byte slotSteps = rampUpSteps + highSteps + rampDownSteps;
|
||||
byte brightnessRampUpStep = this->brightness[brightnessState]/rampUpSteps;
|
||||
byte brightnessRampDownStep = this->brightness[brightnessState]/rampDownSteps;
|
||||
|
||||
int lowerBoundSlot = 0;
|
||||
int upperBoundSlot = slotSteps;
|
||||
uint8_t index;
|
||||
|
||||
state = state % (slotSteps+12*2);
|
||||
|
||||
// sequence of 70 states
|
||||
for(int i = 0; i < 13; i++){
|
||||
|
||||
lowerBoundSlot = i*2;
|
||||
upperBoundSlot = i*2 + slotSteps;
|
||||
|
||||
if(i < 7){
|
||||
index = i;
|
||||
}
|
||||
else{
|
||||
index = 6-(i-6);
|
||||
}
|
||||
|
||||
if (state >= lowerBoundSlot && state < upperBoundSlot){
|
||||
// ramp up
|
||||
if(state >= lowerBoundSlot && state < lowerBoundSlot + rampUpSteps){
|
||||
this->stateLEDs[index][color] = brightnessRampUpStep * (state-lowerBoundSlot);
|
||||
if(this->stateLEDs[index][color] > this->brightness[brightnessState]){
|
||||
this->stateLEDs[index][color] = this->brightness[brightnessState];
|
||||
}
|
||||
}
|
||||
// high
|
||||
if (state >= lowerBoundSlot + rampUpSteps && state < lowerBoundSlot+rampUpSteps+highSteps) {
|
||||
this->stateLEDs[index][color] = this->brightness[brightnessState];
|
||||
}
|
||||
// ramp down
|
||||
else if(state >= lowerBoundSlot + rampUpSteps + highSteps && state < upperBoundSlot){
|
||||
this->stateLEDs[index][color] = this->brightness[this->brightnessState] - brightnessRampDownStep*(state-(upperBoundSlot-rampDownSteps));
|
||||
if(this->stateLEDs[index][color] < 0){
|
||||
this->stateLEDs[index][color] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this->writeLEDs();
|
||||
}
|
||||
|
||||
byte scaleColor(byte color, int scaleFactor){
|
||||
return byte(color * (float)scaleFactor / 255);
|
||||
}
|
||||
|
||||
void Cockpit::knightRiderColored(int state, byte color[]){
|
||||
int step = state % (this->leds*2-1);
|
||||
if (step < this->leds){
|
||||
this->stateLEDs[step][0] = scaleColor(color[0], this->brightness[this->brightnessState]);
|
||||
this->stateLEDs[step][1] = scaleColor(color[1], this->brightness[this->brightnessState]);
|
||||
this->stateLEDs[step][2] = scaleColor(color[2], this->brightness[this->brightnessState]);
|
||||
if(step < 1){
|
||||
this->stateLEDs[1][0] = 0;
|
||||
this->stateLEDs[1][1] = 0;
|
||||
this->stateLEDs[1][2] = 0;
|
||||
this->stateLEDs[2][0] = 0;
|
||||
this->stateLEDs[2][1] = 0;
|
||||
this->stateLEDs[2][2] = 0;
|
||||
}
|
||||
if (step > 0){
|
||||
this->stateLEDs[step-1][0] = scaleColor(color[0], this->brightness[this->brightnessState]/2);
|
||||
this->stateLEDs[step-1][1] = scaleColor(color[1], this->brightness[this->brightnessState]/2);
|
||||
this->stateLEDs[step-1][2] = scaleColor(color[2], this->brightness[this->brightnessState]/2);
|
||||
}
|
||||
if (step > 1){
|
||||
this->stateLEDs[step-2][0] = scaleColor(color[0], this->brightness[this->brightnessState]/4);
|
||||
this->stateLEDs[step-2][1] = scaleColor(color[1], this->brightness[this->brightnessState]/4);
|
||||
this->stateLEDs[step-2][2] = scaleColor(color[2], this->brightness[this->brightnessState]/4);
|
||||
}
|
||||
if (step > 2){
|
||||
this->stateLEDs[step-3][0] = 0;
|
||||
this->stateLEDs[step-3][1] = 0;
|
||||
this->stateLEDs[step-3][2] = 0;
|
||||
}
|
||||
}
|
||||
else{
|
||||
int curLed = this->leds-2 - (step-this->leds);
|
||||
// int curLed = step - (step%(this->leds-1))*2;
|
||||
this->stateLEDs[curLed][0] = scaleColor(color[0], this->brightness[this->brightnessState]);
|
||||
this->stateLEDs[curLed][1] = scaleColor(color[1], this->brightness[this->brightnessState]);
|
||||
this->stateLEDs[curLed][2] = scaleColor(color[2], this->brightness[this->brightnessState]);
|
||||
if(curLed < this->leds-1){
|
||||
this->stateLEDs[curLed+1][0] = scaleColor(color[0], this->brightness[this->brightnessState]/2);
|
||||
this->stateLEDs[curLed+1][1] = scaleColor(color[1], this->brightness[this->brightnessState]/2);
|
||||
this->stateLEDs[curLed+1][2] = scaleColor(color[2], this->brightness[this->brightnessState]/2);
|
||||
}
|
||||
if(curLed < this->leds-2){
|
||||
this->stateLEDs[curLed+2][0] = scaleColor(color[0], this->brightness[this->brightnessState]/4);
|
||||
this->stateLEDs[curLed+2][1] = scaleColor(color[1], this->brightness[this->brightnessState]/4);
|
||||
this->stateLEDs[curLed+2][2] = scaleColor(color[2], this->brightness[this->brightnessState]/4);
|
||||
}
|
||||
if(curLed < this->leds-3){
|
||||
this->stateLEDs[curLed+3][0] = 0;
|
||||
this->stateLEDs[curLed+3][1] = 0;
|
||||
this->stateLEDs[curLed+3][2] = 0;
|
||||
}
|
||||
}
|
||||
this->writeLEDs();
|
||||
}
|
||||
|
||||
void Cockpit::turnOffLeds(){
|
||||
for(byte led = 0; led < 7; led++){
|
||||
for(byte color = 0; color < 3; color++){
|
||||
this->stateLEDs[led][color] = 0;
|
||||
}
|
||||
}
|
||||
this->writeLEDs();
|
||||
}
|
||||
|
||||
void Cockpit::runningLights(byte color){
|
||||
this->turnOffLeds();
|
||||
for(byte led = 0; led < 7; led++){
|
||||
this->stateLEDs[led][0] = 10;
|
||||
writeLEDs();
|
||||
delay(30);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Cockpit::setBrightnessState(int brightnessState, int state){
|
||||
this->brightnessState = brightnessState;
|
||||
if (state < 10){
|
||||
for(int i = 0; i < this->leds; i++){
|
||||
if(i <= brightnessState){
|
||||
this->stateLEDs[i][0] = this->brightness[this->brightnessState];
|
||||
this->stateLEDs[i][1] = 0;
|
||||
this->stateLEDs[i][2] = 0;
|
||||
}
|
||||
else{
|
||||
this->stateLEDs[i][0] = 0;
|
||||
this->stateLEDs[i][1] = 0;
|
||||
this->stateLEDs[i][2] = 0;
|
||||
}
|
||||
}
|
||||
writeLEDs();
|
||||
}
|
||||
else{
|
||||
for(int i = 0; i < this->leds; i++){
|
||||
this->stateLEDs[i][0] = 0;
|
||||
this->stateLEDs[i][1] = 0;
|
||||
this->stateLEDs[i][2] = 0;
|
||||
}
|
||||
writeLEDs();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Cockpit::alternating(byte color[3], int state, int frequencyDivider){
|
||||
if(state % (frequencyDivider*2) < frequencyDivider){
|
||||
state = 0;
|
||||
}
|
||||
else{
|
||||
state = 1;
|
||||
}
|
||||
if(state % 2 == 0){
|
||||
for(byte led = 0; led < this->leds; led++){
|
||||
if(led % 2 == 0){
|
||||
this->stateLEDs[led][0] = int(color[0] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led][1] = int(color[1] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led][2] = int(color[2] * (float)this->brightness[this->brightnessState] / 255);
|
||||
}
|
||||
else{
|
||||
this->stateLEDs[led][0] = 0;
|
||||
this->stateLEDs[led][1] = 0;
|
||||
this->stateLEDs[led][2] = 0;
|
||||
}
|
||||
|
||||
}
|
||||
this->writeLEDs();
|
||||
}
|
||||
else{
|
||||
for(byte led = 0; led < this->leds; led++){
|
||||
if(led % 2 == 1){
|
||||
this->stateLEDs[led][0] = int(color[0] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led][1] = int(color[1] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led][2] = int(color[2] * (float)this->brightness[this->brightnessState] / 255);
|
||||
}
|
||||
else{
|
||||
this->stateLEDs[led][0] = 0;
|
||||
this->stateLEDs[led][1] = 0;
|
||||
this->stateLEDs[led][2] = 0;
|
||||
}
|
||||
|
||||
}
|
||||
this->writeLEDs();
|
||||
}
|
||||
}
|
||||
|
||||
void Cockpit::getColor(int state, byte color[]){
|
||||
color[0] = (state * 10 ) % 255;
|
||||
color[1] = (state * 10 +100) % 255;
|
||||
color[2] = (state * 10 +200) % 255;
|
||||
}
|
||||
|
||||
void Cockpit::flashing(byte color[3], int state, uint8_t frequencyDivider){
|
||||
|
||||
if(state % frequencyDivider < frequencyDivider/2){
|
||||
for(byte led = 0; led < this->leds; led++){
|
||||
|
||||
this->stateLEDs[led][0] = int(color[0] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led][1] = int(color[1] * (float)this->brightness[this->brightnessState] / 255);
|
||||
this->stateLEDs[led][2] = int(color[2] * (float)this->brightness[this->brightnessState] / 255);
|
||||
|
||||
}
|
||||
this->writeLEDs();
|
||||
}
|
||||
else{
|
||||
for(byte led = 0; led < this->leds; led++){
|
||||
// for(byte colorCtr = 0; colorCtr < 3; colorCtr++){
|
||||
// stateled[led][colorCtr] = 0;
|
||||
// }
|
||||
this->stateLEDs[led][0] = 0;
|
||||
this->stateLEDs[led][1] = 0;
|
||||
this->stateLEDs[led][2] = 0;
|
||||
// stateled[led][color[1]] = 10;
|
||||
// stateled[led][color[2]] = 10;
|
||||
// stateled[led][1] = 10;
|
||||
}
|
||||
this->writeLEDs();
|
||||
}
|
||||
}
|
||||
|
||||
void Cockpit::writeLEDs(){
|
||||
this->writeCommTimer();
|
||||
for (int i = 0; i < this->leds; i++) {
|
||||
this->writeCommand();
|
||||
|
||||
this->writeData(this->stateLEDs[i][0]);
|
||||
this->writeData(this->stateLEDs[i][1]);
|
||||
this->writeData(this->stateLEDs[i][2]);
|
||||
|
||||
this->waitEOS();
|
||||
}
|
||||
this->waitGSLAT();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Cockpit::writeLED(byte led) {
|
||||
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][0]);
|
||||
this->writeData(this->stateLEDs[i][1]);
|
||||
this->writeData(this->stateLEDs[i][2]);
|
||||
}
|
||||
else{
|
||||
this->writeData(byte(0));
|
||||
this->writeData(byte(0));
|
||||
this->writeData(byte(0));
|
||||
}
|
||||
this->waitEOS();
|
||||
}
|
||||
this->waitGSLAT();
|
||||
}
|
||||
|
||||
void Cockpit::writeZero() {
|
||||
// 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++;
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
|
||||
#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++;
|
||||
|
||||
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
|
||||
|
||||
#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
|
||||
gpio_set_level(this->pin, 0);
|
||||
delayMicroseconds(pTime);
|
||||
// PORTB &= B111110; //uno
|
||||
gpio_set_level(this->pin, 1);
|
||||
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);
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
void Cockpit::writeCommand() {
|
||||
this->writeZero();
|
||||
this->writeZero();
|
||||
this->writeOne();
|
||||
this->writeOne();
|
||||
|
||||
this->writeOne();
|
||||
this->writeZero();
|
||||
this->writeOne();
|
||||
this->writeZero();
|
||||
}
|
||||
|
||||
//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(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)
|
||||
void Cockpit::waitGSLAT() {
|
||||
// PORTB &= B111110; //uno
|
||||
|
||||
#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 += 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_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(byte data) {
|
||||
for (byte i = 0; i<8; i++) {
|
||||
if(data & B10000000) {
|
||||
this->writeOne();
|
||||
}
|
||||
else{
|
||||
this->writeZero();
|
||||
}
|
||||
data <<= 1;
|
||||
}
|
||||
}
|
||||
96
src/tlc.hpp
96
src/tlc.hpp
@@ -1,45 +1,103 @@
|
||||
#include <Arduino.h>
|
||||
#ifndef TLC_H
|
||||
#define TLC_H
|
||||
// #include <cstdint>
|
||||
// #include <cstdint>
|
||||
#include "Arduino.h"
|
||||
#include <stdint.h>
|
||||
#define INVERSE_MODE
|
||||
#include "driver/gpio.h"
|
||||
#include <inttypes.h>
|
||||
#include "driver/rmt.h"
|
||||
#define BUFFER_SIZE 1818
|
||||
// #include "config.h"
|
||||
|
||||
// #define MODE_INTERRUPT
|
||||
#define MODE_RMT
|
||||
#define NUM_LEDS 7
|
||||
#define COLOR_CHANNELS 3
|
||||
|
||||
class Cockpit;
|
||||
|
||||
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 Dimmer {
|
||||
BRIGHTNESS_UP,
|
||||
BRIGHTNESS_DOWN,
|
||||
NO_DIMMER
|
||||
};
|
||||
|
||||
enum Brightness{
|
||||
BRIGHTER,
|
||||
DARKER,
|
||||
NO_CHANGE
|
||||
};
|
||||
|
||||
class Cockpit{
|
||||
private:
|
||||
// byte brightness[7] = {5, 10, 20, 50, 70, 90, 100};
|
||||
byte brightness[7] = {13, 25, 50, 127, 178, 229, 255};
|
||||
int pTime;
|
||||
int tCycle;
|
||||
int leds; // amout of leds
|
||||
// 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 tCycle;
|
||||
uint8_t leds; // amout of leds
|
||||
// int pin; // pin connected to uC
|
||||
gpio_num_t pin;
|
||||
byte stateLEDs[7][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
|
||||
byte brightnessState;
|
||||
// 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, byte brightness);
|
||||
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 knightRiderColored(int state, byte color[]);
|
||||
// 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 runningLights(byte color);
|
||||
void flashing(byte color[3], int state, u_int8_t frequencyDivider);
|
||||
void alternating(byte color[3], int state, int frequencyDivider);
|
||||
|
||||
|
||||
void getColor(int state, byte color[3]);
|
||||
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(byte ledNum);
|
||||
void writeLED(uint8_t ledNum);
|
||||
void writeZero();
|
||||
void writeOne();
|
||||
void writeCommTimer();
|
||||
@@ -48,6 +106,6 @@ class Cockpit{
|
||||
void waitEOS();
|
||||
//grayscale latch (for the end of a chain of driver chips)
|
||||
void waitGSLAT();
|
||||
void writeData(byte data);
|
||||
void writeData(uint8_t data);
|
||||
};
|
||||
#endif
|
||||
54
src/tlc.hpp.old
Normal file
54
src/tlc.hpp.old
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <Arduino.h>
|
||||
#ifndef TLC_H
|
||||
#define TLC_H
|
||||
#define INVERSE_MODE
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/rmt.h"
|
||||
#define BUFFER_SIZE 1818
|
||||
|
||||
// #define MODE_INTERRUPT
|
||||
#define MODE_RMT
|
||||
|
||||
class Cockpit{
|
||||
private:
|
||||
// byte brightness[7] = {5, 10, 20, 50, 70, 90, 100};
|
||||
byte brightness[7] = {6, 25, 50, 127, 178, 229, 255};
|
||||
int pTime;
|
||||
int tCycle;
|
||||
int leds; // amout of leds
|
||||
// int pin; // pin connected to uC
|
||||
gpio_num_t pin;
|
||||
byte stateLEDs[7][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
|
||||
byte brightnessState;
|
||||
|
||||
public:
|
||||
int cp;
|
||||
bool buffer[BUFFER_SIZE];
|
||||
rmt_item32_t led_data[BUFFER_SIZE];
|
||||
Cockpit(gpio_num_t pin, int leds, int pTime, int tCycle, byte brightness);
|
||||
void setBrightnessState(int brightness, int state);
|
||||
void turnOffLeds();
|
||||
void knightRider(int state);
|
||||
void knightRiderAdvanced(int state);
|
||||
void knightRiderColored(int state, byte color[]);
|
||||
|
||||
void runningLights(byte color);
|
||||
void flashing(byte color[3], int state, u_int8_t frequencyDivider);
|
||||
void alternating(byte color[3], int state, int frequencyDivider);
|
||||
|
||||
|
||||
void getColor(int state, byte color[3]);
|
||||
|
||||
void writeLEDs();
|
||||
void writeLED(byte 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(byte data);
|
||||
};
|
||||
#endif
|
||||
Reference in New Issue
Block a user