TouchDesigner is a node-based visual programming environment used to create interactive installations, control live performances and manipulate media in realtime. It can be enhanced with custom Python programming.
In this specific case, I generate visual textures that are then sent over serial to LED strips (via Arduino). Actually, this is for a class I teach where students are expected to work with LED strips in realtime.
Is it important in this application that the bytes are received as fast as possible?
Yes. It needs to be fast because the texture sent to the strips are constantly changing. When I tried parsing each character, I was getting much slower results. By reading the serial data directly into the FastLED buffer, it yields a very significant speed improvement.
Just to be extra clear, the code I have works (since I corrected the mistake pointed out by @J-M-L).
The only problem that remains is that I want to introduce a separator in the byte stream to append other control information after the LED colour information. When I use readSerialBytes()
, it returns more bytes than expected. I send 180 bytes (60 LEDs with 3 colours each) and then a separator. However, it is as though readSerialBytes()
does not recognize the separator and just continues reading data. I just do not understand why the function does not stop at my separator (255).
what do the parameters 1 and big mean in r.to_bytes(1, "big")
This converts the red value (an int) to a byte using Big Endian.
If people want to run your applications to try and find out what is wrong then they need to know this information.
I'm afraid it would be difficult to replicate the whole setup on your side. That's why I'm trying to narrow it down to something that's not too involved for you guys.
Here is the code with translated variable names (see comments in code):
#include <FastLED.h> // https://fastled.io/
const int LED_PIN = 6;
const int NUM_LEDS = 60;
unsigned long SPEED = 115200;
const CRGB CORRECTION(255, 175, 235);
const int BRIGHTNESS = 32;
size_t count = 0;
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(SPEED);
FastLED.addLeds<NEOPIXEL, LED_PIN>(dels, NUM_LEDS);
FastLED.show();
FastLED.setBrightness(BRIGHTNESS);
FastLED.setCorrection(CORRECTION);
}
void loop() {
// This works fine:
count = Serial.readBytesUntil(255, (char*)dels, NUM_LEDS * 3 + 1);
// However, if I do this (below), "count" will be greater than the
// expected 180. It is as though my separator byte (255) is not being
// recognized.
count = Serial.readBytesUntil(255, (char*)dels, 200);
}
So, I guess I'm not sending the separator character correctly but I have no idea why it does not work. In Python, this is the code that adds the separator at the end of the colour bytes:
opArduino.sendBytes(int(255).to_bytes(1, "big"))
I have no clue why the 255 byte generated in this way is not recognized by the Serial.readBytesUntil()
method. Its basically the same as when I send the colours and the colours are received okay.
I hope this is clearer. Thanks all for your help.