Skip to content

Commit 17d056f

Browse files
committed
setters: SetHmirror SetVflip
refs #55
1 parent b88adca commit 17d056f

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
lines changed

examples/AsyncCam/handlers.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Resolution
1111
<select id="resolution" required>%RESOLUTION_OPTIONS%</select>
1212
<input type="submit" value="change">
1313
</p></form>
14+
<p id="toggles">
15+
<label><input type="checkbox" data-act="hmirror">hmirror</label>
16+
<label><input type="checkbox" data-act="vflip">vflip</label>
17+
</p>
1418
<p id="controls">
1519
<button data-act="mjpeg">show Motion JPEG stream</button>
1620
<button data-act="jpg">show still JPEG image</button>
@@ -34,7 +38,7 @@ const $resolution = document.querySelector("#resolution");
3438
evt.preventDefault();
3539
const [width, height] = $resolution.value.split("x");
3640
try {
37-
const change = await fetchText("/change-resolution.cgi", {
41+
await fetchText("/change-resolution.cgi", {
3842
method: "POST",
3943
body: new URLSearchParams({ width, height }),
4044
});
@@ -43,6 +47,21 @@ const $resolution = document.querySelector("#resolution");
4347
}
4448
});
4549
50+
for (const $ctrl of document.querySelectorAll("#toggles input[type=checkbox]")) {
51+
$ctrl.addEventListener("change", async (evt) => {
52+
evt.preventDefault();
53+
const act = evt.target.getAttribute("data-act");
54+
try {
55+
await fetchText(`/set-${act}.cgi`, {
56+
method: "POST",
57+
body: new URLSearchParams({ enable: Number(evt.target.checked) }),
58+
});
59+
} catch (err) {
60+
$display.textContent = err.toString();
61+
}
62+
});
63+
}
64+
4665
for (const $ctrl of document.querySelectorAll("#controls button")) {
4766
$ctrl.addEventListener("click", (evt) => {
4867
evt.preventDefault();
@@ -116,6 +135,26 @@ addRequestHandlers() {
116135
request->send(200, "text/plain", static_cast<String>(b));
117136
});
118137

138+
server.on("/set-hmirror.cgi", HTTP_POST, [](AsyncWebServerRequest* request) {
139+
long enable = request->arg("enable").toInt();
140+
if (!esp32cam::Camera.update(esp32cam::SetHmirror(enable != 0))) {
141+
request->send(500, "text/plain", "SetHmirror error\n");
142+
return;
143+
}
144+
Serial.printf("SetHmirror(%ld) success\n", enable);
145+
request->send(200, "text/plain", "SetHmirror success\n");
146+
});
147+
148+
server.on("/set-vflip.cgi", HTTP_POST, [](AsyncWebServerRequest* request) {
149+
long enable = request->arg("enable").toInt();
150+
if (!esp32cam::Camera.update(esp32cam::SetVflip(enable != 0))) {
151+
request->send(500, "text/plain", "SetVflip error\n");
152+
return;
153+
}
154+
Serial.printf("SetVflip(%ld) success\n", enable);
155+
request->send(200, "text/plain", "SetVflip success\n");
156+
});
157+
119158
server.on("/cam.jpg", esp32cam::asyncweb::handleStill);
120159
server.on("/cam.mjpeg", esp32cam::asyncweb::handleMjpeg);
121160
}

src/esp32cam/camera.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ CameraClass::changeResolution(const Resolution& resolution, int sleepFor) {
5454
return true;
5555
}
5656

57+
bool
58+
CameraClass::update(const SensorSetter& setter) {
59+
sensor_t* sensor = esp_camera_sensor_get();
60+
if (sensor == nullptr) {
61+
return false;
62+
}
63+
return setter(sensor);
64+
}
65+
5766
std::unique_ptr<Frame>
5867
CameraClass::capture() {
5968
camera_fb_t* fb = esp_camera_fb_get();

src/esp32cam/camera.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "config.hpp"
55
#include "mjpeg.hpp"
6+
#include "setters.hpp"
67

78
namespace esp32cam {
89

@@ -34,6 +35,18 @@ class CameraClass {
3435
*/
3536
bool changeResolution(const Resolution& resolution, int sleepFor = 500);
3637

38+
/**
39+
* @brief Update camera configuration.
40+
* @param setter See `setters.hpp`.
41+
* @return whether success.
42+
*
43+
* Example:
44+
* @code
45+
* bool ok = Camera.update(SetVflip(true));
46+
* @endcode
47+
*/
48+
bool update(const SensorSetter& setter);
49+
3750
/**
3851
* @brief Capture a frame of picture.
3952
* @pre Camera is enabled.

src/esp32cam/setters.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "setters.hpp"
2+
#include <esp_camera.h>
3+
4+
namespace esp32cam {
5+
6+
SensorSetterInt::SensorSetterInt(size_t fn, int value)
7+
: m_fn(fn)
8+
, m_value(value) {}
9+
10+
bool
11+
SensorSetterInt::operator()(sensor_t* sensor) const {
12+
using Fn = int (*)(sensor_t*, int);
13+
Fn* fn = reinterpret_cast<Fn*>(reinterpret_cast<uintptr_t>(sensor) + m_fn);
14+
return (*fn)(sensor, m_value) == 0;
15+
}
16+
17+
SetHmirror::SetHmirror(bool enable)
18+
: SensorSetterInt(offsetof(sensor_t, set_hmirror), static_cast<int>(enable)) {}
19+
20+
SetVflip::SetVflip(bool enable)
21+
: SensorSetterInt(offsetof(sensor_t, set_vflip), static_cast<int>(enable)) {}
22+
23+
} // namespace esp32cam

src/esp32cam/setters.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef ESP32CAM_SETTERS_HPP
2+
#define ESP32CAM_SETTERS_HPP
3+
4+
#include <cstddef>
5+
6+
extern "C" {
7+
typedef struct _sensor sensor_t;
8+
} // extern "C"
9+
10+
namespace esp32cam {
11+
12+
class SensorSetter {
13+
public:
14+
virtual ~SensorSetter() {}
15+
16+
virtual bool operator()(sensor_t* sensor) const = 0;
17+
};
18+
19+
class SensorSetterInt : public SensorSetter {
20+
public:
21+
bool operator()(sensor_t* sensor) const override;
22+
23+
protected:
24+
explicit SensorSetterInt(size_t fn, int value);
25+
26+
private:
27+
size_t m_fn;
28+
int m_value;
29+
};
30+
31+
/**
32+
* @brief Enable or disable horizontal flip.
33+
* @sa Camera.update()
34+
*/
35+
class SetHmirror : public SensorSetterInt {
36+
public:
37+
explicit SetHmirror(bool enable);
38+
};
39+
40+
/**
41+
* @brief Enable or disable vertical flip.
42+
* @sa Camera.update()
43+
*/
44+
class SetVflip : public SensorSetterInt {
45+
public:
46+
explicit SetVflip(bool enable);
47+
};
48+
49+
} // namespace esp32cam
50+
51+
#endif // ESP32CAM_SETTERS_HPP

0 commit comments

Comments
 (0)