ESPHome  2024.12.2
bedjet_codec.cpp
Go to the documentation of this file.
1 #include "bedjet_codec.h"
2 #include <cstdio>
3 #include <cstring>
4 
5 namespace esphome {
6 namespace bedjet {
7 
9 float bedjet_temp_to_f(const uint8_t temp) {
10  // BedJet temp is "C*2"; to get F, multiply by 0.9 (half 1.8) and add 32.
11  return 0.9f * temp + 32.0f;
12 }
13 
16  // So far no commands require more than 2 bytes of data
17  if (this->packet_.data_length > 2) {
18  ESP_LOGW(TAG, "Packet may be malformed");
19  }
20  for (int i = this->packet_.data_length; i < 2; i++) {
21  this->packet_.data[i] = '\0';
22  }
23  ESP_LOGV(TAG, "Created packet: %02X, %02X %02X", this->packet_.command, this->packet_.data[0], this->packet_.data[1]);
24  return &this->packet_;
25 }
26 
29  this->packet_.command = CMD_BUTTON;
30  this->packet_.data_length = 1;
31  this->packet_.data[0] = button;
32  return this->clean_packet_();
33 }
34 
38  this->packet_.data_length = 1;
39  this->packet_.data[0] = temperature * 2;
40  return this->clean_packet_();
41 }
42 
45  this->packet_.command = CMD_SET_FAN;
46  this->packet_.data_length = 1;
47  this->packet_.data[0] = fan_step;
48  return this->clean_packet_();
49 }
50 
54  this->packet_.data_length = 2;
55  this->packet_.data[0] = hour;
56  this->packet_.data[1] = minute;
57  return this->clean_packet_();
58 }
59 
63  this->packet_.data_length = 2;
64  this->packet_.data[0] = hour;
65  this->packet_.data[1] = minute;
66  return this->clean_packet_();
67 }
68 
70 void BedjetCodec::decode_extra(const uint8_t *data, uint16_t length) {
71  ESP_LOGVV(TAG, "Received extra: %d bytes: %d %d %d %d", length, data[1], data[2], data[3], data[4]);
72  uint8_t offset = this->last_buffer_size_;
73  if (offset > 0 && length + offset <= sizeof(BedjetStatusPacket)) {
74  memcpy(((uint8_t *) (&this->buf_)) + offset, data, length);
75  ESP_LOGVV(TAG,
76  "Extra bytes: skip1=0x%08x, skip2=0x%04x, skip3=0x%02x; update phase=0x%02x, "
77  "flags=BedjetFlags <conn=%c, leds=%c, units=%c, mute=%c; packed=%02x>",
78  this->buf_.unused_1, this->buf_.unused_2, this->buf_.unused_3, this->buf_.update_phase,
79  this->buf_.flags.conn_test_passed ? '1' : '0', this->buf_.flags.leds_enabled ? '1' : '0',
80  this->buf_.flags.units_setup ? '1' : '0', this->buf_.flags.beeps_muted ? '1' : '0',
81  this->buf_.flags_packed);
82  } else {
83  ESP_LOGI(TAG, "Could not determine where to append to, last offset=%d, max size=%u, new size would be %d", offset,
84  sizeof(BedjetStatusPacket), length + offset);
85  }
86 }
87 
92 bool BedjetCodec::decode_notify(const uint8_t *data, uint16_t length) {
93  ESP_LOGV(TAG, "Received: %d bytes: %d %d %d %d", length, data[1], data[2], data[3], data[4]);
94 
95  if (data[1] == PACKET_FORMAT_V3_HOME && data[3] == PACKET_TYPE_STATUS) {
96  // Clear old buffer
97  memset(&this->buf_, 0, sizeof(BedjetStatusPacket));
98  // Copy new data into buffer
99  memcpy(&this->buf_, data, length);
100  this->last_buffer_size_ = length;
101 
102  // TODO: validate the packet checksum?
103  if (this->buf_.mode < 7 && this->buf_.target_temp_step >= 38 && this->buf_.target_temp_step <= 86 &&
104  this->buf_.actual_temp_step > 1 && this->buf_.actual_temp_step <= 100 && this->buf_.ambient_temp_step > 1 &&
105  this->buf_.ambient_temp_step <= 100) {
106  // and save it for the update() loop
107  this->status_packet_ = &this->buf_;
108  return this->buf_.is_partial;
109  } else {
110  this->status_packet_ = nullptr;
111  // TODO: log a warning if we detect that we connected to a non-V3 device.
112  ESP_LOGW(TAG, "Received potentially invalid packet (len %d):", length);
113  }
114  } else if (data[1] == PACKET_FORMAT_DEBUG || data[3] == PACKET_TYPE_DEBUG) {
115  // We don't actually know the packet format for this. Dump packets to log, in case a pattern presents itself.
116  ESP_LOGVV(TAG,
117  "received DEBUG packet: set1=%01fF, set2=%01fF, air=%01fF; [7]=%d, [8]=%d, [9]=%d, [10]=%d, [11]=%d, "
118  "[12]=%d, [-1]=%d",
119  bedjet_temp_to_f(data[4]), bedjet_temp_to_f(data[5]), bedjet_temp_to_f(data[6]), data[7], data[8],
120  data[9], data[10], data[11], data[12], data[length - 1]);
121 
122  if (this->has_status()) {
123  this->status_packet_->ambient_temp_step = data[6];
124  }
125  } else {
126  // TODO: log a warning if we detect that we connected to a non-V3 device.
127  }
128 
129  return false;
130 }
131 
133 bool BedjetCodec::compare(const uint8_t *data, uint16_t length) {
134  if (data == nullptr) {
135  return false;
136  }
137 
138  if (length < 17) {
139  // New packet looks small, skip it.
140  return false;
141  }
142 
144  this->buf_.packet_type != PACKET_TYPE_STATUS) { // No last seen packet, so take the new one.
145  return true;
146  }
147 
148  if (data[1] != PACKET_FORMAT_V3_HOME || data[3] != PACKET_TYPE_STATUS) { // New packet is not a v3 status, skip it.
149  return false;
150  }
151 
152  // Now coerce it to a status packet and compare some key fields
153  const BedjetStatusPacket *test = reinterpret_cast<const BedjetStatusPacket *>(data);
154  // These are fields that will only change due to explicit action.
155  // That is why we do not check ambient or actual temp here, because those are environmental.
156  bool explicit_fields_changed = this->buf_.mode != test->mode || this->buf_.fan_step != test->fan_step ||
157  this->buf_.target_temp_step != test->target_temp_step;
158 
159  return explicit_fields_changed;
160 }
161 
163 float bedjet_temp_to_c(uint8_t temp) {
164  // BedJet temp is "C*2"; to get C, divide by 2.
165  return temp / 2.0f;
166 }
167 
168 } // namespace bedjet
169 } // namespace esphome
BedjetPacketFormat packet_format
BedjetPacketFormat::PACKET_FORMAT_V3_HOME for BedJet V3 status packet format.
Definition: bedjet_codec.h:43
bool compare(const uint8_t *data, uint16_t length)
uint8_t fan_step
BedJet fan speed; value is in the 0-19 range, representing 5% increments (5%-100%): `5 + 5...
Definition: bedjet_codec.h:62
BedjetStatusPacket buf_
Definition: bedjet_codec.h:187
bool is_partial
1 indicates that this is a partial packet, and more data can be read directly from the characteristic...
Definition: bedjet_codec.h:41
BedjetPacket * get_set_target_temp_request(float temperature)
Returns a BedjetPacket that will set the device&#39;s target temperature.
uint8_t ambient_temp_step
Current ambient air temp.
Definition: bedjet_codec.h:73
The format of a BedJet V3 status packet.
Definition: bedjet_codec.h:39
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:59
float bedjet_temp_to_c(uint8_t temp)
Converts a BedJet temp step into degrees Celsius.
uint8_t minute
BedjetPacket * clean_packet_()
Cleans up the packet before sending.
float bedjet_temp_to_f(const uint8_t temp)
Converts a BedJet temp step into degrees Fahrenheit.
Definition: bedjet_codec.cpp:9
uint8_t hour
void decode_extra(const uint8_t *data, uint16_t length)
Decodes the extra bytes that were received after being notified with a partial packet.
BedjetPacket * get_set_fan_speed_request(uint8_t fan_step)
Returns a BedjetPacket that will set the device&#39;s target fan speed.
uint16_t temperature
Definition: sun_gtil2.cpp:26
BedjetPacket * get_button_request(BedjetButton button)
Returns a BedjetPacket that will initiate a BedjetButton press.
BedjetPacket * get_set_time_request(uint8_t hour, uint8_t minute)
Returns a BedjetPacket that will set the device&#39;s current time.
BedjetPacket * get_set_runtime_remaining_request(uint8_t hour, uint8_t minute)
Returns a BedjetPacket that will set the device&#39;s remaining runtime.
uint8_t target_temp_step
Target temp that the BedJet will try to heat to. See actual_temp_step.
Definition: bedjet_codec.h:56
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t fan_step
BedJet fan speed; value is in the 0-19 range, representing 5% increments (5%-100%): `5 + 5...
Definition: bedjet_codec.h:186
BedjetStatusPacket * status_packet_
Definition: bedjet_codec.h:186
bool decode_notify(const uint8_t *data, uint16_t length)
Decodes the incoming status packet received on the BEDJET_STATUS_UUID.