Instrument Neutral Distributed Interface INDI  1.6.0
indifilterinterface.cpp
Go to the documentation of this file.
1 /*
2  Filter Interface
3  Copyright (C) 2011 Jasem Mutlaq (mutlaqja@ikarustech.com)
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 
21 #include "indifilterinterface.h"
22 #include <cstring>
23 #include "indilogger.h"
24 
25 namespace INDI
26 {
27 
28 FilterInterface::FilterInterface(DefaultDevice *defaultDevice) : m_defaultDevice(defaultDevice)
29 {
31  FilterNameT = nullptr;
32 }
33 
35 {
36  delete FilterNameTP;
37 }
38 
39 void FilterInterface::initProperties(const char *groupName)
40 {
41  IUFillNumber(&FilterSlotN[0], "FILTER_SLOT_VALUE", "Filter", "%3.0f", 1.0, 12.0, 1.0, 1.0);
42  IUFillNumberVector(&FilterSlotNP, FilterSlotN, 1, m_defaultDevice->getDeviceName(), "FILTER_SLOT", "Filter Slot", groupName, IP_RW, 60,
43  IPS_IDLE);
44 }
45 
47 {
49  {
50  // Define the Filter Slot and name properties
52  if (FilterNameT == nullptr)
53  {
54  if (GetFilterNames() == true)
56  }
57  else
59  }
60  else
61  {
64  }
65 
66  return true;
67 }
68 
69 bool FilterInterface::processNumber(const char *dev, const char *name, double values[], char *names[], int n)
70 {
71  INDI_UNUSED(n);
72 
73  if (dev && !strcmp(dev, m_defaultDevice->getDeviceName()) && !strcmp(name, FilterSlotNP.name))
74  {
75  TargetFilter = values[0];
76 
77  INumber *np = IUFindNumber(&FilterSlotNP, names[0]);
78 
79  if (!np)
80  {
82  DEBUGFDEVICE(m_defaultDevice->getDeviceName(), Logger::DBG_ERROR, "Unknown error. %s is not a member of %s property.", names[0], FilterSlotNP.name);
83  IDSetNumber(&FilterSlotNP, nullptr);
84  return false;
85  }
86 
88  {
90  DEBUGFDEVICE(m_defaultDevice->getDeviceName(), Logger::DBG_ERROR, "Error: valid range of filter is from %g to %g", FilterSlotN[0].min, FilterSlotN[0].max);
91  IDSetNumber(&FilterSlotNP, nullptr);
92  return false;
93  }
94 
96  DEBUGFDEVICE(m_defaultDevice->getDeviceName(), Logger::DBG_SESSION, "Setting current filter to slot %d", TargetFilter);
97 
98  if (SelectFilter(TargetFilter) == false)
99  {
101  }
102 
103  IDSetNumber(&FilterSlotNP, nullptr);
104  return true;
105  }
106 
107  return false;
108 }
109 
110 bool FilterInterface::processText(const char *dev, const char *name, char *texts[], char *names[], int n)
111 {
112  if (dev && !strcmp(dev, m_defaultDevice->getDeviceName()) && !strcmp(name, "FILTER_NAME"))
113  {
114  // If this call due to config loading, let's delete existing dummy property and define the full one
115  if (loadingFromConfig)
116  {
117  loadingFromConfig = false;
118  m_defaultDevice->deleteProperty("FILTER_NAME");
119 
120  char filterName[MAXINDINAME];
121  char filterLabel[MAXINDILABEL];
122 
123  if (FilterNameT != nullptr)
124  delete [] FilterNameT;
125 
126  FilterNameT = new IText[n];
127 
128  for (int i = 0; i < n; i++)
129  {
130  snprintf(filterName, MAXINDINAME, "FILTER_SLOT_NAME_%d", i + 1);
131  snprintf(filterLabel, MAXINDILABEL, "Filter#%d", i + 1);
132  IUFillText(&FilterNameT[i], filterName, filterLabel, texts[i]);
133  }
134 
137  return true;
138  }
139 
140  IUUpdateText(FilterNameTP, texts, names, n);
141  FilterNameTP->s = IPS_OK;
142 
143  if (SetFilterNames() == true)
144  {
145  IDSetText(FilterNameTP, nullptr);
146  return true;
147  }
148  else
149  {
151  DEBUGDEVICE(m_defaultDevice->getDeviceName(), Logger::DBG_ERROR, "Error updating names of filters.");
152  IDSetText(FilterNameTP, nullptr);
153  return false;
154  }
155  }
156 
157  return false;
158 }
159 
161 {
163  if (FilterNameTP)
165 
166  return true;
167 }
168 
170 {
171  // The hardware has finished changing
172  // filters
173  FilterSlotN[0].value = f;
175  // Tell the clients we are done, and
176  // filter is now useable
177  IDSetNumber(&FilterSlotNP, nullptr);
178 }
179 
181 {
182  char filterName[MAXINDINAME];
183  char filterLabel[MAXINDILABEL];
184  int MaxFilter = FilterSlotN[0].max;
185 
186  const char *filterDesignation[8] = { "Red", "Green", "Blue", "H_Alpha", "SII", "OIII", "LPR", "Luminance" };
187 
188  if (FilterNameT != nullptr)
189  delete [] FilterNameT;
190 
191  FilterNameT = new IText[MaxFilter];
192 
193  for (int i = 0; i < MaxFilter; i++)
194  {
195  snprintf(filterName, MAXINDINAME, "FILTER_SLOT_NAME_%d", i + 1);
196  snprintf(filterLabel, MAXINDILABEL, "Filter#%d", i + 1);
197  IUFillText(&FilterNameT[i], filterName, filterLabel, i < 8 ? filterDesignation[i] : filterLabel);
198  }
199 
201 }
202 
204 {
205  // Load from config
206  if (FilterNameT == nullptr)
207  {
209  // If property is found, let's define it once loaded to the client and delete
210  // the generate sample filters above
211  if (m_defaultDevice->loadConfig(true, "FILTER_NAME"))
212  loadingFromConfig = true;
213  }
214 
215  return true;
216 }
217 
219 {
220  return m_defaultDevice->saveConfig(true, "FILTER_NAME");
221 }
222 }
void IUSaveConfigText(FILE *fp, const ITextVectorProperty *tvp)
Add a text vector property value to the configuration file.
Definition: indidriver.c:1477
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
void generateSampleFilters()
generateSampleFilters Generate sample 8-filter wheel and fill it sample filters
bool processNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process number properties.
#define MAXINDINAME
Definition: indiapi.h:172
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
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&#39;s auxiliary elements will be set to NULL...
Definition: indidriver.c:545
void IDSetText(const ITextVectorProperty *t, const char *msg,...)
Tell client to update an existing text vector property.
Definition: indidriver.c:1853
void initProperties(const char *groupName)
Initilize filter wheel properties. It is recommended to call this function within initProperties() of...
#define DEBUGDEVICE(device, priority, msg)
Definition: indilogger.h:59
virtual bool GetFilterNames()
Obtains a list of filter names from the hardware and initializes the FilterNameTP property...
int IUUpdateText(ITextVectorProperty *tvp, char *texts[], char *names[], int n)
Update all text members in a text vector property.
Definition: indidriver.c:319
One number descriptor.
Definition: indiapi.h:256
char group[MAXINDIGROUP]
Definition: indiapi.h:308
virtual bool SelectFilter(int position)=0
Select a new filter position.
INumberVectorProperty FilterSlotNP
double max(void)
Definition: indiapi.h:167
const char * name
Definition: indiserver.c:113
void IDSetNumber(const INumberVectorProperty *n, const char *msg,...)
Tell client to update an existing number vector property.
Definition: indidriver.c:1895
virtual bool SetFilterNames()
Set filter names as defined by the client for each filter position. The desired filter names are stor...
Namespace to encapsulate INDI client, drivers, and mediator classes. Developers can subclass the base...
ITextVectorProperty * FilterNameTP
char name[MAXINDINAME]
Definition: indiapi.h:304
bool saveConfigItems(FILE *fp)
saveConfigItems save Filter Names in config file
void IUFillText(IText *tp, const char *name, const char *label, const char *initialText)
Assign attributes for a text property. The text&#39;s auxiliary elements will be set to NULL...
Definition: indidriver.c:445
void IUSaveConfigNumber(FILE *fp, const INumberVectorProperty *nvp)
Add a number vector property value to the configuration file.
Definition: indidriver.c:1458
bool updateProperties()
updateProperties Defines or Delete proprties based on default device connection status ...
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&#39;s auxiliary elements will be set to NULL...
Definition: indidriver.c:420
bool processText(const char *dev, const char *name, char *texts[], char *names[], int n)
Process text properties.
INumber * IUFindNumber(const INumberVectorProperty *nvp, const char *name)
Find an INumber member in a number text property.
Definition: indicom.c:1327
One text descriptor.
Definition: indiapi.h:206
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
const char * getDeviceName()
void SelectFilterDone(int newpos)
The child class calls this function when the hardware successfully finished selecting a new filter wh...
double min
Definition: indiapi.h:265
char name[MAXINDINAME]
Definition: indiapi.h:231
#define INDI_UNUSED(x)
Definition: indidevapi.h:797
double max
Definition: indiapi.h:267
void defineText(ITextVectorProperty *tvp)
Define text vector to client & register it. Alternatively, IDDefText can be used but the property wil...
DefaultDevice * m_defaultDevice
virtual bool loadConfig(bool silent=false, const char *property=nullptr)
Load the last saved configuration file.
void IUFillTextVector(ITextVectorProperty *tvp, IText *tp, int ntp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a text vector property. The vector&#39;s auxiliary elements will be set to NULL...
Definition: indidriver.c:571
struct _ITextVectorProperty ITextVectorProperty
#define MAXINDILABEL
Definition: indiapi.h:173
double min(void)
#define DEBUGFDEVICE(device, priority, msg,...)
Definition: indilogger.h:60
FilterInterface(DefaultDevice *defaultDevice)
FilterInterface Initiailize Filter Interface.
void defineNumber(INumberVectorProperty *nvp)
Define number vector to client & register it. Alternatively, IDDefNumber can be used but the property...
double value
Definition: indiapi.h:271