Instrument Neutral Distributed Interface INDI  2.0.2
qhycfw1.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright(c) 2018 Jasem Mutlaq. All rights reserved.
3 
4  Old QHYCFW1 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 "qhycfw1.h"
22 
23 #include "indicom.h"
24 
25 #include <cstring>
26 #include <memory>
27 #include <thread>
28 #include <chrono>
29 
30 // We declare an auto pointer to QHYCFW.
31 static std::unique_ptr<QHYCFW1> qhycfw(new QHYCFW1());
32 
34 {
35  setVersion(1, 2);
37 }
38 
40 {
41  return "QHYCFW1";
42 }
43 
44 void QHYCFW1::ISGetProperties(const char *dev)
45 {
47 
48  // Only read value when we're offline
49  if (isConnected() == false)
50  {
51  double maxCount = 5;
52  IUGetConfigNumber(getDeviceName(), "MAX_FILTER", "Count", &maxCount);
53  FilterSlotN[0].max = maxCount;
54  if (FilterNameTP->ntp != maxCount)
55  {
56  char filterName[MAXINDINAME];
57  char filterLabel[MAXINDILABEL];
58  if (FilterNameT != nullptr)
59  {
60  for (int i = 0; i < FilterNameTP->ntp; i++)
61  free(FilterNameT[i].text);
62  delete [] FilterNameT;
63  }
64 
65  FilterNameT = new IText[static_cast<int>(maxCount)];
66  memset(FilterNameT, 0, sizeof(IText) * maxCount);
67  for (int i = 0; i < maxCount; i++)
68  {
69  snprintf(filterName, MAXINDINAME, "FILTER_SLOT_NAME_%d", i + 1);
70  snprintf(filterLabel, MAXINDILABEL, "Filter#%d", i + 1);
71  IUFillText(&FilterNameT[i], filterName, filterLabel, filterLabel);
72  }
73  IUFillTextVector(FilterNameTP, FilterNameT, maxCount, m_defaultDevice->getDeviceName(), "FILTER_NAME", "Filter",
75  }
76  }
77  defineProperty(&MaxFilterNP);
78 }
79 
81 {
83 
84  IUFillNumber(&MaxFilterN[0], "Count", "Count", "%.f", 1, 16, 1, 5);
85  IUFillNumberVector(&MaxFilterNP, MaxFilterN, 1, getDeviceName(), "MAX_FILTER", "Filters", MAIN_CONTROL_TAB, IP_RW, 60,
86  IPS_IDLE);
87 
88  CurrentFilter = 1;
89  FilterSlotN[0].min = 1;
90  FilterSlotN[0].max = 5;
91 
93 
94  return true;
95 }
96 
98 {
99  TargetFilter = f;
100  char cmd[8] = {0}, res[8] = {0};
101  int rc = -1, nbytes_written = 0, nbytes_read = 0;
102 
103  LOGF_DEBUG("CMD <%d>", TargetFilter - 1);
104 
105  snprintf(cmd, 2, "%d", TargetFilter - 1);
106 
107  if (isSimulation())
108  snprintf(res, 8, "%d", TargetFilter - 1);
109  else
110  {
111  if ((rc = tty_write_string(PortFD, cmd, &nbytes_written)) != TTY_OK)
112  {
113  char error_message[ERRMSG_SIZE];
114  tty_error_msg(rc, error_message, ERRMSG_SIZE);
115 
116  LOGF_ERROR("Sending command select filter failed: %s", error_message);
117  return false;
118  }
119 
120  // Wait 10 seconds
121  std::this_thread::sleep_for(std::chrono::seconds(10));
122 
123  if ((rc = tty_read(PortFD, res, 1, 30, &nbytes_read)) != TTY_OK)
124  {
125  char error_message[ERRMSG_SIZE];
126  tty_error_msg(rc, error_message, ERRMSG_SIZE);
127 
128  LOGF_ERROR("Reading select filter response failed: %s", error_message);
129  return false;
130  }
131 
132  LOGF_DEBUG("RES <%s>", res);
133  }
134 
135  //if (atoi(res) + 1 == TargetFilter)
136  if (res[0] == '-')
137  {
140  return true;
141  }
142 
143  return false;
144 }
145 
146 bool QHYCFW1::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
147 {
148  if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
149  {
150  if (!strcmp(name, MaxFilterNP.name))
151  {
152  if (values[0] != MaxFilterN[0].value)
153  {
154  IUUpdateNumber(&MaxFilterNP, values, names, n);
155  saveConfig();
156  LOG_INFO("Max number of filters updated. You must reconnect for this change to take effect.");
157  }
158 
159  MaxFilterNP.s = IPS_OK;
160  IDSetNumber(&MaxFilterNP, nullptr);
161  return true;
162  }
163 
164  }
165 
166  return INDI::FilterWheel::ISNewNumber(dev, name, values, names, n);
167 }
168 
170 {
171  FilterWheel::saveConfigItems(fp);
172 
173  IUSaveConfigNumber(fp, &MaxFilterNP);
174 
175  return true;
176 }
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 SelectFilter(int) override
Select a new filter position.
Definition: qhycfw1.cpp:97
QHYCFW1()
Definition: qhycfw1.cpp:33
bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
Definition: qhycfw1.cpp:146
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: qhycfw1.cpp:44
const char * getDefaultName() override
Definition: qhycfw1.cpp:39
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: qhycfw1.cpp:80
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
Definition: qhycfw1.cpp:169
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