Public Access
Revert to force linkage.
This commit is contained in:
1 parent
74cdb3d56f
commit
4012ccaa96
2 files changed
+87
-150
No files matched your search
@@ -9,11 +9,10 @@ namespace ups_hid {
|
||||
|
||||
static const char *const GM_TAG = "ups_hid.goldenmate";
|
||||
|
||||
// Register for vendor 0x075D with a UNIQUE macro name identifier
|
||||
REGISTER_UPS_PROTOCOL_FOR_VENDOR(
|
||||
0x075D, GoldenMateProtocol_075D,
|
||||
[](UpsHidComponent *p) { return std::make_unique<GoldenMateProtocol>(p); },
|
||||
"GoldenMate", "UPS Pro", 200);
|
||||
// Explicit global creator function to satisfy the factory's extern requirement
|
||||
std::unique_ptr<UpsProtocolBase> create_goldenmate_protocol(UpsHidComponent* parent) {
|
||||
return std::make_unique<GoldenMateProtocol>(parent);
|
||||
}
|
||||
|
||||
bool GoldenMateProtocol::read_feature_report(uint8_t report_id, HidReport &report) {
|
||||
if (!parent_->is_device_connected()) {
|
||||
@@ -57,11 +56,9 @@ bool GoldenMateProtocol::initialize() {
|
||||
data.device.usb_vendor_id = parent_->get_vendor_id();
|
||||
data.device.usb_product_id = parent_->get_product_id();
|
||||
|
||||
// Set actual names
|
||||
data.device.manufacturer = "GoldenMate";
|
||||
data.device.model = "UPS Pro";
|
||||
|
||||
// Get Serial Number from descriptor 3
|
||||
std::string str;
|
||||
if (parent_->get_string_descriptor(3, str) == ESP_OK && !str.empty()) {
|
||||
data.device.serial_number = str;
|
||||
@@ -79,8 +76,6 @@ bool GoldenMateProtocol::initialize() {
|
||||
}
|
||||
|
||||
bool GoldenMateProtocol::parse_binary_status(const HidReport &/*report*/, UpsData &/*data*/) {
|
||||
// We ignore the binary runtime here to prevent the 16-bit overflow bug
|
||||
// The true runtime will be grabbed from the ASCII string instead.
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -90,32 +85,23 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData &
|
||||
const uint8_t *d = report.data.data();
|
||||
std::string packed;
|
||||
|
||||
// Extract exactly bytes 30 to 61 without shifting
|
||||
for (int i = 30; i < 62; i++) {
|
||||
char c = static_cast<char>(d[i]);
|
||||
if (c >= '0' && c <= '9') {
|
||||
packed += c;
|
||||
} else if (c == ' ' || c == 0x00) {
|
||||
// Normalize spaces/nulls to zeros
|
||||
packed += '0';
|
||||
} else {
|
||||
// If we hit random garbage (like 0xFF), the buffer is mid-write!
|
||||
ESP_LOGW(GM_TAG, "Garbage byte detected (0x%02X). Hardware buffer is mid-write, dropping read.", d[i]);
|
||||
return false; // Instantly abort to prevent string shifting
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (packed.size() != 32) {
|
||||
return false;
|
||||
}
|
||||
if (packed.size() != 32) return false;
|
||||
|
||||
// Extract the live values safely
|
||||
// NOTE: Verify if substr(0,4) is actually runtime or current as per the header comments
|
||||
float runtime_mins = std::atof(packed.substr(0, 4).c_str());
|
||||
float battery_pct = std::atof(packed.substr(12, 3).c_str());
|
||||
|
||||
// --- SANITY CHECK FILTER ---
|
||||
// Catch perfectly formatted transient zeroes from wiped buffers
|
||||
if (battery_pct == 0.0f || runtime_mins == 0.0f) {
|
||||
ESP_LOGW(GM_TAG, "Transient hardware read (0 values detected). Ignoring to prevent graph dips.");
|
||||
return false;
|
||||
@@ -124,17 +110,14 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData &
|
||||
data.battery.runtime_minutes = runtime_mins;
|
||||
data.battery.level = battery_pct;
|
||||
|
||||
// Parse Status
|
||||
std::string status_str = packed.substr(24, 8);
|
||||
bool on_battery = (status_str.length() > 0 && status_str[0] == '1');
|
||||
|
||||
// --- TIME-BASED DEBOUNCE LOGIC ---
|
||||
if (battery_pct >= 100.0f) {
|
||||
this->is_full_ = true;
|
||||
this->below_100_count_ = 0; // Reset counter immediately on a 100% read
|
||||
this->below_100_count_ = 0;
|
||||
} else {
|
||||
this->below_100_count_++;
|
||||
// Require 2 consecutive reads (~20 seconds) below 100% to drop the 'Full' status
|
||||
if (this->below_100_count_ >= 2) {
|
||||
this->is_full_ = false;
|
||||
}
|
||||
@@ -143,8 +126,8 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData &
|
||||
if (on_battery) {
|
||||
data.power.status = "OB DISCHRG";
|
||||
data.battery.status = "Discharging";
|
||||
this->is_full_ = false; // Force reset if we lose AC power
|
||||
this->below_100_count_ = 2; // Keep counter aligned with state
|
||||
this->is_full_ = false;
|
||||
this->below_100_count_ = 2;
|
||||
} else {
|
||||
if (this->is_full_) {
|
||||
data.power.status = "OL";
|
||||
@@ -163,15 +146,10 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData &
|
||||
|
||||
bool GoldenMateProtocol::read_data(UpsData &data) {
|
||||
bool success = false;
|
||||
|
||||
// CRITICAL HARDWARE TRIGGER:
|
||||
// We MUST poll Report 0x01 first to force the firmware to refresh Report 0x0C
|
||||
HidReport status_report;
|
||||
// If this read times out, we still attempt the Megatec read below,
|
||||
// but it may return stale data if the hardware trigger failed.
|
||||
|
||||
read_feature_report(REPORT_ID_STATUS, status_report);
|
||||
|
||||
// Now read the freshly updated ASCII data from Report 0x0C
|
||||
HidReport megatec_report;
|
||||
if (read_feature_report(REPORT_ID_MEGATEC, megatec_report)) {
|
||||
if (parse_megatec_string(megatec_report, data)) {
|
||||
|
||||
Reference in new issue
Block a user