@@ -16,7 +16,8 @@ Serial myPort;
1616final int cameraWidth = 320 ;
1717final int cameraHeight = 240 ;
1818final int cameraBytesPerPixel = 1 ;
19- final int bytesPerFrame = cameraWidth * cameraHeight * cameraBytesPerPixel;
19+ final int cameraPixelCount = cameraWidth * cameraHeight;
20+ final int bytesPerFrame = cameraPixelCount * cameraBytesPerPixel;
2021
2122PImage myImage;
2223byte [] frameBuffer = new byte [bytesPerFrame];
@@ -30,13 +31,16 @@ void setup()
3031
3132 // if you know the serial port name
3233 // 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, "/dev/ttyACM0", 921600); // Linux
35+ myPort = new Serial (this , " /dev/cu.usbmodem14401" , 9600 ); // Mac
3536
3637 // wait for full frame of bytes
3738 myPort. buffer(bytesPerFrame);
3839
3940 myImage = createImage (cameraWidth, cameraHeight, ALPHA );
41+
42+ // Let the Arduino sketch know we're ready to receive data
43+ myPort. write(1 );
4044}
4145
4246void draw ()
@@ -63,7 +67,12 @@ void serialEvent(Serial myPort) {
6367
6468 // set pixel color
6569 myImage .pixels [i++ ] = color (Byte . toUnsignedInt(p));
70+
71+ // Let the Arduino sketch know we received all pixels
72+ // and are ready for the next frame
73+ if (i == cameraPixelCount){
74+ myPort. write(1 );
75+ }
6676 }
67- myImage. updatePixels();
68-
77+ myImage. updatePixels();
6978}
0 commit comments