mirror of
https://github.com/esphome/esphome.git
synced 2024-12-28 08:21:44 +01:00
Merge remote-tracking branch 'origin/dev' into platform
This commit is contained in:
commit
f52e76381b
44 changed files with 1107 additions and 482 deletions
|
@ -75,6 +75,9 @@ target/
|
|||
# pyenv
|
||||
.python-version
|
||||
|
||||
# asdf
|
||||
.tool-versions
|
||||
|
||||
# celery beat schedule file
|
||||
celerybeat-schedule
|
||||
|
||||
|
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -75,6 +75,9 @@ cov.xml
|
|||
# pyenv
|
||||
.python-version
|
||||
|
||||
# asdf
|
||||
.tool-versions
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
|
|
|
@ -85,6 +85,7 @@ esphome/components/bmp581/* @kahrendt
|
|||
esphome/components/bp1658cj/* @Cossid
|
||||
esphome/components/bp5758d/* @Cossid
|
||||
esphome/components/button/* @esphome/core
|
||||
esphome/components/bytebuffer/* @clydebarrow
|
||||
esphome/components/canbus/* @danielschramm @mvturnho
|
||||
esphome/components/cap1188/* @mreditor97
|
||||
esphome/components/captive_portal/* @OttoWinter
|
||||
|
|
5
esphome/components/bytebuffer/__init__.py
Normal file
5
esphome/components/bytebuffer/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
CODEOWNERS = ["@clydebarrow"]
|
||||
|
||||
# Allows bytebuffer to be configured in yaml, to allow use of the C++ api.
|
||||
|
||||
CONFIG_SCHEMA = {}
|
421
esphome/components/bytebuffer/bytebuffer.h
Normal file
421
esphome/components/bytebuffer/bytebuffer.h
Normal file
|
@ -0,0 +1,421 @@
|
|||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <cinttypes>
|
||||
#include <cstddef>
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace bytebuffer {
|
||||
|
||||
enum Endian { LITTLE, BIG };
|
||||
|
||||
/**
|
||||
* A class modelled on the Java ByteBuffer class. It wraps a vector of bytes and permits putting and getting
|
||||
* items of various sizes, with an automatically incremented position.
|
||||
*
|
||||
* There are three variables maintained pointing into the buffer:
|
||||
*
|
||||
* capacity: the maximum amount of data that can be stored - set on construction and cannot be changed
|
||||
* limit: the limit of the data currently available to get or put
|
||||
* position: the current insert or extract position
|
||||
*
|
||||
* 0 <= position <= limit <= capacity
|
||||
*
|
||||
* In addition a mark can be set to the current position with mark(). A subsequent call to reset() will restore
|
||||
* the position to the mark.
|
||||
*
|
||||
* The buffer can be marked to be little-endian (default) or big-endian. All subsequent operations will use that order.
|
||||
*
|
||||
* The flip() operation will reset the position to 0 and limit to the current position. This is useful for reading
|
||||
* data from a buffer after it has been written.
|
||||
*
|
||||
* The code is defined here in the header file rather than in a .cpp file, so that it does not get compiled if not used.
|
||||
* The templated functions ensure that only those typed functions actually used are compiled. The functions
|
||||
* are implicitly inline-able which will aid performance.
|
||||
*/
|
||||
class ByteBuffer {
|
||||
public:
|
||||
// Default constructor (compatibility with TEMPLATABLE_VALUE)
|
||||
// Creates a zero-length ByteBuffer which is little use to anybody.
|
||||
ByteBuffer() : ByteBuffer(std::vector<uint8_t>()) {}
|
||||
|
||||
/**
|
||||
* Create a new Bytebuffer with the given capacity
|
||||
*/
|
||||
ByteBuffer(size_t capacity, Endian endianness = LITTLE)
|
||||
: data_(std::vector<uint8_t>(capacity)), endianness_(endianness), limit_(capacity){};
|
||||
|
||||
// templated functions to implement putting and getting data of various types. There are two flavours of all
|
||||
// functions - one that uses the position as the offset, and updates the position accordingly, and one that
|
||||
// takes an explicit offset and does not update the position.
|
||||
// Separate temnplates are provided for types that fit into 32 bits and those that are bigger. These delegate
|
||||
// the actual put/get to common code based around those sizes.
|
||||
// This reduces the code size and execution time for smaller types. A similar structure for e.g. 16 bits is unlikely
|
||||
// to provide any further benefit given that all target platforms are native 32 bit.
|
||||
|
||||
template<typename T>
|
||||
T get(typename std::enable_if<std::is_integral<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) <= sizeof(uint32_t)), T>::type * = 0) {
|
||||
// integral types that fit into 32 bit
|
||||
return static_cast<T>(this->get_uint32_(sizeof(T)));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get(size_t offset, typename std::enable_if<std::is_integral<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) <= sizeof(uint32_t)), T>::type * = 0) {
|
||||
return static_cast<T>(this->get_uint32_(offset, sizeof(T)));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void put(const T &value, typename std::enable_if<std::is_integral<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) <= sizeof(uint32_t)), T>::type * = 0) {
|
||||
this->put_uint32_(static_cast<uint32_t>(value), sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void put(const T &value, size_t offset, typename std::enable_if<std::is_integral<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) <= sizeof(uint32_t)), T>::type * = 0) {
|
||||
this->put_uint32_(static_cast<uint32_t>(value), offset, sizeof(T));
|
||||
}
|
||||
|
||||
// integral types that do not fit into 32 bit (basically only 64 bit types)
|
||||
template<typename T>
|
||||
T get(typename std::enable_if<std::is_integral<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
|
||||
return static_cast<T>(this->get_uint64_(sizeof(T)));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get(size_t offset, typename std::enable_if<std::is_integral<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
|
||||
return static_cast<T>(this->get_uint64_(offset, sizeof(T)));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void put(const T &value, typename std::enable_if<std::is_integral<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
|
||||
this->put_uint64_(value, sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void put(const T &value, size_t offset, typename std::enable_if<std::is_integral<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
|
||||
this->put_uint64_(static_cast<uint64_t>(value), offset, sizeof(T));
|
||||
}
|
||||
|
||||
// floating point types. Caters for 32 and 64 bit floating point.
|
||||
template<typename T>
|
||||
T get(typename std::enable_if<std::is_floating_point<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) == sizeof(uint32_t)), T>::type * = 0) {
|
||||
return bit_cast<T>(this->get_uint32_(sizeof(T)));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get(typename std::enable_if<std::is_floating_point<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
|
||||
return bit_cast<T>(this->get_uint64_(sizeof(T)));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get(size_t offset, typename std::enable_if<std::is_floating_point<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) == sizeof(uint32_t)), T>::type * = 0) {
|
||||
return bit_cast<T>(this->get_uint32_(offset, sizeof(T)));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get(size_t offset, typename std::enable_if<std::is_floating_point<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
|
||||
return bit_cast<T>(this->get_uint64_(offset, sizeof(T)));
|
||||
}
|
||||
template<typename T>
|
||||
void put(const T &value, typename std::enable_if<std::is_floating_point<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) <= sizeof(uint32_t)), T>::type * = 0) {
|
||||
this->put_uint32_(bit_cast<uint32_t>(value), sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void put(const T &value, typename std::enable_if<std::is_floating_point<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
|
||||
this->put_uint64_(bit_cast<uint64_t>(value), sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void put(const T &value, size_t offset, typename std::enable_if<std::is_floating_point<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) <= sizeof(uint32_t)), T>::type * = 0) {
|
||||
this->put_uint32_(bit_cast<uint32_t>(value), offset, sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void put(const T &value, size_t offset, typename std::enable_if<std::is_floating_point<T>::value, T>::type * = 0,
|
||||
typename std::enable_if<(sizeof(T) == sizeof(uint64_t)), T>::type * = 0) {
|
||||
this->put_uint64_(bit_cast<uint64_t>(value), offset, sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T> static ByteBuffer wrap(T value, Endian endianness = LITTLE) {
|
||||
ByteBuffer buffer = ByteBuffer(sizeof(T), endianness);
|
||||
buffer.put(value);
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static ByteBuffer wrap(std::vector<uint8_t> const &data, Endian endianness = LITTLE) {
|
||||
ByteBuffer buffer = {data};
|
||||
buffer.endianness_ = endianness;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static ByteBuffer wrap(const uint8_t *ptr, size_t len, Endian endianness = LITTLE) {
|
||||
return wrap(std::vector<uint8_t>(ptr, ptr + len), endianness);
|
||||
}
|
||||
|
||||
// convenience functions with explicit types named..
|
||||
void put_float(float value) { this->put(value); }
|
||||
void put_double(double value) { this->put(value); }
|
||||
|
||||
uint8_t get_uint8() { return this->data_[this->position_++]; }
|
||||
// Get a 16 bit unsigned value, increment by 2
|
||||
uint16_t get_uint16() { return this->get<uint16_t>(); }
|
||||
// Get a 24 bit unsigned value, increment by 3
|
||||
uint32_t get_uint24() { return this->get_uint32_(3); };
|
||||
// Get a 32 bit unsigned value, increment by 4
|
||||
uint32_t get_uint32() { return this->get<uint32_t>(); };
|
||||
// Get a 64 bit unsigned value, increment by 8
|
||||
uint64_t get_uint64() { return this->get<uint64_t>(); };
|
||||
// Signed versions of the get functions
|
||||
uint8_t get_int8() { return static_cast<int8_t>(this->get_uint8()); };
|
||||
int16_t get_int16() { return this->get<uint16_t>(); }
|
||||
int32_t get_int32() { return this->get<int32_t>(); }
|
||||
int64_t get_int64() { return this->get<int64_t>(); }
|
||||
// Get a float value, increment by 4
|
||||
float get_float() { return this->get<float>(); }
|
||||
// Get a double value, increment by 8
|
||||
double get_double() { return this->get<double>(); }
|
||||
|
||||
// Get a bool value, increment by 1
|
||||
bool get_bool() { return static_cast<bool>(this->get_uint8()); }
|
||||
|
||||
uint32_t get_int24(size_t offset) {
|
||||
auto value = this->get_uint24(offset);
|
||||
uint32_t mask = (~static_cast<uint32_t>(0)) << 23;
|
||||
if ((value & mask) != 0)
|
||||
value |= mask;
|
||||
return value;
|
||||
}
|
||||
|
||||
uint32_t get_int24() {
|
||||
auto value = this->get_uint24();
|
||||
uint32_t mask = (~static_cast<uint32_t>(0)) << 23;
|
||||
if ((value & mask) != 0)
|
||||
value |= mask;
|
||||
return value;
|
||||
}
|
||||
std::vector<uint8_t> get_vector(size_t length, size_t offset) {
|
||||
auto start = this->data_.begin() + offset;
|
||||
return {start, start + length};
|
||||
}
|
||||
|
||||
std::vector<uint8_t> get_vector(size_t length) {
|
||||
auto result = this->get_vector(length, this->position_);
|
||||
this->position_ += length;
|
||||
return result;
|
||||
}
|
||||
|
||||
// Convenience named functions
|
||||
void put_uint8(uint8_t value) { this->data_[this->position_++] = value; }
|
||||
void put_uint16(uint16_t value) { this->put(value); }
|
||||
void put_uint24(uint32_t value) { this->put_uint32_(value, 3); }
|
||||
void put_uint32(uint32_t value) { this->put(value); }
|
||||
void put_uint64(uint64_t value) { this->put(value); }
|
||||
// Signed versions of the put functions
|
||||
void put_int8(int8_t value) { this->put_uint8(static_cast<uint8_t>(value)); }
|
||||
void put_int16(int16_t value) { this->put(value); }
|
||||
void put_int24(int32_t value) { this->put_uint32_(value, 3); }
|
||||
void put_int32(int32_t value) { this->put(value); }
|
||||
void put_int64(int64_t value) { this->put(value); }
|
||||
// Extra put functions
|
||||
void put_bool(bool value) { this->put_uint8(value); }
|
||||
|
||||
// versions of the above with an offset, these do not update the position
|
||||
|
||||
uint64_t get_uint64(size_t offset) { return this->get<uint64_t>(offset); }
|
||||
uint32_t get_uint24(size_t offset) { return this->get_uint32_(offset, 3); };
|
||||
double get_double(size_t offset) { return get<double>(offset); }
|
||||
|
||||
// Get one byte from the buffer, increment position by 1
|
||||
uint8_t get_uint8(size_t offset) { return this->data_[offset]; }
|
||||
// Get a 16 bit unsigned value, increment by 2
|
||||
uint16_t get_uint16(size_t offset) { return get<uint16_t>(offset); }
|
||||
// Get a 24 bit unsigned value, increment by 3
|
||||
uint32_t get_uint32(size_t offset) { return this->get<uint32_t>(offset); };
|
||||
// Get a 64 bit unsigned value, increment by 8
|
||||
uint8_t get_int8(size_t offset) { return get<int8_t>(offset); }
|
||||
int16_t get_int16(size_t offset) { return get<int16_t>(offset); }
|
||||
int32_t get_int32(size_t offset) { return get<int32_t>(offset); }
|
||||
int64_t get_int64(size_t offset) { return get<int64_t>(offset); }
|
||||
// Get a float value, increment by 4
|
||||
float get_float(size_t offset) { return get<float>(offset); }
|
||||
// Get a double value, increment by 8
|
||||
|
||||
// Get a bool value, increment by 1
|
||||
bool get_bool(size_t offset) { return this->get_uint8(offset); }
|
||||
|
||||
void put_uint8(uint8_t value, size_t offset) { this->data_[offset] = value; }
|
||||
void put_uint16(uint16_t value, size_t offset) { this->put(value, offset); }
|
||||
void put_uint24(uint32_t value, size_t offset) { this->put(value, offset); }
|
||||
void put_uint32(uint32_t value, size_t offset) { this->put(value, offset); }
|
||||
void put_uint64(uint64_t value, size_t offset) { this->put(value, offset); }
|
||||
// Signed versions of the put functions
|
||||
void put_int8(int8_t value, size_t offset) { this->put_uint8(static_cast<uint8_t>(value), offset); }
|
||||
void put_int16(int16_t value, size_t offset) { this->put(value, offset); }
|
||||
void put_int24(int32_t value, size_t offset) { this->put_uint32_(value, offset, 3); }
|
||||
void put_int32(int32_t value, size_t offset) { this->put(value, offset); }
|
||||
void put_int64(int64_t value, size_t offset) { this->put(value, offset); }
|
||||
// Extra put functions
|
||||
void put_float(float value, size_t offset) { this->put(value, offset); }
|
||||
void put_double(double value, size_t offset) { this->put(value, offset); }
|
||||
void put_bool(bool value, size_t offset) { this->put_uint8(value, offset); }
|
||||
void put(const std::vector<uint8_t> &value, size_t offset) {
|
||||
std::copy(value.begin(), value.end(), this->data_.begin() + offset);
|
||||
}
|
||||
void put_vector(const std::vector<uint8_t> &value, size_t offset) { this->put(value, offset); }
|
||||
void put(const std::vector<uint8_t> &value) {
|
||||
this->put_vector(value, this->position_);
|
||||
this->position_ += value.size();
|
||||
}
|
||||
void put_vector(const std::vector<uint8_t> &value) { this->put(value); }
|
||||
|
||||
// Getters
|
||||
|
||||
inline size_t get_capacity() const { return this->data_.size(); }
|
||||
inline size_t get_position() const { return this->position_; }
|
||||
inline size_t get_limit() const { return this->limit_; }
|
||||
inline size_t get_remaining() const { return this->get_limit() - this->get_position(); }
|
||||
inline Endian get_endianness() const { return this->endianness_; }
|
||||
inline void mark() { this->mark_ = this->position_; }
|
||||
inline void big_endian() { this->endianness_ = BIG; }
|
||||
inline void little_endian() { this->endianness_ = LITTLE; }
|
||||
// retrieve a pointer to the underlying data.
|
||||
std::vector<uint8_t> get_data() { return this->data_; };
|
||||
|
||||
void get_bytes(void *dest, size_t length) {
|
||||
std::copy(this->data_.begin() + this->position_, this->data_.begin() + this->position_ + length, (uint8_t *) dest);
|
||||
this->position_ += length;
|
||||
}
|
||||
|
||||
void get_bytes(void *dest, size_t length, size_t offset) {
|
||||
std::copy(this->data_.begin() + offset, this->data_.begin() + offset + length, (uint8_t *) dest);
|
||||
}
|
||||
|
||||
void rewind() { this->position_ = 0; }
|
||||
void reset() { this->position_ = this->mark_; }
|
||||
|
||||
void set_limit(size_t limit) { this->limit_ = limit; }
|
||||
void set_position(size_t position) { this->position_ = position; }
|
||||
void clear() {
|
||||
this->limit_ = this->get_capacity();
|
||||
this->position_ = 0;
|
||||
}
|
||||
void flip() {
|
||||
this->limit_ = this->position_;
|
||||
this->position_ = 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
uint64_t get_uint64_(size_t offset, size_t length) const {
|
||||
uint64_t value = 0;
|
||||
if (this->endianness_ == LITTLE) {
|
||||
offset += length;
|
||||
while (length-- != 0) {
|
||||
value <<= 8;
|
||||
value |= this->data_[--offset];
|
||||
}
|
||||
} else {
|
||||
while (length-- != 0) {
|
||||
value <<= 8;
|
||||
value |= this->data_[offset++];
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
uint64_t get_uint64_(size_t length) {
|
||||
auto result = this->get_uint64_(this->position_, length);
|
||||
this->position_ += length;
|
||||
return result;
|
||||
}
|
||||
uint32_t get_uint32_(size_t offset, size_t length) const {
|
||||
uint32_t value = 0;
|
||||
if (this->endianness_ == LITTLE) {
|
||||
offset += length;
|
||||
while (length-- != 0) {
|
||||
value <<= 8;
|
||||
value |= this->data_[--offset];
|
||||
}
|
||||
} else {
|
||||
while (length-- != 0) {
|
||||
value <<= 8;
|
||||
value |= this->data_[offset++];
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
uint32_t get_uint32_(size_t length) {
|
||||
auto result = this->get_uint32_(this->position_, length);
|
||||
this->position_ += length;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Putters
|
||||
|
||||
void put_uint64_(uint64_t value, size_t length) {
|
||||
this->put_uint64_(value, this->position_, length);
|
||||
this->position_ += length;
|
||||
}
|
||||
void put_uint32_(uint32_t value, size_t length) {
|
||||
this->put_uint32_(value, this->position_, length);
|
||||
this->position_ += length;
|
||||
}
|
||||
|
||||
void put_uint64_(uint64_t value, size_t offset, size_t length) {
|
||||
if (this->endianness_ == LITTLE) {
|
||||
while (length-- != 0) {
|
||||
this->data_[offset++] = static_cast<uint8_t>(value);
|
||||
value >>= 8;
|
||||
}
|
||||
} else {
|
||||
offset += length;
|
||||
while (length-- != 0) {
|
||||
this->data_[--offset] = static_cast<uint8_t>(value);
|
||||
value >>= 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void put_uint32_(uint32_t value, size_t offset, size_t length) {
|
||||
if (this->endianness_ == LITTLE) {
|
||||
while (length-- != 0) {
|
||||
this->data_[offset++] = static_cast<uint8_t>(value);
|
||||
value >>= 8;
|
||||
}
|
||||
} else {
|
||||
offset += length;
|
||||
while (length-- != 0) {
|
||||
this->data_[--offset] = static_cast<uint8_t>(value);
|
||||
value >>= 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
ByteBuffer(std::vector<uint8_t> const &data) : data_(data), limit_(data.size()) {}
|
||||
|
||||
std::vector<uint8_t> data_;
|
||||
Endian endianness_{LITTLE};
|
||||
size_t position_{0};
|
||||
size_t mark_{0};
|
||||
size_t limit_{0};
|
||||
};
|
||||
|
||||
} // namespace bytebuffer
|
||||
} // namespace esphome
|
|
@ -1,3 +1,4 @@
|
|||
import logging
|
||||
from esphome import pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.esp32 import add_idf_sdkconfig_option, get_esp32_variant
|
||||
|
@ -23,6 +24,7 @@ from esphome.const import (
|
|||
CONF_MISO_PIN,
|
||||
CONF_MOSI_PIN,
|
||||
CONF_PAGE_ID,
|
||||
CONF_POLLING_INTERVAL,
|
||||
CONF_RESET_PIN,
|
||||
CONF_SPI,
|
||||
CONF_STATIC_IP,
|
||||
|
@ -30,13 +32,16 @@ from esphome.const import (
|
|||
CONF_TYPE,
|
||||
CONF_USE_ADDRESS,
|
||||
CONF_VALUE,
|
||||
KEY_CORE,
|
||||
KEY_FRAMEWORK_VERSION,
|
||||
)
|
||||
from esphome.core import CORE, coroutine_with_priority
|
||||
from esphome.core import CORE, TimePeriodMilliseconds, coroutine_with_priority
|
||||
import esphome.final_validate as fv
|
||||
|
||||
CONFLICTS_WITH = ["wifi"]
|
||||
DEPENDENCIES = ["esp32"]
|
||||
AUTO_LOAD = ["network"]
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ethernet_ns = cg.esphome_ns.namespace("ethernet")
|
||||
PHYRegister = ethernet_ns.struct("PHYRegister")
|
||||
|
@ -63,6 +68,7 @@ ETHERNET_TYPES = {
|
|||
}
|
||||
|
||||
SPI_ETHERNET_TYPES = ["W5500"]
|
||||
SPI_ETHERNET_DEFAULT_POLLING_INTERVAL = TimePeriodMilliseconds(milliseconds=10)
|
||||
|
||||
emac_rmii_clock_mode_t = cg.global_ns.enum("emac_rmii_clock_mode_t")
|
||||
emac_rmii_clock_gpio_t = cg.global_ns.enum("emac_rmii_clock_gpio_t")
|
||||
|
@ -100,6 +106,24 @@ EthernetComponent = ethernet_ns.class_("EthernetComponent", cg.Component)
|
|||
ManualIP = ethernet_ns.struct("ManualIP")
|
||||
|
||||
|
||||
def _is_framework_spi_polling_mode_supported():
|
||||
# SPI Ethernet without IRQ feature is added in
|
||||
# esp-idf >= (5.3+ ,5.2.1+, 5.1.4) and arduino-esp32 >= 3.0.0
|
||||
framework_version = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
|
||||
if CORE.using_esp_idf:
|
||||
if framework_version >= cv.Version(5, 3, 0):
|
||||
return True
|
||||
if cv.Version(5, 3, 0) > framework_version >= cv.Version(5, 2, 1):
|
||||
return True
|
||||
if cv.Version(5, 2, 0) > framework_version >= cv.Version(5, 1, 4):
|
||||
return True
|
||||
return False
|
||||
if CORE.using_arduino:
|
||||
return framework_version >= cv.Version(3, 0, 0)
|
||||
# fail safe: Unknown framework
|
||||
return False
|
||||
|
||||
|
||||
def _validate(config):
|
||||
if CONF_USE_ADDRESS not in config:
|
||||
if CONF_MANUAL_IP in config:
|
||||
|
@ -107,6 +131,27 @@ def _validate(config):
|
|||
else:
|
||||
use_address = CORE.name + config[CONF_DOMAIN]
|
||||
config[CONF_USE_ADDRESS] = use_address
|
||||
if config[CONF_TYPE] in SPI_ETHERNET_TYPES:
|
||||
if _is_framework_spi_polling_mode_supported():
|
||||
if CONF_POLLING_INTERVAL in config and CONF_INTERRUPT_PIN in config:
|
||||
raise cv.Invalid(
|
||||
f"Cannot specify more than one of {CONF_INTERRUPT_PIN}, {CONF_POLLING_INTERVAL}"
|
||||
)
|
||||
if CONF_POLLING_INTERVAL not in config and CONF_INTERRUPT_PIN not in config:
|
||||
config[CONF_POLLING_INTERVAL] = SPI_ETHERNET_DEFAULT_POLLING_INTERVAL
|
||||
else:
|
||||
if CONF_POLLING_INTERVAL in config:
|
||||
raise cv.Invalid(
|
||||
"In this version of the framework "
|
||||
f"({CORE.target_framework} {CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]}), "
|
||||
f"'{CONF_POLLING_INTERVAL}' is not supported."
|
||||
)
|
||||
if CONF_INTERRUPT_PIN not in config:
|
||||
raise cv.Invalid(
|
||||
"In this version of the framework "
|
||||
f"({CORE.target_framework} {CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]}), "
|
||||
f"'{CONF_INTERRUPT_PIN}' is a required option for [ethernet]."
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
|
@ -157,6 +202,11 @@ SPI_SCHEMA = BASE_SCHEMA.extend(
|
|||
cv.Optional(CONF_CLOCK_SPEED, default="26.67MHz"): cv.All(
|
||||
cv.frequency, cv.int_range(int(8e6), int(80e6))
|
||||
),
|
||||
# Set default value (SPI_ETHERNET_DEFAULT_POLLING_INTERVAL) at _validate()
|
||||
cv.Optional(CONF_POLLING_INTERVAL): cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(min=TimePeriodMilliseconds(milliseconds=1)),
|
||||
),
|
||||
}
|
||||
),
|
||||
)
|
||||
|
@ -234,6 +284,10 @@ async def to_code(config):
|
|||
cg.add(var.set_cs_pin(config[CONF_CS_PIN]))
|
||||
if CONF_INTERRUPT_PIN in config:
|
||||
cg.add(var.set_interrupt_pin(config[CONF_INTERRUPT_PIN]))
|
||||
else:
|
||||
cg.add(var.set_polling_interval(config[CONF_POLLING_INTERVAL]))
|
||||
if _is_framework_spi_polling_mode_supported():
|
||||
cg.add_define("USE_ETHERNET_SPI_POLLING_SUPPORT")
|
||||
if CONF_RESET_PIN in config:
|
||||
cg.add(var.set_reset_pin(config[CONF_RESET_PIN]))
|
||||
cg.add(var.set_clock_speed(config[CONF_CLOCK_SPEED]))
|
||||
|
|
|
@ -116,6 +116,9 @@ void EthernetComponent::setup() {
|
|||
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
|
||||
#endif
|
||||
w5500_config.int_gpio_num = this->interrupt_pin_;
|
||||
#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
|
||||
w5500_config.poll_period_ms = this->polling_interval_;
|
||||
#endif
|
||||
phy_config.phy_addr = this->phy_addr_spi_;
|
||||
phy_config.reset_gpio_num = this->reset_pin_;
|
||||
|
||||
|
@ -327,7 +330,14 @@ void EthernetComponent::dump_config() {
|
|||
ESP_LOGCONFIG(TAG, " MISO Pin: %u", this->miso_pin_);
|
||||
ESP_LOGCONFIG(TAG, " MOSI Pin: %u", this->mosi_pin_);
|
||||
ESP_LOGCONFIG(TAG, " CS Pin: %u", this->cs_pin_);
|
||||
ESP_LOGCONFIG(TAG, " IRQ Pin: %u", this->interrupt_pin_);
|
||||
#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
|
||||
if (this->polling_interval_ != 0) {
|
||||
ESP_LOGCONFIG(TAG, " Polling Interval: %lu ms", this->polling_interval_);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
ESP_LOGCONFIG(TAG, " IRQ Pin: %d", this->interrupt_pin_);
|
||||
}
|
||||
ESP_LOGCONFIG(TAG, " Reset Pin: %d", this->reset_pin_);
|
||||
ESP_LOGCONFIG(TAG, " Clock Speed: %d MHz", this->clock_speed_ / 1000000);
|
||||
#else
|
||||
|
@ -536,6 +546,9 @@ void EthernetComponent::set_cs_pin(uint8_t cs_pin) { this->cs_pin_ = cs_pin; }
|
|||
void EthernetComponent::set_interrupt_pin(uint8_t interrupt_pin) { this->interrupt_pin_ = interrupt_pin; }
|
||||
void EthernetComponent::set_reset_pin(uint8_t reset_pin) { this->reset_pin_ = reset_pin; }
|
||||
void EthernetComponent::set_clock_speed(int clock_speed) { this->clock_speed_ = clock_speed; }
|
||||
#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
|
||||
void EthernetComponent::set_polling_interval(uint32_t polling_interval) { this->polling_interval_ = polling_interval; }
|
||||
#endif
|
||||
#else
|
||||
void EthernetComponent::set_phy_addr(uint8_t phy_addr) { this->phy_addr_ = phy_addr; }
|
||||
void EthernetComponent::set_power_pin(int power_pin) { this->power_pin_ = power_pin; }
|
||||
|
|
|
@ -67,6 +67,9 @@ class EthernetComponent : public Component {
|
|||
void set_interrupt_pin(uint8_t interrupt_pin);
|
||||
void set_reset_pin(uint8_t reset_pin);
|
||||
void set_clock_speed(int clock_speed);
|
||||
#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
|
||||
void set_polling_interval(uint32_t polling_interval);
|
||||
#endif
|
||||
#else
|
||||
void set_phy_addr(uint8_t phy_addr);
|
||||
void set_power_pin(int power_pin);
|
||||
|
@ -108,10 +111,13 @@ class EthernetComponent : public Component {
|
|||
uint8_t miso_pin_;
|
||||
uint8_t mosi_pin_;
|
||||
uint8_t cs_pin_;
|
||||
uint8_t interrupt_pin_;
|
||||
int interrupt_pin_{-1};
|
||||
int reset_pin_{-1};
|
||||
int phy_addr_spi_{-1};
|
||||
int clock_speed_;
|
||||
#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
|
||||
uint32_t polling_interval_{0};
|
||||
#endif
|
||||
#else
|
||||
uint8_t phy_addr_{0};
|
||||
int power_pin_{-1};
|
||||
|
|
|
@ -344,7 +344,7 @@ class TrueTypeFontWrapper:
|
|||
return offset_x, offset_y
|
||||
|
||||
def getmask(self, glyph, **kwargs):
|
||||
return self.font.getmask(glyph, **kwargs)
|
||||
return self.font.getmask(str(glyph), **kwargs)
|
||||
|
||||
def getmetrics(self, glyphs):
|
||||
return self.font.getmetrics()
|
||||
|
@ -359,7 +359,7 @@ class BitmapFontWrapper:
|
|||
return 0, 0
|
||||
|
||||
def getmask(self, glyph, **kwargs):
|
||||
return self.font.getmask(glyph, **kwargs)
|
||||
return self.font.getmask(str(glyph), **kwargs)
|
||||
|
||||
def getmetrics(self, glyphs):
|
||||
max_height = 0
|
||||
|
|
|
@ -22,6 +22,63 @@ struct Header {
|
|||
const char *value;
|
||||
};
|
||||
|
||||
// Some common HTTP status codes
|
||||
enum HttpStatus {
|
||||
HTTP_STATUS_OK = 200,
|
||||
HTTP_STATUS_NO_CONTENT = 204,
|
||||
HTTP_STATUS_PARTIAL_CONTENT = 206,
|
||||
|
||||
/* 3xx - Redirection */
|
||||
HTTP_STATUS_MULTIPLE_CHOICES = 300,
|
||||
HTTP_STATUS_MOVED_PERMANENTLY = 301,
|
||||
HTTP_STATUS_FOUND = 302,
|
||||
HTTP_STATUS_SEE_OTHER = 303,
|
||||
HTTP_STATUS_NOT_MODIFIED = 304,
|
||||
HTTP_STATUS_TEMPORARY_REDIRECT = 307,
|
||||
HTTP_STATUS_PERMANENT_REDIRECT = 308,
|
||||
|
||||
/* 4XX - CLIENT ERROR */
|
||||
HTTP_STATUS_BAD_REQUEST = 400,
|
||||
HTTP_STATUS_UNAUTHORIZED = 401,
|
||||
HTTP_STATUS_FORBIDDEN = 403,
|
||||
HTTP_STATUS_NOT_FOUND = 404,
|
||||
HTTP_STATUS_METHOD_NOT_ALLOWED = 405,
|
||||
HTTP_STATUS_NOT_ACCEPTABLE = 406,
|
||||
HTTP_STATUS_LENGTH_REQUIRED = 411,
|
||||
|
||||
/* 5xx - Server Error */
|
||||
HTTP_STATUS_INTERNAL_ERROR = 500
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Returns true if the HTTP status code is a redirect.
|
||||
*
|
||||
* @param status the HTTP status code to check
|
||||
* @return true if the status code is a redirect, false otherwise
|
||||
*/
|
||||
inline bool is_redirect(int const status) {
|
||||
switch (status) {
|
||||
case HTTP_STATUS_MOVED_PERMANENTLY:
|
||||
case HTTP_STATUS_FOUND:
|
||||
case HTTP_STATUS_SEE_OTHER:
|
||||
case HTTP_STATUS_TEMPORARY_REDIRECT:
|
||||
case HTTP_STATUS_PERMANENT_REDIRECT:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if the given HTTP status code indicates a successful request.
|
||||
*
|
||||
* A successful request is one where the status code is in the range 200-299
|
||||
*
|
||||
* @param status the HTTP status code to check
|
||||
* @return true if the status code indicates a successful request, false otherwise
|
||||
*/
|
||||
inline bool is_success(int const status) { return status >= HTTP_STATUS_OK && status < HTTP_STATUS_MULTIPLE_CHOICES; }
|
||||
|
||||
class HttpRequestComponent;
|
||||
|
||||
class HttpContainer : public Parented<HttpRequestComponent> {
|
||||
|
|
|
@ -113,11 +113,10 @@ std::shared_ptr<HttpContainer> HttpRequestArduino::start(std::string url, std::s
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
if (container->status_code < 200 || container->status_code >= 300) {
|
||||
if (!is_success(container->status_code)) {
|
||||
ESP_LOGE(TAG, "HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
|
||||
this->status_momentary_error("failed", 1000);
|
||||
container->end();
|
||||
return nullptr;
|
||||
// Still return the container, so it can be used to get the status code and error message
|
||||
}
|
||||
|
||||
int content_length = container->client_.getSize();
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
#include "esphome/components/watchdog/watchdog.h"
|
||||
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
|
||||
|
@ -118,20 +117,14 @@ std::shared_ptr<HttpContainer> HttpRequestIDF::start(std::string url, std::strin
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
auto is_ok = [](int code) { return code >= HttpStatus_Ok && code < HttpStatus_MultipleChoices; };
|
||||
|
||||
container->content_length = esp_http_client_fetch_headers(client);
|
||||
container->status_code = esp_http_client_get_status_code(client);
|
||||
if (is_ok(container->status_code)) {
|
||||
if (is_success(container->status_code)) {
|
||||
container->duration_ms = millis() - start;
|
||||
return container;
|
||||
}
|
||||
|
||||
if (this->follow_redirects_) {
|
||||
auto is_redirect = [](int code) {
|
||||
return code == HttpStatus_MovedPermanently || code == HttpStatus_Found || code == HttpStatus_SeeOther ||
|
||||
code == HttpStatus_TemporaryRedirect || code == HttpStatus_PermanentRedirect;
|
||||
};
|
||||
auto num_redirects = this->redirect_limit_;
|
||||
while (is_redirect(container->status_code) && num_redirects > 0) {
|
||||
err = esp_http_client_set_redirection(client);
|
||||
|
@ -142,9 +135,9 @@ std::shared_ptr<HttpContainer> HttpRequestIDF::start(std::string url, std::strin
|
|||
return nullptr;
|
||||
}
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
|
||||
char url[256]{};
|
||||
if (esp_http_client_get_url(client, url, sizeof(url) - 1) == ESP_OK) {
|
||||
ESP_LOGV(TAG, "redirecting to url: %s", url);
|
||||
char redirect_url[256]{};
|
||||
if (esp_http_client_get_url(client, redirect_url, sizeof(redirect_url) - 1) == ESP_OK) {
|
||||
ESP_LOGV(TAG, "redirecting to url: %s", redirect_url);
|
||||
}
|
||||
#endif
|
||||
err = esp_http_client_open(client, 0);
|
||||
|
@ -157,7 +150,7 @@ std::shared_ptr<HttpContainer> HttpRequestIDF::start(std::string url, std::strin
|
|||
|
||||
container->content_length = esp_http_client_fetch_headers(client);
|
||||
container->status_code = esp_http_client_get_status_code(client);
|
||||
if (is_ok(container->status_code)) {
|
||||
if (is_success(container->status_code)) {
|
||||
container->duration_ms = millis() - start;
|
||||
return container;
|
||||
}
|
||||
|
@ -172,8 +165,7 @@ std::shared_ptr<HttpContainer> HttpRequestIDF::start(std::string url, std::strin
|
|||
|
||||
ESP_LOGE(TAG, "HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
|
||||
this->status_momentary_error("failed", 1000);
|
||||
esp_http_client_cleanup(client);
|
||||
return nullptr;
|
||||
return container;
|
||||
}
|
||||
|
||||
int HttpContainerIDF::read(uint8_t *buf, size_t max_len) {
|
||||
|
|
|
@ -106,7 +106,7 @@ uint8_t OtaHttpRequestComponent::do_ota_() {
|
|||
|
||||
auto container = this->parent_->get(url_with_auth);
|
||||
|
||||
if (container == nullptr) {
|
||||
if (container == nullptr || container->status_code != HTTP_STATUS_OK) {
|
||||
return OTA_CONNECTION_ERROR;
|
||||
}
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ void HttpRequestUpdate::setup() {
|
|||
void HttpRequestUpdate::update() {
|
||||
auto container = this->request_parent_->get(this->source_url_);
|
||||
|
||||
if (container == nullptr) {
|
||||
if (container == nullptr || container->status_code != HTTP_STATUS_OK) {
|
||||
std::string msg = str_sprintf("Failed to fetch manifest from %s", this->source_url_.c_str());
|
||||
this->status_set_error(msg.c_str());
|
||||
return;
|
||||
|
|
|
@ -71,6 +71,7 @@ from .widgets.meter import meter_spec
|
|||
from .widgets.msgbox import MSGBOX_SCHEMA, msgboxes_to_code
|
||||
from .widgets.obj import obj_spec
|
||||
from .widgets.page import add_pages, generate_page_triggers, page_spec
|
||||
from .widgets.qrcode import qr_code_spec
|
||||
from .widgets.roller import roller_spec
|
||||
from .widgets.slider import slider_spec
|
||||
from .widgets.spinbox import spinbox_spec
|
||||
|
@ -109,6 +110,7 @@ for w_type in (
|
|||
spinbox_spec,
|
||||
keyboard_spec,
|
||||
tileview_spec,
|
||||
qr_code_spec,
|
||||
):
|
||||
WIDGET_TYPES[w_type.name] = w_type
|
||||
|
||||
|
|
54
esphome/components/lvgl/widgets/qrcode.py
Normal file
54
esphome/components/lvgl/widgets/qrcode.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_SIZE, CONF_TEXT
|
||||
from esphome.cpp_generator import MockObjClass
|
||||
|
||||
from ..defines import CONF_MAIN, literal
|
||||
from ..lv_validation import color, color_retmapper, lv_text
|
||||
from ..lvcode import LocalVariable, lv, lv_expr
|
||||
from ..schemas import TEXT_SCHEMA
|
||||
from ..types import WidgetType, lv_obj_t
|
||||
from . import Widget
|
||||
|
||||
CONF_QRCODE = "qrcode"
|
||||
CONF_DARK_COLOR = "dark_color"
|
||||
CONF_LIGHT_COLOR = "light_color"
|
||||
|
||||
QRCODE_SCHEMA = TEXT_SCHEMA.extend(
|
||||
{
|
||||
cv.Optional(CONF_DARK_COLOR, default="black"): color,
|
||||
cv.Optional(CONF_LIGHT_COLOR, default="white"): color,
|
||||
cv.Required(CONF_SIZE): cv.int_,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class QrCodeType(WidgetType):
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
CONF_QRCODE,
|
||||
lv_obj_t,
|
||||
(CONF_MAIN,),
|
||||
QRCODE_SCHEMA,
|
||||
modify_schema=TEXT_SCHEMA,
|
||||
)
|
||||
|
||||
def get_uses(self):
|
||||
return ("canvas", "img")
|
||||
|
||||
def obj_creator(self, parent: MockObjClass, config: dict):
|
||||
dark_color = color_retmapper(config[CONF_DARK_COLOR])
|
||||
light_color = color_retmapper(config[CONF_LIGHT_COLOR])
|
||||
size = config[CONF_SIZE]
|
||||
return lv_expr.call("qrcode_create", parent, size, dark_color, light_color)
|
||||
|
||||
async def to_code(self, w: Widget, config):
|
||||
if (value := config.get(CONF_TEXT)) is not None:
|
||||
value = await lv_text.process(value)
|
||||
with LocalVariable(
|
||||
"qr_text", cg.const_char_ptr, value, modifier=""
|
||||
) as str_obj:
|
||||
lv.qrcode_update(w.obj, str_obj, literal(f"strlen({str_obj})"))
|
||||
|
||||
|
||||
qr_code_spec = QrCodeType()
|
|
@ -15,23 +15,33 @@ void Modbus::setup() {
|
|||
void Modbus::loop() {
|
||||
const uint32_t now = millis();
|
||||
|
||||
if (now - this->last_modbus_byte_ > 50) {
|
||||
this->rx_buffer_.clear();
|
||||
this->last_modbus_byte_ = now;
|
||||
}
|
||||
// stop blocking new send commands after send_wait_time_ ms regardless if a response has been received since then
|
||||
if (now - this->last_send_ > send_wait_time_) {
|
||||
waiting_for_response = 0;
|
||||
}
|
||||
|
||||
while (this->available()) {
|
||||
uint8_t byte;
|
||||
this->read_byte(&byte);
|
||||
if (this->parse_modbus_byte_(byte)) {
|
||||
this->last_modbus_byte_ = now;
|
||||
} else {
|
||||
size_t at = this->rx_buffer_.size();
|
||||
if (at > 0) {
|
||||
ESP_LOGV(TAG, "Clearing buffer of %d bytes - parse failed", at);
|
||||
this->rx_buffer_.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (now - this->last_modbus_byte_ > 50) {
|
||||
size_t at = this->rx_buffer_.size();
|
||||
if (at > 0) {
|
||||
ESP_LOGV(TAG, "Clearing buffer of %d bytes - timeout", at);
|
||||
this->rx_buffer_.clear();
|
||||
}
|
||||
|
||||
// stop blocking new send commands after sent_wait_time_ ms after response received
|
||||
if (now - this->last_send_ > send_wait_time_) {
|
||||
if (waiting_for_response > 0)
|
||||
ESP_LOGV(TAG, "Stop waiting for response from %d", waiting_for_response);
|
||||
waiting_for_response = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,7 +49,7 @@ bool Modbus::parse_modbus_byte_(uint8_t byte) {
|
|||
size_t at = this->rx_buffer_.size();
|
||||
this->rx_buffer_.push_back(byte);
|
||||
const uint8_t *raw = &this->rx_buffer_[0];
|
||||
ESP_LOGV(TAG, "Modbus received Byte %d (0X%x)", byte, byte);
|
||||
ESP_LOGVV(TAG, "Modbus received Byte %d (0X%x)", byte, byte);
|
||||
// Byte 0: modbus address (match all)
|
||||
if (at == 0)
|
||||
return true;
|
||||
|
@ -144,8 +154,10 @@ bool Modbus::parse_modbus_byte_(uint8_t byte) {
|
|||
ESP_LOGW(TAG, "Got Modbus frame from unknown address 0x%02X! ", address);
|
||||
}
|
||||
|
||||
// return false to reset buffer
|
||||
return false;
|
||||
// reset buffer
|
||||
ESP_LOGV(TAG, "Clearing buffer of %d bytes - parse succeeded", at);
|
||||
this->rx_buffer_.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
void Modbus::dump_config() {
|
||||
|
|
|
@ -17,6 +17,8 @@ void MopekaProCheck::dump_config() {
|
|||
LOG_SENSOR(" ", "Temperature", this->temperature_);
|
||||
LOG_SENSOR(" ", "Battery Level", this->battery_level_);
|
||||
LOG_SENSOR(" ", "Reading Distance", this->distance_);
|
||||
LOG_SENSOR(" ", "Read Quality", this->read_quality_);
|
||||
LOG_SENSOR(" ", "Ignored Reads", this->ignored_reads_);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,34 +68,49 @@ bool MopekaProCheck::parse_device(const esp32_ble_tracker::ESPBTDevice &device)
|
|||
this->battery_level_->publish_state(level);
|
||||
}
|
||||
|
||||
// Get the quality value
|
||||
SensorReadQuality quality_value = this->parse_read_quality_(manu_data.data);
|
||||
if (this->read_quality_ != nullptr) {
|
||||
this->read_quality_->publish_state(static_cast<int>(quality_value));
|
||||
}
|
||||
|
||||
// Determine if we have a good enough quality of read to report level and distance
|
||||
// sensors. This sensor is reported regardless of distance or level sensors being enabled
|
||||
if (quality_value < this->min_signal_quality_) {
|
||||
ESP_LOGW(TAG, "Read Quality too low to report distance or level");
|
||||
this->ignored_read_count_++;
|
||||
} else {
|
||||
// reset to zero since read quality was sufficient
|
||||
this->ignored_read_count_ = 0;
|
||||
}
|
||||
// Report number of contiguous ignored reads if sensor defined
|
||||
if (this->ignored_reads_ != nullptr) {
|
||||
this->ignored_reads_->publish_state(this->ignored_read_count_);
|
||||
}
|
||||
|
||||
// Get distance and level if either are sensors
|
||||
if ((this->distance_ != nullptr) || (this->level_ != nullptr)) {
|
||||
uint32_t distance_value = this->parse_distance_(manu_data.data);
|
||||
SensorReadQuality quality_value = this->parse_read_quality_(manu_data.data);
|
||||
ESP_LOGD(TAG, "Distance Sensor: Quality (0x%X) Distance (%" PRId32 "mm)", quality_value, distance_value);
|
||||
if (quality_value < QUALITY_HIGH) {
|
||||
ESP_LOGW(TAG, "Poor read quality.");
|
||||
}
|
||||
if (quality_value < QUALITY_MED) {
|
||||
// if really bad reading set to 0
|
||||
ESP_LOGW(TAG, "Setting distance to 0");
|
||||
distance_value = 0;
|
||||
}
|
||||
|
||||
// update distance sensor
|
||||
if (this->distance_ != nullptr) {
|
||||
this->distance_->publish_state(distance_value);
|
||||
}
|
||||
|
||||
// update level sensor
|
||||
if (this->level_ != nullptr) {
|
||||
uint8_t tank_level = 0;
|
||||
if (distance_value >= this->full_mm_) {
|
||||
tank_level = 100; // cap at 100%
|
||||
} else if (distance_value > this->empty_mm_) {
|
||||
tank_level = ((100.0f / (this->full_mm_ - this->empty_mm_)) * (distance_value - this->empty_mm_));
|
||||
// only update distance and level sensors if read quality was sufficient. This can be determined by
|
||||
// if the ignored_read_count is zero.
|
||||
if (this->ignored_read_count_ == 0) {
|
||||
// update distance sensor
|
||||
if (this->distance_ != nullptr) {
|
||||
this->distance_->publish_state(distance_value);
|
||||
}
|
||||
|
||||
// update level sensor
|
||||
if (this->level_ != nullptr) {
|
||||
uint8_t tank_level = 0;
|
||||
if (distance_value >= this->full_mm_) {
|
||||
tank_level = 100; // cap at 100%
|
||||
} else if (distance_value > this->empty_mm_) {
|
||||
tank_level = ((100.0f / (this->full_mm_ - this->empty_mm_)) * (distance_value - this->empty_mm_));
|
||||
}
|
||||
this->level_->publish_state(tank_level);
|
||||
}
|
||||
this->level_->publish_state(tank_level);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -131,6 +148,8 @@ uint32_t MopekaProCheck::parse_distance_(const std::vector<uint8_t> &message) {
|
|||
uint8_t MopekaProCheck::parse_temperature_(const std::vector<uint8_t> &message) { return (message[2] & 0x7F) - 40; }
|
||||
|
||||
SensorReadQuality MopekaProCheck::parse_read_quality_(const std::vector<uint8_t> &message) {
|
||||
// Since a 8 bit value is being shifted and truncated to 2 bits all possible values are defined as enumeration
|
||||
// value and the static cast is safe.
|
||||
return static_cast<SensorReadQuality>(message[4] >> 6);
|
||||
}
|
||||
|
||||
|
|
|
@ -24,9 +24,9 @@ enum SensorType {
|
|||
};
|
||||
|
||||
// Sensor read quality. If sensor is poorly placed or tank level
|
||||
// gets too low the read quality will show and the distanace
|
||||
// gets too low the read quality will show and the distance
|
||||
// measurement may be inaccurate.
|
||||
enum SensorReadQuality { QUALITY_HIGH = 0x3, QUALITY_MED = 0x2, QUALITY_LOW = 0x1, QUALITY_NONE = 0x0 };
|
||||
enum SensorReadQuality { QUALITY_HIGH = 0x3, QUALITY_MED = 0x2, QUALITY_LOW = 0x1, QUALITY_ZERO = 0x0 };
|
||||
|
||||
class MopekaProCheck : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
|
@ -35,11 +35,14 @@ class MopekaProCheck : public Component, public esp32_ble_tracker::ESPBTDeviceLi
|
|||
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
void set_min_signal_quality(SensorReadQuality min) { this->min_signal_quality_ = min; };
|
||||
|
||||
void set_level(sensor::Sensor *level) { level_ = level; };
|
||||
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; };
|
||||
void set_battery_level(sensor::Sensor *bat) { battery_level_ = bat; };
|
||||
void set_distance(sensor::Sensor *distance) { distance_ = distance; };
|
||||
void set_signal_quality(sensor::Sensor *rq) { read_quality_ = rq; };
|
||||
void set_ignored_reads(sensor::Sensor *ir) { ignored_reads_ = ir; };
|
||||
void set_tank_full(float full) { full_mm_ = full; };
|
||||
void set_tank_empty(float empty) { empty_mm_ = empty; };
|
||||
|
||||
|
@ -49,9 +52,13 @@ class MopekaProCheck : public Component, public esp32_ble_tracker::ESPBTDeviceLi
|
|||
sensor::Sensor *temperature_{nullptr};
|
||||
sensor::Sensor *distance_{nullptr};
|
||||
sensor::Sensor *battery_level_{nullptr};
|
||||
sensor::Sensor *read_quality_{nullptr};
|
||||
sensor::Sensor *ignored_reads_{nullptr};
|
||||
|
||||
uint32_t full_mm_;
|
||||
uint32_t empty_mm_;
|
||||
uint32_t ignored_read_count_ = 0;
|
||||
SensorReadQuality min_signal_quality_ = QUALITY_MED;
|
||||
|
||||
uint8_t parse_battery_level_(const std::vector<uint8_t> &message);
|
||||
uint32_t parse_distance_(const std::vector<uint8_t> &message);
|
||||
|
|
|
@ -5,9 +5,12 @@ from esphome.const import (
|
|||
CONF_DISTANCE,
|
||||
CONF_MAC_ADDRESS,
|
||||
CONF_ID,
|
||||
ICON_COUNTER,
|
||||
ICON_THERMOMETER,
|
||||
ICON_RULER,
|
||||
ICON_SIGNAL,
|
||||
UNIT_PERCENT,
|
||||
UNIT_EMPTY,
|
||||
CONF_LEVEL,
|
||||
CONF_TEMPERATURE,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
|
@ -16,11 +19,15 @@ from esphome.const import (
|
|||
STATE_CLASS_MEASUREMENT,
|
||||
CONF_BATTERY_LEVEL,
|
||||
DEVICE_CLASS_BATTERY,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
)
|
||||
|
||||
CONF_TANK_TYPE = "tank_type"
|
||||
CONF_CUSTOM_DISTANCE_FULL = "custom_distance_full"
|
||||
CONF_CUSTOM_DISTANCE_EMPTY = "custom_distance_empty"
|
||||
CONF_SIGNAL_QUALITY = "signal_quality"
|
||||
CONF_MINIMUM_SIGNAL_QUALITY = "minimum_signal_quality"
|
||||
CONF_IGNORED_READS = "ignored_reads"
|
||||
|
||||
ICON_PROPANE_TANK = "mdi:propane-tank"
|
||||
|
||||
|
@ -56,6 +63,14 @@ MopekaProCheck = mopeka_pro_check_ns.class_(
|
|||
"MopekaProCheck", esp32_ble_tracker.ESPBTDeviceListener, cg.Component
|
||||
)
|
||||
|
||||
SensorReadQuality = mopeka_pro_check_ns.enum("SensorReadQuality")
|
||||
SIGNAL_QUALITIES = {
|
||||
"ZERO": SensorReadQuality.QUALITY_ZERO,
|
||||
"LOW": SensorReadQuality.QUALITY_LOW,
|
||||
"MEDIUM": SensorReadQuality.QUALITY_MED,
|
||||
"HIGH": SensorReadQuality.QUALITY_HIGH,
|
||||
}
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
|
@ -89,6 +104,21 @@ CONFIG_SCHEMA = (
|
|||
device_class=DEVICE_CLASS_BATTERY,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_SIGNAL_QUALITY): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_EMPTY,
|
||||
icon=ICON_SIGNAL,
|
||||
accuracy_decimals=0,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_IGNORED_READS): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_EMPTY,
|
||||
icon=ICON_COUNTER,
|
||||
accuracy_decimals=0,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
cv.Optional(CONF_MINIMUM_SIGNAL_QUALITY, default="MEDIUM"): cv.enum(
|
||||
SIGNAL_QUALITIES, upper=True
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
|
||||
|
@ -119,6 +149,11 @@ async def to_code(config):
|
|||
cg.add(var.set_tank_empty(CONF_SUPPORTED_TANKS_MAP[t][0]))
|
||||
cg.add(var.set_tank_full(CONF_SUPPORTED_TANKS_MAP[t][1]))
|
||||
|
||||
if (
|
||||
minimum_signal_quality := config.get(CONF_MINIMUM_SIGNAL_QUALITY, None)
|
||||
) is not None:
|
||||
cg.add(var.set_min_signal_quality(minimum_signal_quality))
|
||||
|
||||
if CONF_TEMPERATURE in config:
|
||||
sens = await sensor.new_sensor(config[CONF_TEMPERATURE])
|
||||
cg.add(var.set_temperature(sens))
|
||||
|
@ -131,3 +166,9 @@ async def to_code(config):
|
|||
if CONF_BATTERY_LEVEL in config:
|
||||
sens = await sensor.new_sensor(config[CONF_BATTERY_LEVEL])
|
||||
cg.add(var.set_battery_level(sens))
|
||||
if CONF_SIGNAL_QUALITY in config:
|
||||
sens = await sensor.new_sensor(config[CONF_SIGNAL_QUALITY])
|
||||
cg.add(var.set_signal_quality(sens))
|
||||
if CONF_IGNORED_READS in config:
|
||||
sens = await sensor.new_sensor(config[CONF_IGNORED_READS])
|
||||
cg.add(var.set_ignored_reads(sens))
|
||||
|
|
|
@ -50,6 +50,12 @@ void PrometheusHandler::handleRequest(AsyncWebServerRequest *req) {
|
|||
this->lock_row_(stream, obj);
|
||||
#endif
|
||||
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
this->text_sensor_type_(stream);
|
||||
for (auto *obj : App.get_text_sensors())
|
||||
this->text_sensor_row_(stream, obj);
|
||||
#endif
|
||||
|
||||
req->send(stream);
|
||||
}
|
||||
|
||||
|
@ -349,6 +355,43 @@ void PrometheusHandler::lock_row_(AsyncResponseStream *stream, lock::Lock *obj)
|
|||
}
|
||||
#endif
|
||||
|
||||
// Type-specific implementation
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
void PrometheusHandler::text_sensor_type_(AsyncResponseStream *stream) {
|
||||
stream->print(F("#TYPE esphome_text_sensor_value gauge\n"));
|
||||
stream->print(F("#TYPE esphome_text_sensor_failed gauge\n"));
|
||||
}
|
||||
void PrometheusHandler::text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj) {
|
||||
if (obj->is_internal() && !this->include_internal_)
|
||||
return;
|
||||
if (obj->has_state()) {
|
||||
// We have a valid value, output this value
|
||||
stream->print(F("esphome_text_sensor_failed{id=\""));
|
||||
stream->print(relabel_id_(obj).c_str());
|
||||
stream->print(F("\",name=\""));
|
||||
stream->print(relabel_name_(obj).c_str());
|
||||
stream->print(F("\"} 0\n"));
|
||||
// Data itself
|
||||
stream->print(F("esphome_text_sensor_value{id=\""));
|
||||
stream->print(relabel_id_(obj).c_str());
|
||||
stream->print(F("\",name=\""));
|
||||
stream->print(relabel_name_(obj).c_str());
|
||||
stream->print(F("\",value=\""));
|
||||
stream->print(obj->state.c_str());
|
||||
stream->print(F("\"} "));
|
||||
stream->print(F("1.0"));
|
||||
stream->print(F("\n"));
|
||||
} else {
|
||||
// Invalid state
|
||||
stream->print(F("esphome_text_sensor_failed{id=\""));
|
||||
stream->print(relabel_id_(obj).c_str());
|
||||
stream->print(F("\",name=\""));
|
||||
stream->print(relabel_name_(obj).c_str());
|
||||
stream->print(F("\"} 1\n"));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace prometheus
|
||||
} // namespace esphome
|
||||
#endif
|
||||
|
|
|
@ -110,6 +110,13 @@ class PrometheusHandler : public AsyncWebHandler, public Component {
|
|||
void lock_row_(AsyncResponseStream *stream, lock::Lock *obj);
|
||||
#endif
|
||||
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
/// Return the type for prometheus
|
||||
void text_sensor_type_(AsyncResponseStream *stream);
|
||||
/// Return the lock Values state as prometheus data point
|
||||
void text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj);
|
||||
#endif
|
||||
|
||||
web_server_base::WebServerBase *base_;
|
||||
bool include_internal_{false};
|
||||
std::map<EntityBase *, std::string> relabel_map_id_;
|
||||
|
|
|
@ -111,7 +111,7 @@ void SGP4xComponent::setup() {
|
|||
number of records reported from being overwhelming.
|
||||
*/
|
||||
ESP_LOGD(TAG, "Component requires sampling of 1Hz, setting up background sampler");
|
||||
this->set_interval(1000, [this]() { this->update_gas_indices(); });
|
||||
this->set_interval(1000, [this]() { this->take_sample(); });
|
||||
}
|
||||
|
||||
void SGP4xComponent::self_test_() {
|
||||
|
@ -146,31 +146,15 @@ void SGP4xComponent::self_test_() {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Combined the measured gasses, temperature, and humidity
|
||||
* to calculate the VOC Index
|
||||
*
|
||||
* @param temperature The measured temperature in degrees C
|
||||
* @param humidity The measured relative humidity in % rH
|
||||
* @return int32_t The VOC Index
|
||||
*/
|
||||
bool SGP4xComponent::measure_gas_indices_(int32_t &voc, int32_t &nox) {
|
||||
uint16_t voc_sraw;
|
||||
uint16_t nox_sraw;
|
||||
if (!measure_raw_(voc_sraw, nox_sraw))
|
||||
return false;
|
||||
|
||||
this->status_clear_warning();
|
||||
|
||||
voc = voc_algorithm_.process(voc_sraw);
|
||||
if (nox_sensor_) {
|
||||
nox = nox_algorithm_.process(nox_sraw);
|
||||
}
|
||||
ESP_LOGV(TAG, "VOC = %" PRId32 ", NOx = %" PRId32, voc, nox);
|
||||
void SGP4xComponent::update_gas_indices_() {
|
||||
this->voc_index_ = this->voc_algorithm_.process(this->voc_sraw_);
|
||||
if (this->nox_sensor_ != nullptr)
|
||||
this->nox_index_ = this->nox_algorithm_.process(this->nox_sraw_);
|
||||
ESP_LOGV(TAG, "VOC = %" PRId32 ", NOx = %" PRId32, this->voc_index_, this->nox_index_);
|
||||
// Store baselines after defined interval or if the difference between current and stored baseline becomes too
|
||||
// much
|
||||
if (this->store_baseline_ && this->seconds_since_last_store_ > SHORTEST_BASELINE_STORE_INTERVAL) {
|
||||
voc_algorithm_.get_states(this->voc_state0_, this->voc_state1_);
|
||||
this->voc_algorithm_.get_states(this->voc_state0_, this->voc_state1_);
|
||||
if (std::abs(this->voc_baselines_storage_.state0 - this->voc_state0_) > MAXIMUM_STORAGE_DIFF ||
|
||||
std::abs(this->voc_baselines_storage_.state1 - this->voc_state1_) > MAXIMUM_STORAGE_DIFF) {
|
||||
this->seconds_since_last_store_ = 0;
|
||||
|
@ -179,29 +163,27 @@ bool SGP4xComponent::measure_gas_indices_(int32_t &voc, int32_t &nox) {
|
|||
|
||||
if (this->pref_.save(&this->voc_baselines_storage_)) {
|
||||
ESP_LOGI(TAG, "Stored VOC baseline state0: 0x%04" PRIX32 " ,state1: 0x%04" PRIX32,
|
||||
this->voc_baselines_storage_.state0, voc_baselines_storage_.state1);
|
||||
this->voc_baselines_storage_.state0, this->voc_baselines_storage_.state1);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Could not store VOC baselines");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
if (this->samples_read_ < this->samples_to_stabilize_) {
|
||||
this->samples_read_++;
|
||||
ESP_LOGD(TAG, "Sensor has not collected enough samples yet. (%d/%d) VOC index is: %" PRIu32, this->samples_read_,
|
||||
this->samples_to_stabilize_, this->voc_index_);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief Return the raw gas measurement
|
||||
*
|
||||
* @param temperature The measured temperature in degrees C
|
||||
* @param humidity The measured relative humidity in % rH
|
||||
* @return uint16_t The current raw gas measurement
|
||||
*/
|
||||
bool SGP4xComponent::measure_raw_(uint16_t &voc_raw, uint16_t &nox_raw) {
|
||||
|
||||
void SGP4xComponent::measure_raw_() {
|
||||
float humidity = NAN;
|
||||
static uint32_t nox_conditioning_start = millis();
|
||||
|
||||
if (!this->self_test_complete_) {
|
||||
ESP_LOGD(TAG, "Self-test not yet complete");
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
if (this->humidity_sensor_ != nullptr) {
|
||||
humidity = this->humidity_sensor_->state;
|
||||
|
@ -243,61 +225,45 @@ bool SGP4xComponent::measure_raw_(uint16_t &voc_raw, uint16_t &nox_raw) {
|
|||
data[1] = tempticks;
|
||||
|
||||
if (!this->write_command(command, data, 2)) {
|
||||
this->status_set_warning();
|
||||
ESP_LOGD(TAG, "write error (%d)", this->last_error_);
|
||||
return false;
|
||||
this->status_set_warning("measurement request failed");
|
||||
return;
|
||||
}
|
||||
delay(measure_time_);
|
||||
uint16_t raw_data[2];
|
||||
raw_data[1] = 0;
|
||||
if (!this->read_data(raw_data, response_words)) {
|
||||
this->status_set_warning();
|
||||
ESP_LOGD(TAG, "read error (%d)", this->last_error_);
|
||||
return false;
|
||||
}
|
||||
voc_raw = raw_data[0];
|
||||
nox_raw = raw_data[1]; // either 0 or the measured NOx ticks
|
||||
return true;
|
||||
|
||||
this->set_timeout(this->measure_time_, [this, response_words]() {
|
||||
uint16_t raw_data[2];
|
||||
raw_data[1] = 0;
|
||||
if (!this->read_data(raw_data, response_words)) {
|
||||
ESP_LOGD(TAG, "read error (%d)", this->last_error_);
|
||||
this->status_set_warning("measurement read failed");
|
||||
this->voc_index_ = this->nox_index_ = UINT16_MAX;
|
||||
return;
|
||||
}
|
||||
this->voc_sraw_ = raw_data[0];
|
||||
this->nox_sraw_ = raw_data[1]; // either 0 or the measured NOx ticks
|
||||
this->status_clear_warning();
|
||||
this->update_gas_indices_();
|
||||
});
|
||||
}
|
||||
|
||||
void SGP4xComponent::update_gas_indices() {
|
||||
void SGP4xComponent::take_sample() {
|
||||
if (!this->self_test_complete_)
|
||||
return;
|
||||
|
||||
this->seconds_since_last_store_ += 1;
|
||||
if (!this->measure_gas_indices_(this->voc_index_, this->nox_index_)) {
|
||||
// Set values to UINT16_MAX to indicate failure
|
||||
this->voc_index_ = this->nox_index_ = UINT16_MAX;
|
||||
ESP_LOGE(TAG, "measure gas indices failed");
|
||||
return;
|
||||
}
|
||||
if (this->samples_read_ < this->samples_to_stabilize_) {
|
||||
this->samples_read_++;
|
||||
ESP_LOGD(TAG, "Sensor has not collected enough samples yet. (%d/%d) VOC index is: %" PRIu32, this->samples_read_,
|
||||
this->samples_to_stabilize_, this->voc_index_);
|
||||
return;
|
||||
}
|
||||
this->measure_raw_();
|
||||
}
|
||||
|
||||
void SGP4xComponent::update() {
|
||||
if (this->samples_read_ < this->samples_to_stabilize_) {
|
||||
return;
|
||||
}
|
||||
if (this->voc_sensor_) {
|
||||
if (this->voc_index_ != UINT16_MAX) {
|
||||
this->status_clear_warning();
|
||||
if (this->voc_sensor_ != nullptr) {
|
||||
if (this->voc_index_ != UINT16_MAX)
|
||||
this->voc_sensor_->publish_state(this->voc_index_);
|
||||
} else {
|
||||
this->status_set_warning();
|
||||
}
|
||||
}
|
||||
if (this->nox_sensor_) {
|
||||
if (this->nox_index_ != UINT16_MAX) {
|
||||
this->status_clear_warning();
|
||||
if (this->nox_sensor_ != nullptr) {
|
||||
if (this->nox_index_ != UINT16_MAX)
|
||||
this->nox_sensor_->publish_state(this->nox_index_);
|
||||
} else {
|
||||
this->status_set_warning();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -329,7 +295,7 @@ void SGP4xComponent::dump_config() {
|
|||
}
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
|
||||
if (this->humidity_sensor_ != nullptr && this->temperature_sensor_ != nullptr) {
|
||||
if (this->humidity_sensor_ != nullptr || this->temperature_sensor_ != nullptr) {
|
||||
ESP_LOGCONFIG(TAG, " Compensation:");
|
||||
LOG_SENSOR(" ", "Temperature Source:", this->temperature_sensor_);
|
||||
LOG_SENSOR(" ", "Humidity Source:", this->humidity_sensor_);
|
||||
|
|
|
@ -73,7 +73,7 @@ class SGP4xComponent : public PollingComponent, public sensor::Sensor, public se
|
|||
|
||||
void setup() override;
|
||||
void update() override;
|
||||
void update_gas_indices();
|
||||
void take_sample();
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
void set_store_baseline(bool store_baseline) { store_baseline_ = store_baseline; }
|
||||
|
@ -108,8 +108,10 @@ class SGP4xComponent : public PollingComponent, public sensor::Sensor, public se
|
|||
sensor::Sensor *temperature_sensor_{nullptr};
|
||||
int16_t sensirion_init_sensors_();
|
||||
|
||||
bool measure_gas_indices_(int32_t &voc, int32_t &nox);
|
||||
bool measure_raw_(uint16_t &voc_raw, uint16_t &nox_raw);
|
||||
void update_gas_indices_();
|
||||
void measure_raw_();
|
||||
uint16_t voc_sraw_;
|
||||
uint16_t nox_sraw_;
|
||||
|
||||
SgpType sgp_type_{SGP40};
|
||||
uint64_t serial_number_;
|
||||
|
|
|
@ -297,8 +297,8 @@ void WiFiComponent::set_sta(const WiFiAP &ap) {
|
|||
void WiFiComponent::clear_sta() { this->sta_.clear(); }
|
||||
void WiFiComponent::save_wifi_sta(const std::string &ssid, const std::string &password) {
|
||||
SavedWifiSettings save{};
|
||||
strncpy(save.ssid, ssid.c_str(), sizeof(save.ssid));
|
||||
strncpy(save.password, password.c_str(), sizeof(save.password));
|
||||
snprintf(save.ssid, sizeof(save.ssid), "%s", ssid.c_str());
|
||||
snprintf(save.password, sizeof(save.password), "%s", password.c_str());
|
||||
this->pref_.save(&save);
|
||||
// ensure it's written immediately
|
||||
global_preferences->sync();
|
||||
|
|
|
@ -137,8 +137,8 @@ bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
|
|||
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_wifi.html#_CPPv417wifi_sta_config_t
|
||||
wifi_config_t conf;
|
||||
memset(&conf, 0, sizeof(conf));
|
||||
strncpy(reinterpret_cast<char *>(conf.sta.ssid), ap.get_ssid().c_str(), sizeof(conf.sta.ssid));
|
||||
strncpy(reinterpret_cast<char *>(conf.sta.password), ap.get_password().c_str(), sizeof(conf.sta.password));
|
||||
snprintf(reinterpret_cast<char *>(conf.sta.ssid), sizeof(conf.sta.ssid), "%s", ap.get_ssid().c_str());
|
||||
snprintf(reinterpret_cast<char *>(conf.sta.password), sizeof(conf.sta.password), "%s", ap.get_password().c_str());
|
||||
|
||||
// The weakest authmode to accept in the fast scan mode
|
||||
if (ap.get_password().empty()) {
|
||||
|
@ -746,7 +746,7 @@ bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
|
|||
|
||||
wifi_config_t conf;
|
||||
memset(&conf, 0, sizeof(conf));
|
||||
strncpy(reinterpret_cast<char *>(conf.ap.ssid), ap.get_ssid().c_str(), sizeof(conf.ap.ssid));
|
||||
snprintf(reinterpret_cast<char *>(conf.ap.ssid), sizeof(conf.ap.ssid), "%s", ap.get_ssid().c_str());
|
||||
conf.ap.channel = ap.get_channel().value_or(1);
|
||||
conf.ap.ssid_hidden = ap.get_ssid().size();
|
||||
conf.ap.max_connection = 5;
|
||||
|
@ -757,7 +757,7 @@ bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
|
|||
*conf.ap.password = 0;
|
||||
} else {
|
||||
conf.ap.authmode = WIFI_AUTH_WPA2_PSK;
|
||||
strncpy(reinterpret_cast<char *>(conf.ap.password), ap.get_password().c_str(), sizeof(conf.ap.password));
|
||||
snprintf(reinterpret_cast<char *>(conf.ap.password), sizeof(conf.ap.password), "%s", ap.get_password().c_str());
|
||||
}
|
||||
|
||||
// pairwise cipher of SoftAP, group cipher will be derived using this.
|
||||
|
|
|
@ -236,8 +236,8 @@ bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
|
|||
|
||||
struct station_config conf {};
|
||||
memset(&conf, 0, sizeof(conf));
|
||||
strncpy(reinterpret_cast<char *>(conf.ssid), ap.get_ssid().c_str(), sizeof(conf.ssid));
|
||||
strncpy(reinterpret_cast<char *>(conf.password), ap.get_password().c_str(), sizeof(conf.password));
|
||||
snprintf(reinterpret_cast<char *>(conf.ssid), sizeof(conf.ssid), "%s", ap.get_ssid().c_str());
|
||||
snprintf(reinterpret_cast<char *>(conf.password), sizeof(conf.password), "%s", ap.get_password().c_str());
|
||||
|
||||
if (ap.get_bssid().has_value()) {
|
||||
conf.bssid_set = 1;
|
||||
|
@ -775,7 +775,7 @@ bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
|
|||
return false;
|
||||
|
||||
struct softap_config conf {};
|
||||
strncpy(reinterpret_cast<char *>(conf.ssid), ap.get_ssid().c_str(), sizeof(conf.ssid));
|
||||
snprintf(reinterpret_cast<char *>(conf.ssid), sizeof(conf.ssid), "%s", ap.get_ssid().c_str());
|
||||
conf.ssid_len = static_cast<uint8>(ap.get_ssid().size());
|
||||
conf.channel = ap.get_channel().value_or(1);
|
||||
conf.ssid_hidden = ap.get_hidden();
|
||||
|
@ -787,7 +787,7 @@ bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
|
|||
*conf.password = 0;
|
||||
} else {
|
||||
conf.authmode = AUTH_WPA2_PSK;
|
||||
strncpy(reinterpret_cast<char *>(conf.password), ap.get_password().c_str(), sizeof(conf.password));
|
||||
snprintf(reinterpret_cast<char *>(conf.password), sizeof(conf.password), "%s", ap.get_password().c_str());
|
||||
}
|
||||
|
||||
ETS_UART_INTR_DISABLE();
|
||||
|
|
|
@ -289,8 +289,8 @@ bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
|
|||
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_wifi.html#_CPPv417wifi_sta_config_t
|
||||
wifi_config_t conf;
|
||||
memset(&conf, 0, sizeof(conf));
|
||||
strncpy(reinterpret_cast<char *>(conf.sta.ssid), ap.get_ssid().c_str(), sizeof(conf.sta.ssid));
|
||||
strncpy(reinterpret_cast<char *>(conf.sta.password), ap.get_password().c_str(), sizeof(conf.sta.password));
|
||||
snprintf(reinterpret_cast<char *>(conf.sta.ssid), sizeof(conf.sta.ssid), "%s", ap.get_ssid().c_str());
|
||||
snprintf(reinterpret_cast<char *>(conf.sta.password), sizeof(conf.sta.password), "%s", ap.get_password().c_str());
|
||||
|
||||
// The weakest authmode to accept in the fast scan mode
|
||||
if (ap.get_password().empty()) {
|
||||
|
|
|
@ -657,6 +657,7 @@ CONF_PMC_1_0 = "pmc_1_0"
|
|||
CONF_PMC_10_0 = "pmc_10_0"
|
||||
CONF_PMC_2_5 = "pmc_2_5"
|
||||
CONF_PMC_4_0 = "pmc_4_0"
|
||||
CONF_POLLING_INTERVAL = "polling_interval"
|
||||
CONF_PORT = "port"
|
||||
CONF_POSITION = "position"
|
||||
CONF_POSITION_ACTION = "position_action"
|
||||
|
|
|
@ -1,167 +0,0 @@
|
|||
#include "bytebuffer.h"
|
||||
#include <cassert>
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
namespace esphome {
|
||||
|
||||
ByteBuffer ByteBuffer::wrap(const uint8_t *ptr, size_t len, Endian endianness) {
|
||||
// there is a double copy happening here, could be optimized but at cost of clarity.
|
||||
std::vector<uint8_t> data(ptr, ptr + len);
|
||||
ByteBuffer buffer = {data};
|
||||
buffer.endianness_ = endianness;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
ByteBuffer ByteBuffer::wrap(std::vector<uint8_t> const &data, Endian endianness) {
|
||||
ByteBuffer buffer = {data};
|
||||
buffer.endianness_ = endianness;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
ByteBuffer ByteBuffer::wrap(uint8_t value) {
|
||||
ByteBuffer buffer = ByteBuffer(1);
|
||||
buffer.put_uint8(value);
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
ByteBuffer ByteBuffer::wrap(uint16_t value, Endian endianness) {
|
||||
ByteBuffer buffer = ByteBuffer(2, endianness);
|
||||
buffer.put_uint16(value);
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
ByteBuffer ByteBuffer::wrap(uint32_t value, Endian endianness) {
|
||||
ByteBuffer buffer = ByteBuffer(4, endianness);
|
||||
buffer.put_uint32(value);
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
ByteBuffer ByteBuffer::wrap(uint64_t value, Endian endianness) {
|
||||
ByteBuffer buffer = ByteBuffer(8, endianness);
|
||||
buffer.put_uint64(value);
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
ByteBuffer ByteBuffer::wrap(float value, Endian endianness) {
|
||||
ByteBuffer buffer = ByteBuffer(sizeof(float), endianness);
|
||||
buffer.put_float(value);
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
ByteBuffer ByteBuffer::wrap(double value, Endian endianness) {
|
||||
ByteBuffer buffer = ByteBuffer(sizeof(double), endianness);
|
||||
buffer.put_double(value);
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void ByteBuffer::set_limit(size_t limit) {
|
||||
assert(limit <= this->get_capacity());
|
||||
this->limit_ = limit;
|
||||
}
|
||||
void ByteBuffer::set_position(size_t position) {
|
||||
assert(position <= this->get_limit());
|
||||
this->position_ = position;
|
||||
}
|
||||
void ByteBuffer::clear() {
|
||||
this->limit_ = this->get_capacity();
|
||||
this->position_ = 0;
|
||||
}
|
||||
void ByteBuffer::flip() {
|
||||
this->limit_ = this->position_;
|
||||
this->position_ = 0;
|
||||
}
|
||||
|
||||
/// Getters
|
||||
uint8_t ByteBuffer::get_uint8() {
|
||||
assert(this->get_remaining() >= 1);
|
||||
return this->data_[this->position_++];
|
||||
}
|
||||
uint64_t ByteBuffer::get_uint(size_t length) {
|
||||
assert(this->get_remaining() >= length);
|
||||
uint64_t value = 0;
|
||||
if (this->endianness_ == LITTLE) {
|
||||
this->position_ += length;
|
||||
auto index = this->position_;
|
||||
while (length-- != 0) {
|
||||
value <<= 8;
|
||||
value |= this->data_[--index];
|
||||
}
|
||||
} else {
|
||||
while (length-- != 0) {
|
||||
value <<= 8;
|
||||
value |= this->data_[this->position_++];
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
uint32_t ByteBuffer::get_int24() {
|
||||
auto value = this->get_uint24();
|
||||
uint32_t mask = (~static_cast<uint32_t>(0)) << 23;
|
||||
if ((value & mask) != 0)
|
||||
value |= mask;
|
||||
return value;
|
||||
}
|
||||
float ByteBuffer::get_float() {
|
||||
assert(this->get_remaining() >= sizeof(float));
|
||||
return bit_cast<float>(this->get_uint32());
|
||||
}
|
||||
double ByteBuffer::get_double() {
|
||||
assert(this->get_remaining() >= sizeof(double));
|
||||
return bit_cast<double>(this->get_uint64());
|
||||
}
|
||||
|
||||
std::vector<uint8_t> ByteBuffer::get_vector(size_t length) {
|
||||
assert(this->get_remaining() >= length);
|
||||
auto start = this->data_.begin() + this->position_;
|
||||
this->position_ += length;
|
||||
return {start, start + length};
|
||||
}
|
||||
|
||||
/// Putters
|
||||
void ByteBuffer::put_uint8(uint8_t value) {
|
||||
assert(this->get_remaining() >= 1);
|
||||
this->data_[this->position_++] = value;
|
||||
}
|
||||
|
||||
void ByteBuffer::put_uint(uint64_t value, size_t length) {
|
||||
assert(this->get_remaining() >= length);
|
||||
if (this->endianness_ == LITTLE) {
|
||||
while (length-- != 0) {
|
||||
this->data_[this->position_++] = static_cast<uint8_t>(value);
|
||||
value >>= 8;
|
||||
}
|
||||
} else {
|
||||
this->position_ += length;
|
||||
auto index = this->position_;
|
||||
while (length-- != 0) {
|
||||
this->data_[--index] = static_cast<uint8_t>(value);
|
||||
value >>= 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
void ByteBuffer::put_float(float value) {
|
||||
static_assert(sizeof(float) == sizeof(uint32_t), "Float sizes other than 32 bit not supported");
|
||||
assert(this->get_remaining() >= sizeof(float));
|
||||
this->put_uint32(bit_cast<uint32_t>(value));
|
||||
}
|
||||
void ByteBuffer::put_double(double value) {
|
||||
static_assert(sizeof(double) == sizeof(uint64_t), "Double sizes other than 64 bit not supported");
|
||||
assert(this->get_remaining() >= sizeof(double));
|
||||
this->put_uint64(bit_cast<uint64_t>(value));
|
||||
}
|
||||
void ByteBuffer::put_vector(const std::vector<uint8_t> &value) {
|
||||
assert(this->get_remaining() >= value.size());
|
||||
std::copy(value.begin(), value.end(), this->data_.begin() + this->position_);
|
||||
this->position_ += value.size();
|
||||
}
|
||||
} // namespace esphome
|
|
@ -1,144 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <cinttypes>
|
||||
#include <cstddef>
|
||||
|
||||
namespace esphome {
|
||||
|
||||
enum Endian { LITTLE, BIG };
|
||||
|
||||
/**
|
||||
* A class modelled on the Java ByteBuffer class. It wraps a vector of bytes and permits putting and getting
|
||||
* items of various sizes, with an automatically incremented position.
|
||||
*
|
||||
* There are three variables maintained pointing into the buffer:
|
||||
*
|
||||
* capacity: the maximum amount of data that can be stored - set on construction and cannot be changed
|
||||
* limit: the limit of the data currently available to get or put
|
||||
* position: the current insert or extract position
|
||||
*
|
||||
* 0 <= position <= limit <= capacity
|
||||
*
|
||||
* In addition a mark can be set to the current position with mark(). A subsequent call to reset() will restore
|
||||
* the position to the mark.
|
||||
*
|
||||
* The buffer can be marked to be little-endian (default) or big-endian. All subsequent operations will use that order.
|
||||
*
|
||||
* The flip() operation will reset the position to 0 and limit to the current position. This is useful for reading
|
||||
* data from a buffer after it has been written.
|
||||
*
|
||||
*/
|
||||
class ByteBuffer {
|
||||
public:
|
||||
// Default constructor (compatibility with TEMPLATABLE_VALUE)
|
||||
ByteBuffer() : ByteBuffer(std::vector<uint8_t>()) {}
|
||||
/**
|
||||
* Create a new Bytebuffer with the given capacity
|
||||
*/
|
||||
ByteBuffer(size_t capacity, Endian endianness = LITTLE)
|
||||
: data_(std::vector<uint8_t>(capacity)), endianness_(endianness), limit_(capacity){};
|
||||
/**
|
||||
* Wrap an existing vector in a ByteBufffer
|
||||
*/
|
||||
static ByteBuffer wrap(std::vector<uint8_t> const &data, Endian endianness = LITTLE);
|
||||
/**
|
||||
* Wrap an existing array in a ByteBuffer. Note that this will create a copy of the data.
|
||||
*/
|
||||
static ByteBuffer wrap(const uint8_t *ptr, size_t len, Endian endianness = LITTLE);
|
||||
// Convenience functions to create a ByteBuffer from a value
|
||||
static ByteBuffer wrap(uint8_t value);
|
||||
static ByteBuffer wrap(uint16_t value, Endian endianness = LITTLE);
|
||||
static ByteBuffer wrap(uint32_t value, Endian endianness = LITTLE);
|
||||
static ByteBuffer wrap(uint64_t value, Endian endianness = LITTLE);
|
||||
static ByteBuffer wrap(int8_t value) { return wrap(static_cast<uint8_t>(value)); }
|
||||
static ByteBuffer wrap(int16_t value, Endian endianness = LITTLE) {
|
||||
return wrap(static_cast<uint16_t>(value), endianness);
|
||||
}
|
||||
static ByteBuffer wrap(int32_t value, Endian endianness = LITTLE) {
|
||||
return wrap(static_cast<uint32_t>(value), endianness);
|
||||
}
|
||||
static ByteBuffer wrap(int64_t value, Endian endianness = LITTLE) {
|
||||
return wrap(static_cast<uint64_t>(value), endianness);
|
||||
}
|
||||
static ByteBuffer wrap(float value, Endian endianness = LITTLE);
|
||||
static ByteBuffer wrap(double value, Endian endianness = LITTLE);
|
||||
static ByteBuffer wrap(bool value) { return wrap(static_cast<uint8_t>(value)); }
|
||||
|
||||
// Get an integral value from the buffer, increment position by length
|
||||
uint64_t get_uint(size_t length);
|
||||
// Get one byte from the buffer, increment position by 1
|
||||
uint8_t get_uint8();
|
||||
// Get a 16 bit unsigned value, increment by 2
|
||||
uint16_t get_uint16() { return static_cast<uint16_t>(this->get_uint(sizeof(uint16_t))); };
|
||||
// Get a 24 bit unsigned value, increment by 3
|
||||
uint32_t get_uint24() { return static_cast<uint32_t>(this->get_uint(3)); };
|
||||
// Get a 32 bit unsigned value, increment by 4
|
||||
uint32_t get_uint32() { return static_cast<uint32_t>(this->get_uint(sizeof(uint32_t))); };
|
||||
// Get a 64 bit unsigned value, increment by 8
|
||||
uint64_t get_uint64() { return this->get_uint(sizeof(uint64_t)); };
|
||||
// Signed versions of the get functions
|
||||
uint8_t get_int8() { return static_cast<int8_t>(this->get_uint8()); };
|
||||
int16_t get_int16() { return static_cast<int16_t>(this->get_uint(sizeof(int16_t))); }
|
||||
uint32_t get_int24();
|
||||
int32_t get_int32() { return static_cast<int32_t>(this->get_uint(sizeof(int32_t))); }
|
||||
int64_t get_int64() { return static_cast<int64_t>(this->get_uint(sizeof(int64_t))); }
|
||||
// Get a float value, increment by 4
|
||||
float get_float();
|
||||
// Get a double value, increment by 8
|
||||
double get_double();
|
||||
// Get a bool value, increment by 1
|
||||
bool get_bool() { return this->get_uint8(); }
|
||||
// Get vector of bytes, increment by length
|
||||
std::vector<uint8_t> get_vector(size_t length);
|
||||
|
||||
// Put values into the buffer, increment the position accordingly
|
||||
// put any integral value, length represents the number of bytes
|
||||
void put_uint(uint64_t value, size_t length);
|
||||
void put_uint8(uint8_t value);
|
||||
void put_uint16(uint16_t value) { this->put_uint(value, sizeof(uint16_t)); }
|
||||
void put_uint24(uint32_t value) { this->put_uint(value, 3); }
|
||||
void put_uint32(uint32_t value) { this->put_uint(value, sizeof(uint32_t)); }
|
||||
void put_uint64(uint64_t value) { this->put_uint(value, sizeof(uint64_t)); }
|
||||
// Signed versions of the put functions
|
||||
void put_int8(int8_t value) { this->put_uint8(static_cast<uint8_t>(value)); }
|
||||
void put_int16(int32_t value) { this->put_uint(static_cast<uint16_t>(value), sizeof(uint16_t)); }
|
||||
void put_int24(int32_t value) { this->put_uint(static_cast<uint32_t>(value), 3); }
|
||||
void put_int32(int32_t value) { this->put_uint(static_cast<uint32_t>(value), sizeof(uint32_t)); }
|
||||
void put_int64(int64_t value) { this->put_uint(static_cast<uint64_t>(value), sizeof(uint64_t)); }
|
||||
// Extra put functions
|
||||
void put_float(float value);
|
||||
void put_double(double value);
|
||||
void put_bool(bool value) { this->put_uint8(value); }
|
||||
void put_vector(const std::vector<uint8_t> &value);
|
||||
|
||||
inline size_t get_capacity() const { return this->data_.size(); }
|
||||
inline size_t get_position() const { return this->position_; }
|
||||
inline size_t get_limit() const { return this->limit_; }
|
||||
inline size_t get_remaining() const { return this->get_limit() - this->get_position(); }
|
||||
inline Endian get_endianness() const { return this->endianness_; }
|
||||
inline void mark() { this->mark_ = this->position_; }
|
||||
inline void big_endian() { this->endianness_ = BIG; }
|
||||
inline void little_endian() { this->endianness_ = LITTLE; }
|
||||
void set_limit(size_t limit);
|
||||
void set_position(size_t position);
|
||||
// set position to 0, limit to capacity.
|
||||
void clear();
|
||||
// set limit to current position, postition to zero. Used when swapping from write to read operations.
|
||||
void flip();
|
||||
// retrieve a pointer to the underlying data.
|
||||
std::vector<uint8_t> get_data() { return this->data_; };
|
||||
void rewind() { this->position_ = 0; }
|
||||
void reset() { this->position_ = this->mark_; }
|
||||
|
||||
protected:
|
||||
ByteBuffer(std::vector<uint8_t> const &data) : data_(data), limit_(data.size()) {}
|
||||
std::vector<uint8_t> data_;
|
||||
Endian endianness_{LITTLE};
|
||||
size_t position_{0};
|
||||
size_t mark_{0};
|
||||
size_t limit_{0};
|
||||
};
|
||||
|
||||
} // namespace esphome
|
|
@ -10,6 +10,7 @@
|
|||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <strings.h>
|
||||
|
||||
#ifdef USE_HOST
|
||||
#ifndef _WIN32
|
||||
|
@ -188,37 +189,39 @@ uint32_t fnv1_hash(const std::string &str) {
|
|||
return hash;
|
||||
}
|
||||
|
||||
uint32_t random_uint32() {
|
||||
#ifdef USE_ESP32
|
||||
return esp_random();
|
||||
uint32_t random_uint32() { return esp_random(); }
|
||||
#elif defined(USE_ESP8266)
|
||||
return os_random();
|
||||
uint32_t random_uint32() { return os_random(); }
|
||||
#elif defined(USE_RP2040)
|
||||
uint32_t random_uint32() {
|
||||
uint32_t result = 0;
|
||||
for (uint8_t i = 0; i < 32; i++) {
|
||||
result <<= 1;
|
||||
result |= rosc_hw->randombit;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#elif defined(USE_LIBRETINY)
|
||||
return rand();
|
||||
uint32_t random_uint32() { return rand(); }
|
||||
#elif defined(USE_HOST)
|
||||
uint32_t random_uint32() {
|
||||
std::random_device dev;
|
||||
std::mt19937 rng(dev());
|
||||
std::uniform_int_distribution<uint32_t> dist(0, std::numeric_limits<uint32_t>::max());
|
||||
return dist(rng);
|
||||
#else
|
||||
#error "No random source available for this configuration."
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
float random_float() { return static_cast<float>(random_uint32()) / static_cast<float>(UINT32_MAX); }
|
||||
bool random_bytes(uint8_t *data, size_t len) {
|
||||
#ifdef USE_ESP32
|
||||
bool random_bytes(uint8_t *data, size_t len) {
|
||||
esp_fill_random(data, len);
|
||||
return true;
|
||||
}
|
||||
#elif defined(USE_ESP8266)
|
||||
return os_get_random(data, len) == 0;
|
||||
bool random_bytes(uint8_t *data, size_t len) { return os_get_random(data, len) == 0; }
|
||||
#elif defined(USE_RP2040)
|
||||
bool random_bytes(uint8_t *data, size_t len) {
|
||||
while (len-- != 0) {
|
||||
uint8_t result = 0;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
|
@ -228,10 +231,14 @@ bool random_bytes(uint8_t *data, size_t len) {
|
|||
*data++ = result;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#elif defined(USE_LIBRETINY)
|
||||
bool random_bytes(uint8_t *data, size_t len) {
|
||||
lt_rand_bytes(data, len);
|
||||
return true;
|
||||
}
|
||||
#elif defined(USE_HOST)
|
||||
bool random_bytes(uint8_t *data, size_t len) {
|
||||
FILE *fp = fopen("/dev/urandom", "r");
|
||||
if (fp == nullptr) {
|
||||
ESP_LOGW(TAG, "Could not open /dev/urandom, errno=%d", errno);
|
||||
|
@ -244,10 +251,8 @@ bool random_bytes(uint8_t *data, size_t len) {
|
|||
}
|
||||
fclose(fp);
|
||||
return true;
|
||||
#else
|
||||
#error "No random source available for this configuration."
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
// Strings
|
||||
|
||||
|
@ -619,11 +624,13 @@ void hsv_to_rgb(int hue, float saturation, float value, float &red, float &green
|
|||
#if defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_HOST)
|
||||
// ESP8266 doesn't have mutexes, but that shouldn't be an issue as it's single-core and non-preemptive OS.
|
||||
Mutex::Mutex() {}
|
||||
Mutex::~Mutex() {}
|
||||
void Mutex::lock() {}
|
||||
bool Mutex::try_lock() { return true; }
|
||||
void Mutex::unlock() {}
|
||||
#elif defined(USE_ESP32) || defined(USE_LIBRETINY)
|
||||
Mutex::Mutex() { handle_ = xSemaphoreCreateMutex(); }
|
||||
Mutex::~Mutex() {}
|
||||
void Mutex::lock() { xSemaphoreTake(this->handle_, portMAX_DELAY); }
|
||||
bool Mutex::try_lock() { return xSemaphoreTake(this->handle_, 0) == pdTRUE; }
|
||||
void Mutex::unlock() { xSemaphoreGive(this->handle_); }
|
||||
|
@ -657,11 +664,13 @@ void HighFrequencyLoopRequester::stop() {
|
|||
}
|
||||
bool HighFrequencyLoopRequester::is_high_frequency() { return num_requests > 0; }
|
||||
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
#if defined(USE_HOST)
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
static const uint8_t esphome_host_mac_address[6] = USE_ESPHOME_HOST_MAC_ADDRESS;
|
||||
memcpy(mac, esphome_host_mac_address, sizeof(esphome_host_mac_address));
|
||||
}
|
||||
#elif defined(USE_ESP32)
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
#if defined(CONFIG_SOC_IEEE802154_SUPPORTED)
|
||||
// When CONFIG_SOC_IEEE802154_SUPPORTED is defined, esp_efuse_mac_get_default
|
||||
// returns the 802.15.4 EUI-64 address, so we read directly from eFuse instead.
|
||||
|
@ -677,16 +686,20 @@ void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parame
|
|||
esp_efuse_mac_get_default(mac);
|
||||
}
|
||||
#endif
|
||||
#elif defined(USE_ESP8266)
|
||||
wifi_get_macaddr(STATION_IF, mac);
|
||||
#elif defined(USE_RP2040) && defined(USE_WIFI)
|
||||
WiFi.macAddress(mac);
|
||||
#elif defined(USE_LIBRETINY)
|
||||
WiFi.macAddress(mac);
|
||||
#else
|
||||
// this should be an error, but that messes with CI checks. #error No mac address method defined
|
||||
#endif
|
||||
}
|
||||
#elif defined(USE_ESP8266)
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
wifi_get_macaddr(STATION_IF, mac);
|
||||
}
|
||||
#elif defined(USE_RP2040) && defined(USE_WIFI)
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
WiFi.macAddress(mac);
|
||||
}
|
||||
#elif defined(USE_LIBRETINY)
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
WiFi.macAddress(mac);
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string get_mac_address() {
|
||||
uint8_t mac[6];
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
|
||||
#include "esphome/core/optional.h"
|
||||
|
||||
|
@ -545,6 +546,7 @@ class Mutex {
|
|||
public:
|
||||
Mutex();
|
||||
Mutex(const Mutex &) = delete;
|
||||
~Mutex();
|
||||
void lock();
|
||||
bool try_lock();
|
||||
void unlock();
|
||||
|
@ -554,6 +556,8 @@ class Mutex {
|
|||
private:
|
||||
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
|
||||
SemaphoreHandle_t handle_;
|
||||
#else
|
||||
void *handle_; // d-pointer to store private data on new platforms
|
||||
#endif
|
||||
};
|
||||
|
||||
|
|
161
tests/components/bytebuffer/common.yaml
Normal file
161
tests/components/bytebuffer/common.yaml
Normal file
|
@ -0,0 +1,161 @@
|
|||
bytebuffer:
|
||||
|
||||
esphome:
|
||||
on_boot:
|
||||
- lambda: |-
|
||||
using namespace bytebuffer;
|
||||
auto buf = ByteBuffer(16);
|
||||
assert(buf.get_endianness() == LITTLE);
|
||||
assert(buf.get_remaining() == 16);
|
||||
buf.set_limit(10);
|
||||
assert(buf.get_capacity() == 16);
|
||||
buf.put_uint8(1);
|
||||
assert(buf.get_remaining() == 9);
|
||||
buf.put_uint16(0xABCD);
|
||||
auto da = buf.get_data();
|
||||
assert(buf.get_uint8(0) == 1);
|
||||
auto x = buf.get_uint16(1);
|
||||
assert(buf.get_uint16(1) == 0xABCD);
|
||||
assert(buf.get_remaining() == 7);
|
||||
buf.put_uint32(0x12345678UL);
|
||||
assert(buf.get_uint32(3) == 0x12345678UL);
|
||||
assert(buf.get_remaining() == 3);
|
||||
assert(buf.get_data()[1] == 0xCD);
|
||||
assert(buf.get_data()[2] == 0xAB);
|
||||
assert(buf.get_data()[3] == 0x78);
|
||||
assert(buf.get_data()[4] == 0x56);
|
||||
assert(buf.get_data()[5] == 0x34);
|
||||
assert(buf.get_data()[6] == 0x12);
|
||||
buf.flip();
|
||||
assert(buf.get_capacity() == 16);
|
||||
assert(buf.get_uint32(3) == 0x12345678UL);
|
||||
assert(buf.get_uint8(0) == 1);
|
||||
assert(buf.get_uint16(1) == 0xABCD);
|
||||
buf.put_uint16(0x1234, 1);
|
||||
assert(buf.get_uint16(1) == 0x1234);
|
||||
assert(buf.get_remaining() == 7);
|
||||
assert(buf.get_uint8() == 1);
|
||||
assert(buf.get_uint16() == 0x1234);
|
||||
assert(buf.get_uint32() == 0x12345678ul);
|
||||
assert(buf.get_remaining() == 0);
|
||||
assert(buf.get_remaining() == 0);
|
||||
buf.rewind();
|
||||
buf.big_endian();
|
||||
assert(buf.get_remaining() == 7);
|
||||
assert(buf.get_uint8() == 1);
|
||||
assert(buf.get_uint16() == 0x3412);
|
||||
buf.mark();
|
||||
assert(buf.get_uint32() == 0x78563412ul);
|
||||
assert(buf.get_remaining() == 0);
|
||||
buf.reset();
|
||||
assert(buf.get_remaining() == 4);
|
||||
assert(buf.get_uint32() == 0x78563412ul);
|
||||
auto buf1 = ByteBuffer::wrap(buf.get_data().data(), buf.get_limit());
|
||||
buf.clear();
|
||||
assert(buf.get_position() == 0);
|
||||
assert(buf.get_capacity() == 16);
|
||||
assert(buf.get_limit() == 16);
|
||||
assert(buf1.get_remaining() == 7);
|
||||
assert(buf1.get_capacity() == 7);
|
||||
buf1.set_position(3);
|
||||
assert(buf1.get_uint32() == 0x12345678ul);
|
||||
buf1.clear();
|
||||
assert(buf1.get_limit() == 7);
|
||||
assert(buf1.get_capacity() == 7);
|
||||
assert(buf1.get_position() == 0);
|
||||
float f = 1.2345;
|
||||
buf1.put_float(f);
|
||||
buf1.flip();
|
||||
assert(buf1.get_remaining() == 4);
|
||||
assert(buf1.get_float() == f);
|
||||
buf1.clear();
|
||||
buf1.put_uint16(-32760);
|
||||
buf1.put_uint24(-302760);
|
||||
buf1.flip();
|
||||
assert(buf1.get_int16() == -32760);
|
||||
assert(buf1.get_int24() == -302760);
|
||||
uint8_t arr[4] = {0x10, 0x20, 0x30, 0x40};
|
||||
buf1 = ByteBuffer::wrap(arr, 4);
|
||||
assert(buf1.get_capacity() == 4);
|
||||
assert(buf1.get_limit() == 4);
|
||||
assert(buf1.get_position() == 0);
|
||||
assert(buf1.get_uint32() == 0x40302010UL);
|
||||
assert(buf1.get_position() == 4);
|
||||
assert(buf1.get_remaining() == 0);
|
||||
std::vector<uint8_t> vec{};
|
||||
vec.push_back(0x10);
|
||||
vec.push_back(0x20);
|
||||
vec.push_back(0x30);
|
||||
vec.push_back(0x40);
|
||||
buf1 = ByteBuffer::wrap(vec);
|
||||
assert(buf1.get_capacity() == 4);
|
||||
assert(buf1.get_limit() == 4);
|
||||
assert(buf1.get_position() == 0);
|
||||
buf1.mark();
|
||||
buf1.reset();
|
||||
assert(buf1.get_uint32() == 0x40302010UL);
|
||||
buf = ByteBuffer::wrap(true);
|
||||
assert(buf.get_bool() == true);
|
||||
buf = ByteBuffer::wrap((uint8_t)0xFE);
|
||||
assert(buf.get_uint8() == 0xFE);
|
||||
buf = ByteBuffer::wrap((uint16_t)0xA5A6, BIG);
|
||||
assert(buf.get_remaining() == 2);
|
||||
assert(buf.get_position() == 0);
|
||||
assert(buf.get_capacity() == 2);
|
||||
assert(buf.get_endianness() == BIG);
|
||||
assert(buf.get_data()[0] == 0xA5);
|
||||
assert(buf.get_uint16() == 0xA5A6);
|
||||
buf.flip();
|
||||
buf.little_endian();
|
||||
assert(buf.get_uint16() == 0xA6A5);
|
||||
buf = ByteBuffer::wrap(f, BIG);
|
||||
assert(buf.get_float() == f);
|
||||
double d = 1.2345678E7;
|
||||
buf = ByteBuffer::wrap(d, BIG);
|
||||
assert(buf.get_double() == d);
|
||||
buf = ByteBuffer::wrap({1, 2, 3, 4}, BIG);
|
||||
assert(buf.get_endianness() == BIG);
|
||||
assert(buf.get_remaining() == 4);
|
||||
assert(buf.get_data()[2] == 3);
|
||||
buf.little_endian();
|
||||
assert(buf.get_data()[2] == 3);
|
||||
assert(buf.get_uint16() == 0x0201);
|
||||
buf.big_endian();
|
||||
assert(buf.get_uint16() == 0x0304);
|
||||
buf.rewind();
|
||||
vec = buf.get_vector(3);
|
||||
assert(buf.get_remaining() == 1);
|
||||
assert(vec[0] == 1);
|
||||
assert(vec.size() == 3);
|
||||
buf = ByteBuffer(10);
|
||||
buf.put_vector(vec);
|
||||
assert(buf.get_remaining() == 7);
|
||||
buf.flip();
|
||||
assert(buf.get_remaining() == 3);
|
||||
assert(buf.get_uint24() == 0x030201);
|
||||
buf = ByteBuffer(64);
|
||||
buf.put_uint8(1, 1);
|
||||
buf.put_uint16(16, 2);
|
||||
buf.put_uint32(1232, 4);
|
||||
buf.put_uint64(123432ul, 8);
|
||||
buf.put_float(1.2f, 16);
|
||||
buf.put_int24(0x678, 20);
|
||||
|
||||
assert(buf.get_uint8(1) == 1);
|
||||
assert(buf.get<uint8_t>(1) == 1);
|
||||
assert(buf.get_uint16(2) == 16);
|
||||
assert(buf.get<uint16_t>(2) == 16);
|
||||
assert(buf.get_uint32(4) == 1232);
|
||||
assert(buf.get<uint32_t>(4) == 1232);
|
||||
assert(buf.get_uint64(8) == 123432ul);
|
||||
assert(buf.get<uint64_t>(8) == 123432ul);
|
||||
assert(buf.get_float(16) == 1.2f);
|
||||
assert(buf.get<float>(16) == 1.2f);
|
||||
assert(buf.get_int24(20) == 0x678);
|
||||
buf.clear();
|
||||
buf.put(1.234, 10);
|
||||
double dx = buf.get<double>(10);
|
||||
assert(dx == 1.234);
|
||||
buf.put((uint16_t)1, 10);
|
||||
assert(buf.get_uint16(10) == 1);
|
||||
ESP_LOGD("bytebuffer", "******************** All tests succeeded");
|
1
tests/components/bytebuffer/test.esp32-ard.yaml
Normal file
1
tests/components/bytebuffer/test.esp32-ard.yaml
Normal file
|
@ -0,0 +1 @@
|
|||
!include common.yaml
|
1
tests/components/bytebuffer/test.esp32-c3-ard.yaml
Normal file
1
tests/components/bytebuffer/test.esp32-c3-ard.yaml
Normal file
|
@ -0,0 +1 @@
|
|||
!include common.yaml
|
1
tests/components/bytebuffer/test.esp32-c3-idf.yaml
Normal file
1
tests/components/bytebuffer/test.esp32-c3-idf.yaml
Normal file
|
@ -0,0 +1 @@
|
|||
!include common.yaml
|
1
tests/components/bytebuffer/test.esp32-idf.yaml
Normal file
1
tests/components/bytebuffer/test.esp32-idf.yaml
Normal file
|
@ -0,0 +1 @@
|
|||
!include common.yaml
|
1
tests/components/bytebuffer/test.esp8266-ard.yaml
Normal file
1
tests/components/bytebuffer/test.esp8266-ard.yaml
Normal file
|
@ -0,0 +1 @@
|
|||
!include common.yaml
|
1
tests/components/bytebuffer/test.host.yaml
Normal file
1
tests/components/bytebuffer/test.host.yaml
Normal file
|
@ -0,0 +1 @@
|
|||
!include common.yaml
|
1
tests/components/bytebuffer/test.rp2040-ard.yaml
Normal file
1
tests/components/bytebuffer/test.rp2040-ard.yaml
Normal file
|
@ -0,0 +1 @@
|
|||
!include common.yaml
|
|
@ -458,6 +458,18 @@ lvgl:
|
|||
|
||||
- id: page2
|
||||
widgets:
|
||||
- qrcode:
|
||||
id: lv_qr
|
||||
align: left_mid
|
||||
size: 100
|
||||
light_color: whitesmoke
|
||||
dark_color: steelblue
|
||||
text: esphome.io
|
||||
on_click:
|
||||
lvgl.qrcode.update:
|
||||
id: lv_qr
|
||||
text: homeassistant.io
|
||||
|
||||
- slider:
|
||||
min_value: 0
|
||||
max_value: 255
|
||||
|
|
|
@ -14,3 +14,20 @@ sensor:
|
|||
name: Propane test distance
|
||||
battery_level:
|
||||
name: Propane test battery level
|
||||
|
||||
- platform: mopeka_pro_check
|
||||
mac_address: AA:BB:CC:DD:EE:FF
|
||||
tank_type: 20LB_V
|
||||
temperature:
|
||||
name: "Propane test2 temp"
|
||||
level:
|
||||
name: "Propane test2 level"
|
||||
distance:
|
||||
name: "Propane test2 distance"
|
||||
battery_level:
|
||||
name: "Propane test2 battery level"
|
||||
signal_quality:
|
||||
name: "propane test2 read quality"
|
||||
ignored_reads:
|
||||
name: "propane test2 ignored reads"
|
||||
minimum_signal_quality: "LOW"
|
||||
|
|
|
@ -13,9 +13,23 @@ sensor:
|
|||
}
|
||||
update_interval: 60s
|
||||
|
||||
text_sensor:
|
||||
- platform: template
|
||||
id: template_text_sensor1
|
||||
lambda: |-
|
||||
if (millis() > 10000) {
|
||||
return {"Hello World"};
|
||||
} else {
|
||||
return {"Goodbye (cruel) World"};
|
||||
}
|
||||
update_interval: 60s
|
||||
|
||||
prometheus:
|
||||
include_internal: true
|
||||
relabel:
|
||||
template_sensor1:
|
||||
id: hellow_world
|
||||
name: Hello World
|
||||
template_text_sensor1:
|
||||
id: hello_text
|
||||
name: Text Substitution
|
||||
|
|
Loading…
Reference in a new issue