diff --git a/libraries/Portenta_Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino b/libraries/Portenta_Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino index f69cbc5fb..ecf61ad82 100644 --- a/libraries/Portenta_Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino +++ b/libraries/Portenta_Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino @@ -4,8 +4,7 @@ CameraClass cam; uint8_t fb[320*240]; void setup() { - - Serial.begin(921600); + Serial.begin(921600); // Init the cam QVGA, 30FPS cam.begin(CAMERA_R320x240, 30); @@ -13,10 +12,14 @@ void setup() { void loop() { // put your main code here, to run repeatedly: - if (Serial) { - // Grab frame and write to serial - if (cam.grab(fb) == 0) { - Serial.write(fb, 320*240); - } + + // Wait until the receiver acknowledges + // that they are ready to receive new data + while(Serial.read() != 1){}; + + // Grab frame and write to serial + if (cam.grab(fb) == 0) { + Serial.write(fb, 320*240); } + } diff --git a/libraries/Portenta_Camera/extras/CameraRawBytesVisualizer/CameraRawBytesVisualizer.pde b/libraries/Portenta_Camera/extras/CameraRawBytesVisualizer/CameraRawBytesVisualizer.pde index 7c8393fdc..1bc6746ee 100644 --- a/libraries/Portenta_Camera/extras/CameraRawBytesVisualizer/CameraRawBytesVisualizer.pde +++ b/libraries/Portenta_Camera/extras/CameraRawBytesVisualizer/CameraRawBytesVisualizer.pde @@ -16,54 +16,82 @@ Serial myPort; final int cameraWidth = 320; final int cameraHeight = 240; final int cameraBytesPerPixel = 1; -final int bytesPerFrame = cameraWidth * cameraHeight * cameraBytesPerPixel; +final int cameraPixelCount = cameraWidth * cameraHeight; +final int bytesPerFrame = cameraPixelCount * cameraBytesPerPixel; PImage myImage; byte[] frameBuffer = new byte[bytesPerFrame]; +int lastUpdate = 0; +boolean shouldRedraw = false; -void setup() -{ +void setup() { size(640, 480); // if you have only ONE serial port active - //myPort = new Serial(this, Serial.list()[0], 9600); // if you have only ONE serial port active + //myPort = new Serial(this, Serial.list()[0], 921600); // if you have only ONE serial port active // if you know the serial port name - //myPort = new Serial(this, "COM5", 9600); // Windows - myPort = new Serial(this, "/dev/ttyACM0", 921600); // Linux - //myPort = new Serial(this, "/dev/cu.usbmodem14401", 9600); // Mac + //myPort = new Serial(this, "COM5", 921600); // Windows + //myPort = new Serial(this, "/dev/ttyACM0", 921600); // Linux + myPort = new Serial(this, "/dev/cu.usbmodem14401", 921600); // Mac // wait for full frame of bytes myPort.buffer(bytesPerFrame); myImage = createImage(cameraWidth, cameraHeight, ALPHA); + + // Let the Arduino sketch know we're ready to receive data + myPort.write(1); } -void draw() -{ - PImage img = myImage.copy(); - img.resize(640, 480); - image(img, 0, 0); +void draw() { + // Time out after 1.5 seconds and ask for new data + if(millis() - lastUpdate > 1500) { + println("Connection timed out."); + myPort.clear(); + myPort.write(1); + } + + if(shouldRedraw){ + PImage img = myImage.copy(); + img.resize(640, 480); + image(img, 0, 0); + shouldRedraw = false; + } } void serialEvent(Serial myPort) { - // read the saw bytes in + lastUpdate = millis(); + + // read the received bytes myPort.readBytes(frameBuffer); - // access raw bytes via byte buffer + // Access raw bytes via byte buffer ByteBuffer bb = ByteBuffer.wrap(frameBuffer); - bb.order(ByteOrder.BIG_ENDIAN); + + /* + Ensure proper endianness of the data for > 8 bit values. + When using > 8bit values uncomment the following line and + adjust the translation to the pixel color. + */ + //bb.order(ByteOrder.BIG_ENDIAN); int i = 0; while (bb.hasRemaining()) { - // read 16-bit pixel - byte p = bb.get(); - + // read 8-bit pixel + byte pixelValue = bb.get(); // set pixel color - myImage .pixels[i++] = color(Byte.toUnsignedInt(p)); + myImage.pixels[i++] = color(Byte.toUnsignedInt(pixelValue)); } - myImage.updatePixels(); - + + myImage.updatePixels(); + + // Ensures that the new image data is drawn in the next draw loop + shouldRedraw = true; + + // Let the Arduino sketch know we received all pixels + // and are ready for the next frame + myPort.write(1); }