Fix parsing numbers in Anova (#2816)

This commit is contained in:
Oxan van Leeuwen 2021-11-28 20:00:29 +01:00 committed by Jesse Hills
parent 21db43db06
commit db2128a344
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
3 changed files with 14 additions and 3 deletions

View file

@ -104,21 +104,21 @@ void AnovaCodec::decode(const uint8_t *data, uint16_t length) {
break; break;
} }
case READ_TARGET_TEMPERATURE: { case READ_TARGET_TEMPERATURE: {
this->target_temp_ = parse_number<float>(buf, sizeof(buf)).value_or(0.0f); this->target_temp_ = parse_number<float>(str_until(buf, '\r')).value_or(0.0f);
if (this->fahrenheit_) if (this->fahrenheit_)
this->target_temp_ = ftoc(this->target_temp_); this->target_temp_ = ftoc(this->target_temp_);
this->has_target_temp_ = true; this->has_target_temp_ = true;
break; break;
} }
case SET_TARGET_TEMPERATURE: { case SET_TARGET_TEMPERATURE: {
this->target_temp_ = parse_number<float>(buf, sizeof(buf)).value_or(0.0f); this->target_temp_ = parse_number<float>(str_until(buf, '\r')).value_or(0.0f);
if (this->fahrenheit_) if (this->fahrenheit_)
this->target_temp_ = ftoc(this->target_temp_); this->target_temp_ = ftoc(this->target_temp_);
this->has_target_temp_ = true; this->has_target_temp_ = true;
break; break;
} }
case READ_CURRENT_TEMPERATURE: { case READ_CURRENT_TEMPERATURE: {
this->current_temp_ = parse_number<float>(buf, sizeof(buf)).value_or(0.0f); this->current_temp_ = parse_number<float>(str_until(buf, '\r')).value_or(0.0f);
if (this->fahrenheit_) if (this->fahrenheit_)
this->current_temp_ = ftoc(this->current_temp_); this->current_temp_ = ftoc(this->current_temp_);
this->has_current_temp_ = true; this->has_current_temp_ = true;

View file

@ -444,6 +444,11 @@ IRAM_ATTR InterruptLock::~InterruptLock() { portENABLE_INTERRUPTS(); }
std::string str_truncate(const std::string &str, size_t length) { std::string str_truncate(const std::string &str, size_t length) {
return str.length() > length ? str.substr(0, length) : str; return str.length() > length ? str.substr(0, length) : str;
} }
std::string str_until(const char *str, char ch) {
char *pos = strchr(str, ch);
return pos == nullptr ? std::string(str) : std::string(str, pos - str);
}
std::string str_until(const std::string &str, char ch) { return str.substr(0, str.find(ch)); }
std::string str_snake_case(const std::string &str) { std::string str_snake_case(const std::string &str) {
std::string result; std::string result;
result.resize(str.length()); result.resize(str.length());

View file

@ -351,6 +351,12 @@ template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> constexpr
/// Truncate a string to a specific length. /// Truncate a string to a specific length.
std::string str_truncate(const std::string &str, size_t length); std::string str_truncate(const std::string &str, size_t length);
/// Extract the part of the string until either the first occurence of the specified character, or the end (requires str
/// to be null-terminated).
std::string str_until(const char *str, char ch);
/// Extract the part of the string until either the first occurence of the specified character, or the end.
std::string str_until(const std::string &str, char ch);
/// Convert the string to snake case (lowercase with underscores). /// Convert the string to snake case (lowercase with underscores).
std::string str_snake_case(const std::string &str); std::string str_snake_case(const std::string &str);