From e7a9e0316ebb1a52f270f2b61d44bc290b13e75c Mon Sep 17 00:00:00 2001 From: Jakob Gegeniger Date: Wed, 28 Oct 2026 21:19:48 +0100 Subject: [PATCH] wip --- platform | 2 +- src/sword/swordMain.cpp | 27 ++++++++++++++++++--------- src/sword/swordMain.hpp | 15 +++++++++++---- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/platform b/platform index 387735f..dd23ecf 160000 --- a/platform +++ b/platform @@ -1 +1 @@ -Subproject commit 387735f701e4ea682438247ff0ea435fbda294f2 +Subproject commit dd23ecf4a703a6f45d91b7f2d0b9fbe22c9be8ff diff --git a/src/sword/swordMain.cpp b/src/sword/swordMain.cpp index 9ff565e..77ecb76 100644 --- a/src/sword/swordMain.cpp +++ b/src/sword/swordMain.cpp @@ -78,15 +78,25 @@ int main(int argc, char* argv[]) { InputTransAware SwordEnd(sePort); InputTransAware SwordPulled(spPort); - I2cMaster i2cDev(II2cMaster::ClockRate::_400k); - AdcAds1115 adc(i2cDev, AdcAds1115::I2CAddress::Gnd); - // Check if the ADC is connected - if (!adc.isConnected()) { - fmt::println("Failed to connect ot Sensor {}", modeArg); - } - CurrMon mon(adc); - LinMot SwordLatch(mOut1, mOut2, mon, saveFile); + log.sendBootUp(Version, MqttChannel::Modes[static_cast(IoStat->Mode)]); + ICurrentMon* pMon = nullptr; + try { + static I2cMaster i2cDev(II2cMaster::ClockRate::_400k); + static AdcAds1115 adc(i2cDev, AdcAds1115::I2CAddress::Gnd); + // Check if the ADC is connected + if (!adc.isConnected()) { + fmt::println("Failed to connect ot Sensor {}", modeArg); + } + static CurrMon mon(adc); + pMon = &mon; + } catch (const std::exception& e) { + std::cerr << e.what() << '\n'; + log.sendEvent("HW-Err", "Failed to init II2-Hardware"); + static ICurrentMon fakeMon; + pMon = &fakeMon; + } + LinMot SwordLatch(mOut1, mOut2, *pMon, saveFile); IoStat = make_unique(SwordEntry, SwordEnd, SwordPulled, SwordLatch); fmt::println("{}", app.get_description()); @@ -96,7 +106,6 @@ int main(int argc, char* argv[]) { } else { fmt::println("unknown {}", modeArg); } - log.sendBootUp(Version, MqttChannel::Modes[static_cast(IoStat->Mode)]); MqttChannel mqtt(appName, Version, *SwordCfg, *IoStat, log); auto resetCall = [&]() { diff --git a/src/sword/swordMain.hpp b/src/sword/swordMain.hpp index cd3ba98..fb40725 100644 --- a/src/sword/swordMain.hpp +++ b/src/sword/swordMain.hpp @@ -33,7 +33,14 @@ #include "textUtils.hpp" #include "thread.hpp" -class CurrMon { +class ICurrentMon { + public: + virtual float sampleCurrent() { + return 0; + } +}; + +class CurrMon : public ICurrentMon { // https://www.monolithicpower.com/en/documentview/productdocument/index/version/2/document_type/Datasheet/lang/EN/sku/MCS1802/document_id/5066/ public: CurrMon(AdcAds1115& adc) @@ -48,7 +55,7 @@ class CurrMon { _refVal = val / 8; } - float sampleCurrent() { + float sampleCurrent() override { auto val = sample() - _refVal; val = val / 0.264; // fmt::println("{:.2} A", val); @@ -70,7 +77,7 @@ class LinMot { public: using SaveCall = void (*)(string, vector); - LinMot(OutputPort& o1, OutputPort& o2, CurrMon& mon, SaveCall call) + LinMot(OutputPort& o1, OutputPort& o2, ICurrentMon& mon, SaveCall call) : _out1(o1), _out2(o2), _mon(mon), @@ -92,7 +99,7 @@ class LinMot { private: OutputPort& _out1; OutputPort& _out2; - CurrMon& _mon; + ICurrentMon& _mon; SaveCall _saveCb; const float FinThresh = 0.020; const float AbortThresh = 0.050;