ESPHome  2022.6.3
bedjet_base.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 struct BedjetFlags {
18  /* uint8_t */
19  int a_ : 1; // 0x80
20  int b_ : 1; // 0x40
21  int conn_test_passed : 1;
22  int leds_enabled : 1;
23  int c_ : 1; // 0x08
24  int units_setup : 1;
25  int d_ : 1; // 0x02
26  int beeps_muted : 1;
27 } __attribute__((packed));
28 
29 enum BedjetPacketFormat : uint8_t {
30  PACKET_FORMAT_DEBUG = 0x05, // 5
31  PACKET_FORMAT_V3_HOME = 0x56, // 86
32 };
33 
34 enum BedjetPacketType : uint8_t {
37 };
38 
41  // [0]
42  uint8_t is_partial : 8;
45  uint8_t
49 
50  // [4]
51  uint8_t time_remaining_hrs : 8;
52  uint8_t time_remaining_mins : 8;
53  uint8_t time_remaining_secs : 8;
54 
55  // [7]
56  uint8_t actual_temp_step : 8;
57  uint8_t target_temp_step : 8;
59 
60  // [9]
62 
63  // [10]
64  uint8_t fan_step : 8;
65  uint8_t max_hrs : 8;
67  uint8_t max_mins : 8;
68  uint8_t min_temp_step : 8;
69  uint8_t max_temp_step : 8;
70 
71  // [15-16]
72  uint16_t turbo_time : 16;
73 
74  // [17]
75  uint8_t ambient_temp_step : 8;
76  uint8_t shutdown_reason : 8;
78 
79  // [19-25]; the initial partial packet cuts off here after [19]
80  // Skip 7 bytes?
81  uint32_t _skip_1_ : 32; // Unknown 19-22 = 0x01810112
82 
83  uint16_t _skip_2_ : 16; // Unknown 23-24 = 0x1310
84  uint8_t _skip_3_ : 8; // Unknown 25 = 0x00
85 
86  // [26]
87  // 0x18(24) = "Connection test has completed OK"
88  // 0x1a(26) = "Firmware update is not needed"
89  uint8_t update_phase : 8;
90 
91  // [27]
92  // FIXME: cannot nest packed struct of matching length here?
93  /* BedjetFlags */ uint8_t flags : 8;
94  // [28-31]; 20+11 bytes
95  uint32_t _skip_4_ : 32; // Unknown
96 
97 } __attribute__((packed));
98 
133 class BedjetCodec {
134  public:
135  BedjetPacket *get_button_request(BedjetButton button);
136  BedjetPacket *get_set_target_temp_request(float temperature);
137  BedjetPacket *get_set_fan_speed_request(uint8_t fan_step);
138  BedjetPacket *get_set_time_request(uint8_t hour, uint8_t minute);
139 
140  bool decode_notify(const uint8_t *data, uint16_t length);
141  void decode_extra(const uint8_t *data, uint16_t length);
142 
143  inline bool has_status() { return this->status_packet_.has_value(); }
144  const optional<BedjetStatusPacket> &get_status_packet() const { return this->status_packet_; }
145  void clear_status() { this->status_packet_.reset(); }
146 
147  protected:
148  BedjetPacket *clean_packet_();
149 
150  uint8_t last_buffer_size_ = 0;
151 
153 
156 };
157 
158 } // namespace bedjet
159 } // namespace esphome
uint8_t fan_step
BedJet fan speed; value is in the 0-19 range, representing 5% increments (5%-100%): `5 + 5...
Definition: bedjet_base.h:64
BedjetStatusPacket buf_
Definition: bedjet_base.h:155
uint16_t turbo_time
Time remaining in BedjetMode::MODE_TURBO.
Definition: bedjet_base.h:72
uint8_t max_temp_step
Max temp allowed in mode. See actual_temp_step.
Definition: bedjet_base.h:69
float temperature
Definition: qmp6988.h:71
int beeps_muted
(0x01) Bit is set 1 if the device&#39;s sound output is muted.
Definition: bedjet_base.h:26
uint8_t min_temp_step
Min temp allowed in mode. See actual_temp_step.
Definition: bedjet_base.h:68
uint8_t ambient_temp_step
Current ambient air temp.
Definition: bedjet_base.h:75
uint8_t expecting_length
The expected total length of the status packet after merging the additional packet.
Definition: bedjet_base.h:47
The format of a BedJet V3 status packet.
Definition: bedjet_base.h:40
int conn_test_passed
(0x20) Bit is set 1 if the last connection test passed.
Definition: bedjet_base.h:21
BedjetMode mode
BedJet operating mode.
Definition: bedjet_base.h:61
uint8_t target_temp_step
Target temp that the BedJet will try to heat to. See actual_temp_step.
Definition: bedjet_base.h:99
uint8_t is_partial
1 indicates that this is a partial packet, and more data can be read directly from the characteristic...
Definition: bedjet_base.h:42
uint8_t max_mins
Max minutes of mode runtime.
Definition: bedjet_base.h:67
BedjetPacketType packet_type
Typically BedjetPacketType::PACKET_TYPE_STATUS for BedJet V3 status packet.
Definition: bedjet_base.h:48
uint8_t shutdown_reason
The reason for the last device shutdown.
Definition: bedjet_base.h:118
uint8_t flags
Definition: bedjet_base.h:134
uint8_t time_remaining_mins
Minutes remaining in program runtime.
Definition: bedjet_base.h:52
This class is responsible for encoding command packets and decoding status packets.
Definition: bedjet_base.h:133
int leds_enabled
(0x10) Bit is set 1 if the LEDs on the device are enabled.
Definition: bedjet_base.h:22
uint32_t _skip_4_
See BedjetFlags for the packed byte flags.
Definition: bedjet_base.h:95
optional< BedjetStatusPacket > status_packet_
Definition: bedjet_base.h:154
BedjetPacketFormat packet_format
BedjetPacketFormat::PACKET_FORMAT_V3_HOME for BedJet V3 status packet format.
Definition: bedjet_base.h:85
Definition: a4988.cpp:4
uint8_t time_remaining_secs
Seconds remaining in program runtime.
Definition: bedjet_base.h:53
uint8_t time_remaining_hrs
Hours remaining in program runtime.
Definition: bedjet_base.h:51
uint8_t max_hrs
Max hours of mode runtime.
Definition: bedjet_base.h:107
enum esphome::bedjet::BedjetPacketFormat __attribute__
uint8_t fan_step
BedJet fan speed; value is in the 0-19 range, representing 5% increments (5%-100%): `5 + 5...
Definition: bedjet_base.h:105
uint8_t actual_temp_step
Actual temp of the air blown by the BedJet fan; value represents 2 * degrees_celsius.
Definition: bedjet_base.h:56
uint16_t _skip_2_
Definition: bedjet_base.h:124
uint8_t _skip_3_
Definition: bedjet_base.h:125
int units_setup
(0x04) Bit is set 1 if the device&#39;s units have been configured.
Definition: bedjet_base.h:24
uint8_t update_phase
The current status/phase of a firmware update.
Definition: bedjet_base.h:130
const optional< BedjetStatusPacket > & get_status_packet() const
Definition: bedjet_base.h:144