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