Instrument Neutral Distributed Interface INDI  2.0.2
qhycfw2.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright(c) 2018 Jasem Mutlaq. All rights reserved.
3 
4  QHYCFW2/3 Filter Wheel Driver
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
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  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 *******************************************************************************/
20 
21 #include "qhycfw2.h"
22 
23 #include "indicom.h"
24 
25 #include <cstring>
26 #include <memory>
27 
28 // We declare an auto pointer to QHYCFW.
29 static std::unique_ptr<QHYCFW2> qhycfw(new QHYCFW2());
30 
32 {
33  setVersion(1, 2);
35 }
36 
38 {
39  return "QHYCFW2";
40 }
41 
42 void QHYCFW2::ISGetProperties(const char *dev)
43 {
45 
46  // Only read value when we're offline
47  if (isConnected() == false)
48  {
49  double maxCount = 5;
50  IUGetConfigNumber(getDeviceName(), "MAX_FILTER", "Count", &maxCount);
51  FilterSlotN[0].max = maxCount;
52  if (FilterNameTP->ntp != maxCount)
53  {
54  char filterName[MAXINDINAME];
55  char filterLabel[MAXINDILABEL];
56  if (FilterNameT != nullptr)
57  {
58  for (int i = 0; i < FilterNameTP->ntp; i++)
59  free(FilterNameT[i].text);
60  delete [] FilterNameT;
61  }
62 
63  FilterNameT = new IText[static_cast<int>(maxCount)];
64  memset(FilterNameT, 0, sizeof(IText) * maxCount);
65  for (int i = 0; i < maxCount; i++)
66  {
67  snprintf(filterName, MAXINDINAME, "FILTER_SLOT_NAME_%d", i + 1);
68  snprintf(filterLabel, MAXINDILABEL, "Filter#%d", i + 1);
69  IUFillText(&FilterNameT[i], filterName, filterLabel, filterLabel);
70  }
71  IUFillTextVector(FilterNameTP, FilterNameT, maxCount, m_defaultDevice->getDeviceName(), "FILTER_NAME", "Filter",
73  }
74  }
75  defineProperty(&MaxFilterNP);
76 }
77 
79 {
81 
82  IUFillNumber(&MaxFilterN[0], "Count", "Count", "%.f", 1, 16, 1, 5);
83  IUFillNumberVector(&MaxFilterNP, MaxFilterN, 1, getDeviceName(), "MAX_FILTER", "Filters", MAIN_CONTROL_TAB, IP_RW, 60,
84  IPS_IDLE);
85 
86  CurrentFilter = 1;
87  FilterSlotN[0].min = 1;
88  FilterSlotN[0].max = 5;
89 
91 
92  return true;
93 }
94 
96 {
97  TargetFilter = f;
98  char cmd[8] = {0}, res[8] = {0};
99  int rc = -1, nbytes_written = 0, nbytes_read = 0;
100 
101  LOGF_DEBUG("CMD <%d>", TargetFilter - 1);
102 
103  snprintf(cmd, 2, "%d", TargetFilter - 1);
104 
105  if (isSimulation())
106  snprintf(res, 8, "%d", TargetFilter - 1);
107  else
108  {
109  if ((rc = tty_write_string(PortFD, cmd, &nbytes_written)) != TTY_OK)
110  {
111  char error_message[ERRMSG_SIZE];
112  tty_error_msg(rc, error_message, ERRMSG_SIZE);
113 
114  LOGF_ERROR("Sending command select filter failed: %s", error_message);
115  return false;
116  }
117 
118  if ((rc = tty_read(PortFD, res, 1, 30, &nbytes_read)) != TTY_OK)
119  {
120  char error_message[ERRMSG_SIZE];
121  tty_error_msg(rc, error_message, ERRMSG_SIZE);
122 
123  LOGF_ERROR("Reading select filter response failed: %s", error_message);
124  return false;
125  }
126 
127  LOGF_DEBUG("RES <%s>", res);
128  }
129 
130  if (atoi(res) + 1 == TargetFilter)
131  {
134  return true;
135  }
136 
137  return false;
138 }
139 
140 bool QHYCFW2::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
141 {
142  if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
143  {
144  if (!strcmp(name, MaxFilterNP.name))
145  {
146  if (values[0] != MaxFilterN[0].value)
147  {
148  IUUpdateNumber(&MaxFilterNP, values, names, n);
149  saveConfig();
150  LOG_INFO("Max number of filters updated. You must reconnect for this change to take effect.");
151  }
152 
153  MaxFilterNP.s = IPS_OK;
154  IDSetNumber(&MaxFilterNP, nullptr);
155  return true;
156  }
157 
158  }
159 
160  return INDI::FilterWheel::ISNewNumber(dev, name, values, names, n);
161 }
162 
164 {
165  FilterWheel::saveConfigItems(fp);
166 
167  IUSaveConfigNumber(fp, &MaxFilterNP);
168 
169  return true;
170 }
bool isConnected() const
Definition: basedevice.cpp:520
const char * getDeviceName() const
Definition: basedevice.cpp:821
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
void setVersion(uint16_t vMajor, uint16_t vMinor)
Set driver version information to be defined in DRIVER_INFO property as vMajor.vMinor.
void defineProperty(INumberVectorProperty *property)
bool isSimulation() const
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
DefaultDevice * m_defaultDevice
ITextVectorProperty * FilterNameTP
INumberVectorProperty FilterSlotNP
void SelectFilterDone(int newpos)
The child class calls this function when the hardware successfully finished selecting a new filter wh...
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...
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
void setFilterConnection(const uint8_t &value)
setFilterConnection Set Filter connection mode. Child class should call this in the constructor befor...
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
int PortFD
For Serial & TCP connections.
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
Definition: qhycfw2.cpp:163
virtual bool SelectFilter(int) override
Select a new filter position.
Definition: qhycfw2.cpp:95
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: qhycfw2.cpp:78
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: qhycfw2.cpp:42
const char * getDefaultName() override
Definition: qhycfw2.cpp:37
bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
Definition: qhycfw2.cpp:140
QHYCFW2()
Definition: qhycfw2.cpp:31
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
@ IP_RW
Definition: indiapi.h:186
@ IPS_IDLE
Definition: indiapi.h:161
@ IPS_OK
Definition: indiapi.h:162
#define MAXINDILABEL
Definition: indiapi.h:192
#define MAXINDINAME
Definition: indiapi.h:191
int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read)
read buffer from terminal
Definition: indicom.c:482
int tty_write_string(int fd, const char *buf, int *nbytes_written)
Writes a null terminated string to fd.
Definition: indicom.c:474
void tty_error_msg(int err_code, char *err_msg, int err_msg_len)
Retrieve the tty error message.
Definition: indicom.c:1167
Implementations for common driver routines.
@ TTY_OK
Definition: indicom.h:150
#define ERRMSG_SIZE
Definition: indicom.h:49
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
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's auxiliary elements will be set to NULL.
Definition: indidevapi.c:291
void IUSaveConfigNumber(FILE *fp, const INumberVectorProperty *nvp)
Add a number vector property value to the configuration file.
Definition: indidevapi.c:15
void IUFillText(IText *tp, const char *name, const char *label, const char *initialText)
Assign attributes for a text property. The text's auxiliary elements will be set to NULL.
Definition: indidevapi.c:198
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 IDSetNumber(const INumberVectorProperty *nvp, const char *fmt,...)
Definition: indidriver.c:1211
int IUGetConfigNumber(const char *dev, const char *property, const char *member, double *value)
IUGetConfigNumber Opens configuration file and reads single number property.
Definition: indidriver.c:831
int IUUpdateNumber(INumberVectorProperty *nvp, double values[], char *names[], int n)
Update all numbers in a number vector property.
Definition: indidriver.c:1362
#define LOGF_DEBUG(fmt,...)
Definition: indilogger.h:83
#define LOGF_ERROR(fmt,...)
Definition: indilogger.h:80
#define LOG_INFO(txt)
Definition: indilogger.h:74
__u8 cmd[4]
Definition: pwc-ioctl.h:2
One text descriptor.
char group[MAXINDIGROUP]
Definition: indiapi.h:327
char name[MAXINDINAME]
Definition: indiapi.h:323