Compare commits

...

4 Commits

Author SHA1 Message Date
0260c3bb6b
Cleanup 2021-08-30 10:11:46 +02:00
4f6666a874
Remove old code 2021-08-30 10:11:06 +02:00
d76bfcafe8
Cleanup 2021-08-30 10:10:33 +02:00
503690e9da
Implement triple buffering 2021-08-30 09:33:28 +02:00
5 changed files with 168 additions and 174 deletions

View File

@ -1,3 +1,4 @@
.vscode .vscode
.mypy_cache .mypy_cache
venv venv
build

View File

@ -2,6 +2,8 @@ import serial
import time import time
import colorsys import colorsys
import sys
channels = [ channels = [
192, # pan 192, # pan
0, # tilt 0, # tilt
@ -21,6 +23,9 @@ with serial.Serial("/dev/ttyUSB0", 500000) as ser:
payload = bytearray(512) payload = bytearray(512)
FPS = 30 FPS = 30
if len(sys.argv) > 1:
FPS = int(sys.argv[1])
FRAME_TIME = 1 / FPS FRAME_TIME = 1 / FPS
t = 0 t = 0

View File

@ -1,87 +0,0 @@
unsigned long tic_loop = 0;
unsigned long time_since_last_sync;
const unsigned long SYNC_TIMEOUT = 1000;
const unsigned int FRAME_TIME = 20; // 20 ms -> 50 FPS
const size_t UNIVERSE_SIZE = 512;
byte channels_buffer[UNIVERSE_SIZE] = {0};
size_t bytes_read = 0;
void setup()
{
Serial.begin(500000); // USB
while (!Serial)
{
// spin until serial is up
}
Serial.println();
Serial.println("Sync.");
time_since_last_sync = millis();
Serial1.begin(250000, SERIAL_8N2); // DMX
}
void loop()
{
bool packet_ready = update_buffer();
if (packet_ready)
{
send_packet();
Serial.println("Ack.");
}
}
void send_packet()
{
send_dmx_header();
Serial1.write(channels_buffer, UNIVERSE_SIZE);
}
bool update_buffer()
{
unsigned long now = millis();
size_t n = Serial.available();
if (!n)
{
// nothing available to read
if (now - time_since_last_sync > SYNC_TIMEOUT)
{
// re-sync
bytes_read = 0;
Serial.println("Sync.");
time_since_last_sync = now;
}
return false;
}
time_since_last_sync = now;
int bytes_received = Serial.read(channels_buffer + bytes_read, std::min(n, UNIVERSE_SIZE - bytes_read));
bytes_read += bytes_received;
if (bytes_read == UNIVERSE_SIZE)
{
bytes_read = 0;
return true;
}
else
{
return false;
}
}
void send_dmx_header()
{
Serial1.flush();
Serial1.begin(90000, SERIAL_8N2);
// send the break as a "slow" byte
Serial1.write(0);
// switch back to the original baud rate
Serial1.flush();
Serial1.begin(250000, SERIAL_8N2);
Serial1.write(0); // Start-Byte
}

162
microcontroller/test.ino Normal file
View File

@ -0,0 +1,162 @@
/*
Triple buffering data structure
Example writing:
----------------
DMXTripleBuffer buffer;
bytes_written = producer.write(
buffer.fill_buffer + buffer.fill_pos, // destination pointer
512 - buffer.fill_pos // maximum allowed
);
buffer.fill_pos += bytes_written;
if buffer.fill_pos == 512 {
buffer.on_fill_complete(); // swap buffers
buffer.fill_pos = 0; // reset fill pos
}
*/
struct InternalBuffer {
char data[512] = {0};
bool is_fresh = false;
};
class DMXTripleBuffer
{
public:
DMXTripleBuffer() {
fill_buffer = &_buffers[0];
drain_buffer = &_buffers[1];
off_buffer = &_buffers[2];
}
void on_fill_complete() {
fill_pos = 0;
fill_buffer->is_fresh = true;
std::swap(fill_buffer, off_buffer);
}
void on_drain_complete() {
drain_pos = 0;
drain_buffer->is_fresh = false;
if (off_buffer->is_fresh) {
std::swap(drain_buffer, off_buffer);
}
}
InternalBuffer *fill_buffer, *drain_buffer, *off_buffer;
size_t fill_pos = 0, drain_pos = 0;
private:
InternalBuffer _buffers[3];
};
/*
Some globals
*/
unsigned long time_since_last_sync;
const unsigned long SYNC_TIMEOUT = 1000;
const unsigned int FRAME_TIME = 20; // 20 ms -> 50 FPS
DMXTripleBuffer buffer;
bool packet_ready = true;
unsigned long pkt_count = 0;
/*
main
*/
void setup()
{
Serial.begin(500000); // USB
Serial.setRxBufferSize(512); // to fit the full DMX packet. (default is 128)
while (!Serial)
{
// spin until serial is up
}
Serial1.begin(250000, SERIAL_8N2); // DMX
while (!Serial1)
{
// spin until serial1 is up
}
Serial.println();
Serial.println("Sync.");
time_since_last_sync = millis();
}
void loop()
{
// output
if (packet_ready)
{
send_dmx_header();
packet_ready = false;
pkt_count += 1;
}
size_t n = Serial1.availableForWrite();
size_t written = Serial1.write(
buffer.drain_buffer->data + buffer.drain_pos,
std::min(n, 512 - buffer.drain_pos));
buffer.drain_pos += written;
if (buffer.drain_pos == 512)
{
buffer.on_drain_complete();
packet_ready = true;
}
// input
n = Serial.available();
unsigned long now = millis();
if (!n)
{
if (now - time_since_last_sync > 1000)
{
buffer.fill_pos = 0;
Serial.println("Sync.");
time_since_last_sync = now;
}
return;
}
time_since_last_sync = now;
size_t read = Serial.read(
buffer.fill_buffer->data + buffer.fill_pos,
std::min(n, 512 - buffer.fill_pos));
buffer.fill_pos += read;
if (buffer.fill_pos == 512)
{
buffer.on_fill_complete();
Serial.println("Ack.");
}
}
void send_dmx_header()
{
Serial1.flush();
Serial1.begin(90000, SERIAL_8N2);
// send the break as a "slow" byte
Serial1.write(0);
// switch back to the original baud rate
Serial1.flush();
Serial1.begin(250000, SERIAL_8N2);
Serial1.write(0); // Start-Byte
}

View File

@ -1,87 +0,0 @@
unsigned long tic_loop = 0;
const unsigned int FRAME_TIME = 25; // 20 ms -> 50 FPS
byte channels_buffer[512] = {0};
unsigned int bytes_to_write = 512;
const unsigned int START_ADDR = 10;
const unsigned int NUM_CHANNELS = 9;
byte dmx_data[] = {
0,
0,
134,
255,
0,
0,
0,
0,
0,
};
void setup()
{
Serial.begin(115200); // USB
Serial1.begin(250000, SERIAL_8N2); // DMX
tic_loop = millis();
for (int i = 0; i < NUM_CHANNELS; ++i)
{
channels_buffer[START_ADDR - 1 + i] = dmx_data[i];
}
}
void loop()
{
// update_buffer();
// this section gets executed at a maximum rate of around 40Hz
if ((millis() - tic_loop) > FRAME_TIME)
{
tic_loop = millis();
send_dmx_header();
Serial1.write(channels_buffer, bytes_to_write);
}
delay(1);
}
void update_buffer()
{
int n = Serial.available();
if (n < 1)
return;
n -= 1;
if (Serial.read() == n)
{
Serial.read(channels_buffer, n);
bytes_to_write = n;
}
else
{
// incomplete
while (Serial.available())
Serial.read();
}
}
void send_dmx_header()
{
Serial1.flush();
Serial1.begin(90000, SERIAL_8N2);
while (Serial1.available())
Serial1.read();
// send the break as a "slow" byte
Serial1.write(0);
// switch back to the original baud rate
Serial1.flush();
Serial1.begin(250000, SERIAL_8N2);
while (Serial1.available())
Serial1.read();
Serial1.write(0); // Start-Byte
}