8000 Send bytes in chunks · arduino/ArduinoCore-mbed@229f167 · GitHub
[go: up one dir, main page]

Skip to content

Commit 229f167

Browse files
committed
Send bytes in chunks
1 parent d149870 commit 229f167

File tree

1 file changed

+61
-23
lines changed

1 file changed

+61
-23
lines changed

libraries/Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
Camera cam(galaxyCore);
77
#define IMAGE_MODE CAMERA_RGB565
88
#elif defined(ARDUINO_PORTENTA_H7_M7)
9-
#include "hm0360.h"
10-
HM0360 himax;
9+
//#include "hm0360.h"
10+
//HM0360 himax;
11+
#include "himax.h";
12+
HM01B0 himax;
1113
Camera cam(himax);
1214
#define IMAGE_MODE CAMERA_GRAYSCALE
1315
#elif defined(ARDUINO_GIGA)
@@ -33,14 +35,17 @@ If resolution higher than 320x240 is required, please use external RAM via
3335
// and adding in setup()
3436
SDRAM.begin();
3537
*/
38+
#define CHUNK_SIZE 512 // Size of chunks in bytes
39+
#define RESOLUTION CAMERA_R320x240
3640
FrameBuffer fb;
3741

3842
unsigned long lastUpdate = 0;
3943

4044

4145
void blinkLED(uint32_t count = 0xFFFFFFFF)
4246
{
43-
pinMode(LED_BUILTIN, OUTPUT);
47+
pinMode(LED_BUILTIN, OUTPUT);
48+
4449
while (count--) {
4550
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
4651
delay(50); // wait for a second
@@ -49,38 +54,71 @@ void blinkLED(uint32_t count = 0xFFFFFFFF)
4954
}
5055
}
5156

57+
void clearSerialBuffer(){
58+
while(Serial.available()){
59+
Serial.read();
60+
}
61+
Serial.end();
62+
Serial.begin(9600);
63+
}
64+
5265
void setup() {
5366
// Init the cam QVGA, 30FPS
54-
if (!cam.begin(CAMERA_R320x240, IMAGE_MODE, 30)) {
67+
if (!cam.begin(RESOLUTION, IMAGE_MODE, 30)) {
5568
blinkLED();
5669
}
5770

5871
blinkLED(5);
72+
73+
pinMode(LEDB, OUTPUT);
74+
digitalWrite(LEDB, HIGH);
75+
}
76+
77+
void sendFrame(){
78+
// Grab frame and write to serial
79+
if (cam.grabFrame(fb, 3000) == 0) {
80+
byte* buffer = fb.getBuffer();
81+
size_t bufferSize = cam.frameSize();
82+
digitalWrite(LEDB, LOW);
83+
84+
// Split buffer into chunks
85+
for(size_t i = 0; i < bufferSize; i += CHUNK_SIZE) {
86+
size_t chunkSize = min(bufferSize - i, CHUNK_SIZE);
87+
Serial.write(buffer + i, chunkSize);
88+
Serial.flush();
89+
delay(1); // Optional: Add a small delay to allow the receiver to process the chunk
90+
}
91+
92+
digitalWrite(LEDB, HIGH);
93+
} else {
94+
blinkLED(20);
95+
}
96+
}
97+
98+
void sendCameraConfig(){
99+
Serial.write(RESOLUTION);
100+
Serial.write(IMAGE_MODE);
101+
Serial.flush();
102+
delay(1);
59103
}
60104

61105
void loop() {
62106
if(!Serial) {
63-
Serial.begin(921600);
64-
while(!Serial);
107+
Serial.begin(115200, SERIAL_8E2);
108+
while(!Serial);
65109
}
66110

67-
// Time out after 2 seconds, which sets the (constant) frame rate
68-
bool timeoutDetected = millis() - lastUpdate > 2000;
69-
70-
// Wait for sync byte and timeout
71-
// Notice that this order must be kept, or the sync bytes will be
72-
// consumed prematurely
73-
if ((!timeoutDetected) || (Serial.read() != 1))
74-
{
75-
return;
76-
}
111+
if(!Serial.available()) return;
77112

78-
lastUpdate = millis();
79-
80-
// Grab frame and write to serial
81-
if (cam.grabFrame(fb, 3000) == 0) {
82-
Serial.write(fb.getBuffer(), cam.frameSize());
83-
} else {
84-
blinkLED(20);
113+
byte request = Serial.read();
114+
115+
switch(request){
116+
case 1:
117+
sendFrame();
118+
break;
119+
case 2:
120+
sendCameraConfig();
121+
break;
85122
}
123+
86124
}

0 commit comments

Comments
 (0)
0