ESPHome  2023.11.6
pvvx_display.cpp
Go to the documentation of this file.
1 #include "pvvx_display.h"
2 #include "esphome/core/log.h"
3 
4 #ifdef USE_ESP32
5 namespace esphome {
6 namespace pvvx_mithermometer {
7 
8 static const char *const TAG = "display.pvvx_mithermometer";
9 
11  ESP_LOGCONFIG(TAG, "PVVX MiThermometer display:");
12  ESP_LOGCONFIG(TAG, " MAC address : %s", this->parent_->address_str().c_str());
13  ESP_LOGCONFIG(TAG, " Service UUID : %s", this->service_uuid_.to_string().c_str());
14  ESP_LOGCONFIG(TAG, " Characteristic UUID : %s", this->char_uuid_.to_string().c_str());
15  ESP_LOGCONFIG(TAG, " Auto clear : %s", YESNO(this->auto_clear_enabled_));
16  ESP_LOGCONFIG(TAG, " Set time on connection: %s", YESNO(this->time_ != nullptr));
17  ESP_LOGCONFIG(TAG, " Disconnect delay : %" PRIu32 "ms", this->disconnect_delay_ms_);
18  LOG_UPDATE_INTERVAL(this);
19 }
20 
21 void PVVXDisplay::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
22  esp_ble_gattc_cb_param_t *param) {
23  switch (event) {
24  case ESP_GATTC_OPEN_EVT:
25  ESP_LOGV(TAG, "[%s] Connected successfully!", this->parent_->address_str().c_str());
26  this->delayed_disconnect_();
27  break;
28  case ESP_GATTC_DISCONNECT_EVT:
29  ESP_LOGV(TAG, "[%s] Disconnected", this->parent_->address_str().c_str());
30  this->connection_established_ = false;
31  this->cancel_timeout("disconnect");
32  this->char_handle_ = 0;
33  break;
34  case ESP_GATTC_SEARCH_CMPL_EVT: {
35  auto *chr = this->parent_->get_characteristic(this->service_uuid_, this->char_uuid_);
36  if (chr == nullptr) {
37  ESP_LOGW(TAG, "[%s] Characteristic not found.", this->parent_->address_str().c_str());
38  break;
39  }
40  this->connection_established_ = true;
41  this->char_handle_ = chr->handle;
42 #ifdef USE_TIME
43  this->sync_time_();
44 #endif
45  this->display();
46  break;
47  }
48  default:
49  break;
50  }
51 }
52 
54  if (this->auto_clear_enabled_)
55  this->clear();
56  if (this->writer_.has_value())
57  (*this->writer_)(*this);
58  this->display();
59 }
60 
62  if (!this->parent_->enabled) {
63  ESP_LOGD(TAG, "[%s] BLE client not enabled. Init connection.", this->parent_->address_str().c_str());
64  this->parent_->set_enabled(true);
65  return;
66  }
67  if (!this->connection_established_) {
68  ESP_LOGW(TAG, "[%s] Not connected to BLE client. State update can not be written.",
69  this->parent_->address_str().c_str());
70  return;
71  }
72  if (!this->char_handle_) {
73  ESP_LOGW(TAG, "[%s] No ble handle to BLE client. State update can not be written.",
74  this->parent_->address_str().c_str());
75  return;
76  }
77  ESP_LOGD(TAG, "[%s] Send to display: bignum %d, smallnum: %d, cfg: 0x%02x, validity period: %u.",
78  this->parent_->address_str().c_str(), this->bignum_, this->smallnum_, this->cfg_, this->validity_period_);
79  uint8_t blk[8] = {};
80  blk[0] = 0x22;
81  blk[1] = this->bignum_ & 0xff;
82  blk[2] = (this->bignum_ >> 8) & 0xff;
83  blk[3] = this->smallnum_ & 0xff;
84  blk[4] = (this->smallnum_ >> 8) & 0xff;
85  blk[5] = this->validity_period_ & 0xff;
86  blk[6] = (this->validity_period_ >> 8) & 0xff;
87  blk[7] = this->cfg_;
88  this->send_to_setup_char_(blk, sizeof(blk));
89 }
90 
91 void PVVXDisplay::setcfgbit_(uint8_t bit, bool value) {
92  uint8_t mask = 1 << bit;
93  if (value) {
94  this->cfg_ |= mask;
95  } else {
96  this->cfg_ &= (0xFF ^ mask);
97  }
98 }
99 
100 void PVVXDisplay::send_to_setup_char_(uint8_t *blk, size_t size) {
101  if (!this->connection_established_) {
102  ESP_LOGW(TAG, "[%s] Not connected to BLE client.", this->parent_->address_str().c_str());
103  return;
104  }
105  auto status =
106  esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_, size,
107  blk, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
108  if (status) {
109  ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str().c_str(), status);
110  } else {
111  ESP_LOGV(TAG, "[%s] send %u bytes", this->parent_->address_str().c_str(), size);
112  this->delayed_disconnect_();
113  }
114 }
115 
117  if (this->disconnect_delay_ms_ == 0)
118  return;
119  this->cancel_timeout("disconnect");
120  this->set_timeout("disconnect", this->disconnect_delay_ms_, [this]() { this->parent_->set_enabled(false); });
121 }
122 
123 #ifdef USE_TIME
125  if (this->time_ == nullptr)
126  return;
127  if (!this->connection_established_) {
128  ESP_LOGW(TAG, "[%s] Not connected to BLE client. Time can not be synced.", this->parent_->address_str().c_str());
129  return;
130  }
131  if (!this->char_handle_) {
132  ESP_LOGW(TAG, "[%s] No ble handle to BLE client. Time can not be synced.", this->parent_->address_str().c_str());
133  return;
134  }
135  auto time = this->time_->now();
136  if (!time.is_valid()) {
137  ESP_LOGW(TAG, "[%s] Time is not yet valid. Time can not be synced.", this->parent_->address_str().c_str());
138  return;
139  }
140  time.recalc_timestamp_utc(true); // calculate timestamp of local time
141  uint8_t blk[5] = {};
142 #if ESP_IDF_VERSION_MAJOR >= 5
143  ESP_LOGD(TAG, "[%s] Sync time with timestamp %" PRIu64 ".", this->parent_->address_str().c_str(), time.timestamp);
144 #else
145  ESP_LOGD(TAG, "[%s] Sync time with timestamp %lu.", this->parent_->address_str().c_str(), time.timestamp);
146 #endif
147  blk[0] = 0x23;
148  blk[1] = time.timestamp & 0xff;
149  blk[2] = (time.timestamp >> 8) & 0xff;
150  blk[3] = (time.timestamp >> 16) & 0xff;
151  blk[4] = (time.timestamp >> 24) & 0xff;
152  this->send_to_setup_char_(blk, sizeof(blk));
153 }
154 #endif
155 
156 } // namespace pvvx_mithermometer
157 } // namespace esphome
158 
159 #endif
ESPTime now()
Get the time in the currently defined timezone.
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
Definition: component.cpp:72
optional< pvvx_writer_t > writer_
Definition: pvvx_display.h:129
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:68
void setcfgbit_(uint8_t bit, bool value)
bool has_value() const
Definition: optional.h:87
void set_enabled(bool enabled)
Definition: ble_client.cpp:37
void send_to_setup_char_(uint8_t *blk, size_t size)
esp32_ble_tracker::ESPBTUUID char_uuid_
Definition: pvvx_display.h:126
std::string to_string() const
Definition: ble_uuid.cpp:165
uint8_t status
Definition: bl0942.h:23
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
esp32_ble_tracker::ESPBTUUID service_uuid_
Definition: pvvx_display.h:124
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void recalc_timestamp_utc(bool use_day_of_year=true)
Recalculate the timestamp field from the other fields of this ESPTime instance (must be UTC)...
Definition: time.cpp:108
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override