ESPHome  2022.12.8
improv_serial_component.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/defines.h"
6 #include "esphome/core/helpers.h"
7 
8 #include <improv.h>
9 #include <vector>
10 
11 #ifdef USE_ARDUINO
12 #include <HardwareSerial.h>
13 #endif
14 #ifdef USE_ESP_IDF
15 #include <driver/uart.h>
16 #endif
17 
18 namespace esphome {
19 namespace improv_serial {
20 
21 enum ImprovSerialType : uint8_t {
24  TYPE_RPC = 0x03,
26 };
27 
28 static const uint8_t IMPROV_SERIAL_VERSION = 1;
29 
31  public:
32  void setup() override;
33  void loop() override;
34  void dump_config() override;
35 
36  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
37 
38  protected:
39  bool parse_improv_serial_byte_(uint8_t byte);
40  bool parse_improv_payload_(improv::ImprovCommand &command);
41 
43  void set_error_(improv::Error error);
44  void send_response_(std::vector<uint8_t> &response);
46 
47  std::vector<uint8_t> build_rpc_settings_response_(improv::Command command);
48  std::vector<uint8_t> build_version_info_();
49 
50  int available_();
51  uint8_t read_byte_();
52  void write_data_(std::vector<uint8_t> &data);
53 
54 #ifdef USE_ARDUINO
55  Stream *hw_serial_{nullptr};
56 #endif
57 #ifdef USE_ESP_IDF
58  uart_port_t uart_num_;
59 #endif
60 
61  std::vector<uint8_t> rx_buffer_;
62  uint32_t last_read_byte_{0};
64  improv::State state_{improv::STATE_AUTHORIZED};
65 };
66 
68  *global_improv_serial_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
69 
70 } // namespace improv_serial
71 } // namespace esphome
bool parse_improv_payload_(improv::ImprovCommand &command)
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition: component.cpp:25
void send_response_(std::vector< uint8_t > &response)
ImprovSerialComponent * global_improv_serial_component
Definition: a4988.cpp:4
std::vector< uint8_t > build_rpc_settings_response_(improv::Command command)
bool state
Definition: fan.h:34