diff --git a/Software/Arduino_CSPS_Lib/KCORES_CSPS.cpp b/Software/Arduino_CSPS_Lib/KCORES_CSPS.cpp index 3a19da6..557f1ad 100644 --- a/Software/Arduino_CSPS_Lib/KCORES_CSPS.cpp +++ b/Software/Arduino_CSPS_Lib/KCORES_CSPS.cpp @@ -14,6 +14,7 @@ CSPS::CSPS(byte CSPS_addr) { _CSPS_addr = CSPS_addr; _ROM_addr = CSPS_addr - 8; + _CSPS_READ_CHECKSUM = false; Wire.begin(); } @@ -21,6 +22,16 @@ CSPS::CSPS(byte CSPS_addr, byte ROM_addr) { _CSPS_addr = CSPS_addr; _ROM_addr = ROM_addr; + _CSPS_READ_CHECKSUM = false; + Wire.begin(); +} + + +CSPS::CSPS(byte CSPS_addr, byte ROM_addr, bool ENABLE_CHECKSUM) +{ + _CSPS_addr = CSPS_addr; + _ROM_addr = ROM_addr; + _CSPS_READ_CHECKSUM = ENABLE_CHECKSUM; Wire.begin(); } @@ -44,6 +55,17 @@ byte CSPS::readROM(byte dataAddr) return rec; } +// Calclate checksum read from PM BUS +uint8_t checksum(uint32_t data) { + uint8_t cs = 0; + cs += ((data & 0xFF0000) >> 16); + cs += ((data & 0xFF00) >> 8); + cs += (data & 0xFF); + + cs = ((0xFF - cs) + 1) & 0xFF; + return cs; +} + uint32_t CSPS::readCSPSword(byte dataAddr) { byte regCS = ((0xFF - (dataAddr + (_CSPS_addr << 1))) + 1) & 0xFF; @@ -58,6 +80,20 @@ uint32_t CSPS::readCSPSword(byte dataAddr) rec = Wire.read(); rec |= Wire.read() << 8; rec |= Wire.read() << 16; + + if (_CSPS_READ_CHECKSUM) { + uint8_t cs = checksum(rec); + + if (cs != 0) { + // Output error to serial + if (Serial) { + Serial.printf("CSPS::readCSPSword() Missmatch checksum. addr: 0x%02x, chk: 0x%02x\n", dataAddr, cs); + } + } + + // Don't return checksum + return rec & 0x00FFFF; + } return rec; } else diff --git a/Software/Arduino_CSPS_Lib/KCORES_CSPS.h b/Software/Arduino_CSPS_Lib/KCORES_CSPS.h index 4fcae99..dadadfe 100644 --- a/Software/Arduino_CSPS_Lib/KCORES_CSPS.h +++ b/Software/Arduino_CSPS_Lib/KCORES_CSPS.h @@ -19,6 +19,7 @@ class CSPS public: CSPS(byte CSPS_addr); CSPS(byte CSPS_addr, byte ROM_addr); + CSPS(byte CSPS_addr, byte ROM_addr, bool ENABLE_CHECKSUM); String getROM(byte addr, byte len); @@ -114,6 +115,7 @@ class CSPS }; private: + bool _CSPS_READ_CHECKSUM; byte _CSPS_addr; byte _ROM_addr; byte readROM(byte dataAddr); diff --git a/Software/Arduino_CSPS_Lib/examples/ESPHome/README.md b/Software/Arduino_CSPS_Lib/examples/ESPHome/README.md new file mode 100644 index 0000000..b52d0f9 --- /dev/null +++ b/Software/Arduino_CSPS_Lib/examples/ESPHome/README.md @@ -0,0 +1,56 @@ +# ESPHome 集成 + +在 ESPHome 的 yaml 中增加 + +```yaml +esphome: + # 原先内容 + includes: + - CSPS/main.hpp + - CSPS/KCORES_CSPS.h + - CSPS/KCORES_CSPS.cpp +``` + +创建文件夹 `CSPS` 将 `main.hpp`, `KCORES_CSPS.h` 以及 `KCORES_CSPS.cpp` 丢进去 + +## 增加传感器 + +使用自定义传感器获取数据 + +```yaml +sensor: + - platform: custom + lambda: |- + auto power = new CSPSPower(); + App.register_component(power); + return {power->fan_speed, power->temp1, power->temp2, power->power_out, power->power_in, power->current_out, power->current_in, power->voltage_out, power->voltage_in}; + + sensors: + - name: "Fan Speed" + accuracy_decimals: 0 + unit_of_measurement: RPM + - name: "Temp 1" + accuracy_decimals: 1 + unit_of_measurement: °C + - name: "Temp 2" + accuracy_decimals: 1 + unit_of_measurement: °C + - name: "Power Out" + accuracy_decimals: 0 + unit_of_measurement: W + - name: "Power In" + accuracy_decimals: 0 + unit_of_measurement: W + - name: "Current Out" + accuracy_decimals: 1 + unit_of_measurement: A + - name: "Current In" + accuracy_decimals: 1 + unit_of_measurement: A + - name: "Voltage Out" + accuracy_decimals: 1 + unit_of_measurement: V + - name: "Voltage In" + accuracy_decimals: 1 + unit_of_measurement: V +``` diff --git a/Software/Arduino_CSPS_Lib/examples/ESPHome/main.cpp b/Software/Arduino_CSPS_Lib/examples/ESPHome/main.cpp new file mode 100644 index 0000000..b73749b --- /dev/null +++ b/Software/Arduino_CSPS_Lib/examples/ESPHome/main.cpp @@ -0,0 +1,44 @@ +#include "KCORES_CSPS.h" +#include "esphome.h" + +CSPS PowerSupply(0x5F, 0x57, true); + +class CSPSPower : public PollingComponent { + public: + Sensor *fan_speed = new Sensor(); + Sensor *temp1 = new Sensor(); + Sensor *temp2 = new Sensor(); + Sensor *power_in = new Sensor(); + Sensor *power_out = new Sensor(); + Sensor *current_out = new Sensor(); + Sensor *current_in = new Sensor(); + Sensor *voltage_out = new Sensor(); + Sensor *voltage_in = new Sensor(); + + CSPSPower(): PollingComponent(15000) { } + + void setup() override { + Wire.setClock(10000); + + ESP_LOGD("Power Supply", "Spare Part No: %s", PowerSupply.getSPN().c_str()); + ESP_LOGD("Power Supply", "Manufacture Date: %s", PowerSupply.getMFG().c_str()); + ESP_LOGD("Power Supply", "Manufacturer: %s", PowerSupply.getMFR().c_str()); + ESP_LOGD("Power Supply", "Power Name: %s", PowerSupply.getName().c_str()); + ESP_LOGD("Power Supply", "Option Kit No: %s", PowerSupply.getOKN().c_str()); + ESP_LOGD("Power Supply", "CT Date Codes: %s", PowerSupply.getCT().c_str()); + } + + void update() override { + // float current_out = PowerSupply.getOutputCurrent() / 256 / 256 / 256; + + fan_speed->publish_state(PowerSupply.getFanRPM()); + temp1->publish_state(PowerSupply.getTemp1()); + temp2->publish_state(PowerSupply.getTemp2()); + power_out->publish_state(PowerSupply.getOutputPower()); + power_in->publish_state(PowerSupply.getInputPower()); + current_out->publish_state(PowerSupply.getOutputCurrent()); + current_in->publish_state(PowerSupply.getInputCurrent()); + voltage_out->publish_state(PowerSupply.getOutputVoltage()); + voltage_in->publish_state(PowerSupply.getInputVoltage()); + } +};