From 34867c9298477bcc25504e2bd8cd114f729340a1 Mon Sep 17 00:00:00 2001 From: Jasem Mutlaq Date: Fri, 26 Jul 2024 07:48:16 +0300 Subject: [PATCH] Simpliy interfaces and fix formatting issue --- drivers/auxiliary/waveshare_modbus_relay.cpp | 23 +++++++++----------- drivers/auxiliary/waveshare_modbus_relay.h | 2 +- libs/indibase/indiinputinterface.cpp | 13 ++++++----- libs/indibase/indiinputinterface.h | 13 ++++++----- libs/indibase/indioutputinterface.cpp | 12 +++++----- libs/indibase/indioutputinterface.h | 18 +++++---------- 6 files changed, 38 insertions(+), 43 deletions(-) diff --git a/drivers/auxiliary/waveshare_modbus_relay.cpp b/drivers/auxiliary/waveshare_modbus_relay.cpp index be585b4f61..5f45bee413 100644 --- a/drivers/auxiliary/waveshare_modbus_relay.cpp +++ b/drivers/auxiliary/waveshare_modbus_relay.cpp @@ -19,6 +19,7 @@ #include #include +#include #include static std::unique_ptr sesto(new WaveshareRelay()); @@ -108,8 +109,9 @@ bool WaveshareRelay::Handshake() err = nmbs_read_holding_registers(&nmbs, 0x8000, 1, &output); if (err == NMBS_ERROR_NONE) { - auto version = std::to_string(output / 100.0); - FirmwareVersionTP[0].setText(version); + std::stringstream ss; + ss << std::fixed << std::setprecision(2) << output / 100.0; + FirmwareVersionTP[0].setText(ss.str().c_str()); FirmwareVersionTP.setState(IPS_OK); return true; } @@ -124,7 +126,7 @@ bool WaveshareRelay::Handshake() //////////////////////////////////////////////////////////////////////////////////////////////////////// const char *WaveshareRelay::getDefaultName() { - return "Waveshare Output"; + return "Waveshare Relay"; } //////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -188,13 +190,12 @@ bool WaveshareRelay::UpdateDigitalOutputs() { for (size_t i = 0; i < DigitalOutputsSP.size(); i++) { - auto oldState = DigitalOutputsSP[i].findOnSwitchIndex() == 0 ? Opened : Closed; - auto newState = (nmbs_bitfield_read(coils, i) == 0) ? Closed : Opened; + auto oldState = DigitalOutputsSP[i].findOnSwitchIndex(); + auto newState = nmbs_bitfield_read(coils, i); if (oldState != newState) { - auto index = newState == Opened ? 0 : 1; DigitalOutputsSP[i].reset(); - DigitalOutputsSP[i][index].setState(ISS_ON); + DigitalOutputsSP[i][newState].setState(ISS_ON); DigitalOutputsSP[i].setState(IPS_OK); DigitalOutputsSP[i].apply(); } @@ -206,13 +207,9 @@ bool WaveshareRelay::UpdateDigitalOutputs() //////////////////////////////////////////////////////////////////////////////////////////////////////// /// //////////////////////////////////////////////////////////////////////////////////////////////////////// -bool WaveshareRelay::CommandOutput(uint32_t index, Command command) +bool WaveshareRelay::CommandOutput(uint32_t index, Status command) { - uint16_t value = 0; - if (command == Open) - value = 0xFF00; - else if (command == Flip) - value = 0x5500; + uint16_t value = (command == On) ? 0xFF00 : 0; auto err = nmbs_write_single_coil(&nmbs, index, value); if (err != NMBS_ERROR_NONE) diff --git a/drivers/auxiliary/waveshare_modbus_relay.h b/drivers/auxiliary/waveshare_modbus_relay.h index a8c9fd219f..812e63c868 100644 --- a/drivers/auxiliary/waveshare_modbus_relay.h +++ b/drivers/auxiliary/waveshare_modbus_relay.h @@ -52,7 +52,7 @@ class WaveshareRelay : public INDI::DefaultDevice, public INDI::OutputInterface * \brief Send command to relay * \return True if operation is successful, false otherwise */ - virtual bool CommandOutput(uint32_t index, Command command) override; + virtual bool CommandOutput(uint32_t index, Status command) override; virtual void TimerHit() override; virtual bool saveConfigItems(FILE *fp) override; diff --git a/libs/indibase/indiinputinterface.cpp b/libs/indibase/indiinputinterface.cpp index 7b28c10244..624f3d9142 100644 --- a/libs/indibase/indiinputinterface.cpp +++ b/libs/indibase/indiinputinterface.cpp @@ -42,7 +42,8 @@ InputInterface::~InputInterface() ///////////////////////////////////////////////////////////////////////////////////////////// /// ///////////////////////////////////////////////////////////////////////////////////////////// -void InputInterface::initProperties(const char *groupName, uint8_t digital, uint8_t analog, const std::string &digitalPrefix, const std::string &analogPrefix) +void InputInterface::initProperties(const char *groupName, uint8_t digital, uint8_t analog, + const std::string &digitalPrefix, const std::string &analogPrefix) { DigitalInputLabelsTP.reserve(digital); // Digital labels @@ -58,7 +59,8 @@ void InputInterface::initProperties(const char *groupName, uint8_t digital, uint if (digital > 0) { - DigitalInputLabelsTP.fill(m_defaultDevice->getDeviceName(), "DIGITAL_INPUT_LABELS", "Digital Labels", groupName, IP_RW, 60, IPS_IDLE); + DigitalInputLabelsTP.fill(m_defaultDevice->getDeviceName(), "DIGITAL_INPUT_LABELS", "Digital Labels", groupName, IP_RW, 60, + IPS_IDLE); DigitalInputLabelsTP.shrink_to_fit(); DigitalInputLabelsTP.load(); } @@ -77,7 +79,8 @@ void InputInterface::initProperties(const char *groupName, uint8_t digital, uint if (analog > 0) { - AnalogInputLabelsTP.fill(m_defaultDevice->getDeviceName(), "ANALOG_INPUT_LABELS", "Analog Labels", groupName, IP_RW, 60, IPS_IDLE); + AnalogInputLabelsTP.fill(m_defaultDevice->getDeviceName(), "ANALOG_INPUT_LABELS", "Analog Labels", groupName, IP_RW, 60, + IPS_IDLE); AnalogInputLabelsTP.shrink_to_fit(); AnalogInputLabelsTP.load(); } @@ -106,8 +109,8 @@ void InputInterface::initProperties(const char *groupName, uint8_t digital, uint auto label = digitalPrefix + " #" + std::to_string(i + 1); INDI::PropertySwitch oneInput {2}; - oneInput[On].fill("ON", "ON", ISS_OFF); - oneInput[Off].fill("OFF", "OFF", ISS_OFF); + oneInput[Off].fill("OFF", "Off", ISS_OFF); + oneInput[On].fill("ON", "On", ISS_OFF); if (i < DigitalInputLabelsTP.count()) label = DigitalInputLabelsTP[i].getText(); diff --git a/libs/indibase/indiinputinterface.h b/libs/indibase/indiinputinterface.h index 583436e212..baa8c89022 100644 --- a/libs/indibase/indiinputinterface.h +++ b/libs/indibase/indiinputinterface.h @@ -44,12 +44,14 @@ namespace INDI class InputInterface { public: - /*! Input boolean status. This is regardless on whether Input is Active low or high. */ + /*! Input boolean status. This is regardless on whether Input is Active low or high. + For relay, Off = Open Circuit. On = Closed Circuit + */ typedef enum { - On, /*!< Input is on. */ - Off, /*!< Input is off. */ - Unknown /*!< Could not determined switch status. */ + Off, /*!< Input is off. */ + On, /*!< Input is on. */ + Unknown /*!< Could not determined input status. */ } Status; /** @@ -81,7 +83,8 @@ class InputInterface * \param digitalPrefix Prefix used to label digital Inputs. By default, Digital #1, Digital #2..etc * \param analogPrefix Prefix used to label analog Inputs. By default, Analog #1, Analog #2..etc */ - void initProperties(const char *groupName, uint8_t digital, uint8_t analog, const std::string &digitalPrefix = "Digital", const std::string &analogPrefix = "Analog"); + void initProperties(const char *groupName, uint8_t digital, uint8_t analog, const std::string &digitalPrefix = "Digital", + const std::string &analogPrefix = "Analog"); /** * @brief updateProperties Defines or Delete properties based on default device connection status diff --git a/libs/indibase/indioutputinterface.cpp b/libs/indibase/indioutputinterface.cpp index 2825e9cdd8..2e42f7f7de 100644 --- a/libs/indibase/indioutputinterface.cpp +++ b/libs/indibase/indioutputinterface.cpp @@ -57,7 +57,8 @@ void OutputInterface::initProperties(const char *groupName, uint8_t Outputs, con DigitalOutputLabelsTP.push(std::move(oneLabel)); } - DigitalOutputLabelsTP.fill(m_defaultDevice->getDeviceName(), "DIGITAL_OUTPUT_LABELS", "Labels", groupName, IP_RW, 60, IPS_IDLE); + DigitalOutputLabelsTP.fill(m_defaultDevice->getDeviceName(), "DIGITAL_OUTPUT_LABELS", "Labels", groupName, IP_RW, 60, + IPS_IDLE); DigitalOutputLabelsTP.shrink_to_fit(); DigitalOutputLabelsTP.load(); @@ -68,10 +69,9 @@ void OutputInterface::initProperties(const char *groupName, uint8_t Outputs, con auto name = "DIGITAL_OUTPUT_" + std::to_string(i + 1); auto label = prefix + " #" + std::to_string(i + 1); - INDI::PropertySwitch oneOutput {3}; - oneOutput[Open].fill("OPEN", "Open", ISS_OFF); - oneOutput[Close].fill("CLOSE", "Close", ISS_OFF); - oneOutput[Flip].fill("FLIP", "Flip", ISS_OFF); + INDI::PropertySwitch oneOutput {2}; + oneOutput[Off].fill("OFF", "Off", ISS_OFF); + oneOutput[On].fill("ON", "On", ISS_OFF); if (i < DigitalOutputLabelsTP.count()) label = DigitalOutputLabelsTP[i].getText(); @@ -118,7 +118,7 @@ bool OutputInterface::processSwitch(const char *dev, const char *name, ISState s if (oldState != newState) { // Cast to Command and send - if (CommandOutput(i, static_cast(newState))) + if (CommandOutput(i, static_cast(newState))) { DigitalOutputsSP[i].setState(IPS_OK); } diff --git a/libs/indibase/indioutputinterface.h b/libs/indibase/indioutputinterface.h index 451205c40a..f1904a56e3 100644 --- a/libs/indibase/indioutputinterface.h +++ b/libs/indibase/indioutputinterface.h @@ -44,22 +44,14 @@ namespace INDI class OutputInterface { public: - /*! Output switch status. This is regardless on whether switch is normally closed or normally opened. */ + /*! Digital Output status. + */ typedef enum { - Opened, /*!< Switch is open circuit. */ - Closed, /*!< Switch is close circuit. */ - Unknown /*!< Could not determined switch status. */ + Off, /*!< Output is off. For Relays, it is open circuit. */ + On, /*!< Output is on. For Relays, it is closed circuit. */ } Status; - /*! Output switch Command. */ - typedef enum - { - Open, - Close, - Flip - } Command; - /** * \brief Update all digital outputs * \return True if operation is successful, false otherwise @@ -74,7 +66,7 @@ class OutputInterface * \brief Send command to output * \return True if operation is successful, false otherwise */ - virtual bool CommandOutput(uint32_t index, Command command) = 0; + virtual bool CommandOutput(uint32_t index, Status command) = 0; protected: /**