Instrument Neutral Distributed Interface INDI  2.0.2
qhycfw3.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 "qhycfw3.h"
22 
23 #include "indicom.h"
24 
25 #include <cstring>
26 #include <memory>
27 
28 // We declare an auto pointer to QHYCFW3.
29 static std::unique_ptr<QHYCFW3> qhycfw(new QHYCFW3());
30 
32 {
33  setVersion(1, 1);
35 }
36 
38 {
39  return static_cast<const char *>("QHYCFW3");
40 }
41 
43 {
45 
46  CurrentFilter = 1;
47  FilterSlotN[0].min = 1;
48  FilterSlotN[0].max = 4;
49 
51 
52  return true;
53 }
54 
56 {
57  int rc = -1, nbytes_written = 0, nbytes_read = 0;
58  char res[32] = {0};
59 
60  if (isSimulation())
61  return true;
62 
63  LOG_DEBUG("HANDSHAKE");
64 
65  if ( (rc = tty_read(PortFD, res, 1, 25, &nbytes_read)) != TTY_OK)
66  {
67  char error_message[ERRMSG_SIZE];
68  tty_error_msg(rc, error_message, ERRMSG_SIZE);
69 
70  LOGF_ERROR("Handshake failed: %s. Firmware must be higher than 201409", error_message);
71  return false;
72  }
73 
74  LOGF_DEBUG("RES <%s>", res);
75 
76  LOG_DEBUG("CMD <VRS>");
77 
78  if ( (rc = tty_write_string(PortFD, "VRS", &nbytes_written)) != TTY_OK)
79  {
80  char error_message[ERRMSG_SIZE];
81  tty_error_msg(rc, error_message, ERRMSG_SIZE);
82 
83  LOGF_ERROR("Handshake failed: %s. Firmware must be higher than 201409", error_message);
84  return false;
85  }
86 
87  if ( (rc = tty_read(PortFD, res, 8, 3, &nbytes_read)) != TTY_OK)
88  {
89  char error_message[ERRMSG_SIZE];
90  tty_error_msg(rc, error_message, ERRMSG_SIZE);
91 
92  LOGF_ERROR("Handshake failed: %s. Firmware must be higher than 201409", error_message);
93  return false;
94  }
95 
96  LOGF_DEBUG("RES <%s>", res);
97 
98  LOGF_INFO("Detected firmware version %s", res);
99 
100  // Now get maximum positions
101  LOG_DEBUG("CMD <MXP>");
102 
103  if ( (rc = tty_write_string(PortFD, "MXP", &nbytes_written)) != TTY_OK)
104  {
105  char error_message[ERRMSG_SIZE];
106  tty_error_msg(rc, error_message, ERRMSG_SIZE);
107 
108  LOGF_ERROR("Querying maximum position failed: %s.", error_message);
109  return false;
110  }
111 
112  memset(res, 0, 32);
113 
114  if ( (rc = tty_read(PortFD, res, 1, 3, &nbytes_read)) != TTY_OK)
115  {
116  char error_message[ERRMSG_SIZE];
117  tty_error_msg(rc, error_message, ERRMSG_SIZE);
118 
119  LOGF_ERROR("Reading maximum position failed: %s.", error_message);
120  return false;
121  }
122 
123  LOGF_DEBUG("RES <%s>", res);
124 
125  if (res[0] == 'F')
126  FilterSlotN[0].max = 16;
127  else
128  FilterSlotN[0].max = atoi(res);
129 
130  // Now get current position
131  LOG_DEBUG("CMD <NOW>");
132 
133  if ( (rc = tty_write_string(PortFD, "NOW", &nbytes_written)) != TTY_OK)
134  {
135  char error_message[ERRMSG_SIZE];
136  tty_error_msg(rc, error_message, ERRMSG_SIZE);
137 
138  LOGF_ERROR("Querying current position failed: %s.", error_message);
139  return false;
140  }
141 
142  memset(res, 0, 32);
143 
144  if ( (rc = tty_read(PortFD, res, 1, 3, &nbytes_read)) != TTY_OK)
145  {
146  char error_message[ERRMSG_SIZE];
147  tty_error_msg(rc, error_message, ERRMSG_SIZE);
148 
149  LOGF_ERROR("Reading current position failed: %s.", error_message);
150  return false;
151  }
152 
153  LOGF_DEBUG("RES <%s>", res);
154 
155  CurrentFilter = atoi(res) + 1;
156  FilterSlotN[0].value = CurrentFilter;
157 
158  return true;
159 }
160 
162 {
163  TargetFilter = f;
164  char cmd[8] = {0}, res[8] = {0};
165  int rc = -1, nbytes_written = 0, nbytes_read = 0;
166 
167  LOGF_DEBUG("CMD <%d>", TargetFilter - 1);
168 
169  snprintf(cmd, 2, "%d", TargetFilter - 1);
170 
171  if (isSimulation())
172  snprintf(res, 8, "%d", TargetFilter - 1);
173  else
174  {
175  if ((rc = tty_write_string(PortFD, cmd, &nbytes_written)) != TTY_OK)
176  {
177  char error_message[ERRMSG_SIZE];
178  tty_error_msg(rc, error_message, ERRMSG_SIZE);
179 
180  LOGF_ERROR("Sending command select filter failed: %s", error_message);
181  return false;
182  }
183 
184  if ((rc = tty_read(PortFD, res, 1, 30, &nbytes_read)) != TTY_OK)
185  {
186  char error_message[ERRMSG_SIZE];
187  tty_error_msg(rc, error_message, ERRMSG_SIZE);
188 
189  LOGF_ERROR("Reading select filter response failed: %s", error_message);
190  return false;
191  }
192 
193  LOGF_DEBUG("RES <%s>", res);
194  }
195 
196  if (atoi(res) + 1 == TargetFilter)
197  {
200  return true;
201  }
202 
203  return false;
204 }
void setVersion(uint16_t vMajor, uint16_t vMinor)
Set driver version information to be defined in DRIVER_INFO property as vMajor.vMinor.
bool isSimulation() const
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
void SelectFilterDone(int newpos)
The child class calls this function when the hardware successfully finished selecting a new filter wh...
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...
int PortFD
For Serial & TCP connections.
bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: qhycfw3.cpp:42
bool Handshake() override
perform handshake with device to check communication
Definition: qhycfw3.cpp:55
QHYCFW3()
Definition: qhycfw3.cpp:31
const char * getDefaultName() override
Definition: qhycfw3.cpp:37
bool SelectFilter(int) override
Select a new filter position.
Definition: qhycfw3.cpp:161
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
#define LOGF_INFO(fmt,...)
Definition: indilogger.h:82
#define LOG_DEBUG(txt)
Definition: indilogger.h:75
#define LOGF_DEBUG(fmt,...)
Definition: indilogger.h:83
#define LOGF_ERROR(fmt,...)
Definition: indilogger.h:80
__u8 cmd[4]
Definition: pwc-ioctl.h:2