"Hopefully" improved comms on sthome canbus.

This commit is contained in:
Chris Stuurman 2025-09-04 21:36:22 +02:00
parent ea865dfa94
commit d44b60f637
5 changed files with 88 additions and 24 deletions

View File

@ -236,6 +236,54 @@ namespace solar
}
// Helper function to convert four scaled values into a byte stream
// set scale to negative for signed values
std::vector<uint8_t> cb_frame::get_byte_stream(double value1, int scale1)
{
std::vector<uint8_t> byte_stream(2, 0);
int value;
if(scale1 != 0) {
value = (scale1 < 0) ? static_cast<int16_t>(value1 * -scale1) : static_cast<uint16_t>(value1 * scale1);
byte_stream[0] = value % 256;
byte_stream[1] = (value >> 8) % 256;
}
return byte_stream;
}
std::vector<uint8_t> cb_frame::get_byte_stream(double value1, int scale1, double value2, int scale2)
{
std::vector<uint8_t> byte_stream(4, 0);
int value;
if(scale1 != 0) {
value = (scale1 < 0) ? static_cast<int16_t>(value1 * -scale1) : static_cast<uint16_t>(value1 * scale1);
byte_stream[0] = value % 256;
byte_stream[1] = (value >> 8) % 256;
}
if(scale2 != 0) {
value = (scale2 < 0) ? static_cast<int16_t>(value2 * -scale2) : static_cast<uint16_t>(value2 * scale2);
byte_stream[2] = value % 256;
byte_stream[3] = (value >> 8) % 256;
}
return byte_stream;
}
std::vector<uint8_t> cb_frame::get_byte_stream(double value1, int scale1, double value2, int scale2, double value3, int scale3)
{
std::vector<uint8_t> byte_stream(6, 0);
int value;
if(scale1 != 0) {
value = (scale1 < 0) ? static_cast<int16_t>(value1 * -scale1) : static_cast<uint16_t>(value1 * scale1);
byte_stream[0] = value % 256;
byte_stream[1] = (value >> 8) % 256;
}
if(scale2 != 0) {
value = (scale2 < 0) ? static_cast<int16_t>(value2 * -scale2) : static_cast<uint16_t>(value2 * scale2);
byte_stream[2] = value % 256;
byte_stream[3] = (value >> 8) % 256;
}
if(scale3 != 0) {
value = (scale3 < 0) ? static_cast<int16_t>(value3 * -scale3) : static_cast<uint16_t>(value3 * scale3);
byte_stream[4] = value % 256;
byte_stream[5] = (value >> 8) % 256;
}
return byte_stream;
}
std::vector<uint8_t> cb_frame::get_byte_stream(double value1, int scale1, double value2, int scale2, double value3, int scale3, double value4, int scale4)
{
//ESP_LOGI("cb_frame::get_byte_stream", "1: %.3f 2: %.3f 3: %.3f 4: %.3f", value1, value2, value3, value4);

View File

@ -47,7 +47,10 @@ namespace solar
/// Helper functions to convert four scaled values into a byte stream
/// set scale to negative for signed values
static std::vector<uint8_t> get_byte_stream(double value1, int scale1, double value2 = 0, int scale2 = 0, double value3 = 0, int scale3 = 0, double value4 = 0, int scale4 = 0);
static std::vector<uint8_t> get_byte_stream(double value1, int scale1);
static std::vector<uint8_t> get_byte_stream(double value1, int scale1, double value2, int scale2);
static std::vector<uint8_t> get_byte_stream(double value1, int scale1, double value2, int scale2, double value3, int scale3);
static std::vector<uint8_t> get_byte_stream(double value1, int scale1, double value2, int scale2, double value3, int scale3, double value4, int scale4);
/// Overloaded function to handle double scale for the fourth value
static std::vector<uint8_t> get_byte_stream(double value1, int scale1, double value2, int scale2, double value3, int scale3, double value4, double scale4);

View File

@ -12,21 +12,25 @@ namespace solar
{
return cache_map.size();
}
bool cbf_cache::hasitem(uint32_t can_id) const
bool cbf_cache::hasitem(uint32_t can_id, bool remote_transmission_request) const
{
return cache_map.find(can_id) != cache_map.end();
return hasitem(canid_rtr_t(can_id, remote_transmission_request));
}
bool cbf_cache::hasitem(canid_rtr_t key) const
{
return cache_map.find(key) != cache_map.end();
}
cbf_cache_item& cbf_cache::getitem(uint32_t can_id)
cbf_cache_item& cbf_cache::getitem(uint32_t can_id, bool remote_transmission_request)
{
return cache_map.at(can_id);
return cache_map.at(canid_rtr_t(can_id, remote_transmission_request));
}
const cbf_cache_item& cbf_cache::getitem(uint32_t can_id) const
const cbf_cache_item& cbf_cache::getitem(uint32_t can_id, bool remote_transmission_request) const
{
return cache_map.at(can_id);
return cache_map.at(canid_rtr_t(can_id, remote_transmission_request));
}
bool cbf_cache::additem(const cbf_store& storeitem)
{
const auto& ret = cache_map.emplace(storeitem.can_id, cbf_cache_item(storeitem));
const auto& ret = cache_map.emplace(canid_rtr_t(storeitem.can_id, storeitem.rtr), cbf_cache_item(storeitem));
if(ret.second) {
return false; // new item inserted, no publish
}
@ -44,17 +48,15 @@ namespace solar
return updateresult & cbf_store::cbf_updateresult::stu_PUBLISH;
}
item.update(storeitem);
//cache_map.erase(kvp.first);
//ret = cache_map.emplace(storeitem.can_id, item);
//if(!ret.second) {
// ESP_LOGE(item.store0.tag("== ST1 ERR == ").c_str(), "Error re-inserting item into cache_map");
// ESP_LOGE(item.store1.tag("== ST2 ERR == ").c_str(), "Error re-inserting item into cache_map");
//}
return false;
}
const cb_frame& cbf_cache::get_frame(uint32_t can_id) const
const cb_frame& cbf_cache::get_frame(uint32_t can_id, bool remote_transmission_request) const
{
auto it = cache_map.find(can_id);
return get_frame(canid_rtr_t(can_id, remote_transmission_request));
}
const cb_frame& cbf_cache::get_frame(canid_rtr_t key) const
{
auto it = cache_map.find(key);
if(it == cache_map.end())
return emptyframe; // return reference to static empty frame if not found
const auto& item = it->second;
@ -62,13 +64,21 @@ namespace solar
}
bool cbf_cache::send_frame(esphome::canbus::Canbus *canbus, uint32_t can_id, bool extended_id, bool remote_transmission_request)
{
if(!this->hasitem(can_id))
auto key = canid_rtr_t(can_id, remote_transmission_request);
if(!this->hasitem(key))
return false;
const auto& cbf = get_frame(can_id);
const auto& cbf = get_frame(key);
if(cbf.getpublish()) {
return cbf.send_frame(canbus, extended_id, remote_transmission_request);
}
return false;
for(const auto& kvp : cache_map) {
const auto& item = kvp.second;
const auto& cbf = item.get_frame();
if(cbf.getpublish()) {
cbf.send_frame(canbus, extended_id, remote_transmission_request);
}
}
}
// static version to send arbitrary frame
bool cbf_cache::send_frame(esphome::canbus::Canbus *canbus, uint32_t can_id, const byte_vector& frame, bool extended_id, bool remote_transmission_request)

