ESPHome  2024.4.1
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 #ifdef USE_TIME
17  ESP_LOGCONFIG(TAG, " Set time on connection: %s", YESNO(this->time_ != nullptr));
18 #endif
19  ESP_LOGCONFIG(TAG, " Disconnect delay : %" PRIu32 "ms", this->disconnect_delay_ms_);
20  LOG_UPDATE_INTERVAL(this);
21 }
22 
23 void PVVXDisplay::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
24  esp_ble_gattc_cb_param_t *param) {
25  switch (event) {
26  case ESP_GATTC_OPEN_EVT:
27  ESP_LOGV(TAG, "[%s] Connected successfully!", this->parent_->address_str().c_str());
28  this->delayed_disconnect_();
29  break;
30  case ESP_GATTC_DISCONNECT_EVT:
31  ESP_LOGV(TAG, "[%s] Disconnected", this->parent_->address_str().c_str());
32  this->connection_established_ = false;
33  this->cancel_timeout("disconnect");
34  this->char_handle_ = 0;
35  break;
36  case ESP_GATTC_SEARCH_CMPL_EVT: {
37  auto *chr = this->parent_->get_characteristic(this->service_uuid_, this->char_uuid_);
38  if (chr == nullptr) {
39  ESP_LOGW(TAG, "[%s] Characteristic not found.", this->parent_->address_str().c_str());
40  break;
41  }
42  this->connection_established_ = true;
43  this->char_handle_ = chr->handle;
44 #ifdef USE_TIME
45  this->sync_time_();
46 #endif
47  this->display();
48  break;
49  }
50  default:
51  break;
52  }
53 }
54 
56  if (this->auto_clear_enabled_)
57  this->clear();
58  if (this->writer_.has_value())
59  (*this->writer_)(*this);
60  this->display();
61 }
62 
64  if (!this->parent_->enabled) {
65  ESP_LOGD(TAG, "[%s] BLE client not enabled. Init connection.", this->parent_->address_str().c_str());
66  this->parent_->set_enabled(true);
67  return;
68  }
69  if (!this->connection_established_) {
70  ESP_LOGW(TAG, "[%s] Not connected to BLE client. State update can not be written.",
71  this->parent_->address_str().c_str());
72  return;
73  }
74  if (!this->char_handle_) {
75  ESP_LOGW(TAG, "[%s] No ble handle to BLE client. State update can not be written.",
76  this->parent_->address_str().c_str());
77  return;
78  }
79  ESP_LOGD(TAG, "[%s] Send to display: bignum %d, smallnum: %d, cfg: 0x%02x, validity period: %u.",
80  this->parent_->address_str().c_str(), this->bignum_, this->smallnum_, this->cfg_, this->validity_period_);
81  uint8_t blk[8] = {};
82  blk[0] = 0x22;
83  blk[1] = this->bignum_ & 0xff;
84  blk[2] = (this->bignum_ >> 8) & 0xff;
85  blk[3] = this->smallnum_ & 0xff;
86  blk[4] = (this->smallnum_ >> 8) & 0xff;
87  blk[5] = this->validity_period_ & 0xff;
88  blk[6] = (this->validity_period_ >> 8) & 0xff;
89  blk[7] = this->cfg_;
90  this->send_to_setup_char_(blk, sizeof(blk));
91 }
92 
93 void PVVXDisplay::setcfgbit_(uint8_t bit, bool value) {
94  uint8_t mask = 1 << bit;
95  if (value) {
96  this->cfg_ |= mask;
97  } else {
98  this->cfg_ &= (0xFF ^ mask);
99  }
100 }
101 
102 void PVVXDisplay::send_to_setup_char_(uint8_t *blk, size_t size) {
103  if (!this->connection_established_) {
104  ESP_LOGW(TAG, "[%s] Not connected to BLE client.", this->parent_->address_str().c_str());
105  return;
106  }
107  auto status =
108  esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_, size,
109  blk, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
110  if (status) {
111  ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str().c_str(), status);
112  } else {
113  ESP_LOGV(TAG, "[%s] send %u bytes", this->parent_->address_str().c_str(), size);
114  this->delayed_disconnect_();
115  }
116 }
117 
119  if (this->disconnect_delay_ms_ == 0)
120  return;
121  this->cancel_timeout("disconnect");
122  this->set_timeout("disconnect", this->disconnect_delay_ms_, [this]() { this->parent_->set_enabled(false); });
123 }
124 
125 #ifdef USE_TIME
127  if (this->time_ == nullptr)
128  return;
129  if (!this->connection_established_) {
130  ESP_LOGW(TAG, "[%s] Not connected to BLE client. Time can not be synced.", this->parent_->address_str().c_str());
131  return;
132  }
133  if (!this->char_handle_) {
134  ESP_LOGW(TAG, "[%s] No ble handle to BLE client. Time can not be synced.", this->parent_->address_str().c_str());
135  return;
136  }
137  auto time = this->time_->now();
138  if (!time.is_valid()) {
139  ESP_LOGW(TAG, "[%s] Time is not yet valid. Time can not be synced.", this->parent_->address_str().c_str());
140  return;
141  }
142  time.recalc_timestamp_utc(true); // calculate timestamp of local time
143  uint8_t blk[5] = {};
144 #if ESP_IDF_VERSION_MAJOR >= 5
145  ESP_LOGD(TAG, "[%s] Sync time with timestamp %" PRIu64 ".", this->parent_->address_str().c_str(), time.timestamp);
146 #else
147  ESP_LOGD(TAG, "[%s] Sync time with timestamp %lu.", this->parent_->address_str().c_str(), time.timestamp);
148 #endif
149  blk[0] = 0x23;
150  blk[1] = time.timestamp & 0xff;
151  blk[2] = (time.timestamp >> 8) & 0xff;
152  blk[3] = (time.timestamp >> 16) & 0xff;
153  blk[4] = (time.timestamp >> 24) & 0xff;
154  this->send_to_setup_char_(blk, sizeof(blk));
155 }
156 #endif
157 
158 } // namespace pvvx_mithermometer
159 } // namespace esphome
160 
161 #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:73
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:69
void setcfgbit_(uint8_t bit, bool value)
bool has_value() const
Definition: optional.h:87
void set_enabled(bool enabled)
Definition: ble_client.cpp:38
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
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:152
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override