ESPHome  2024.12.2
bedjet_codec.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/helpers.h"
4 #include "esphome/core/log.h"
5 
6 #include "bedjet_const.h"
7 
8 namespace esphome {
9 namespace bedjet {
10 
11 struct BedjetPacket {
12  uint8_t data_length;
14  uint8_t data[2];
15 };
16 
17 enum BedjetPacketFormat : uint8_t {
18  PACKET_FORMAT_DEBUG = 0x05, // 5
19  PACKET_FORMAT_V3_HOME = 0x56, // 86
20 };
21 
22 enum BedjetPacketType : uint8_t {
25 };
26 
27 enum BedjetNotification : uint8_t {
34  // Note: after handling a notification, send MAGIC_NOTIFY_ACK
36 };
37 
40  // [0]
41  bool is_partial : 8;
44  uint8_t expecting_length : 8;
47 
48  // [4]
49  uint8_t time_remaining_hrs : 8;
50  uint8_t time_remaining_mins : 8;
51  uint8_t time_remaining_secs : 8;
52 
53  // [7]
54  uint8_t actual_temp_step : 8;
55  uint8_t target_temp_step : 8;
57 
58  // [9]
60 
61  // [10]
62  uint8_t fan_step : 8;
63  uint8_t max_hrs : 8;
65  uint8_t max_mins : 8;
66  uint8_t min_temp_step : 8;
67  uint8_t max_temp_step : 8;
68 
69  // [15-16]
70  uint16_t turbo_time : 16;
71 
72  // [17]
73  uint8_t ambient_temp_step : 8;
74  uint8_t shutdown_reason : 8;
76 
77  // [19-25]; the initial partial packet cuts off here after [19]
78 
79  uint8_t unused_1 : 8; // Unknown [19] = 0x01
80  uint8_t unused_2 : 8; // Unknown [20] = 0x81
81  uint8_t unused_3 : 8; // Unknown [21] = 0x01
82 
83  // [22]: 0x2=is_dual_zone, ...?
84  struct {
85  int unused_1 : 1; // 0x80
86  int unused_2 : 1; // 0x40
87  int unused_3 : 1; // 0x20
88  int unused_4 : 1; // 0x10
89  int unused_5 : 1; // 0x8
90  int unused_6 : 1; // 0x4
91  bool is_dual_zone : 1;
92  int unused_7 : 1; // 0x1
93  } dual_zone_flags; // NOLINT(clang-diagnostic-unaligned-access)
94 
95  uint8_t unused_4 : 8; // Unknown 23-24 = 0x1310
96  uint8_t unused_5 : 8; // Unknown 23-24 = 0x1310
97  uint8_t unused_6 : 8; // Unknown 25 = 0x00
98 
99  // [26]
100  // 0x18(24) = "Connection test has completed OK"
101  // 0x1a(26) = "Firmware update is not needed"
102  uint8_t update_phase : 8;
103 
104  // [27]
105  union {
106  uint8_t flags_packed;
107  struct {
108  /* uint8_t */
109  int unused_1 : 1; // 0x80
110  int unused_2 : 1; // 0x40
111  bool conn_test_passed : 1;
112  bool leds_enabled : 1;
113  int unused_3 : 1; // 0x08
114  bool units_setup : 1;
115  int unused_4 : 1; // 0x02
116  bool beeps_muted : 1;
117  } __attribute__((packed)) flags;
118  };
119 
120  // [28] = (biorhythm?) sequence step
121  uint8_t bio_sequence_step : 8;
122  // [29] = notify_code:
124 
125  uint16_t unused_7 : 16; // Unknown
126 
127 } __attribute__((packed));
128 
163 class BedjetCodec {
164  public:
165  BedjetPacket *get_button_request(BedjetButton button);
166  BedjetPacket *get_set_target_temp_request(float temperature);
167  BedjetPacket *get_set_fan_speed_request(uint8_t fan_step);
168  BedjetPacket *get_set_time_request(uint8_t hour, uint8_t minute);
169  BedjetPacket *get_set_runtime_remaining_request(uint8_t hour, uint8_t minute);
171  bool decode_notify(const uint8_t *data, uint16_t length);
172  void decode_extra(const uint8_t *data, uint16_t length);
173  bool compare(const uint8_t *data, uint16_t length);
175  inline bool has_status() { return this->status_packet_ != nullptr; }
176  const BedjetStatusPacket *get_status_packet() const { return this->status_packet_; }
177  void clear_status() { this->status_packet_ = nullptr; }
179  protected:
180  BedjetPacket *clean_packet_();
181 
182  uint8_t last_buffer_size_ = 0;
185 
186  BedjetStatusPacket *status_packet_;
188 };
191 float bedjet_temp_to_c(uint8_t temp);
192 
193 } // namespace bedjet
194 } // namespace esphome
int unused_1
Definition: bedjet_codec.h:108
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
uint16_t unused_7
See BedjetNotification.
Definition: bedjet_codec.h:125
uint8_t shutdown_reason
The reason for the last device shutdown.
Definition: bedjet_codec.h:199
BedjetStatusPacket buf_
Definition: bedjet_codec.h:187
uint16_t turbo_time
Time remaining in BedjetMode::MODE_TURBO.
Definition: bedjet_codec.h:70
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
uint8_t target_temp_step
Target temp that the BedJet will try to heat to. See actual_temp_step.
Definition: bedjet_codec.h:180
No notification pending.
Definition: bedjet_codec.h:28
The specified sequence cannot be run because the clock is not set.
Definition: bedjet_codec.h:32
uint8_t max_hrs
Max hours of mode runtime.
Definition: bedjet_codec.h:188
class esphome::bedjet::BedjetCodec __attribute__
uint8_t max_temp_step
Max temp allowed in mode. See actual_temp_step.
Definition: bedjet_codec.h:67
uint8_t update_phase
The current status/phase of a firmware update.
Definition: bedjet_codec.h:102
uint8_t min_temp_step
Min temp allowed in mode. See actual_temp_step.
Definition: bedjet_codec.h:66
uint8_t ambient_temp_step
Current ambient air temp.
Definition: bedjet_codec.h:73
Firmware Update / A newer version of firmware is available.
Definition: bedjet_codec.h:30
bool conn_test_passed
(0x20) Bit is set 1 if the last connection test passed.
Definition: bedjet_codec.h:110
Clean Filter / Please check BedJet air filter and clean if necessary.
Definition: bedjet_codec.h:29
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
BedjetPacketFormat packet_format
BedjetPacketFormat::PACKET_FORMAT_V3_HOME for BedJet V3 status packet format.
Definition: bedjet_codec.h:167
bool beeps_muted
(0x01) Bit is set 1 if the device's sound output is muted.
Definition: bedjet_codec.h:116
int unused_4
Definition: bedjet_codec.h:114
uint8_t max_mins
Max minutes of mode runtime.
Definition: bedjet_codec.h:65
BedjetPacketType packet_type
Typically BedjetPacketType::PACKET_TYPE_STATUS for BedJet V3 status packet.
Definition: bedjet_codec.h:46
bool units_setup
(0x04) Bit is set 1 if the device's units have been configured.
Definition: bedjet_codec.h:113
uint8_t hour
uint8_t expecting_length
The expected total length of the status packet after merging the extra packet.
Definition: bedjet_codec.h:169
int unused_7
Is part of a Dual Zone configuration.
Definition: bedjet_codec.h:92
uint16_t temperature
Definition: sun_gtil2.cpp:26
uint8_t time_remaining_mins
Minutes remaining in program runtime.
Definition: bedjet_codec.h:50
int unused_3
Definition: bedjet_codec.h:112
bool leds_enabled
(0x10) Bit is set 1 if the LEDs on the device are enabled.
Definition: bedjet_codec.h:111
This class is responsible for encoding command packets and decoding status packets.
Definition: bedjet_codec.h:163
const uint32_t flags
Definition: stm32flash.h:85
const BedjetStatusPacket * get_status_packet() const
Definition: bedjet_codec.h:176
Firmware Update / Unable to connect to the firmware update server.
Definition: bedjet_codec.h:31
int unused_2
Definition: bedjet_codec.h:109
struct @9 dual_zone_flags
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t time_remaining_secs
Seconds remaining in program runtime.
Definition: bedjet_codec.h:51
uint8_t time_remaining_hrs
Hours remaining in program runtime.
Definition: bedjet_codec.h:49
uint8_t actual_temp_step
Actual temp of the air blown by the BedJet fan; value represents 2 * degrees_celsius.
Definition: bedjet_codec.h:54
The specified sequence cannot be run because it contains steps that would be too long running from th...
Definition: bedjet_codec.h:33
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
BedjetNotification notify_code
Biorhythm sequence step number.
Definition: bedjet_codec.h:123