Cleanup
This commit is contained in:
parent
4f6666a874
commit
0260c3bb6b
@ -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
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user