Instrument Neutral Distributed Interface INDI 2.2.5
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.
29static std::unique_ptr<QHYCFW2> qhycfw(new QHYCFW2());
30
36
38{
39 return "QHYCFW2";
40}
41
42void 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 FilterSlotNP[0].setMax(maxCount);
52 if (FilterNameTP.size() != maxCount)
53 {
54 char filterName[MAXINDINAME];
55 char filterLabel[MAXINDILABEL];
56
58
59 for (int i = 0; i < maxCount; i++)
60 {
61 snprintf(filterName, MAXINDINAME, "FILTER_SLOT_NAME_%d", i + 1);
62 snprintf(filterLabel, MAXINDILABEL, "Filter#%d", i + 1);
63
64 INDI::WidgetText oneText;
65 oneText.fill(filterName, filterLabel, filterLabel);
66 FilterNameTP.push(std::move(oneText));
67 }
68
69 FilterNameTP.fill(getDeviceName(), "FILTER_NAME", "Filter",
72 }
73 }
74 defineProperty(&MaxFilterNP);
75}
76
78{
80
81 IUFillNumber(&MaxFilterN[0], "Count", "Count", "%.f", 1, 16, 1, 5);
82 IUFillNumberVector(&MaxFilterNP, MaxFilterN, 1, getDeviceName(), "MAX_FILTER", "Filters", MAIN_CONTROL_TAB, IP_RW, 60,
83 IPS_IDLE);
84
85 CurrentFilter = 1;
86 FilterSlotNP[0].setMin(1);
87 FilterSlotNP[0].setMax(5);
88
90
91 return true;
92}
93
95{
96 TargetFilter = f;
97 char cmd[8] = {0}, res[8] = {0};
98 int rc = -1, nbytes_written = 0, nbytes_read = 0;
99
100 LOGF_DEBUG("CMD <%d>", TargetFilter - 1);
101
102 snprintf(cmd, 2, "%d", TargetFilter - 1);
103
104 if (isSimulation())
105 snprintf(res, 8, "%d", TargetFilter - 1);
106 else
107 {
108 if ((rc = tty_write_string(PortFD, cmd, &nbytes_written)) != TTY_OK)
109 {
110 char error_message[ERRMSG_SIZE];
111 tty_error_msg(rc, error_message, ERRMSG_SIZE);
112
113 LOGF_ERROR("Sending command select filter failed: %s", error_message);
114 return false;
115 }
116
117 if ((rc = tty_read(PortFD, res, 1, 30, &nbytes_read)) != TTY_OK)
118 {
119 char error_message[ERRMSG_SIZE];
120 tty_error_msg(rc, error_message, ERRMSG_SIZE);
121
122 LOGF_ERROR("Reading select filter response failed: %s", error_message);
123 return false;
124 }
125
126 LOGF_DEBUG("RES <%s>", res);
127 }
128
129 if (atoi(res) + 1 == TargetFilter)
130 {
133 return true;
134 }
135
136 return false;
137}
138
139bool QHYCFW2::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
140{
141 if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
142 {
143 if (!strcmp(name, MaxFilterNP.name))
144 {
145 if (values[0] != MaxFilterN[0].value)
146 {
147 IUUpdateNumber(&MaxFilterNP, values, names, n);
148 saveConfig();
149 LOG_INFO("Max number of filters updated. You must reconnect for this change to take effect.");
150 }
151
152 MaxFilterNP.s = IPS_OK;
153 IDSetNumber(&MaxFilterNP, nullptr);
154 return true;
155 }
156
157 }
158
159 return INDI::FilterWheel::ISNewNumber(dev, name, values, names, n);
160}
161
163{
164 FilterWheel::saveConfigItems(fp);
165
166 IUSaveConfigNumber(fp, &MaxFilterNP);
167
168 return true;
169}
bool isConnected() const
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.
INDI::PropertyNumber FilterSlotNP
INDI::PropertyText FilterNameTP
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
Initialize 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.
void push(WidgetView< T > &&item)
void resize(size_t size)
const char * getGroupName() const
void fill(const char *device, const char *name, const char *label, const char *group, IPerm permission, double timeout, IPState state)
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
Definition qhycfw2.cpp:162
virtual bool SelectFilter(int) override
Select a new filter position.
Definition qhycfw2.cpp:94
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
Definition qhycfw2.cpp:77
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:139
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:184
@ IPS_IDLE
Definition indiapi.h:159
@ IPS_OK
Definition indiapi.h:160
#define MAXINDILABEL
Definition indiapi.h:190
#define MAXINDINAME
Definition indiapi.h:189
int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read)
read buffer from terminal
Definition indicom.c:548
int tty_write_string(int fd, const char *buf, int *nbytes_written)
Writes a null terminated string to fd.
Definition indicom.c:473
void tty_error_msg(int err_code, char *err_msg, int err_msg_len)
Retrieve the tty error message.
Definition indicom.c:1243
Implementations for common driver routines.
@ TTY_OK
Definition indicom.h:54
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:490
void IUSaveConfigNumber(FILE *fp, const INumberVectorProperty *nvp)
Add a number vector property value to the configuration file.
Definition indidevapi.c:23
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:398
void IDSetNumber(const INumberVectorProperty *nvp, const char *fmt,...)
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:846
int IUUpdateNumber(INumberVectorProperty *nvp, double values[], char *names[], int n)
Update all numbers in a number vector property.
#define LOGF_DEBUG(fmt,...)
Definition indilogger.h:83
#define LOGF_ERROR(fmt,...)
Definition indilogger.h:80
#define LOG_INFO(txt)
Definition indilogger.h:74
#define ERRMSG_SIZE
Definition indimacros.h:33
#define getDeviceName
uint8_t cmd[4]
Definition pwc-ioctl.h:2
void fill(const char *name, const char *label, const char *initialText)
char name[MAXINDINAME]
Definition indiapi.h:321