99 // uncomment the correct camera in use
1010 #include " hm0360.h"
1111 HM0360 himax;
12- // #include "himax.h";
12+
13+ // #include "himax.h"
1314 // HM01B0 himax;
15+ // Camera cam(himax);
16+
1417 Camera cam (himax);
1518 #define IMAGE_MODE CAMERA_GRAYSCALE
1619#elif defined(ARDUINO_GIGA)
@@ -36,16 +39,14 @@ If resolution higher than 320x240 is required, please use external RAM via
3639 // and adding in setup()
3740 SDRAM.begin();
3841*/
39- #define CHUNK_SIZE 512 // Size of chunks in bytes
40- #define RESOLUTION CAMERA_R320x240
4142FrameBuffer fb;
4243
4344unsigned long lastUpdate = 0 ;
4445
46+
4547void blinkLED (uint32_t count = 0xFFFFFFFF )
4648{
47- pinMode (LED_BUILTIN, OUTPUT);
48-
49+ pinMode (LED_BUILTIN, OUTPUT);
4950 while (count--) {
5051 digitalWrite (LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
5152 delay (50 ); // wait for a second
@@ -56,61 +57,36 @@ void blinkLED(uint32_t count = 0xFFFFFFFF)
5657
5758void setup () {
5859 // Init the cam QVGA, 30FPS
59- if (!cam.begin (RESOLUTION , IMAGE_MODE, 30 )) {
60+ if (!cam.begin (CAMERA_R320x240 , IMAGE_MODE, 30 )) {
6061 blinkLED ();
6162 }
6263
6364 blinkLED (5 );
64-
65- pinMode (LEDB, OUTPUT);
66- digitalWrite (LEDB, HIGH);
67- }
68-
69- void sendFrame (){
70- // Grab frame and write to serial
71- if (cam.grabFrame (fb, 3000 ) == 0 ) {
72- byte* buffer = fb.getBuffer ();
73- size_t bufferSize = cam.frameSize ();
74- digitalWrite (LEDB, LOW);
75-
76- // Split buffer into chunks
77- for (size_t i = 0 ; i < bufferSize; i += CHUNK_SIZE) {
78- size_t chunkSize = min (bufferSize - i, CHUNK_SIZE);
79- Serial.write (buffer + i, chunkSize);
80- Serial.flush ();
81- delay (1 ); // Optional: Add a small delay to allow the receiver to process the chunk
82- }
83-
84- digitalWrite (LEDB, HIGH);
85- } else {
86- blinkLED (20 );
87- }
88- }
89-
90- void sendCameraConfig (){
91- Serial.write (IMAGE_MODE);
92- Serial.write (RESOLUTION);
93- Serial.flush ();
94- delay (1 );
9565}
9666
9767void loop () {
9868 if (!Serial) {
9969 Serial.begin (115200 );
100- while (!Serial);
70+ while (!Serial);
10171 }
10272
103- if (!Serial.available ()) return ;
104-
105- byte request = Serial.read ();
106-
107- switch (request){
108- case 1 :
109- sendFrame ();
110- break ;
111- case 2 :
112- sendCameraConfig ();
113- break ;
73+ // Time out after 2 seconds, which sets the (constant) frame rate
74+ bool timeoutDetected = millis () - lastUpdate > 2000 ;
75+
76+ // Wait for sync byte and timeout
77+ // Notice that this order must be kept, or the sync bytes will be
78+ // consumed prematurely
79+ if ((!timeoutDetected) || (Serial.read () != 1 ))
80+ {
81+ return ;
11482 }
83+
84+ lastUpdate = millis ();
11585
86+ // Grab frame and write to serial
87+ if (cam.grabFrame (fb, 3000 ) == 0 ) {
88+ Serial.write (fb.getBuffer (), cam.frameSize ());
89+ } else {
90+ blinkLED (20 );
91+ }
11692}
0 commit comments