ESPHome  2024.11.2
graph.h
Go to the documentation of this file.
1 #pragma once
2 #include <cstdint>
3 #include <utility>
4 #include <vector>
6 #include "esphome/core/color.h"
8 
9 namespace esphome {
10 
11 // forward declare Display
12 namespace display {
13 class Display;
14 class BaseFont;
15 } // namespace display
16 
17 namespace graph {
18 
19 class Graph;
20 
21 const Color COLOR_ON(255, 255, 255, 255);
22 
24 enum LineType {
25  LINE_TYPE_SOLID = 0b1111,
26  LINE_TYPE_DOTTED = 0b0101,
27  LINE_TYPE_DASHED = 0b1110,
28  // Following defines number of bits used to define line pattern
30 };
31 
36 };
37 
43 };
44 
45 class GraphLegend {
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; }
54  void set_values(ValuePositionType val) { this->values_ = val; }
55  void set_units(bool val) { this->units_ = val; }
56  void set_direction(DirectionType val) { this->direction_ = 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};
66  display::BaseFont *font_label_{nullptr};
67  display::BaseFont *font_value_{nullptr};
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 
86 class HistoryData {
87  public:
88  void init(int length);
89  ~HistoryData();
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};
101  int length_;
102  int count_{0};
103  float recent_min_{NAN};
104  float recent_max_{NAN};
105  std::vector<float> samples_;
106 };
107 
108 class GraphTrace {
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_; }
118  void set_line_color(Color val) { this->line_color_ = val; }
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:
125  sensor::Sensor *sensor_{nullptr};
126  std::string name_{""};
127  uint8_t line_thickness_{3};
128  enum LineType line_type_ { LINE_TYPE_SOLID };
129  Color line_color_{COLOR_ON};
130  bool continuous_{false};
132 
133  friend Graph;
134  friend GraphLegend;
135 };
136 
137 class 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 
146  void set_duration(uint32_t duration) { duration_ = duration; }
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_; }
164 
165  protected:
166  uint32_t duration_;
167  uint32_t width_;
168  uint32_t height_;
169  float min_value_{NAN};
170  float max_value_{NAN};
171  float min_range_{1.0};
172  float max_range_{NAN};
173  float gridspacing_x_{NAN};
174  float gridspacing_y_{NAN};
175  bool border_{true};
176  std::vector<GraphTrace *> traces_;
177  GraphLegend *legend_{nullptr};
178 
179  friend GraphLegend;
180 };
181 
182 } // namespace graph
183 } // namespace esphome
void setup()
const char * name
Definition: stm32flash.h:78
uint32_t get_width()
Definition: graph.h:162
uint32_t get_height()
Definition: graph.h:163
const HistoryData * get_tracedata()
Definition: graph.h:122
float get_value(int idx) const
Definition: graph.h:93
void set_line_color(Color val)
Definition: graph.h:118
void set_height(uint32_t height)
Definition: graph.h:148
void set_grid_x(float val)
Definition: graph.h:153
void add_trace(GraphTrace *trace)
Definition: graph.h:156
void set_max_value(float val)
Definition: graph.h:150
mopeka_std_values val[4]
float get_setup_priority() const override
Definition: graph.h:143
friend GraphLegend
Definition: graph.h:179
void set_direction(DirectionType val)
Definition: graph.h:56
float get_recent_min() const
Definition: graph.h:95
void set_border(bool val)
Definition: graph.h:52
void set_value_font(display::BaseFont *font)
Definition: graph.h:49
void set_name(std::string name)
Definition: graph.h:111
std::vector< GraphTrace * > traces_
Definition: graph.h:176
void set_continuous(bool continuous)
Definition: graph.h:120
uint32_t duration_
Definition: graph.h:166
std::vector< float > samples_
Definition: graph.h:105
void init(Graph *g)
Determine the best coordinates of drawing text + lines.
Definition: graph.cpp:214
const float PROCESSOR
For components that use data from sensors like displays.
Definition: component.cpp:20
void set_grid_y(float val)
Definition: graph.h:154
void set_lines(bool val)
Definition: graph.h:53
uint32_t get_duration()
Definition: graph.h:161
void set_line_type(enum LineType val)
Definition: graph.h:116
void set_line_thickness(uint8_t val)
Definition: graph.h:114
std::string get_name()
Definition: graph.h:121
const Color COLOR_ON(255, 255, 255, 255)
Turn the pixel ON.
Definition: display.h:193
ValuePositionType
Definition: graph.h:38
void add_legend(GraphLegend *legend)
Definition: graph.h:157
uint8_t get_line_thickness()
Definition: graph.h:113
void set_update_time_ms(uint32_t update_time_ms)
Definition: graph.h:90
void set_duration(uint32_t duration)
Definition: graph.h:146
void set_units(bool val)
Definition: graph.h:55
uint16_t length
Definition: tt21100.cpp:12
void set_sensor(sensor::Sensor *sensor)
Definition: graph.h:112
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_width(uint32_t width)
Definition: graph.h:147
LineType
Bit pattern defines the line-type.
Definition: graph.h:24
void set_border(bool val)
Definition: graph.h:155
void init()
Definition: core.cpp:80
void set_name_font(display::BaseFont *font)
Definition: graph.h:48
uint32_t height_
in pixels
Definition: graph.h:168
int get_length() const
Definition: graph.h:92
Base-class for all sensors.
Definition: sensor.h:57
void set_min_range(float val)
Definition: graph.h:151
void set_min_value(float val)
Definition: graph.h:149
float get_recent_max() const
Definition: graph.h:94
uint32_t width_
in seconds
Definition: graph.h:167
esphome::sensor::Sensor * sensor
Definition: statsd.h:38
void set_width(uint32_t width)
Definition: graph.h:50
void set_height(uint32_t height)
Definition: graph.h:51
void set_values(ValuePositionType val)
Definition: graph.h:54
void set_max_range(float val)
Definition: graph.h:152