@@ -17,24 +17,23 @@ Serial myPort;
17
17
// must match resolution used in the sketch
18
18
final int cameraWidth = 320 ;
19
19
final int cameraHeight = 240 ;
20
-
21
20
final int cameraBytesPerPixel = 2 ;
22
-
23
21
final int bytesPerFrame = cameraWidth * cameraHeight * cameraBytesPerPixel;
24
22
25
23
PImage myImage;
24
+ byte [] frameBuffer = new byte [bytesPerFrame];
26
25
27
26
void setup ()
28
27
{
29
28
size (320 , 240 );
30
29
31
30
// if you have only ONE serial port active
32
- // myPort = new Serial(this, Serial.list()[0], 9600); // if you have only ONE serial port active
31
+ // myPort = new Serial(this, Serial.list()[0], 9600); // if you have only ONE serial port active
33
32
34
33
// if you know the serial port name
35
34
// myPort = new Serial(this, "COM5", 9600); // Windows
36
- // myPort = new Serial(this, "/dev/ttyACM0", 9600); // Linux
37
- myPort = new Serial (this , " /dev/cu.usbmodem14101 " , 9600 ); // Mac
35
+ // myPort = new Serial(this, "/dev/ttyACM0", 9600); // Linux
36
+ myPort = new Serial (this , " /dev/cu.usbmodem14401 " , 9600 ); // Mac
38
37
39
38
// wait for full frame of bytes
40
39
myPort. buffer(bytesPerFrame);
@@ -48,21 +47,15 @@ void draw()
48
47
}
49
48
50
49
void serialEvent (Serial myPort ) {
51
- byte [] frameBuffer = new byte [bytesPerFrame];
52
-
53
50
// read the saw bytes in
54
51
myPort. readBytes(frameBuffer);
55
52
56
- // create image to set byte values
57
- PImage img = createImage (cameraWidth, cameraHeight, RGB );
58
-
59
53
// access raw bytes via byte buffer
60
54
ByteBuffer bb = ByteBuffer . wrap(frameBuffer);
61
55
bb. order(ByteOrder . BIG_ENDIAN );
62
56
63
57
int i = 0 ;
64
58
65
- img. loadPixels();
66
59
while (bb. hasRemaining()) {
67
60
// read 16-bit pixel
68
61
short p = bb. getShort();
@@ -73,10 +66,8 @@ void serialEvent(Serial myPort) {
73
66
int b = ((p >> 0 ) & 0x1f ) << 3 ;
74
67
75
68
// set pixel color
76
- img . pixels [i++ ] = color (r, g, b);
69
+ myImage .pixels [i++ ] = color (r, g, b);
77
70
}
78
- img . updatePixels();
71
+ myImage .updatePixels();
79
72
80
- // assign image for next draw
81
- myImage = img;
82
73
}
0 commit comments