Skip to content

Commit 5aa18d7

Browse files
committed
Refactor example sketch
1 parent 4f72204 commit 5aa18d7

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

libraries/Camera/examples/CameraCaptureWebSerial/CameraCaptureWebSerial.ino

+37-17
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,20 @@ If resolution higher than 320x240 is required, please use external RAM via
5151
// and adding in setup()
5252
SDRAM.begin();
5353
*/
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;
5958

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;
6162

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+
*/
6268
void blinkLED(uint32_t count = 0xFFFFFFFF) {
6369
while (count--) {
6470
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
@@ -79,34 +85,48 @@ void setup() {
7985
blinkLED(5);
8086
}
8187

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+
*/
82103
void sendFrame(){
83104
// Grab frame and write to serial
84105
if (cam.grabFrame(fb, 3000) == 0) {
85106
byte* buffer = fb.getBuffer();
86107
size_t bufferSize = cam.frameSize();
87108
digitalWrite(LED_BUILTIN, LOW);
88109

89-
Serial.write(START_SEQUENCE, sizeof(START_SEQUENCE));
90-
Serial.flush();
91-
delay(1);
110+
sendChunk(START_SEQUENCE, sizeof(START_SEQUENCE));
92111

93112
// Split buffer into chunks
94113
for(size_t i = 0; i < bufferSize; i += CHUNK_SIZE) {
95114
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);
99116
}
100-
Serial.write(STOP_SEQUENCE, sizeof(STOP_SEQUENCE));
101-
Serial.flush();
102-
delay(1);
117+
118+
sendChunk(STOP_SEQUENCE, sizeof(STOP_SEQUENCE));
103119

104120
digitalWrite(LED_BUILTIN, HIGH);
105121
} else {
106122
blinkLED(20);
107123
}
108124
}
109125

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+
*/
110130
void sendCameraConfig(){
111131
Serial.write(IMAGE_MODE);
112132
Serial.write(RESOLUTION);
@@ -125,10 +145,10 @@ void loop() {
125145
byte request = Serial.read();
126146

127147
switch(request){
128-
case 1:
148+
case IMAGE_SEND_REQUEST:
129149
sendFrame();
130150
break;
131-
case 2:
151+
case CONFIG_SEND_REQUEST:
132152
sendCameraConfig();
133153
break;
134154
}

0 commit comments

Comments
 (0)