From 3be7107992215beac9ad3e2aeddc75b15282c3b9 Mon Sep 17 00:00:00 2001 From: alrvid <126816223+alrvid@users.noreply.github.com> Date: Sat, 22 Apr 2023 20:58:24 +0200 Subject: [PATCH 1/2] Fix a synchronization bug Fixes a synchronization bug. Return if there's no timeout OR if there's no sync byte received. The previous version needed both to be true at the same time to return without sending a frame, which meant that a frame was sent if there was a timeout even if no sync byte had been received. That, in turn, caused random problems for the Processing sketch on the other end. --- .../CameraCaptureRawBytes/CameraCaptureRawBytes.ino | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libraries/Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino b/libraries/Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino index 99f0a8f89..673dd2158 100644 --- a/libraries/Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino +++ b/libraries/Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino @@ -67,8 +67,11 @@ void loop() { // Time out after 2 seconds and send new data bool timeoutDetected = millis() - lastUpdate > 2000; - // Wait for sync byte. - if(!timeoutDetected && Serial.read() != 1) return; + // Wait for sync byte and timeout + if ((!timeoutDetected) || (Serial.read() != 1)) + { + return; + } lastUpdate = millis(); From f8990320c30486a129c10d2d63a2c41b5e5699bc Mon Sep 17 00:00:00 2001 From: alrvid <126816223+alrvid@users.noreply.github.com> Date: Sun, 23 Apr 2023 09:29:48 +0200 Subject: [PATCH 2/2] Add comments to explain timeout functionality The timeout functionality has changed, and it needs to be explained. Timeouts because of lost sync bytes should be done on the opposite end if needed. Otherwise, the two sides get out of sync every now and then. --- .../examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino b/libraries/Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino index 673dd2158..65458f26a 100644 --- a/libraries/Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino +++ b/libraries/Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino @@ -64,10 +64,12 @@ void loop() { while(!Serial); } - // Time out after 2 seconds and send new data + // Time out after 2 seconds, which sets the (constant) frame rate bool timeoutDetected = millis() - lastUpdate > 2000; // Wait for sync byte and timeout + // Notice that this order must be kept, or the sync bytes will be + // consumed prematurely if ((!timeoutDetected) || (Serial.read() != 1)) { return;