Skip to content

Fix buffer sync issue in camera raw bytes example #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ 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);
}

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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}