Implement sync (e.g. after reset)
This commit is contained in:
parent
406c628810
commit
74526fa07d
@ -24,6 +24,17 @@ with serial.Serial("/dev/ttyUSB0", 115200) as ser:
|
|||||||
FRAME_TIME = 1 / FPS
|
FRAME_TIME = 1 / FPS
|
||||||
t = 0
|
t = 0
|
||||||
|
|
||||||
|
def sync():
|
||||||
|
# wait for sync
|
||||||
|
while True:
|
||||||
|
b = ser.readline()
|
||||||
|
if b.strip() == b"Sync.":
|
||||||
|
return
|
||||||
|
|
||||||
|
sync()
|
||||||
|
|
||||||
|
print("initial sync.")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
loop_start = time.time()
|
loop_start = time.time()
|
||||||
@ -39,6 +50,12 @@ with serial.Serial("/dev/ttyUSB0", 115200) as ser:
|
|||||||
ser.write(payload)
|
ser.write(payload)
|
||||||
ser.flush()
|
ser.flush()
|
||||||
|
|
||||||
|
response = ser.readline()
|
||||||
|
if response.strip() != b"Ack.":
|
||||||
|
print(f"received bad response: {response!r}")
|
||||||
|
sync()
|
||||||
|
continue
|
||||||
|
|
||||||
t += FRAME_TIME
|
t += FRAME_TIME
|
||||||
t %= 1
|
t %= 1
|
||||||
|
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
unsigned long tic_loop = 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 unsigned int FRAME_TIME = 20; // 20 ms -> 50 FPS
|
||||||
|
|
||||||
const size_t UNIVERSE_SIZE = 512;
|
const size_t UNIVERSE_SIZE = 512;
|
||||||
@ -15,7 +18,8 @@ void setup()
|
|||||||
// spin until serial is up
|
// spin until serial is up
|
||||||
}
|
}
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println("Init.");
|
Serial.println("Sync.");
|
||||||
|
time_since_last_sync = millis();
|
||||||
|
|
||||||
Serial1.begin(250000, SERIAL_8N2); // DMX
|
Serial1.begin(250000, SERIAL_8N2); // DMX
|
||||||
}
|
}
|
||||||
@ -38,19 +42,30 @@ void send_packet()
|
|||||||
|
|
||||||
bool update_buffer()
|
bool update_buffer()
|
||||||
{
|
{
|
||||||
|
unsigned long now = millis();
|
||||||
size_t n = Serial.available();
|
size_t n = Serial.available();
|
||||||
if (!n)
|
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;
|
return false;
|
||||||
|
}
|
||||||
|
time_since_last_sync = now;
|
||||||
|
|
||||||
int bytes_received = Serial.read(channels_buffer + bytes_read, std::min(n, UNIVERSE_SIZE - bytes_read));
|
int bytes_received = Serial.read(channels_buffer + bytes_read, std::min(n, UNIVERSE_SIZE - bytes_read));
|
||||||
bytes_read += bytes_received;
|
bytes_read += bytes_received;
|
||||||
|
|
||||||
// Serial.printf("rx n=%d rec=%d total=%d\n", n, bytes_received, bytes_read);
|
|
||||||
|
|
||||||
if (bytes_read == UNIVERSE_SIZE)
|
if (bytes_read == UNIVERSE_SIZE)
|
||||||
{
|
{
|
||||||
bytes_read = 0;
|
bytes_read = 0;
|
||||||
// Serial.println();
|
Serial.println("Ack.");
|
||||||
// Serial.println("Updated.");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -63,18 +78,10 @@ void send_dmx_header()
|
|||||||
{
|
{
|
||||||
Serial1.flush();
|
Serial1.flush();
|
||||||
Serial1.begin(90000, SERIAL_8N2);
|
Serial1.begin(90000, SERIAL_8N2);
|
||||||
/*
|
|
||||||
while (Serial1.available())
|
|
||||||
Serial1.read();
|
|
||||||
*/
|
|
||||||
// send the break as a "slow" byte
|
// send the break as a "slow" byte
|
||||||
Serial1.write(0);
|
Serial1.write(0);
|
||||||
// switch back to the original baud rate
|
// switch back to the original baud rate
|
||||||
Serial1.flush();
|
Serial1.flush();
|
||||||
Serial1.begin(250000, SERIAL_8N2);
|
Serial1.begin(250000, SERIAL_8N2);
|
||||||
/*
|
|
||||||
while (Serial1.available())
|
|
||||||
Serial1.read();
|
|
||||||
*/
|
|
||||||
Serial1.write(0); // Start-Byte
|
Serial1.write(0); // Start-Byte
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user