ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
snapshot.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_HOST
5
6#include <cstddef>
7#include <cstdint>
8#include <string>
9
10// Directory snapshots are written to. Normally set by codegen to a folder under .esphome; the
11// fallback keeps the component compiling for static analysis, where no defines.h is generated.
12#ifndef ESPHOME_SNAPSHOT_DIR
13#define ESPHOME_SNAPSHOT_DIR "."
14#endif
15
16namespace esphome::snapshot {
17
23class Snapshot {
24 public:
25 virtual ~Snapshot() = default;
26
29 void set_snapshot_prefix(const char *prefix) { this->snapshot_prefix_ = prefix; }
30
35 bool take_snapshot(const char *filename);
36
38 static void log_action_failed();
39
40 protected:
42 virtual int snapshot_width() = 0;
44 virtual int snapshot_height() = 0;
48 virtual bool capture_bgr(uint8_t *dest, size_t row_stride) = 0;
49
50 const char *snapshot_prefix_{"snapshot"};
51};
52
53template<typename... Ts> class SnapshotAction final : public Action<Ts...>, public Parented<Snapshot> {
54 public:
55 TEMPLATABLE_VALUE(std::string, filename)
56
57 protected:
58 void play(const Ts &...x) override {
59 bool ok;
60 if (this->filename_.has_value()) {
61 ok = this->parent_->take_snapshot(this->filename_.value(x...).c_str());
62 } else {
63 ok = this->parent_->take_snapshot(nullptr);
64 }
65 if (!ok)
66 this->parent_->log_action_failed();
67 }
68};
69
70} // namespace esphome::snapshot
71
72#endif
virtual void play(const Ts &...x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:1910
Base for anything that can hand over the picture it is showing so it can be written to a file.
Definition snapshot.h:23
virtual ~Snapshot()=default
virtual bool capture_bgr(uint8_t *dest, size_t row_stride)=0
Fill in the picture: three bytes per pixel in blue, green, red order, topmost row first,...
const char * snapshot_prefix_
Definition snapshot.h:50
virtual int snapshot_width()=0
Width of the picture in pixels.
virtual int snapshot_height()=0
Height of the picture in pixels.
static void log_action_failed()
Log that an action-triggered snapshot did not write a file.
Definition snapshot.cpp:196
void set_snapshot_prefix(const char *prefix)
Set the word generated names start with.
Definition snapshot.h:29
bool take_snapshot(const char *filename)
Write the current picture to a BMP file in the snapshot directory.
Definition snapshot.cpp:198
uint16_t x
Definition tt21100.cpp:5