Instrument Neutral Distributed Interface INDI 2.2.5
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.
31static std::unique_ptr<QHYCFW1> qhycfw(new QHYCFW1());
32
38
40{
41 return "QHYCFW1";
42}
43
44void 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 FilterSlotNP[0].setMax(maxCount);
54 if (FilterNameTP.size() != maxCount)
55 {
56 char filterName[MAXINDINAME];
57 char filterLabel[MAXINDILABEL];
58
60
61 for (int i = 0; i < maxCount; i++)
62 {
63 snprintf(filterName, MAXINDINAME, "FILTER_SLOT_NAME_%d", i + 1);
64 snprintf(filterLabel, MAXINDILABEL, "Filter#%d", i + 1);
65
66 INDI::WidgetText oneText;
67 oneText.fill(filterName, filterLabel, filterLabel);
68 FilterNameTP.push(std::move(oneText));
69 }
70
71 FilterNameTP.fill(getDeviceName(), "FILTER_NAME", "Filter",
74 }
75 }
76 defineProperty(&MaxFilterNP);
77}
78
80{
82
83 IUFillNumber(&MaxFilterN[0], "Count", "Count", "%.f", 1, 16, 1, 5);
84 IUFillNumberVector(&MaxFilterNP, MaxFilterN, 1, getDeviceName(), "MAX_FILTER", "Filters", MAIN_CONTROL_TAB, IP_RW, 60,
85 IPS_IDLE);
86
87 CurrentFilter = 1;
88 FilterSlotNP[0].setMin(1);
89 FilterSlotNP[0].setMax(5);
90
92
93 return true;
94}
95
97{
98 TargetFilter = f;
99 char cmd[8] = {0}, res[8] = {0};
100 int rc = -1, nbytes_written = 0, nbytes_read = 0;
101
102 LOGF_DEBUG("CMD <%d>", TargetFilter - 1);
103
104 snprintf(cmd, 2, "%d", TargetFilter - 1);
105
106 if (isSimulation())
107 snprintf(res, 8, "%d", TargetFilter - 1);
108 else
109 {
110 if ((rc = tty_write_string(PortFD, cmd, &nbytes_written)) != TTY_OK)
111 {
112 char error_message[ERRMSG_SIZE];
113 tty_error_msg(rc, error_message, ERRMSG_SIZE);
114
115 LOGF_ERROR("Sending command select filter failed: %s", error_message);
116 return false;
117 }
118
119 // Wait 10 seconds
120 std::this_thread::sleep_for(std::chrono::seconds(10));
121
122 if ((rc = tty_read(PortFD, res, 1, 30, &nbytes_read)) != TTY_OK)
123 {
124 char error_message[ERRMSG_SIZE];
125 tty_error_msg(rc, error_message, ERRMSG_SIZE);
126
127 LOGF_ERROR("Reading select filter response failed: %s", error_message);
128 return false;
129 }
130
131 LOGF_DEBUG("RES <%s>", res);
132 }
133
134 //if (atoi(res) + 1 == TargetFilter)
135 if (res[0] == '-')
136 {
139 return true;
140 }
141
142 return false;
143}
144
145bool QHYCFW1::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
146{
147 if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
148 {
149 if (!strcmp(name, MaxFilterNP.name))
150 {
151 if (values[0] != MaxFilterN[0].value)
152 {
153 IUUpdateNumber(&MaxFilterNP, values, names, n);
154 saveConfig();
155 LOG_INFO("Max number of filters updated. You must reconnect for this change to take effect.");
156 }
157
158 MaxFilterNP.s = IPS_OK;
159 IDSetNumber(&MaxFilterNP, nullptr);
160 return true;
161 }
162
163 }
164
165 return INDI::FilterWheel::ISNewNumber(dev, name, values, names, n);
166}
167
169{
170 FilterWheel::saveConfigItems(fp);
171
172 IUSaveConfigNumber(fp, &MaxFilterNP);
173
174 return true;
175}
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 SelectFilter(int) override
Select a new filter position.
Definition qhycfw1.cpp:96
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:145
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
Initialize properties initial state and value. The child class must implement this function.
Definition qhycfw1.cpp:79
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
Definition qhycfw1.cpp:168
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