8000 Add dedicated sketch for WebSerial · arduino/ArduinoCore-mbed@28d4e37 · GitHub
[go: up one dir, main page]

Skip to content

Commit 28d4e37

Browse files
committed
Add dedicated sketch for WebSerial
1 parent c656793 commit 28d4e37

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* This example shows how to capture images from the camera and send them over Web Serial.
3+
*
4+
* There is a companion web app that receives the images and displays them in a canvas.
5+
* It can be found in the "extras" folder of this library.
6+
* The on-board LED lights up while the image is being sent over serial.
7+
*
8+
* Instructions:
9+
* 1. Make sure the correct camera is selected in the #include section below by uncommenting the correct line.
10+
* 2. Upload this sketch to your camera-equipped board.
11+
* 3. Open the web app in a browser (Chrome or Edge) by opening the index.html file in the "extras" folder.
12+
*
13+
* Initial author: Sebastian Romero @sebromero
14+
*/
15+
16+
#include "camera.h"
17+
18+
#ifdef ARDUINO_NICLA_VISION
19+
#include "gc2145.h"
20+
GC2145 galaxyCore;
21+
Camera cam(galaxyCore);
22+
#define IMAGE_MODE CAMERA_RGB565
23+
#elif defined(ARDUINO_PORTENTA_H7_M7)
24+
// uncomment the correct camera in use
25+
#include "hm0360.h"
26+
HM0360 himax;
27+
// #include "himax.h";
28+
// HM01B0 himax;
29+
Camera cam(himax);
30+
#define IMAGE_MODE CAMERA_GRAYSCALE
31+
#elif defined(ARDUINO_GIGA)
32+
#include "ov767x.h"
33+
// uncomment the correct camera in use
34+
OV7670 ov767x;
35+
// OV7675 ov767x;
36+
Camera cam(ov767x);
37+
#define IMAGE_MODE CAMERA_RGB565
38+
#else
39+
#error "This board is unsupported."
40+
#endif
41+
42+
/*
43+
Other buffer instantiation options:
44+
FrameBuffer fb(0x30000000);
45+
FrameBuffer fb(320,240,2);
46+
47+
If resolution higher than 320x240 is required, please use external RAM via
48+
#include "SDRAM.h"
49+
FrameBuffer fb(SDRAM_START_ADDRESS);
50+
...
51+
// and adding in setup()
52+
SDRAM.begin();
53+
*/
54+
#define CHUNK_SIZE 512 // Size of chunks in bytes
55+
#define RESOLUTION CAMERA_R320x240
56+
FrameBuffer fb;
57+
58+
unsigned long lastUpdate = 0;
59+
60+
void blinkLED(uint32_t count = 0xFFFFFFFF) {
61+
while (count--) {
62+
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
63+
delay(50); // wait for a second
64+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED off by making the voltage LOW
65+
delay(50); // wait for a second
66+
}
67+
}
68+
69+
void setup() {
70+
pinMode(LED_BUILTIN, OUTPUT);
71+
72+
// Init the cam QVGA, 30FPS
73+
if (!cam.begin(RESOLUTION, IMAGE_MODE, 30)) {
74+
blinkLED();
75+
}
76+
77+
blinkLED(5);
78+
}
79+
80+
void sendFrame(){
81+
// Grab frame and write to serial
82+
if (cam.grabFrame(fb, 3000) == 0) {
83+
byte* buffer = fb.getBuffer();
84+
size_t bufferSize = cam.frameSize();
85+
digitalWrite(LED_BUILTIN, LOW);
86+
87+
// Split buffer into chunks
88+
for(size_t i = 0; i < bufferSize; i += CHUNK_SIZE) {
89+
size_t chunkSize = min(bufferSize - i, CHUNK_SIZE);
90+
Serial.write(buffer + i, chunkSize);
91+
Serial.flush();
92+
delay(1); // Optional: Add a small delay to allow the receiver to process the chunk
93+
}
94+
95+
digitalWrite(LED_BUILTIN, HIGH);
96+
} else {
97+
blinkLED(20);
98+
}
99+
}
100+
101+
void sendCameraConfig(){
102+
Serial.write(IMAGE_MODE);
103+
Serial.write(RESOLUTION);
104+
Serial.flush();
105+
delay(1);
106+
}
107+
108+
void loop() {
109+
if(!Serial) {
110+
Serial.begin(115200);
111+
while(!Serial);
112+
}
113+
114+
if(!Serial.available()) return;
115+
116+
byte request = Serial.read();
117+
118+
switch(request){
119+
case 1:
120+
sendFrame();
121+
break;
122+
case 2:
123+
sendCameraConfig();
124+
break;
125+
}
126+
127+
}

0 commit comments

Comments
 (0)
0