ESPHome 2025.5.0
Loading...
Searching...
No Matches
graph.h
Go to the documentation of this file.
1#pragma once
2#include <cstdint>
3#include <utility>
4#include <vector>
8
9namespace esphome {
10
11// forward declare Display
12namespace display {
13class Display;
14class BaseFont;
15} // namespace display
16
17namespace graph {
18
19class Graph;
20
21const Color COLOR_ON(255, 255, 255, 255);
22
28 // Following defines number of bits used to define line pattern
30};
31
37
44
46 public:
47 void init(Graph *g);
48 void set_name_font(display::BaseFont *font) { this->font_label_ = font; }
49 void set_value_font(display::BaseFont *font) { this->font_value_ = font; }
50 void set_width(uint32_t width) { this->width_ = width; }
51 void set_height(uint32_t height) { this->height_ = height; }
52 void set_border(bool val) { this->border_ = val; }
53 void set_lines(bool val) { this->lines_ = val; }
55 void set_units(bool val) { this->units_ = val; }
57
58 protected:
59 uint32_t width_{0};
60 uint32_t height_{0};
61 bool border_{true};
62 bool lines_{true};
64 bool units_{true};
68 // Calculated values
69 Graph *parent_{nullptr};
70 // (x0) (xs,ys) (xs,ys)
71 // <x_offset,y_offset> ------> LABEL1 -------> LABEL2 -------> ...
72 // | \‍(xv,yv) \ .
73 // | \ \-> VALUE1+units
74 // (0,yl)| \-> VALUE1+units
75 // v (top_center)
76 // LINE_SAMPLE
77 int x0_{0}; // X-offset to centre of label text
78 int xs_{0}; // X spacing between labels
79 int ys_{0}; // Y spacing between labels
80 int yl_{0}; // Y spacing from label to line sample
81 int xv_{0}; // X distance between label to value text
82 int yv_{0}; // Y distance between label to value text
83 friend Graph;
84};
85
87 public:
88 void init(int length);
90 void set_update_time_ms(uint32_t update_time_ms) { update_time_ = update_time_ms; }
91 void take_sample(float data);
92 int get_length() const { return length_; }
93 float get_value(int idx) const { return samples_[(count_ + length_ - 1 - idx) % length_]; }
94 float get_recent_max() const { return recent_max_; }
95 float get_recent_min() const { return recent_min_; }
96
97 protected:
98 uint32_t last_sample_;
99 uint32_t period_{0};
100 uint32_t update_time_{0};
102 int count_{0};
103 float recent_min_{NAN};
104 float recent_max_{NAN};
105 std::vector<float> samples_;
106};
107
109 public:
110 void init(Graph *g);
111 void set_name(std::string name) { name_ = std::move(name); }
112 void set_sensor(sensor::Sensor *sensor) { sensor_ = sensor; }
113 uint8_t get_line_thickness() { return this->line_thickness_; }
114 void set_line_thickness(uint8_t val) { this->line_thickness_ = val; }
115 enum LineType get_line_type() { return this->line_type_; }
116 void set_line_type(enum LineType val) { this->line_type_ = val; }
117 Color get_line_color() { return this->line_color_; }
119 bool get_continuous() { return this->continuous_; }
120 void set_continuous(bool continuous) { this->continuous_ = continuous; }
121 std::string get_name() { return name_; }
122 const HistoryData *get_tracedata() { return &data_; }
123
124 protected:
126 std::string name_{""};
127 uint8_t line_thickness_{3};
128 enum LineType line_type_ { LINE_TYPE_SOLID };
130 bool continuous_{false};
132
133 friend Graph;
135};
136
137class Graph : public Component {
138 public:
139 void draw(display::Display *buff, uint16_t x_offset, uint16_t y_offset, Color color);
140 void draw_legend(display::Display *buff, uint16_t x_offset, uint16_t y_offset, Color color);
141
142 void setup() override;
143 float get_setup_priority() const override { return setup_priority::PROCESSOR; }
144 void dump_config() override;
145
147 void set_width(uint32_t width) { width_ = width; }
148 void set_height(uint32_t height) { height_ = height; }
149 void set_min_value(float val) { this->min_value_ = val; }
150 void set_max_value(float val) { this->max_value_ = val; }
151 void set_min_range(float val) { this->min_range_ = val; }
152 void set_max_range(float val) { this->max_range_ = val; }
153 void set_grid_x(float val) { this->gridspacing_x_ = val; }
154 void set_grid_y(float val) { this->gridspacing_y_ = val; }
155 void set_border(bool val) { this->border_ = val; }
156 void add_trace(GraphTrace *trace) { traces_.push_back(trace); }
157 void add_legend(GraphLegend *legend) {
158 this->legend_ = legend;
159 legend->init(this);
160 }
161 uint32_t get_duration() { return duration_; }
162 uint32_t get_width() { return width_; }
163 uint32_t get_height() { return height_; }
166
167 protected:
168 uint32_t duration_;
169 uint32_t width_;
170 uint32_t height_;
173 float min_value_{NAN};
174 float max_value_{NAN};
175 float min_range_{1.0};
176 float max_range_{NAN};
177 float gridspacing_x_{NAN};
178 float gridspacing_y_{NAN};
179 bool border_{true};
180 std::vector<GraphTrace *> traces_;
182
184};
185
186} // namespace graph
187} // namespace esphome
float get_graph_limit_min()
Definition graph.h:164
void add_legend(GraphLegend *legend)
Definition graph.h:157
void set_duration(uint32_t duration)
Definition graph.h:146
void add_trace(GraphTrace *trace)
Definition graph.h:156
void set_border(bool val)
Definition graph.h:155
void dump_config() override
Definition graph.cpp:390
void setup() override
Definition graph.cpp:384
float get_graph_limit_max()
Definition graph.h:165
GraphLegend * legend_
Definition graph.h:181
float graph_limit_min_
in pixels
Definition graph.h:171
void draw(display::Display *buff, uint16_t x_offset, uint16_t y_offset, Color color)
Definition graph.cpp:56
std::vector< GraphTrace * > traces_
Definition graph.h:180
void set_grid_y(float val)
Definition graph.h:154
void set_grid_x(float val)
Definition graph.h:153
uint32_t width_
in seconds
Definition graph.h:169
float get_setup_priority() const override
Definition graph.h:143
void set_height(uint32_t height)
Definition graph.h:148
void draw_legend(display::Display *buff, uint16_t x_offset, uint16_t y_offset, Color color)
Definition graph.cpp:335
void set_width(uint32_t width)
Definition graph.h:147
void set_max_range(float val)
Definition graph.h:152
void set_max_value(float val)
Definition graph.h:150
uint32_t get_duration()
Definition graph.h:161
uint32_t get_width()
Definition graph.h:162
void set_min_value(float val)
Definition graph.h:149
void set_min_range(float val)
Definition graph.h:151
uint32_t get_height()
Definition graph.h:163
uint32_t duration_
Definition graph.h:168
uint32_t height_
in pixels
Definition graph.h:170
void set_height(uint32_t height)
Definition graph.h:51
void set_values(ValuePositionType val)
Definition graph.h:54
display::BaseFont * font_label_
Definition graph.h:66
display::BaseFont * font_value_
Definition graph.h:67
void set_name_font(display::BaseFont *font)
Definition graph.h:48
DirectionType direction_
Definition graph.h:65
void set_units(bool val)
Definition graph.h:55
void set_direction(DirectionType val)
Definition graph.h:56
void set_lines(bool val)
Definition graph.h:53
void init(Graph *g)
Determine the best coordinates of drawing text + lines.
Definition graph.cpp:215
void set_value_font(display::BaseFont *font)
Definition graph.h:49
void set_width(uint32_t width)
Definition graph.h:50
ValuePositionType values_
Definition graph.h:63
void set_border(bool val)
Definition graph.h:52
enum LineType get_line_type()
Definition graph.h:115
void set_line_type(enum LineType val)
Definition graph.h:116
sensor::Sensor * sensor_
Definition graph.h:125
void set_name(std::string name)
Definition graph.h:111
void set_line_thickness(uint8_t val)
Definition graph.h:114
void set_line_color(Color val)
Definition graph.h:118
const HistoryData * get_tracedata()
Definition graph.h:122
uint8_t get_line_thickness()
Definition graph.h:113
std::string get_name()
Definition graph.h:121
void set_sensor(sensor::Sensor *sensor)
Definition graph.h:112
void set_continuous(bool continuous)
Definition graph.h:120
float get_recent_min() const
Definition graph.h:95
float get_value(int idx) const
Definition graph.h:93
int get_length() const
Definition graph.h:92
void set_update_time_ms(uint32_t update_time_ms)
Definition graph.h:90
float get_recent_max() const
Definition graph.h:94
void take_sample(float data)
Definition graph.cpp:21
std::vector< float > samples_
Definition graph.h:105
uint32_t update_time_
in ms
Definition graph.h:100
Base-class for all sensors.
Definition sensor.h:57
mopeka_std_values val[4]
uint8_t duration
Definition msa3xx.h:0
const Color COLOR_ON(255, 255, 255, 255)
Turn the pixel ON.
Definition display.h:193
@ VALUE_POSITION_TYPE_NONE
Definition graph.h:39
@ VALUE_POSITION_TYPE_AUTO
Definition graph.h:40
@ VALUE_POSITION_TYPE_BELOW
Definition graph.h:42
@ VALUE_POSITION_TYPE_BESIDE
Definition graph.h:41
LineType
Bit pattern defines the line-type.
Definition graph.h:24
@ LINE_TYPE_DOTTED
Definition graph.h:26
@ PATTERN_LENGTH
Definition graph.h:29
@ LINE_TYPE_SOLID
Definition graph.h:25
@ LINE_TYPE_DASHED
Definition graph.h:27
@ DIRECTION_TYPE_HORIZONTAL
Definition graph.h:34
@ DIRECTION_TYPE_AUTO
Definition graph.h:33
@ DIRECTION_TYPE_VERTICAL
Definition graph.h:35
const float PROCESSOR
For components that use data from sensors like displays.
Definition component.cpp:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void init()
Definition core.cpp:90
uint16_t length
Definition tt21100.cpp:0