Skip to content

Commit

Permalink
Merge pull request #151 from MalteSchm/huawei_lint_pin_mgr_fixes
Browse files Browse the repository at this point in the history
Huawei lint pin mgr fixes
  • Loading branch information
berni2288 committed Apr 2, 2023
2 parents b3c17c8 + d1e43c1 commit ebaf5c4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
6 changes: 3 additions & 3 deletions include/Huawei_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ class HuaweiCanClass {
void setPower(bool power);

RectifierParameters_t * get();
unsigned long getLastUpdate();
uint32_t getLastUpdate();

private:
void sendRequest();
void onReceive(uint8_t* frame, uint8_t len);

unsigned long previousMillis;
unsigned long lastUpdate;
uint32_t previousMillis;
uint32_t lastUpdate;
RectifierParameters_t _rp;

SPIClass *spi;
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ build_flags = ${env.build_flags}
-DHOYMILES_PIN_CE=4
-DHOYMILES_PIN_CS=5
-DVICTRON_PIN_TX=21
-DVICTRON_PIN_RX=22
-DVICTRON_PIN_RX=22
-DPYLONTECH_PIN_RX=27
-DPYLONTECH_PIN_TX=14
-DHUAWEI_PIN_MISO=12
Expand Down
19 changes: 16 additions & 3 deletions src/Huawei_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
#include "Huawei_can.h"
#include "MessageOutput.h"
#include "Configuration.h"
#include <SPI.h>
#include <mcp_can.h>

Expand All @@ -14,6 +15,12 @@ HuaweiCanClass HuaweiCan;
void HuaweiCanClass::init(uint8_t huawei_miso, uint8_t huawei_mosi, uint8_t huawei_clk, uint8_t huawei_irq, uint8_t huawei_cs, uint8_t huawei_power)
{

const CONFIG_T& config = Configuration.get();

if (!config.Huawei_Enabled) {
return;
}

spi = new SPIClass(VSPI);
spi->begin(huawei_clk, huawei_miso, huawei_mosi, huawei_cs);
pinMode(huawei_cs, OUTPUT);
Expand Down Expand Up @@ -43,7 +50,7 @@ RectifierParameters_t * HuaweiCanClass::get()
return &_rp;
}

unsigned long HuaweiCanClass::getLastUpdate()
uint32_t HuaweiCanClass::getLastUpdate()
{
return lastUpdate;
}
Expand Down Expand Up @@ -142,10 +149,16 @@ void HuaweiCanClass::onReceive(uint8_t* frame, uint8_t len)
void HuaweiCanClass::loop()
{

long unsigned int rxId;
INT32U rxId;
unsigned char len = 0;
unsigned char rxBuf[8];


const CONFIG_T& config = Configuration.get();

if (!config.Huawei_Enabled) {
return;
}

if(!digitalRead(_huawei_irq)) // If CAN_INT pin is low, read receive buffer
{
CAN->readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
Expand Down
18 changes: 14 additions & 4 deletions src/PowerMeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,20 @@ void PowerMeterClass::init()

CONFIG_T& config = Configuration.get();

MqttSettings.subscribe(config.PowerMeter_MqttTopicPowerMeter1, 0, std::bind(&PowerMeterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
MqttSettings.subscribe(config.PowerMeter_MqttTopicPowerMeter2, 0, std::bind(&PowerMeterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
MqttSettings.subscribe(config.PowerMeter_MqttTopicPowerMeter3, 0, std::bind(&PowerMeterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));

if (config.PowerMeter_Enabled && config.PowerMeter_Source == 0) {
if (strlen(config.PowerMeter_MqttTopicPowerMeter1) > 0) {
MqttSettings.subscribe(config.PowerMeter_MqttTopicPowerMeter1, 0, std::bind(&PowerMeterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
}

if (strlen(config.PowerMeter_MqttTopicPowerMeter2) > 0) {
MqttSettings.subscribe(config.PowerMeter_MqttTopicPowerMeter2, 0, std::bind(&PowerMeterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
}

if (strlen(config.PowerMeter_MqttTopicPowerMeter3) > 0) {
MqttSettings.subscribe(config.PowerMeter_MqttTopicPowerMeter3, 0, std::bind(&PowerMeterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
}
}

mqttInitDone = true;

sdm.begin();
Expand Down
16 changes: 15 additions & 1 deletion src/WebApi_Huawei.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "WebApi_Huawei.h"
#include "Huawei_can.h"
#include "Configuration.h"
#include "MessageOutput.h"
#include "PinMapping.h"
#include "WebApi.h"
#include "WebApi_errors.h"
#include <AsyncJson.h>
Expand Down Expand Up @@ -198,7 +200,7 @@ void WebApiHuaweiClass::onAdminPost(AsyncWebServerRequest* request)
if (!WebApi.checkCredentials(request)) {
return;
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
retMsg[F("type")] = F("warning");
Expand Down Expand Up @@ -251,5 +253,17 @@ void WebApiHuaweiClass::onAdminPost(AsyncWebServerRequest* request)
response->setLength();
request->send(response);

const PinMapping_t& pin = PinMapping.get();
if (config.Huawei_Enabled) {
MessageOutput.println(F("Initialize Huawei AC charger interface... "));
if (PinMapping.isValidHuaweiConfig()) {
MessageOutput.printf("Huawei AC-charger miso = %d, mosi = %d, clk = %d, irq = %d, cs = %d, power_pin = %d\r\n", pin.huawei_miso, pin.huawei_mosi, pin.huawei_clk, pin.huawei_irq, pin.huawei_cs, pin.huawei_power);
HuaweiCan.init(pin.huawei_miso, pin.huawei_mosi, pin.huawei_clk, pin.huawei_irq, pin.huawei_cs, pin.huawei_power);
MessageOutput.println(F("done"));
} else {
MessageOutput.println(F("Invalid pin config"));
}
}

HuaweiCan.setPower(config.Huawei_Enabled);
}
8 changes: 8 additions & 0 deletions src/WebApi_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ void WebApiDeviceClass::onDeviceAdminGet(AsyncWebServerRequest* request)
batteryPinObj[F("rx")] = pin.battery_rx;
batteryPinObj[F("tx")] = pin.battery_tx;

JsonObject huaweiPinObj = curPin.createNestedObject("huawei");
huaweiPinObj[F("miso")] = pin.huawei_miso;
huaweiPinObj[F("mosi")] = pin.huawei_mosi;
huaweiPinObj[F("clk")] = pin.huawei_clk;
huaweiPinObj[F("irq")] = pin.huawei_irq;
huaweiPinObj[F("cs")] = pin.huawei_cs;
huaweiPinObj[F("power")] = pin.huawei_power;

response->setLength();
request->send(response);
}
Expand Down

0 comments on commit ebaf5c4

Please sign in to comment.