ESPHome  2024.10.2
anova_base.cpp
Go to the documentation of this file.
1 #include "anova_base.h"
2 #include <cstdio>
3 #include <cstring>
4 
5 namespace esphome {
6 namespace anova {
7 
8 float ftoc(float f) { return (f - 32.0) * (5.0f / 9.0f); }
9 
10 float ctof(float c) { return (c * 9.0f / 5.0f) + 32.0; }
11 
13  this->packet_.length = strlen((char *) this->packet_.data);
14  this->packet_.data[this->packet_.length] = '\0';
15  ESP_LOGV("anova", "SendPkt: %s\n", this->packet_.data);
16  return &this->packet_;
17 }
18 
21  sprintf((char *) this->packet_.data, "%s", CMD_READ_DEVICE_STATUS);
22  return this->clean_packet_();
23 }
24 
27  sprintf((char *) this->packet_.data, "%s", CMD_READ_TARGET_TEMP);
28  return this->clean_packet_();
29 }
30 
33  sprintf((char *) this->packet_.data, "%s", CMD_READ_CURRENT_TEMP);
34  return this->clean_packet_();
35 }
36 
38  this->current_query_ = READ_UNIT;
39  sprintf((char *) this->packet_.data, "%s", CMD_READ_UNIT);
40  return this->clean_packet_();
41 }
42 
44  this->current_query_ = READ_DATA;
45  sprintf((char *) this->packet_.data, "%s", CMD_READ_DATA);
46  return this->clean_packet_();
47 }
48 
51  if (this->fahrenheit_)
52  temperature = ctof(temperature);
53  sprintf((char *) this->packet_.data, CMD_SET_TARGET_TEMP, temperature);
54  return this->clean_packet_();
55 }
56 
58  this->current_query_ = SET_UNIT;
59  sprintf((char *) this->packet_.data, CMD_SET_TEMP_UNIT, unit);
60  return this->clean_packet_();
61 }
62 
64  this->current_query_ = START;
65  sprintf((char *) this->packet_.data, CMD_START);
66  return this->clean_packet_();
67 }
68 
70  this->current_query_ = STOP;
71  sprintf((char *) this->packet_.data, CMD_STOP);
72  return this->clean_packet_();
73 }
74 
75 void AnovaCodec::decode(const uint8_t *data, uint16_t length) {
76  char buf[32];
77  memset(buf, 0, sizeof(buf));
78  strncpy(buf, (char *) data, std::min<uint16_t>(length, sizeof(buf) - 1));
79  this->has_target_temp_ = this->has_current_temp_ = this->has_unit_ = this->has_running_ = false;
80  switch (this->current_query_) {
81  case READ_DEVICE_STATUS: {
82  if (!strncmp(buf, "stopped", 7)) {
83  this->has_running_ = true;
84  this->running_ = false;
85  }
86  if (!strncmp(buf, "running", 7)) {
87  this->has_running_ = true;
88  this->running_ = true;
89  }
90  break;
91  }
92  case START: {
93  if (!strncmp(buf, "start", 5)) {
94  this->has_running_ = true;
95  this->running_ = true;
96  }
97  break;
98  }
99  case STOP: {
100  if (!strncmp(buf, "stop", 4)) {
101  this->has_running_ = true;
102  this->running_ = false;
103  }
104  break;
105  }
107  case SET_TARGET_TEMPERATURE: {
108  this->target_temp_ = parse_number<float>(str_until(buf, '\r')).value_or(0.0f);
109  if (this->fahrenheit_)
110  this->target_temp_ = ftoc(this->target_temp_);
111  this->has_target_temp_ = true;
112  break;
113  }
115  this->current_temp_ = parse_number<float>(str_until(buf, '\r')).value_or(0.0f);
116  if (this->fahrenheit_)
117  this->current_temp_ = ftoc(this->current_temp_);
118  this->has_current_temp_ = true;
119  break;
120  }
121  case SET_UNIT:
122  case READ_UNIT: {
123  this->unit_ = buf[0];
124  this->fahrenheit_ = buf[0] == 'f';
125  this->has_unit_ = true;
126  break;
127  }
128  default:
129  break;
130  }
131 }
132 
133 } // namespace anova
134 } // namespace esphome
AnovaPacket * get_read_data_request()
Definition: anova_base.cpp:43
std::string str_until(const char *str, char ch)
Extract the part of the string until either the first occurrence of the specified character...
Definition: helpers.cpp:264
CurrentQuery current_query_
Definition: anova_base.h:75
AnovaPacket * get_read_current_temp_request()
Definition: anova_base.cpp:31
AnovaPacket * get_set_target_temp_request(float temperature)
Definition: anova_base.cpp:49
float ctof(float c)
Definition: anova_base.cpp:10
AnovaPacket * get_start_request()
Definition: anova_base.cpp:63
AnovaPacket * get_stop_request()
Definition: anova_base.cpp:69
uint16_t temperature
Definition: sun_gtil2.cpp:26
AnovaPacket * get_read_unit_request()
Definition: anova_base.cpp:37
AnovaPacket * get_set_unit_request(char unit)
Definition: anova_base.cpp:57
AnovaPacket * get_read_device_status_request()
Definition: anova_base.cpp:19
AnovaPacket * get_read_target_temp_request()
Definition: anova_base.cpp:25
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
AnovaPacket * clean_packet_()
Definition: anova_base.cpp:12
void decode(const uint8_t *data, uint16_t length)
Definition: anova_base.cpp:75
float ftoc(float f)
Definition: anova_base.cpp:8