Skip to content

Commit

Permalink
Simpliy interfaces and fix formatting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
knro committed Jul 26, 2024
1 parent fda5178 commit 34867c9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 43 deletions.
23 changes: 10 additions & 13 deletions drivers/auxiliary/waveshare_modbus_relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <memory>
#include <unistd.h>
#include <iomanip>
#include <connectionplugins/connectiontcp.h>

static std::unique_ptr<WaveshareRelay> sesto(new WaveshareRelay());
Expand Down Expand Up @@ -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;
}
Expand All @@ -124,7 +126,7 @@ bool WaveshareRelay::Handshake()
////////////////////////////////////////////////////////////////////////////////////////////////////////
const char *WaveshareRelay::getDefaultName()
{
return "Waveshare Output";
return "Waveshare Relay";
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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();
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion drivers/auxiliary/waveshare_modbus_relay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 8 additions & 5 deletions libs/indibase/indiinputinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down
13 changes: 8 additions & 5 deletions libs/indibase/indiinputinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions libs/indibase/indioutputinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand Down Expand Up @@ -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<Command>(newState)))
if (CommandOutput(i, static_cast<Status>(newState)))
{
DigitalOutputsSP[i].setState(IPS_OK);
}
Expand Down
18 changes: 5 additions & 13 deletions libs/indibase/indioutputinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
/**
Expand Down

0 comments on commit 34867c9

Please sign in to comment.