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