Files
Sicherheitssystem/src/tlc.cpp.old
2025-07-20 18:26:24 +02:00

681 lines
20 KiB
C++

#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;
}
}