From 6500b91a58fc52a2ba2697cc93c82ccb6b193a78 Mon Sep 17 00:00:00 2001 From: Jasem Mutlaq Date: Wed, 22 Jan 2025 13:27:18 +0300 Subject: [PATCH] Add isUpdated property function to know if a property would be updated by the passed parameters or not. It is convenient as sometimes we need to know if there is an actual change before we call the update function or save configuration for example. --- .vscode/launch.json | 2 +- drivers/telescope/telescope_simulator.cpp | 23 ++++++------ .../property/indipropertynumber.cpp | 6 ++++ libs/indidevice/property/indipropertynumber.h | 1 + .../property/indipropertyswitch.cpp | 6 ++++ libs/indidevice/property/indipropertyswitch.h | 1 + libs/indidevice/property/indipropertytext.cpp | 6 ++++ libs/indidevice/property/indipropertytext.h | 1 + libs/indidevice/property/indipropertyview.cpp | 36 +++++++++++++++++++ libs/indidevice/property/indipropertyview.h | 9 +++++ 10 files changed, 80 insertions(+), 11 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 485f9d9aa7..130ba4fc23 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "version": "0.2.0", "configurations": [ { - "name": "(gdb) Launch", + "name": "Debug INDI Driver", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/indiserver/indiserver", diff --git a/drivers/telescope/telescope_simulator.cpp b/drivers/telescope/telescope_simulator.cpp index 4bb4b3629a..a141c05c73 100644 --- a/drivers/telescope/telescope_simulator.cpp +++ b/drivers/telescope/telescope_simulator.cpp @@ -409,13 +409,16 @@ bool ScopeSim::ISNewNumber(const char *dev, const char *name, double values[], c #ifdef USE_SIM_TAB if (mountModelNP.isNameMatch(name)) { - mountModelNP.update(values, names, n); + if (mountModelNP.isUpdated(values, names, n)) + { + mountModelNP.update(values, names, n); + alignment.setCorrections(mountModelNP[MM_IH].getValue(), mountModelNP[MM_ID].getValue(), + mountModelNP[MM_CH].getValue(), mountModelNP[MM_NP].getValue(), + mountModelNP[MM_MA].getValue(), mountModelNP[MM_ME].getValue()); + saveConfig(mountModelNP); + } mountModelNP.setState(IPS_OK); mountModelNP.apply(); - alignment.setCorrections(mountModelNP[MM_IH].getValue(), mountModelNP[MM_ID].getValue(), - mountModelNP[MM_CH].getValue(), mountModelNP[MM_NP].getValue(), - mountModelNP[MM_MA].getValue(), mountModelNP[MM_ME].getValue()); - saveConfig(true, mountModelNP.getName()); return true; } @@ -492,11 +495,11 @@ bool ScopeSim::MoveNS(INDI_DIR_NS dir, TelescopeMotionCommand command) return false; } mcRate = static_cast(SlewRateSP.findOnSwitchIndex()) + 1; - mcRate = std::max(1,std::min(4,mcRate)); + mcRate = std::max(1, std::min(4, mcRate)); int rate = (dir == INDI_DIR_NS::DIRECTION_NORTH) ? mcRate : -mcRate; if (HasPierSide() & (currentPierSide == PIER_WEST)) // see scopesim_helper.cpp: alignment - rate = -rate; + rate = -rate; LOGF_DEBUG("MoveNS dir %s, motion %s, rate %d", dir == DIRECTION_NORTH ? "N" : "S", command == 0 ? "start" : "stop", rate); axisSecondary.mcRate = command == MOTION_START ? rate : 0; @@ -513,7 +516,7 @@ bool ScopeSim::MoveWE(INDI_DIR_WE dir, TelescopeMotionCommand command) } mcRate = static_cast(SlewRateSP.findOnSwitchIndex()) + 1; - mcRate = std::max(1,std::min(4,mcRate)); + mcRate = std::max(1, std::min(4, mcRate)); int rate = (dir == INDI_DIR_WE::DIRECTION_EAST) ? -mcRate : mcRate; LOGF_DEBUG("MoveWE dir %d, motion %s, rate %d", dir == DIRECTION_EAST ? "E" : "W", command == 0 ? "start" : "stop", rate); @@ -526,7 +529,7 @@ IPState ScopeSim::GuideNorth(uint32_t ms) { double rate = GuideRateNP[DEC_AXIS].getValue(); if (HasPierSide() & (currentPierSide == PIER_WEST)) // see scopsim_helper.cpp: alignment - rate = -rate; + rate = -rate; axisSecondary.StartGuide(rate, ms); guidingNS = true; return IPS_BUSY; @@ -536,7 +539,7 @@ IPState ScopeSim::GuideSouth(uint32_t ms) { double rate = GuideRateNP[DEC_AXIS].getValue(); if (HasPierSide() & (currentPierSide == PIER_WEST)) // see scopsim_helper.cpp: alignment - rate = -rate; + rate = -rate; axisSecondary.StartGuide(-rate, ms); guidingNS = true; return IPS_BUSY; diff --git a/libs/indidevice/property/indipropertynumber.cpp b/libs/indidevice/property/indipropertynumber.cpp index 6dd5fd4845..da3a31ca40 100644 --- a/libs/indidevice/property/indipropertynumber.cpp +++ b/libs/indidevice/property/indipropertynumber.cpp @@ -62,4 +62,10 @@ void PropertyNumber::updateMinMax() d->typedProperty.updateMinMax(); } +bool PropertyNumber::isUpdated(const double values[], const char * const names[], int n) const +{ + D_PTR(const PropertyNumber); + return d->typedProperty.isUpdated(values, names, n); +} + } diff --git a/libs/indidevice/property/indipropertynumber.h b/libs/indidevice/property/indipropertynumber.h index 7982fda10f..298d2bc955 100644 --- a/libs/indidevice/property/indipropertynumber.h +++ b/libs/indidevice/property/indipropertynumber.h @@ -34,6 +34,7 @@ class PropertyNumber: public INDI::PropertyBasic public: bool update(const double values[], const char * const names[], int n); + bool isUpdated(const double values[], const char * const names[], int n) const; void fill( const char *device, const char *name, const char *label, const char *group, diff --git a/libs/indidevice/property/indipropertyswitch.cpp b/libs/indidevice/property/indipropertyswitch.cpp index 05e7a4788f..6183685e47 100644 --- a/libs/indidevice/property/indipropertyswitch.cpp +++ b/libs/indidevice/property/indipropertyswitch.cpp @@ -121,6 +121,12 @@ const char * PropertySwitch::getRuleAsString() const return d->typedProperty.getRuleAsString(); } +bool PropertySwitch::isUpdated(const ISState states[], const char * const names[], int n) const +{ + D_PTR(const PropertySwitch); + return d->typedProperty.isUpdated(states, names, n); +} + void PropertySwitch::onNewValues(const std::function &callback) { D_PTR(PropertySwitch); diff --git a/libs/indidevice/property/indipropertyswitch.h b/libs/indidevice/property/indipropertyswitch.h index e63af449c5..a8a3875782 100644 --- a/libs/indidevice/property/indipropertyswitch.h +++ b/libs/indidevice/property/indipropertyswitch.h @@ -48,6 +48,7 @@ class PropertySwitch: public INDI::PropertyBasic public: bool update(const ISState states[], const char * const names[], int n); + bool isUpdated(const ISState states[], const char * const names[], int n) const; bool hasUpdateCallback() const; void fill( diff --git a/libs/indidevice/property/indipropertytext.cpp b/libs/indidevice/property/indipropertytext.cpp index 5d25fdb6e7..6c638d1a3e 100644 --- a/libs/indidevice/property/indipropertytext.cpp +++ b/libs/indidevice/property/indipropertytext.cpp @@ -56,4 +56,10 @@ void PropertyText::fill( d->typedProperty.fill(device, name, label, group, permission, timeout, state); } +bool PropertyText::isUpdated(const char * const texts[], const char * const names[], int n) const +{ + D_PTR(const PropertyText); + return d->typedProperty.isUpdated(texts, names, n); +} + } diff --git a/libs/indidevice/property/indipropertytext.h b/libs/indidevice/property/indipropertytext.h index d6880b5c0a..9bf0c23356 100644 --- a/libs/indidevice/property/indipropertytext.h +++ b/libs/indidevice/property/indipropertytext.h @@ -34,6 +34,7 @@ class PropertyText: public INDI::PropertyBasic public: bool update(const char * const texts[], const char * const names[], int n); + bool isUpdated(const char * const texts[], const char * const names[], int n) const; void fill( const char *device, const char *name, const char *label, const char *group, diff --git a/libs/indidevice/property/indipropertyview.cpp b/libs/indidevice/property/indipropertyview.cpp index 76456188e9..091f3e858c 100644 --- a/libs/indidevice/property/indipropertyview.cpp +++ b/libs/indidevice/property/indipropertyview.cpp @@ -265,6 +265,42 @@ void PropertyView::updateMinMax() WeakIUUpdateMinMax(this); } +template <> template<> +bool PropertyView::isUpdated(const char * const texts[], const char * const names[], int n) const +{ + for (int i = 0; i < n; i++) + { + auto widget = findWidgetByName(names[i]); + if (widget && strcmp(widget->getText(), texts[i]) != 0) + return true; + } + return false; +} + +template <> template<> +bool PropertyView::isUpdated(const double values[], const char * const names[], int n) const +{ + for (int i = 0; i < n; i++) + { + auto widget = findWidgetByName(names[i]); + if (widget && widget->getValue() != values[i]) + return true; + } + return false; +} + +template <> template<> +bool PropertyView::isUpdated(const ISState states[], const char * const names[], int n) const +{ + for (int i = 0; i < n; i++) + { + auto widget = findWidgetByName(names[i]); + if (widget && widget->getState() != states[i]) + return true; + } + return false; +} + void WidgetView::fill(const char *name, const char *label, const char *initialText) { IUFillText(this, name, label, initialText); diff --git a/libs/indidevice/property/indipropertyview.h b/libs/indidevice/property/indipropertyview.h index d15947c062..8b1969b6b4 100644 --- a/libs/indidevice/property/indipropertyview.h +++ b/libs/indidevice/property/indipropertyview.h @@ -326,6 +326,15 @@ struct PropertyView: PROPERTYVIEW_BASE_ACCESS WidgetTraits::PropertyType const char * const names[], int n ); /* outside implementation - only driver side, see indipropertyview_driver.cpp */ + template = true> + bool isUpdated(const char * const texts[], const char * const names[], int n) const; + + template = true> + bool isUpdated(const double values[], const char * const names[], int n) const; + + template = true> + bool isUpdated(const ISState states[], const char * const names[], int n) const; + public: WidgetType *begin() const