Skip to content

Commit 9cda904

Browse files
authored
Merge pull request #252 from arduino/marqdevx/portenta-vision-shield/GS-fix
Portenta Vision Shield: Getting Started, fix arduino code [PC-910]
2 parents abb1dfd + 43e5a6c commit 9cda904

File tree

1 file changed

+43
-18
lines changed
  • content/hardware/04.pro/shields/portenta-vision-shield/tutorials/vs-ard-gs

1 file changed

+43
-18
lines changed

content/hardware/04.pro/shields/portenta-vision-shield/tutorials/vs-ard-gs/content.md

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,18 @@ To capture the frames you will need to use the functions contained in `camera.h`
4444

4545
```cpp
4646
#include "camera.h"
47+
#include "himax.h"
4748
```
4849

4950
Next, let's initialize a camera object and a frame buffer of the size 320*240 (76'800 bytes).
5051

5152
```cpp
52-
CameraClass cam;
53-
uint8_t fb[320*240];
53+
HM01B0 himax;
54+
Camera cam(himax);
55+
#define IMAGE_MODE CAMERA_GRAYSCALE
56+
FrameBuffer fb(320,240,2);
57+
58+
unsigned long lastUpdate = 0;
5459
```
5560
5661
In the `setup()` function, let's start the Serial communication at `921600` baud rate and initialize the camera using `cam.begin()`.
@@ -59,7 +64,7 @@ In the `setup()` function, let's start the Serial communication at `921600` baud
5964
void setup() {
6065
Serial.begin(921600);
6166
//Init the cam QVGA, 30FPS
62-
cam.begin(CAMERA_R320x240, 30);
67+
cam.begin(CAMERA_R320x240, IMAGE_MODE, 30);
6368
}
6469
```
6570

@@ -68,16 +73,24 @@ In the loop we need to capture each Frame and send it over a serial connection t
6873
```cpp
6974
void loop() {
7075
// put your main code here, to run repeatedly:
76+
if(!Serial) {
77+
Serial.begin(921600);
78+
while(!Serial);
79+
}
7180

81+
// Time out after 2 seconds and send new data
82+
bool timeoutDetected = millis() - lastUpdate > 2000;
83+
7284
// Wait until the receiver acknowledges
7385
// that they are ready to receive new data
74-
while(Serial.read() != 1){};
86+
if(!timeoutDetected && Serial.read() != 1) return;
87+
88+
lastUpdate = millis();
7589

7690
// Grab frame and write to serial
77-
if (cam.grab(fb) == 0) {
78-
Serial.write(fb, 320*240);
91+
if (cam.grabFrame(fb, 3000) == 0) {
92+
Serial.write(fb.getBuffer(), cam.frameSize());
7993
}
80-
8194
}
8295
```
8396

@@ -226,7 +239,7 @@ while (bb.hasRemaining()) {
226239

227240
Once all the pixels have been updated, you need to tell the sketch to redraw the image. Additionally we send an acknowledgement back to the arduino sketch to ask it to send the pixels for the next frame. We update the image with `updatePixels()` and write `1` to the serial port for the acknowledgement.
228241

229-
``` cpp
242+
```cpp
230243
myImage.updatePixels();
231244

232245
// Ensures that the new image data is drawn in the next draw loop
@@ -252,29 +265,41 @@ The `CaptureRawBytes.ino` Sketch.
252265

253266
```cpp
254267
#include "camera.h"
268+
#include "himax.h"
255269

256-
CameraClass cam;
257-
uint8_t fb[320*240];
270+
HM01B0 himax;
271+
Camera cam(himax);
272+
#define IMAGE_MODE CAMERA_GRAYSCALE
273+
FrameBuffer fb(320,240,2);
258274

259-
void setup() {
260-
Serial.begin(921600);
275+
unsigned long lastUpdate = 0;
261276

262-
// Init the cam QVGA, 30FPS
263-
cam.begin(CAMERA_R320x240, 30);
277+
void setup() {
278+
Serial.begin(921600);
279+
//Init the cam QVGA, 30FPS
280+
cam.begin(CAMERA_R320x240, IMAGE_MODE, 30);
264281
}
265282

266283
void loop() {
267284
// put your main code here, to run repeatedly:
285+
if(!Serial) {
286+
Serial.begin(921600);
287+
while(!Serial);
288+
}
268289

290+
// Time out after 2 seconds and send new data
291+
bool timeoutDetected = millis() - lastUpdate > 2000;
292+
269293
// Wait until the receiver acknowledges
270294
// that they are ready to receive new data
271-
while(Serial.read() != 1){};
295+
if(!timeoutDetected && Serial.read() != 1) return;
296+
297+
lastUpdate = millis();
272298

273299
// Grab frame and write to serial
274-
if (cam.grab(fb) == 0) {
275-
Serial.write(fb, 320*240);
300+
if (cam.grabFrame(fb, 3000) == 0) {
301+
Serial.write(fb.getBuffer(), cam.frameSize());
276302
}
277-
278303
}
279304
```
280305

0 commit comments

Comments
 (0)