ESPHome 2025.6.3
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#ifdef USE_TIME
50 this->sync_time_();
51#endif
52 this->display();
53 break;
54 }
55 default:
56 break;
57 }
58}
59
61 if (this->auto_clear_enabled_)
62 this->clear();
63 if (this->writer_.has_value())
64 (*this->writer_)(*this);
65 this->display();
66}
67
69 if (!this->parent_->enabled) {
70 ESP_LOGD(TAG, "[%s] BLE client not enabled. Init connection.", this->parent_->address_str().c_str());
71 this->parent_->set_enabled(true);
72 return;
73 }
74 if (!this->connection_established_) {
75 ESP_LOGW(TAG, "[%s] Not connected to BLE client. State update can not be written.",
76 this->parent_->address_str().c_str());
77 return;
78 }
79 if (!this->char_handle_) {
80 ESP_LOGW(TAG, "[%s] No ble handle to BLE client. State update can not be written.",
81 this->parent_->address_str().c_str());
82 return;
83 }
84 ESP_LOGD(TAG, "[%s] Send to display: bignum %d, smallnum: %d, cfg: 0x%02x, validity period: %u.",
85 this->parent_->address_str().c_str(), this->bignum_, this->smallnum_, this->cfg_, this->validity_period_);
86 uint8_t blk[8] = {};
87 blk[0] = 0x22;
88 blk[1] = this->bignum_ & 0xff;
89 blk[2] = (this->bignum_ >> 8) & 0xff;
90 blk[3] = this->smallnum_ & 0xff;
91 blk[4] = (this->smallnum_ >> 8) & 0xff;
92 blk[5] = this->validity_period_ & 0xff;
93 blk[6] = (this->validity_period_ >> 8) & 0xff;
94 blk[7] = this->cfg_;
95 this->send_to_setup_char_(blk, sizeof(blk));
96}
97
98void PVVXDisplay::setcfgbit_(uint8_t bit, bool value) {
99 uint8_t mask = 1 << bit;
100 if (value) {
101 this->cfg_ |= mask;
102 } else {
103 this->cfg_ &= (0xFF ^ mask);
104 }
105}
106
107void PVVXDisplay::send_to_setup_char_(uint8_t *blk, size_t size) {
108 if (!this->connection_established_) {
109 ESP_LOGW(TAG, "[%s] Not connected to BLE client.", this->parent_->address_str().c_str());
110 return;
111 }
112 auto status =
113 esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_, size,
114 blk, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
115 if (status) {
116 ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str().c_str(), status);
117 } else {
118 ESP_LOGV(TAG, "[%s] send %u bytes", this->parent_->address_str().c_str(), size);
119 this->delayed_disconnect_();
120 }
121}
122
124 if (this->disconnect_delay_ms_ == 0)
125 return;
126 this->cancel_timeout("disconnect");
127 this->set_timeout("disconnect", this->disconnect_delay_ms_, [this]() { this->parent_->set_enabled(false); });
128}
129
130#ifdef USE_TIME
132 if (this->time_ == nullptr)
133 return;
134 if (!this->connection_established_) {
135 ESP_LOGW(TAG, "[%s] Not connected to BLE client. Time can not be synced.", this->parent_->address_str().c_str());
136 return;
137 }
138 if (!this->char_handle_) {
139 ESP_LOGW(TAG, "[%s] No ble handle to BLE client. Time can not be synced.", this->parent_->address_str().c_str());
140 return;
141 }
142 auto time = this->time_->now();
143 if (!time.is_valid()) {
144 ESP_LOGW(TAG, "[%s] Time is not yet valid. Time can not be synced.", this->parent_->address_str().c_str());
145 return;
146 }
147 time.recalc_timestamp_utc(true); // calculate timestamp of local time
148 uint8_t blk[5] = {};
149#if ESP_IDF_VERSION_MAJOR >= 5
150 ESP_LOGD(TAG, "[%s] Sync time with timestamp %" PRIu64 ".", this->parent_->address_str().c_str(), time.timestamp);
151#else
152 ESP_LOGD(TAG, "[%s] Sync time with timestamp %lu.", this->parent_->address_str().c_str(), time.timestamp);
153#endif
154 blk[0] = 0x23;
155 blk[1] = time.timestamp & 0xff;
156 blk[2] = (time.timestamp >> 8) & 0xff;
157 blk[3] = (time.timestamp >> 16) & 0xff;
158 blk[4] = (time.timestamp >> 24) & 0xff;
159 this->send_to_setup_char_(blk, sizeof(blk));
160}
161#endif
162
163} // namespace pvvx_mithermometer
164} // namespace esphome
165
166#endif
uint8_t status
Definition bl0942.h:8
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
Definition component.cpp:79
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:75
void set_enabled(bool enabled)
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
bool has_value() const
Definition optional.h:87
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 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