diff --git a/cb_frame.cpp b/cb_frame.cpp index 59c629b..389fdaa 100644 --- a/cb_frame.cpp +++ b/cb_frame.cpp @@ -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 cb_frame::get_byte_stream(double value1, int scale1) + { + std::vector byte_stream(2, 0); + int value; + if(scale1 != 0) { + value = (scale1 < 0) ? static_cast(value1 * -scale1) : static_cast(value1 * scale1); + byte_stream[0] = value % 256; + byte_stream[1] = (value >> 8) % 256; + } + return byte_stream; + } + std::vector cb_frame::get_byte_stream(double value1, int scale1, double value2, int scale2) + { + std::vector byte_stream(4, 0); + int value; + if(scale1 != 0) { + value = (scale1 < 0) ? static_cast(value1 * -scale1) : static_cast(value1 * scale1); + byte_stream[0] = value % 256; + byte_stream[1] = (value >> 8) % 256; + } + if(scale2 != 0) { + value = (scale2 < 0) ? static_cast(value2 * -scale2) : static_cast(value2 * scale2); + byte_stream[2] = value % 256; + byte_stream[3] = (value >> 8) % 256; + } + return byte_stream; + } + std::vector cb_frame::get_byte_stream(double value1, int scale1, double value2, int scale2, double value3, int scale3) + { + std::vector byte_stream(6, 0); + int value; + if(scale1 != 0) { + value = (scale1 < 0) ? static_cast(value1 * -scale1) : static_cast(value1 * scale1); + byte_stream[0] = value % 256; + byte_stream[1] = (value >> 8) % 256; + } + if(scale2 != 0) { + value = (scale2 < 0) ? static_cast(value2 * -scale2) : static_cast(value2 * scale2); + byte_stream[2] = value % 256; + byte_stream[3] = (value >> 8) % 256; + } + if(scale3 != 0) { + value = (scale3 < 0) ? static_cast(value3 * -scale3) : static_cast(value3 * scale3); + byte_stream[4] = value % 256; + byte_stream[5] = (value >> 8) % 256; + } + return byte_stream; + } std::vector 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); diff --git a/cb_frame.h b/cb_frame.h index d97889b..0646610 100644 --- a/cb_frame.h +++ b/cb_frame.h @@ -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 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 get_byte_stream(double value1, int scale1); + static std::vector get_byte_stream(double value1, int scale1, double value2, int scale2); + static std::vector get_byte_stream(double value1, int scale1, double value2, int scale2, double value3, int scale3); + static std::vector 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 get_byte_stream(double value1, int scale1, double value2, int scale2, double value3, int scale3, double value4, double scale4); diff --git a/cbf_cache.cpp b/cbf_cache.cpp index eb974de..9b17b17 100644 --- a/cbf_cache.cpp +++ b/cbf_cache.cpp @@ -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) diff --git a/cbf_cache.h b/cbf_cache.h index 26197b9..a88e414 100644 --- a/cbf_cache.h +++ b/cbf_cache.h @@ -13,16 +13,19 @@ namespace solar { class cbf_cache { public: - std::map cache_map; // map of CAN IDs to cache items + typedef std::pair canid_rtr_t; // pair of CAN ID and RTR flag + std::map 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 diff --git a/cbf_pylon.cpp b/cbf_pylon.cpp index 7fb38e5..abb9b35 100644 --- a/cbf_pylon.cpp +++ b/cbf_pylon.cpp @@ -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"; }