Skip to content

Commit 66f1f03

Browse files
authored
Merge pull request #143 from sebromero/camera-example
Fix buffer sync issue in camera raw bytes example
2 parents 3867976 + da37f87 commit 66f1f03

File tree

2 files changed

+59
-28
lines changed

2 files changed

+59
-28
lines changed

libraries/Portenta_Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino

+10-7
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ CameraClass cam;
44
uint8_t fb[320*240];
55

66
void setup() {
7-
8-
Serial.begin(921600);
7+
Serial.begin(921600);
98

109
// Init the cam QVGA, 30FPS
1110
cam.begin(CAMERA_R320x240, 30);
1211
}
1312

1413
void loop() {
1514
// put your main code here, to run repeatedly:
16-
if (Serial) {
17-
// Grab frame and write to serial
18-
if (cam.grab(fb) == 0) {
19-
Serial.write(fb, 320*240);
20-
}
15+
16+
// Wait until the receiver acknowledges
17+
// that they are ready to receive new data
18+
while(Serial.read() != 1){};
19+
20+
// Grab frame and write to serial
21+
if (cam.grab(fb) == 0) {
22+
Serial.write(fb, 320*240);
2123
}
24+
2225
}

libraries/Portenta_Camera/extras/CameraRawBytesVisualizer/CameraRawBytesVisualizer.pde

+49-21
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,82 @@ Serial myPort;
1616
final int cameraWidth = 320;
1717
final int cameraHeight = 240;
1818
final int cameraBytesPerPixel = 1;
19-
final int bytesPerFrame = cameraWidth * cameraHeight * cameraBytesPerPixel;
19+
final int cameraPixelCount = cameraWidth * cameraHeight;
20+
final int bytesPerFrame = cameraPixelCount * cameraBytesPerPixel;
2021

2122
PImage myImage;
2223
byte[] frameBuffer = new byte[bytesPerFrame];
24+
int lastUpdate = 0;
25+
boolean shouldRedraw = false;
2326

24-
void setup()
25-
{
27+
void setup() {
2628
size(640, 480);
2729

2830
// if you have only ONE serial port active
29-
//myPort = new Serial(this, Serial.list()[0], 9600); // if you have only ONE serial port active
31+
//myPort = new Serial(this, Serial.list()[0], 921600); // if you have only ONE serial port active
3032

3133
// if you know the serial port name
32-
//myPort = new Serial(this, "COM5", 9600); // Windows
33-
myPort = new Serial(this, "/dev/ttyACM0", 921600); // Linux
34-
//myPort = new Serial(this, "/dev/cu.usbmodem14401", 9600); // Mac
34+
//myPort = new Serial(this, "COM5", 921600); // Windows
35+
//myPort = new Serial(this, "/dev/ttyACM0", 921600); // Linux
36+
myPort = new Serial(this, "/dev/cu.usbmodem14401", 921600); // Mac
3537

3638
// wait for full frame of bytes
3739
myPort.buffer(bytesPerFrame);
3840

3941
myImage = createImage(cameraWidth, cameraHeight, ALPHA);
42+
43+
// Let the Arduino sketch know we're ready to receive data
44+
myPort.write(1);
4045
}
4146

42-
void draw()
43-
{
44-
PImage img = myImage.copy();
45-
img.resize(640, 480);
46-
image(img, 0, 0);
47+
void draw() {
48+
// Time out after 1.5 seconds and ask for new data
49+
if(millis() - lastUpdate > 1500) {
50+
println("Connection timed out.");
51+
myPort.clear();
52+
myPort.write(1);
53+
}
54+
55+
if(shouldRedraw){
56+
PImage img = myImage.copy();
57+
img.resize(640, 480);
58+
image(img, 0, 0);
59+
shouldRedraw = false;
60+
}
4761
}
4862

4963
void serialEvent(Serial myPort) {
50-
// read the saw bytes in
64+
lastUpdate = millis();
65+
66+
// read the received bytes
5167
myPort.readBytes(frameBuffer);
5268

53-
// access raw bytes via byte buffer
69+
// Access raw bytes via byte buffer
5470
ByteBuffer bb = ByteBuffer.wrap(frameBuffer);
55-
bb.order(ByteOrder.BIG_ENDIAN);
71+
72+
/*
73+
Ensure proper endianness of the data for > 8 bit values.
74+
When using > 8bit values uncomment the following line and
75+
adjust the translation to the pixel color.
76+
*/
77+
//bb.order(ByteOrder.BIG_ENDIAN);
5678

5779
int i = 0;
5880

5981
while (bb.hasRemaining()) {
60-
// read 16-bit pixel
61-
byte p = bb.get();
62-
82+
// read 8-bit pixel
83+
byte pixelValue = bb.get();
6384

6485
// set pixel color
65-
myImage .pixels[i++] = color(Byte.toUnsignedInt(p));
86+
myImage.pixels[i++] = color(Byte.toUnsignedInt(pixelValue));
6687
}
67-
myImage.updatePixels();
68-
88+
89+
myImage.updatePixels();
90+
91+
// Ensures that the new image data is drawn in the next draw loop
92+
shouldRedraw = true;
93+
94+
// Let the Arduino sketch know we received all pixels
95+
// and are ready for the next frame
96+
myPort.write(1);
6997
}

0 commit comments

Comments
 (0)