@@ -51,14 +51,20 @@ If resolution higher than 320x240 is required, please use external RAM via
51
51
// and adding in setup()
52
52
SDRAM.begin();
53
53
*/
54
- #define CHUNK_SIZE 512 // Size of chunks in bytes
55
- #define RESOLUTION CAMERA_R320x240 // CAMERA_R160x120
56
- constexpr uint8_t START_SEQUENCE[4 ] = { 0xfa , 0xce , 0xfe , 0xed };
57
- constexpr uint8_t STOP_SEQUENCE[4 ] = { 0xda , 0xbb , 0xad , 0x00 };
58
- FrameBuffer fb;
54
+ constexpr uint16_t CHUNK_SIZE = 512 ; // Size of chunks in bytes
55
+ constexpr uint8_t RESOLUTION = CAMERA_R320x240; // CAMERA_R160x120
56
+ constexpr uint8_t CONFIG_SEND_REQUEST = 2 ;
57
+ constexpr uint8_t IMAGE_SEND_REQUEST = 1 ;
59
58
60
- unsigned long lastUpdate = 0 ;
59
+ uint8_t START_SEQUENCE[4 ] = { 0xfa , 0xce , 0xfe , 0xed };
60
+ uint8_t STOP_SEQUENCE[4 ] = { 0xda , 0xbb , 0xad , 0x00 };
61
+ FrameBuffer fb;
61
62
63
+ /* *
64
+ * Blinks the LED a specified number of times.
65
+ *
66
+ * @param count The number of times to blink the LED. Default is 0xFFFFFFFF.
67
+ */
62
68
void blinkLED (uint32_t count = 0xFFFFFFFF ) {
63
69
while (count--) {
64
70
digitalWrite (LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
@@ -79,34 +85,48 @@ void setup() {
79
85
blinkLED (5 );
80
86
}
81
87
88
+ /* *
89
+ * Sends a chunk of data over a serial connection.
90
+ *
91
+ * @param buffer The buffer containing the data to be sent.
92
+ * @param bufferSize The size of the buffer.
93
+ */
94
+ void sendChunk (uint8_t * buffer, size_t bufferSize){
95
+ Serial.write (buffer, bufferSize);
96
+ Serial.flush ();
97
+ delay (1 ); // Optional: Add a small delay to allow the receiver to process the chunk
98
+ }
99
+
100
+ /* *
101
+ * Sends a frame of camera image data over a serial connection.
102
+ */
82
103
void sendFrame (){
83
104
// Grab frame and write to serial
84
105
if (cam.grabFrame (fb, 3000 ) == 0 ) {
85
106
byte* buffer = fb.getBuffer ();
86
107
size_t bufferSize = cam.frameSize ();
87
108
digitalWrite (LED_BUILTIN, LOW);
88
109
89
- Serial.write (START_SEQUENCE, sizeof (START_SEQUENCE));
90
- Serial.flush ();
91
- delay (1 );
110
+ sendChunk (START_SEQUENCE, sizeof (START_SEQUENCE));
92
111
93
112
// Split buffer into chunks
94
113
for (size_t i = 0 ; i < bufferSize; i += CHUNK_SIZE) {
95
114
size_t chunkSize = min (bufferSize - i, CHUNK_SIZE);
96
- Serial.write (buffer + i, chunkSize);
97
- Serial.flush ();
98
- delay (1 ); // Optional: Add a small delay to allow the receiver to process the chunk
115
+ sendChunk (buffer + i, chunkSize);
99
116
}
100
- Serial.write (STOP_SEQUENCE, sizeof (STOP_SEQUENCE));
101
- Serial.flush ();
102
- delay (1 );
117
+
118
+ sendChunk (STOP_SEQUENCE, sizeof (STOP_SEQUENCE));
103
119
104
120
digitalWrite (LED_BUILTIN, HIGH);
105
121
} else {
106
122
blinkLED (20 );
107
123
}
108
124
}
109
125
126
+ /* *
127
+ * Sends the camera configuration over a serial connection.
128
+ * This is used to configure the web app to display the image correctly.
129
+ */
110
130
void sendCameraConfig (){
111
131
Serial.write (IMAGE_MODE);
112
132
Serial.write (RESOLUTION);
@@ -125,10 +145,10 @@ void loop() {
125
145
byte request = Serial.read ();
126
146
127
147
switch (request){
128
- case 1 :
148
+ case IMAGE_SEND_REQUEST :
129
149
sendFrame ();
130
150
break ;
131
- case 2 :
151
+ case CONFIG_SEND_REQUEST :
132
152
sendCameraConfig ();
133
153
break ;
134
154
}
0 commit comments