ESPHome 2026.8.0b4
Loading...
Searching...
No Matches
model_data.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
5#include <cstddef>
6#include <cstdint>
8
10
11// Owns the buffer holding a runtime-downloaded TFLite model. The buffer prefers PSRAM but falls back to
12// internal RAM, so a device without PSRAM can still hold a single model. It is filled over HTTP, checked
13// for integrity by the caller (SHA256) and for a usable TFLite header here, then kept alive for the
14// lifetime of the WakeWordModel that uses it. Only ever held behind a std::shared_ptr, so copies and
15// moves are disabled.
16class ModelData {
17 public:
18 ModelData() = default;
19 ~ModelData();
20
21 // Non-copyable, non-movable
22 ModelData(const ModelData &) = delete;
23 ModelData &operator=(const ModelData &) = delete;
24 ModelData(ModelData &&) = delete;
26
27 // Allocate memory for model
28 bool allocate(size_t size);
29
30 // Get stable pointer for TFLite (only valid after validate_and_mark_ready())
31 const uint8_t *get_model_pointer() const;
32
33 // Get writable pointer for downloading (invalidates the model)
34 uint8_t *get_write_pointer();
35
36 // Validate TFLite model and mark as ready for use
38
39 // Check if model is valid and ready for use
40 bool is_valid() const { return this->valid_; }
41
42 // Get size of model data
43 size_t size() const { return this->size_; }
44
45 // Check if memory is allocated
46 bool is_allocated() const { return this->data_ != nullptr; }
47
48 protected:
49 // Deallocate memory
50 void deallocate_();
51
52 uint8_t *data_{nullptr};
53 size_t size_{0};
54 bool valid_{false};
56};
57
58} // namespace esphome::micro_wake_word
59
60#endif // USE_ESP32
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2099
const uint8_t * get_model_pointer() const
ModelData & operator=(const ModelData &)=delete
RAMAllocator< uint8_t > allocator_
Definition model_data.h:55
ModelData(const ModelData &)=delete
ModelData & operator=(ModelData &&)=delete