9
9
// uncomment the correct camera in use
10
10
#include " hm0360.h"
11
11
HM0360 himax;
12
- // #include "himax.h";
12
+
13
+ // #include "himax.h"
13
14
// HM01B0 himax;
15
+ // Camera cam(himax);
16
+
14
17
Camera cam (himax);
15
18
#define IMAGE_MODE CAMERA_GRAYSCALE
16
19
#elif defined(ARDUINO_GIGA)
@@ -36,16 +39,14 @@ If resolution higher than 320x240 is required, please use external RAM via
36
39
// and adding in setup()
37
40
SDRAM.begin();
38
41
*/
39
- #define CHUNK_SIZE 512 // Size of chunks in bytes
40
- #define RESOLUTION CAMERA_R320x240
41
42
FrameBuffer fb;
42
43
43
44
unsigned long lastUpdate = 0 ;
44
45
46
+
45
47
void blinkLED (uint32_t count = 0xFFFFFFFF )
46
48
{
47
- pinMode (LED_BUILTIN, OUTPUT);
48
-
49
+ pinMode (LED_BUILTIN, OUTPUT);
49
50
while (count--) {
50
51
digitalWrite (LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
51
52
delay (50 ); // wait for a second
@@ -56,61 +57,36 @@ void blinkLED(uint32_t count = 0xFFFFFFFF)
56
57
57
58
void setup () {
58
59
// Init the cam QVGA, 30FPS
59
- if (!cam.begin (RESOLUTION , IMAGE_MODE, 30 )) {
60
+ if (!cam.begin (CAMERA_R320x240 , IMAGE_MODE, 30 )) {
60
61
blinkLED ();
61
62
}
62
63
63
64
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 );
95
65
}
96
66
97
67
void loop () {
98
68
if (!Serial) {
99
69
Serial.begin (115200 );
100
- while (!Serial);
70
+ while (!Serial);
101
71
}
102
72
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 ;
114
82
}
83
+
84
+ lastUpdate = millis ();
115
85
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
+ }
116
92
}
0 commit comments