ESPHome 2025.12.2
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(), 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());
32 this->delayed_disconnect_();
33 }
34 break;
35 case ESP_GATTC_DISCONNECT_EVT:
36 ESP_LOGV(TAG, "[%s] Disconnected", this->parent_->address_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());
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());
70 // Now that pairing is complete, perform the pending writes
72 } else {
73 ESP_LOGW(TAG, "[%s] Authentication failed.", this->parent_->address_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());
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.", this->parent_->address_str());
98 return;
99 }
100 if (!this->char_handle_) {
101 ESP_LOGW(TAG, "[%s] No ble handle to BLE client. State update can not be written.", this->parent_->address_str());
102 return;
103 }
104 ESP_LOGD(TAG, "[%s] Send to display: bignum %d, smallnum: %d, cfg: 0x%02x, validity period: %u.",
105 this->parent_->address_str(), this->bignum_, this->smallnum_, this->cfg_, this->validity_period_);
106 uint8_t blk[8] = {};
107 blk[0] = 0x22;
108 blk[1] = this->bignum_ & 0xff;
109 blk[2] = (this->bignum_ >> 8) & 0xff;
110 blk[3] = this->smallnum_ & 0xff;
111 blk[4] = (this->smallnum_ >> 8) & 0xff;
112 blk[5] = this->validity_period_ & 0xff;
113 blk[6] = (this->validity_period_ >> 8) & 0xff;
114 blk[7] = this->cfg_;
115 this->send_to_setup_char_(blk, sizeof(blk));
116}
117
118void PVVXDisplay::setcfgbit_(uint8_t bit, bool value) {
119 uint8_t mask = 1 << bit;
120 if (value) {
121 this->cfg_ |= mask;
122 } else {
123 this->cfg_ &= (0xFF ^ mask);
124 }
125}
126
127void PVVXDisplay::send_to_setup_char_(uint8_t *blk, size_t size) {
128 if (!this->connection_established_) {
129 ESP_LOGW(TAG, "[%s] Not connected to BLE client.", this->parent_->address_str());
130 return;
131 }
132 auto status =
133 esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_, size,
134 blk, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
135 if (status) {
136 ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str(), status);
137 } else {
138 ESP_LOGV(TAG, "[%s] send %u bytes", this->parent_->address_str(), size);
139 this->delayed_disconnect_();
140 }
141}
142
144 if (this->disconnect_delay_ms_ == 0)
145 return;
146 this->cancel_timeout("disconnect");
147 this->set_timeout("disconnect", this->disconnect_delay_ms_, [this]() { this->parent_->set_enabled(false); });
148}
149
151#ifdef USE_TIME
152 this->sync_time_();
153#endif
154 this->display();
155}
156
157#ifdef USE_TIME
159 if (this->time_ == nullptr)
160 return;
161 if (!this->connection_established_) {
162 ESP_LOGW(TAG, "[%s] Not connected to BLE client. Time can not be synced.", this->parent_->address_str());
163 return;
164 }
165 if (!this->char_handle_) {
166 ESP_LOGW(TAG, "[%s] No ble handle to BLE client. Time can not be synced.", this->parent_->address_str());
167 return;
168 }
169 auto time = this->time_->now();
170 if (!time.is_valid()) {
171 ESP_LOGW(TAG, "[%s] Time is not yet valid. Time can not be synced.", this->parent_->address_str());
172 return;
173 }
174 time.recalc_timestamp_utc(true); // calculate timestamp of local time
175 uint8_t blk[5] = {};
176 ESP_LOGD(TAG, "[%s] Sync time with timestamp %" PRIu64 ".", this->parent_->address_str(), time.timestamp);
177 blk[0] = 0x23;
178 blk[1] = time.timestamp & 0xff;
179 blk[2] = (time.timestamp >> 8) & 0xff;
180 blk[3] = (time.timestamp >> 16) & 0xff;
181 blk[4] = (time.timestamp >> 24) & 0xff;
182 this->send_to_setup_char_(blk, sizeof(blk));
183}
184#endif
185
186} // namespace pvvx_mithermometer
187} // namespace esphome
188
189#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)
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
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:158