Instrument Neutral Distributed Interface INDI  2.0.2
indirotator.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright(c) 2017 Jasem Mutlaq. All rights reserved.
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 *******************************************************************************/
18 
19 #include "indirotator.h"
20 
23 
24 #include <cstring>
25 
26 namespace INDI
27 {
28 
30 {
31 }
32 
34 {
35 }
36 
38 {
40 
42 
43  // Presets
44  IUFillNumber(&PresetN[0], "PRESET_1", "Preset 1", "%.f", 0, 360, 10, 0);
45  IUFillNumber(&PresetN[1], "PRESET_2", "Preset 2", "%.f", 0, 360, 10, 0);
46  IUFillNumber(&PresetN[2], "PRESET_3", "Preset 3", "%.f", 0, 360, 10, 0);
47  IUFillNumberVector(&PresetNP, PresetN, 3, getDeviceName(), "Presets", "", "Presets", IP_RW, 0, IPS_IDLE);
48 
49  //Preset GOTO
50  IUFillSwitch(&PresetGotoS[0], "Preset 1", "", ISS_OFF);
51  IUFillSwitch(&PresetGotoS[1], "Preset 2", "", ISS_OFF);
52  IUFillSwitch(&PresetGotoS[2], "Preset 3", "", ISS_OFF);
53  IUFillSwitchVector(&PresetGotoSP, PresetGotoS, 3, getDeviceName(), "Goto", "", "Presets", IP_RW, ISR_1OFMANY, 0,
54  IPS_IDLE);
55 
57 
59 
60  if (rotatorConnection & CONNECTION_SERIAL)
61  {
64  {
65  return callHandshake();
66  });
68  }
69 
70  if (rotatorConnection & CONNECTION_TCP)
71  {
72  tcpConnection = new Connection::TCP(this);
74  {
75  return callHandshake();
76  });
78  }
79 
80  return true;
81 }
82 
83 void Rotator::ISGetProperties(const char *dev)
84 {
86 
87  // If connected, let's define properties.
88  //if (isConnected())
89  // RotatorInterface::updateProperties();
90 
91  return;
92 }
93 
95 {
96  // #1 Update base device properties.
98 
99  // #2 Update rotator interface properties
101 
102  if (isConnected())
103  {
106  }
107  else
108  {
111  }
112 
113  return true;
114 }
115 
116 bool Rotator::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
117 {
118  if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
119  {
120  if (!strcmp(name, PresetNP.name))
121  {
122  IUUpdateNumber(&PresetNP, values, names, n);
123  PresetNP.s = IPS_OK;
124  IDSetNumber(&PresetNP, nullptr);
125 
126  return true;
127  }
128 
129  if (strstr(name, "ROTATOR"))
130  {
131  if (RotatorInterface::processNumber(dev, name, values, names, n))
132  return true;
133  }
134  }
135 
136  return DefaultDevice::ISNewNumber(dev, name, values, names, n);
137 }
138 
139 bool Rotator::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
140 {
141  if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
142  {
143  if (!strcmp(PresetGotoSP.name, name))
144  {
145  IUUpdateSwitch(&PresetGotoSP, states, names, n);
146  int index = IUFindOnSwitchIndex(&PresetGotoSP);
147 
148  if (MoveRotator(PresetN[index].value) != IPS_ALERT)
149  {
150  // Ensure Goto Rotator is set to busy.
152  IDSetNumber(&GotoRotatorNP, nullptr);
153 
155  DEBUGF(Logger::DBG_SESSION, "Moving to Preset %d with angle %g degrees.", index + 1, PresetN[index].value);
156  IDSetSwitch(&PresetGotoSP, nullptr);
157  return true;
158  }
159 
161  IDSetSwitch(&PresetGotoSP, nullptr);
162  return false;
163  }
164 
165  if (strstr(name, "ROTATOR"))
166  {
167  if (RotatorInterface::processSwitch(dev, name, states, names, n))
168  return true;
169  }
170  }
171 
172  return DefaultDevice::ISNewSwitch(dev, name, states, names, n);
173 }
174 
176 {
177  return false;
178 }
179 
181 {
184 
187 
188  return true;
189 }
190 
191 bool Rotator::callHandshake()
192 {
193  if (rotatorConnection > 0)
194  {
197  else if (getActiveConnection() == tcpConnection)
199  }
200 
201  return Handshake();
202 }
203 
205 {
206  return rotatorConnection;
207 }
208 
209 void Rotator::setRotatorConnection(const uint8_t &value)
210 {
212 
213  if (value == 0 || (mask & value) == 0)
214  {
215  DEBUGF(Logger::DBG_ERROR, "Invalid connection mode %d", value);
216  return;
217  }
218 
219  rotatorConnection = value;
220 }
221 }
void registerHandshake(std::function< bool()> callback)
registerHandshake Register a handshake function to be called once the intial connection to the device...
The Serial class manages connection with serial devices including Bluetooth. Serial communication is ...
The TCP class manages connection with devices over the network via TCP/IP. Upon successfull connectio...
Definition: connectiontcp.h:38
int getPortFD() const
Definition: connectiontcp.h:84
bool isConnected() const
Definition: basedevice.cpp:520
const char * getDeviceName() const
Definition: basedevice.cpp:821
virtual bool updateProperties()
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Process the client newSwitch command.
void registerConnection(Connection::Interface *newConnection)
registerConnection Add new connection plugin to the existing connection pool. The connection type sha...
virtual void ISGetProperties(const char *dev)
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
virtual bool saveConfigItems(FILE *fp)
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
virtual bool initProperties()
Initilize properties initial state and value. The child class must implement this function.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process the client newNumber command.
void setDriverInterface(uint16_t value)
setInterface Set driver interface. By default the driver interface is set to GENERAL_DEVICE....
Connection::Interface * getActiveConnection()
void addDebugControl()
Add Debug control to the driver.
bool processSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Process Rotator switch properties.
virtual IPState MoveRotator(double angle)=0
MoveRotator Go to specific angle.
bool saveConfigItems(FILE *fp)
saveConfigItems save focuser properties defined in the interface in config file
INumberVectorProperty GotoRotatorNP
void initProperties(const char *groupName)
Initilize Rotator properties. It is recommended to call this function within initProperties() of your...
bool updateProperties()
updateProperties Define or Delete Rotator properties based on the connection status of the base devic...
ISwitchVectorProperty ReverseRotatorSP
bool processNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process Rotator number properties.
Connection::Serial * serialConnection
Definition: indirotator.h:95
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: indirotator.cpp:37
virtual bool Handshake()
perform handshake with device to check communication
INumberVectorProperty PresetNP
Definition: indirotator.h:91
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual void ISGetProperties(const char *dev) override
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
Definition: indirotator.cpp:83
virtual ~Rotator()
Definition: indirotator.cpp:33
INumber PresetN[3]
Definition: indirotator.h:90
uint8_t getRotatorConnection() const
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: indirotator.cpp:94
ISwitchVectorProperty PresetGotoSP
Definition: indirotator.h:93
void setRotatorConnection(const uint8_t &value)
setRotatorConnection Set Rotator connection mode. Child class should call this in the constructor bef...
ISwitch PresetGotoS[3]
Definition: indirotator.h:92
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Saves the reverse direction property in the configuration file
Connection::TCP * tcpConnection
Definition: indirotator.h:96
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
ISState
Switch state.
Definition: indiapi.h:150
@ ISS_OFF
Definition: indiapi.h:151
@ IP_RW
Definition: indiapi.h:186
@ IPS_BUSY
Definition: indiapi.h:163
@ IPS_ALERT
Definition: indiapi.h:164
@ IPS_IDLE
Definition: indiapi.h:161
@ IPS_OK
Definition: indiapi.h:162
@ ISR_1OFMANY
Definition: indiapi.h:173
void IUSaveConfigSwitch(FILE *fp, const ISwitchVectorProperty *svp)
Add a switch vector property value to the configuration file.
Definition: indidevapi.c:25
void IUFillNumberVector(INumberVectorProperty *nvp, INumber *np, int nnp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a number vector property. The vector's auxiliary elements will be set to NULL.
Definition: indidevapi.c:272
int IUFindOnSwitchIndex(const ISwitchVectorProperty *svp)
Returns the index of first ON switch it finds in the vector switch property.
Definition: indidevapi.c:128
void IUSaveConfigNumber(FILE *fp, const INumberVectorProperty *nvp)
Add a number vector property value to the configuration file.
Definition: indidevapi.c:15
void IUFillSwitch(ISwitch *sp, const char *name, const char *label, ISState s)
Assign attributes for a switch property. The switch's auxiliary elements will be set to NULL.
Definition: indidevapi.c:158
void IUFillNumber(INumber *np, const char *name, const char *label, const char *format, double min, double max, double step, double value)
Assign attributes for a number property. The number's auxiliary elements will be set to NULL.
Definition: indidevapi.c:180
void IUFillSwitchVector(ISwitchVectorProperty *svp, ISwitch *sp, int nsp, const char *dev, const char *name, const char *label, const char *group, IPerm p, ISRule r, double timeout, IPState s)
Assign attributes for a switch vector property. The vector's auxiliary elements will be set to NULL.
Definition: indidevapi.c:235
int IUUpdateSwitch(ISwitchVectorProperty *svp, ISState *states, char *names[], int n)
Update all switches in a switch vector property.
Definition: indidriver.c:1308
void IDSetNumber(const INumberVectorProperty *nvp, const char *fmt,...)
Definition: indidriver.c:1211
void IDSetSwitch(const ISwitchVectorProperty *svp, const char *fmt,...)
Definition: indidriver.c:1231
int IUUpdateNumber(INumberVectorProperty *nvp, double values[], char *names[], int n)
Update all numbers in a number vector property.
Definition: indidriver.c:1362
#define DEBUGF(priority, msg,...)
Definition: indilogger.h:57
Namespace to encapsulate INDI client, drivers, and mediator classes.
char name[MAXINDINAME]
Definition: indiapi.h:323
char name[MAXINDINAME]
Definition: indiapi.h:371