@@ -16,54 +16,82 @@ Serial myPort;
16
16
final int cameraWidth = 320 ;
17
17
final int cameraHeight = 240 ;
18
18
final int cameraBytesPerPixel = 1 ;
19
- final int bytesPerFrame = cameraWidth * cameraHeight * cameraBytesPerPixel;
19
+ final int cameraPixelCount = cameraWidth * cameraHeight;
20
+ final int bytesPerFrame = cameraPixelCount * cameraBytesPerPixel;
20
21
21
22
PImage myImage;
22
23
byte [] frameBuffer = new byte [bytesPerFrame];
24
+ int lastUpdate = 0 ;
25
+ boolean shouldRedraw = false ;
23
26
24
- void setup ()
25
- {
27
+ void setup () {
26
28
size (640 , 480 );
27
29
28
30
// 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
30
32
31
33
// 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
35
37
36
38
// wait for full frame of bytes
37
39
myPort. buffer(bytesPerFrame);
38
40
39
41
myImage = createImage (cameraWidth, cameraHeight, ALPHA );
42
+
43
+ // Let the Arduino sketch know we're ready to receive data
44
+ myPort. write(1 );
40
45
}
41
46
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
+ }
47
61
}
48
62
49
63
void serialEvent (Serial myPort ) {
50
- // read the saw bytes in
64
+ lastUpdate = millis ();
65
+
66
+ // read the received bytes
51
67
myPort. readBytes(frameBuffer);
52
68
53
- // access raw bytes via byte buffer
69
+ // Access raw bytes via byte buffer
54
70
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);
56
78
57
79
int i = 0 ;
58
80
59
81
while (bb. hasRemaining()) {
60
- // read 16-bit pixel
61
- byte p = bb. get();
62
-
82
+ // read 8-bit pixel
83
+ byte pixelValue = bb. get();
63
84
64
85
// set pixel color
65
- myImage .pixels [i++ ] = color (Byte . toUnsignedInt(p ));
86
+ myImage. pixels [i++ ] = color (Byte . toUnsignedInt(pixelValue ));
66
87
}
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 );
69
97
}
0 commit comments