From f1788c1b9133b3d6fc940850686931b43f144cea Mon Sep 17 00:00:00 2001 From: Johannes Paehr Date: Sun, 7 Dec 2025 13:20:26 +0100 Subject: [PATCH] adjusted timings --- .gitignore | 6 + platformio.ini | 5 +- src/MyBLEServer.cpp | 4 + src/main.cpp | 17 +- src/tlc.cpp | 379 +++++++++++++++++++++++--------------------- src/tlc.hpp | 4 +- 6 files changed, 225 insertions(+), 190 deletions(-) diff --git a/.gitignore b/.gitignore index 0ac7f37..586fec7 100644 --- a/.gitignore +++ b/.gitignore @@ -90,3 +90,9 @@ Thumbs.db # debug information files *.dwo + +# o-files +*.o + +# binary +*.bin \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 30fbbbf..dcffc07 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,7 +15,10 @@ board = esp32-c3-devkitm-1 ; board = esp32doit-devkit-v1 framework = arduino ; framework = espidf -monitor_speed = 9600 +monitor_speed = 115200 +monitor_rts = 0 +monitor_dtr = 0 +upload_speed = 921600 ; upload_port = /dev/ttyACM0 build_flags = -DARDUINO_USB_CDC_ON_BOOT=1 -DARDUINO_USB_MODE=1 -Wunused -Wall -Wextra ; upload_protocol = espota diff --git a/src/MyBLEServer.cpp b/src/MyBLEServer.cpp index a786459..41b27ef 100644 --- a/src/MyBLEServer.cpp +++ b/src/MyBLEServer.cpp @@ -1,4 +1,6 @@ #include "MyBLEServer.hpp" +#include "Arduino.h" +#include "HWCDC.h" #include "config.hpp" #include "tlc.hpp" @@ -32,6 +34,7 @@ MyAlarmCallback::MyAlarmCallback(enum Alarm * alarm, enum Dimmer * dimmer, enum } void MyAlarmCallback::onWrite(BLECharacteristic * pCharacteristic) { String value = pCharacteristic->getValue().c_str(); + // Serial.println(value); if (value.compareTo("X") == 0) { // Serial.println("found X"); @@ -62,6 +65,7 @@ void MyAlarmCallback::onWrite(BLECharacteristic * pCharacteristic) { *dimmerState = BRIGHTNESS_UP; } else if (value.compareTo("darker") == 0) { + // Serial.println("found darker"); // alarmFired_100m = 1; // alarmFired = ALARM_100M; diff --git a/src/main.cpp b/src/main.cpp index b8b1bad..7c3be7b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -105,13 +105,13 @@ void setup() { pinMode(DARKER_PIN, INPUT_PULLUP); pinMode(GPIO_NUM_10, INPUT); // former Alarm pin - Serial.begin(9600); - delay(1000); - Serial.println("Hallo Welt"); - delay(1000); - Serial.println("Hallo Welt"); - delay(1000); - Serial.println("Hallo Welt"); + Serial.begin(115200); + // 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) @@ -212,7 +212,6 @@ void loop() { // Serial.print("WIFI"); } else { - vTaskDelay(100 / portTICK_PERIOD_MS); if (serverCallback->getConnectionState()) { @@ -240,7 +239,7 @@ void loop() { // 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 < 49; i++) { + for (int i = 0; i < 48; i++) { // cockpit.knightRiderAdvanced(red, i); // cockpit.outToIn(red, i); cockpit.inToOutMulticolor(c, 12, i); diff --git a/src/tlc.cpp b/src/tlc.cpp index 9461064..ea7ca45 100644 --- a/src/tlc.cpp +++ b/src/tlc.cpp @@ -1,5 +1,7 @@ #include "tlc.hpp" +#include "Arduino.h" #include "config.hpp" +#include Color Cockpit::ORANGE = {.blue = 0, .red = 255, .green = 0xA5}; Color Cockpit::RED = {.blue = 0, .red = 255, .green = 0}; @@ -118,7 +120,7 @@ void Cockpit::outToInMulticolor(Color color[], uint8_t colors, int state) { } void Cockpit::inToOutMulticolor(Color color[], uint8_t colors, int state) { - uint8_t curColorIndex = state / 4; + uint8_t curColorIndex = (state / 4) % colors; state = state % 4; turnOffLeds(); @@ -191,108 +193,6 @@ void Cockpit::colorfade(int state) { 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 @@ -454,25 +354,63 @@ void Cockpit::writeZero() { #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++; + // costs 4 cp + + byte pattern = 0b11100000; // should be 0b11000000 <-hardware hack + byte invPattern = ~pattern; + + for (uint8_t i = 0; i < 8; i++) { + if (invPattern & 0x80) { + if (i % 2 == 0) { + this->led_data[this->cp].level0 = 1; + this->led_data[this->cp].duration0 = 1; + } + else { + this->led_data[this->cp].level1 = 1; + this->led_data[this->cp].duration1 = 1; + this->cp++; + } + } + else { + if (i % 2 == 0) { + this->led_data[this->cp].level0 = 0; + this->led_data[this->cp].duration0 = 1; + } + else { + this->led_data[this->cp].level1 = 0; + this->led_data[this->cp].duration1 = 1; + this->cp++; + } + } + invPattern <<= 1; + // this->led_data[this->cp].duration0 = 1; + // this->led_data[this->cp].duration1 = 1; } + // this->led_data[this->cp].level0 = 0; + + // before + // 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++; + // } + // end before + #else #if defined(MODE_INTERRUPT) @@ -522,42 +460,79 @@ 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++; + // costs 4 cp + + // byte pattern = 0b11001100; // this is how it should be + byte pattern = 0b11101110; // hardware hack, because open collector is slow -> more 1 than 0 + byte invPattern = ~pattern; + + for (uint8_t i = 0; i < 8; i++) { + if (invPattern & 0x80) { + if (i % 2 == 0) { + this->led_data[this->cp].level0 = 1; + this->led_data[this->cp].duration0 = 1; + } + else { + this->led_data[this->cp].level1 = 1; + this->led_data[this->cp].duration1 = 1; + this->cp++; + } + } + else { + if (i % 2 == 0) { + this->led_data[this->cp].level0 = 0; + this->led_data[this->cp].duration0 = 1; + } + else { + this->led_data[this->cp].level1 = 0; + this->led_data[this->cp].duration1 = 1; + this->cp++; + } + } + invPattern <<= 1; + } + + // before + + // 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++; + + // end before #else @@ -614,12 +589,16 @@ void Cockpit::writeOne() { } void Cockpit::writeCommTimer() { - // first two zeroes determine the timing (tcycle) after device is powered up or after a GSLAT + // first two zeroes determine the timing (tcycle) after device is powered up or after a + // GSLAT + + // 8 cp this->writeZero(); this->writeZero(); } void Cockpit::writeCommand() { + // cost 32 cp this->writeZero(); this->writeZero(); this->writeOne(); @@ -637,11 +616,31 @@ 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++; + + // costs 16 cp + + for (uint8_t i = 0; i < CYCLYTIME * 4; i++) { + + if (i % 2 == 0) { + this->led_data[this->cp].level0 = 1; + this->led_data[this->cp].duration0 = 1; + } + else { + this->led_data[this->cp].level1 = 1; + this->led_data[this->cp].duration1 = 1; + this->cp++; + } + } + + // before + // 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++; + + // end before + #else #if defined(MODE_INTERRUPT) this->buffer[this->cp] = 1; @@ -655,13 +654,15 @@ void Cockpit::waitEOS() { #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 + // before + // 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 + // end before #else #if defined(MODE_INTERRUPT) @@ -683,11 +684,28 @@ void Cockpit::waitGSLAT() { #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; + // cost 32 cp + + for (uint8_t i = 0; i < CYCLYTIME * 8; i++) { + + if (i % 2 == 0) { + this->led_data[this->cp].level0 = 1; + this->led_data[this->cp].duration0 = 1; + } + else { + this->led_data[this->cp].level1 = 1; + this->led_data[this->cp].duration1 = 1; + this->cp++; + } + } + this->cp = 0; + // before + // 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; + // end before #else #if defined(MODE_INTERRUPT) @@ -703,13 +721,16 @@ void Cockpit::waitGSLAT() { #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 + + // before + // 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 + // end before #else #if defined(MODE_INTERRUPT) diff --git a/src/tlc.hpp b/src/tlc.hpp index c503657..53a1644 100644 --- a/src/tlc.hpp +++ b/src/tlc.hpp @@ -9,13 +9,15 @@ #include "driver/rmt.h" #include -#define BUFFER_SIZE 1818 +#define BUFFER_SIZE 1048 // 1818 +// #define BUFFER_SIZE 1818 // 1818 // #include "config.h" // #define MODE_INTERRUPT #define MODE_RMT #define NUM_LEDS 7 #define COLOR_CHANNELS 3 +#define CYCLYTIME 8 // us class Cockpit;