@@ -16,54 +16,82 @@ 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];
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
4963void 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