ESPHome 2025.8.4
Loading...
Searching...
No Matches
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
5namespace esphome {
6namespace pvvx_mithermometer {
7
8static const char *const TAG = "display.pvvx_mithermometer";
9
11 ESP_LOGCONFIG(TAG,
12 "PVVX MiThermometer display:\n"
13 " MAC address : %s\n"
14 " Service UUID : %s\n"
15 " Characteristic UUID : %s\n"
16 " Auto clear : %s",
17 this->parent_->address_str().c_str(), this->service_uuid_.to_string().c_str(),
18 this->char_uuid_.to_string().c_str(), YESNO(this->auto_clear_enabled_));
19#ifdef USE_TIME
20 ESP_LOGCONFIG(TAG, " Set time on connection: %s", YESNO(this->time_ != nullptr));
21#endif
22 ESP_LOGCONFIG(TAG, " Disconnect delay : %" PRIu32 "ms", this->disconnect_delay_ms_);
23 LOG_UPDATE_INTERVAL(this);
24}
25
26void PVVXDisplay::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
27 esp_ble_gattc_cb_param_t *param) {
28 switch (event) {
29 case ESP_GATTC_OPEN_EVT:
30 if (param->open.status == ESP_GATT_OK) {
31 ESP_LOGV(TAG, "[%s] Connected successfully!", this->parent_->address_str().c_str());
32 this->delayed_disconnect_();
33 }
34 break;
35 case ESP_GATTC_DISCONNECT_EVT:
36 ESP_LOGV(TAG, "[%s] Disconnected", this->parent_->address_str().c_str());
37 this->connection_established_ = false;
38 this->cancel_timeout("disconnect");
39 this->char_handle_ = 0;
40 break;
41 case ESP_GATTC_SEARCH_CMPL_EVT: {
42 auto *chr = this->parent_->get_characteristic(this->service_uuid_, this->char_uuid_);
43 if (chr == nullptr) {
44 ESP_LOGW(TAG, "[%s] Characteristic not found.", this->parent_->address_str().c_str());
45 break;
46 }
47 this->connection_established_ = true;
48 this->char_handle_ = chr->handle;
49
50 // Attempt to write immediately
51 // For devices without security, this will work
52 // For devices with security that are already paired, this will work
53 // For devices that need pairing, the write will be retried after auth completes
55 break;
56 }
57 default:
58 break;
59 }
60}
61
62void PVVXDisplay::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
63 switch (event) {
64 case ESP_GAP_BLE_AUTH_CMPL_EVT: {
65 if (!this->parent_->check_addr(param->ble_security.auth_cmpl.bd_addr))
66 return;
67
68 if (param->ble_security.auth_cmpl.success) {
69 ESP_LOGD(TAG, "[%s] Authentication successful, performing writes.", this->parent_->address_str().c_str());
70 // Now that pairing is complete, perform the pending writes
72 } else {
73 ESP_LOGW(TAG, "[%s] Authentication failed.", this->parent_->address_str().c_str());
74 }
75 break;
76 }
77 default:
78 break;
79 }
80}
81
83 if (this->auto_clear_enabled_)
84 this->clear();
85 if (this->writer_.has_value())
86 (*this->writer_)(*this);
87 this->display();
88}
89
91 if (!this->parent_->enabled) {
92 ESP_LOGD(TAG, "[%s] BLE client not enabled. Init connection.", this->parent_->address_str().c_str());
93 this->parent_->set_enabled(true);
94 return;
95 }
96 if (!this->connection_established_) {
97 ESP_LOGW(TAG, "[%s] Not connected to BLE client. State update can not be written.",
98 this->parent_->address_str().c_str());
99 return;
100 }
101 if (!this->char_handle_) {
102 ESP_LOGW(TAG, "[%s] No ble handle to BLE client. State update can not be written.",
103 this->parent_->address_str().c_str());
104 return;
105 }
106 ESP_LOGD(TAG, "[%s] Send to display: bignum %d, smallnum: %d, cfg: 0x%02x, validity period: %u.",
107 this->parent_->address_str().c_str(), this->bignum_, this->smallnum_, this->cfg_, this->validity_period_);
108 uint8_t blk[8] = {};
109 blk[0] = 0x22;
110 blk[1] = this->bignum_ & 0xff;
111 blk[2] = (this->bignum_ >> 8) & 0xff;
112 blk[3] = this->smallnum_ & 0xff;
113 blk[4] = (this->smallnum_ >> 8) & 0xff;
114 blk[5] = this->validity_period_ & 0xff;
115 blk[6] = (this->validity_period_ >> 8) & 0xff;
116 blk[7] = this->cfg_;
117 this->send_to_setup_char_(blk, sizeof(blk));
118}
119
120void PVVXDisplay::setcfgbit_(uint8_t bit, bool value) {
121 uint8_t mask = 1 << bit;
122 if (value) {
123 this->cfg_ |= mask;
124 } else {
125 this->cfg_ &= (0xFF ^ mask);
126 }
127}
128
129void PVVXDisplay::send_to_setup_char_(uint8_t *blk, size_t size) {
130 if (!this->connection_established_) {
131 ESP_LOGW(TAG, "[%s] Not connected to BLE client.", this->parent_->address_str().c_str());
132 return;
133 }
134 auto status =
135 esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_, size,
136 blk, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
137 if (status) {
138 ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str().c_str(), status);
139 } else {
140 ESP_LOGV(TAG, "[%s] send %u bytes", this->parent_->address_str().c_str(), size);
141 this->delayed_disconnect_();
142 }
143}
144
146 if (this->disconnect_delay_ms_ == 0)
147 return;
148 this->cancel_timeout("disconnect");
149 this->set_timeout("disconnect", this->disconnect_delay_ms_, [this]() { this->parent_->set_enabled(false); });
150}
151
153#ifdef USE_TIME
154 this->sync_time_();
155#endif
156 this->display();
157}
158
159#ifdef USE_TIME
161 if (this->time_ == nullptr)
162 return;
163 if (!this->connection_established_) {
164 ESP_LOGW(TAG, "[%s] Not connected to BLE client. Time can not be synced.", this->parent_->address_str().c_str());
165 return;
166 }
167 if (!this->char_handle_) {
168 ESP_LOGW(TAG, "[%s] No ble handle to BLE client. Time can not be synced.", this->parent_->address_str().c_str());
169 return;
170 }
171 auto time = this->time_->now();
172 if (!time.is_valid()) {
173 ESP_LOGW(TAG, "[%s] Time is not yet valid. Time can not be synced.", this->parent_->address_str().c_str());
174 return;
175 }
176 time.recalc_timestamp_utc(true); // calculate timestamp of local time
177 uint8_t blk[5] = {};
178 ESP_LOGD(TAG, "[%s] Sync time with timestamp %" PRIu64 ".", this->parent_->address_str().c_str(), time.timestamp);
179 blk[0] = 0x23;
180 blk[1] = time.timestamp & 0xff;
181 blk[2] = (time.timestamp >> 8) & 0xff;
182 blk[3] = (time.timestamp >> 16) & 0xff;
183 blk[4] = (time.timestamp >> 24) & 0xff;
184 this->send_to_setup_char_(blk, sizeof(blk));
185}
186#endif
187
188} // namespace pvvx_mithermometer
189} // namespace esphome
190
191#endif
uint8_t status
Definition bl0942.h:8
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void set_enabled(bool enabled)
const std::string & address_str() const
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
bool has_value() const
Definition optional.h:92
void send_to_setup_char_(uint8_t *blk, size_t size)
void setcfgbit_(uint8_t bit, bool value)
esp32_ble_tracker::ESPBTUUID service_uuid_
esp32_ble_tracker::ESPBTUUID char_uuid_
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
ESPTime now()
Get the time in the currently defined timezone.
Providing packet encoding functions for exchanging data with a remote host.
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:164