View File

@ -13,16 +13,19 @@ namespace solar
{
class cbf_cache {
public:
std::map<uint32_t, cbf_cache_item> cache_map; // map of CAN IDs to cache items
typedef std::pair<uint32_t, bool> canid_rtr_t; // pair of CAN ID and RTR flag
std::map<canid_rtr_t, cbf_cache_item> cache_map; // map of rtr/CAN IDs to cache items
cbf_cache() = default;
void clear();
int size() const;
bool hasitem(uint32_t can_id) const;
cbf_cache_item& getitem(uint32_t can_id);
const cbf_cache_item& getitem(uint32_t can_id) const;
bool hasitem(canid_rtr_t key) const;
bool hasitem(uint32_t can_id, bool remote_transmission_request) const;
cbf_cache_item& getitem(uint32_t can_id, bool remote_transmission_request);
const cbf_cache_item& getitem(uint32_t can_id, bool remote_transmission_request) const;
// Add a new item to the cache or update an existing one
bool additem(const cbf_store& item);
const cb_frame& get_frame(uint32_t can_id) const;
const cb_frame& get_frame(canid_rtr_t key) const;
const cb_frame& get_frame(uint32_t can_id, bool remote_transmission_request) const;
virtual ~cbf_cache() = default; // virtual destructor for base class
bool send_frame(esphome::canbus::Canbus *canbus, uint32_t can_id, bool extended_id = false, bool remote_transmission_request = false);
static bool send_frame(esphome::canbus::Canbus *canbus, uint32_t can_id, const byte_vector& frame, bool extended_id = false, bool remote_transmission_request = false); // static version to send arbitrary frame

View File

@ -151,7 +151,7 @@ namespace solar
break;
}
if(rtr) {
return "Request for unknown CAN ID info";
return "Request for unknown pylon CAN ID";
}
return "Unknown CAN ID";
}