Skip to content

Commit 151b0e4

Browse files
committed
Fix arduino code
1 parent 9cb4df3 commit 151b0e4

File tree

1 file changed

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

1 file changed

+39
-18
lines changed

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

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ 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);
5457
```
5558
5659
In the `setup()` function, let's start the Serial communication at `921600` baud rate and initialize the camera using `cam.begin()`.
@@ -59,7 +62,7 @@ In the `setup()` function, let's start the Serial communication at `921600` baud
5962
void setup() {
6063
Serial.begin(921600);
6164
//Init the cam QVGA, 30FPS
62-
cam.begin(CAMERA_R320x240, 30);
65+
cam.begin(CAMERA_R320x240, IMAGE_MODE, 30);
6366
}
6467
```
6568

@@ -68,16 +71,24 @@ In the loop we need to capture each Frame and send it over a serial connection t
6871
```cpp
6972
void loop() {
7073
// put your main code here, to run repeatedly:
74+
if(!Serial) {
75+
Serial.begin(921600);
76+
while(!Serial);
77+
}
7178

79+
// Time out after 2 seconds and send new data
80+
bool timeoutDetected = millis() - lastUpdate > 2000;
81+
7282
// Wait until the receiver acknowledges
7383
// that they are ready to receive new data
74-
while(Serial.read() != 1){};
84+
if(!timeoutDetected && Serial.read() != 1) return;
85+
86+
lastUpdate = millis();
7587

7688
// Grab frame and write to serial
77-
if (cam.grab(fb) == 0) {
78-
Serial.write(fb, 320*240);
89+
if (cam.grabFrame(fb, 3000) == 0) {
90+
Serial.write(fb.getBuffer(), cam.frameSize());
7991
}
80-
8192
}
8293
```
8394

@@ -226,7 +237,7 @@ while (bb.hasRemaining()) {
226237

227238
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.
228239

229-
``` cpp
240+
```cpp
230241
myImage.updatePixels();
231242

232243
// Ensures that the new image data is drawn in the next draw loop
@@ -252,29 +263,39 @@ The `CaptureRawBytes.ino` Sketch.
252263

253264
```cpp
254265
#include "camera.h"
266+
#include "himax.h"
255267

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

259273
void setup() {
260-
Serial.begin(921600);
261-
262-
// Init the cam QVGA, 30FPS
263-
cam.begin(CAMERA_R320x240, 30);
274+
Serial.begin(921600);
275+
//Init the cam QVGA, 30FPS
276+
cam.begin(CAMERA_R320x240, IMAGE_MODE, 30);
264277
}
265278

266279
void loop() {
267280
// put your main code here, to run repeatedly:
281+
if(!Serial) {
282+
Serial.begin(921600);
283+
while(!Serial);
284+
}
268285

286+
// Time out after 2 seconds and send new data
287+
bool timeoutDetected = millis() - lastUpdate > 2000;
288+
269289
// Wait until the receiver acknowledges
270290
// that they are ready to receive new data
271-
while(Serial.read() != 1){};
291+
if(!timeoutDetected && Serial.read() != 1) return;
292+
293+
lastUpdate = millis();
272294

273295
// Grab frame and write to serial
274-
if (cam.grab(fb) == 0) {
275-
Serial.write(fb, 320*240);
296+
if (cam.grabFrame(fb, 3000) == 0) {
297+
Serial.write(fb.getBuffer(), cam.frameSize());
276298
}
277-
278299
}
279300
```
280301

0 commit comments

Comments
 (0)