Instrument Neutral Distributed Interface INDI 2.2.5
simulator_io.cpp
Go to the documentation of this file.
1/*
2 INDI Simulator IO
3 Copyright (C) 2024 Jasem Mutlaq
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#include "simulator_io.h"
21
22#include <memory>
23#include <unistd.h>
24
25static std::unique_ptr<SimulatorIO> simulatorIO(new SimulatorIO());
26
31
36{
38
39 // Initialize Input Interface with 4 digital inputs and 0 analog inputs
40 INDI::InputInterface::initProperties("Input", 4, 0, "Input");
41
42 // Initialize Output Interface with 4 digital outputs
43 INDI::OutputInterface::initProperties("Output", 4, "Output");
44
45 // Set up simulation controls for inputs
46 char swName[MAXINDINAME];
47 char swLabel[MAXINDILABEL];
48 for (int i = 0; i < 4; i++)
49 {
50 snprintf(swName, MAXINDINAME, "SIM_INPUT_%d", i + 1);
51 snprintf(swLabel, MAXINDILABEL, "Input %d", i + 1);
52 SimulateInputsSP[i].fill(swName, swLabel, ISS_OFF);
53 }
54 SimulateInputsSP.fill(getDeviceName(), "SIMULATOR_INPUT", "Inputs", "Simulation", IP_RW, ISR_NOFMANY, 60, IPS_IDLE);
55 SimulateInputsSP.load();
56
58
60
62
63 return true;
64}
65
70{
74
75 if (isConnected())
76 {
77 defineProperty(SimulateInputsSP);
79 }
80 else
81 {
82 deleteProperty(SimulateInputsSP);
83 }
84
85 return true;
86}
87
92{
93 return "Simulator IO";
94}
95
99bool SimulatorIO::ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
100{
101 // Check Input Properties
102 if (INDI::InputInterface::processText(dev, name, texts, names, n))
103 return true;
104
105 // Check Output Properties
106 if (INDI::OutputInterface::processText(dev, name, texts, names, n))
107 return true;
108
109 return INDI::DefaultDevice::ISNewText(dev, name, texts, names, n);
110}
111
115bool SimulatorIO::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
116{
117 if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
118 {
119 // Check if it's one of our simulation input controls
120
121 if (SimulateInputsSP.isNameMatch(name))
122 {
123 SimulateInputsSP.update(states, names, n);
124 SimulateInputsSP.setState(IPS_OK);
125 SimulateInputsSP.apply();
126 saveConfig(SimulateInputsSP);
127
128 for (int i = 0; i < 4; i++)
129 // Update the input state based on the simulation control
130 m_InputStates[i] = (SimulateInputsSP[i].getState() == ISS_ON);
131
132 // Force an update of the inputs
134
135 return true;
136 }
137 }
138
139 // Check Output Properties
140 if (INDI::OutputInterface::processSwitch(dev, name, states, names, n))
141 return true;
142
143 return INDI::DefaultDevice::ISNewSwitch(dev, name, states, names, n);
144}
145
146bool SimulatorIO::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
147{
148 if (INDI::OutputInterface::processNumber(dev, name, values, names, n))
149 return true;
150
151 return INDI::DefaultDevice::ISNewNumber(dev, name, values, names, n);
152}
153
158{
161
162 // Save simulation control states
163 SimulateInputsSP.save(fp);
164
166}
167
172{
173 if (!isConnected())
174 return;
175
178
180}
181
186{
187 for (size_t i = 0; i < DigitalInputsSP.size(); i++)
188 {
189 auto oldState = DigitalInputsSP[i].findOnSwitchIndex();
190 auto newState = m_InputStates[i] ? 1 : 0;
191
192 if (oldState != newState)
193 {
194 DigitalInputsSP[i].reset();
195 DigitalInputsSP[i][newState].setState(ISS_ON);
196 DigitalInputsSP[i].setState(IPS_OK);
197 DigitalInputsSP[i].apply();
198 }
199 }
200
201 return true;
202}
203
208{
209 // We don't have analog inputs in this simulator
210 return true;
211}
212
217{
218 for (size_t i = 0; i < DigitalOutputsSP.size(); i++)
219 {
220 auto oldState = DigitalOutputsSP[i].findOnSwitchIndex();
221 auto newState = m_OutputStates[i] ? 1 : 0;
222
223 if (oldState != newState)
224 {
225 DigitalOutputsSP[i].reset();
226 DigitalOutputsSP[i][newState].setState(ISS_ON);
227 DigitalOutputsSP[i].setState(IPS_OK);
228 DigitalOutputsSP[i].apply();
229 }
230 }
231
232 return true;
233}
234
238bool SimulatorIO::CommandOutput(uint32_t index, OutputState command)
239{
240 if (index >= 4)
241 return false;
242
243 m_OutputStates[index] = (command == OutputState::On);
244
245 return true;
246}
247
252{
253 return true;
254}
255
260{
261 return true;
262}
bool isConnected() const
const char * getDeviceName() const
virtual bool updateProperties()
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Process the client newSwitch command.
void setDefaultPollingPeriod(uint32_t msec)
setDefaultPollingPeriod Change the default polling period to call TimerHit() function in the driver.
void setDriverInterface(uint32_t value)
setInterface Set driver interface. By default the driver interface is set to GENERAL_DEVICE....
void setVersion(uint16_t vMajor, uint16_t vMinor)
Set driver version information to be defined in DRIVER_INFO property as vMajor.vMinor.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
uint32_t getPollingPeriod() const
getPollingPeriod Return the polling period.
void defineProperty(INumberVectorProperty *property)
virtual bool saveConfigItems(FILE *fp)
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
uint32_t getCurrentPollingPeriod() const
getCurrentPollingPeriod Return the current polling period.
virtual bool initProperties()
Initialize properties initial state and value. The child class must implement this function.
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process the client newNumber command.
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
Process the client newSwitch command.
bool processText(const char *dev, const char *name, char *texts[], char *names[], int n)
Process text properties.
std::vector< INDI::PropertySwitch > DigitalInputsSP
bool saveConfigItems(FILE *fp)
saveConfigItems save Filter Names in config file
void initProperties(const char *groupName, uint8_t digital, uint8_t analog, const std::string &digitalPrefix="Digital", const std::string &analogPrefix="Analog")
Initialize filter wheel properties. It is recommended to call this function within initProperties() o...
bool updateProperties()
updateProperties Defines or Delete properties based on default device connection status
bool processNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process number properties.
void initProperties(const char *groupName, uint8_t outputs, const std::string &prefix="Output")
Initialize filter wheel properties. It is recommended to call this function within initProperties() o...
bool processSwitch(const char *dev, const char *name, ISState states[], char *names[], int n)
Process switch properties.
bool saveConfigItems(FILE *fp)
saveConfigItems save Filter Names in config file
std::vector< INDI::PropertySwitch > DigitalOutputsSP
bool processText(const char *dev, const char *name, char *texts[], char *names[], int n)
Process text properties.
bool updateProperties()
updateProperties Defines or Delete properties based on default device connection status
void setState(IPState state)
void apply(const char *format,...) const ATTRIBUTE_FORMAT_PRINTF(2
bool load()
load Attempt to load property values from configuration file.
void save(FILE *f) const
save Save property to configuration file.
bool isNameMatch(const char *otherName) const
bool update(const ISState states[], const char *const names[], int n)
void fill(const char *device, const char *name, const char *label, const char *group, IPerm permission, ISRule rule, double timeout, IPState state)
Provides interface to implement Digital/Analog input functionality.
Provides interface to implement Digital Boolean Output (On/Off) functionality.
virtual bool UpdateAnalogInputs() override
Update all analog inputs.
virtual bool UpdateDigitalInputs() override
Update all digital inputs.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
virtual bool CommandOutput(uint32_t index, OutputState command) override
Send command to output.
virtual bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
virtual void TimerHit() override
Callback function to be called once SetTimer duration elapses.
virtual bool UpdateDigitalOutputs() override
Update all digital outputs.
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
const char * getDefaultName() override
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual bool Disconnect() override
Disconnect from device.
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
ISState
Switch state.
Definition indiapi.h:148
@ ISS_OFF
Definition indiapi.h:149
@ ISS_ON
Definition indiapi.h:150
@ IP_RW
Definition indiapi.h:184
@ IPS_IDLE
Definition indiapi.h:159
@ IPS_OK
Definition indiapi.h:160
#define MAXINDILABEL
Definition indiapi.h:190
@ ISR_NOFMANY
Definition indiapi.h:173
#define MAXINDINAME
Definition indiapi.h:189