Instrument Neutral Distributed Interface INDI 2.2.5
manual_filter.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2018 Jasem Mutlaq. All rights reserved.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*******************************************************************************/
18
19#include "manual_filter.h"
20
21#include <memory>
22#include <cstring>
23
24// We declare an auto pointer to ManualFilter.
25static std::unique_ptr<ManualFilter> manual_filter(new ManualFilter());
26
28{
29 return static_cast<const char *>("Manual Filter");
30}
31
32void ManualFilter::ISGetProperties(const char *dev)
33{
35
36 defineProperty(MaxFiltersNP);
37}
38
40{
42
43 // Max number of filters
44 MaxFiltersNP[0].fill("MAX", "Filters", "%.f", 1, 16, 1, 5);
45 MaxFiltersNP.fill(getDeviceName(), "MAX_FILTERS", "Max.", MAIN_CONTROL_TAB, IP_RW, 60, IPS_IDLE);
46 MaxFiltersNP.load();
47
48 FilterSlotNP[0].setMax(MaxFiltersNP[0].getValue());
49
50 // If exists filter names count is less than our desired max count. Save them then generate samples to fill the rest,
51 // then restore the current filters before we overwrote them with generateSampleFilters.
52 if (FilterNameTP.count() != MaxFiltersNP[0].getValue())
53 {
54 if (FilterNameTP.count() < MaxFiltersNP[0].getValue())
55 {
56 // Save existing labels and texts
57 std::vector<std::string> labels, values;
58 for (auto oneFilter : FilterNameTP)
59 {
60 labels.push_back(oneFilter.getLabel());
61 values.push_back(oneFilter.getText());
62 }
63
64 // Generate all samples to fill the rest
66
67 // Now restore the first filter names that we loaded from config
68 for (uint8_t i = 0; i < labels.size(); i++)
69 {
70 FilterNameTP[i].setLabel(labels[i]);
71 FilterNameTP[i].setText(values[i]);
72 }
73 }
74 else
75 {
76 FilterNameTP.resize(MaxFiltersNP[0].getValue());
78 }
79
80 // Make sure to save changes to config immediately.
81 saveConfig();
82 }
83
84 // User Set Filter
85 FilterSetSP[0].fill("FILTER_SET", "Filter is set", ISS_OFF);
86 FilterSetSP.fill(getDeviceName(), "CONFIRM_FILTER_SET", "Confirm", MAIN_CONTROL_TAB, IP_RW, ISR_ATMOST1, 60, IPS_IDLE);
87
88 // Sync Filter Position. Sets current position to different position without actually changing filter.
89 SyncNP[0].fill("TARGET_FILTER", "Target Filter", "%.f", 1, 16, 1, 0);
90 SyncNP.fill(getDeviceName(), "SYNC_FILTER", "Sync", MAIN_CONTROL_TAB, IP_WO, 60, IPS_IDLE);
91
92 return true;
93}
94
96{
98
99 if (isConnected())
100 {
101 deleteProperty(MaxFiltersNP);
102
103 defineProperty(SyncNP);
104 defineProperty(FilterSetSP);
105 }
106 else
107 {
108 deleteProperty(SyncNP);
109 deleteProperty(FilterSetSP);
110
111 defineProperty(MaxFiltersNP);
112 }
113
114 return true;
115}
116
117bool ManualFilter::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
118{
119 if (!strcmp(dev, getDeviceName()))
120 {
121 if (SyncNP.isNameMatch(name))
122 {
123 SyncNP.update(values, names, n);
124 CurrentFilter = SyncNP[0].getValue();
125 FilterSlotNP[0].setValue(CurrentFilter);
127 SyncNP.setState(IPS_OK);
128 SyncNP.apply();
129
130 LOGF_INFO("Filter wheel is synced to slot %d", CurrentFilter);
131
132 return true;
133 }
134 else if (MaxFiltersNP.isNameMatch(name))
135 {
136 MaxFiltersNP.update(values, names, n);
137 FilterSlotNP[0].setMax(MaxFiltersNP[0].getValue());
138 MaxFiltersNP.setState(IPS_OK);
139 saveConfig(MaxFiltersNP);
140 MaxFiltersNP.apply();
141
142 return true;
143 }
144 }
145
146 return INDI::FilterWheel::ISNewNumber(dev, name, values, names, n);
147}
148
149bool ManualFilter::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
150{
151 if (!strcmp(dev, getDeviceName()))
152 {
153 if (FilterSetSP.isNameMatch(name))
154 {
156 FilterSetSP.setState(IPS_OK);
157 FilterSetSP.apply();
158 return true;
159 }
160
161 }
162
163 return INDI::FilterWheel::ISNewSwitch(dev, name, states, names, n);
164}
165
167{
168 return true;
169}
170
172{
173 return true;
174}
175
177{
178 CurrentFilter = f;
179
180 FilterSetSP.setState(IPS_BUSY);
181 FilterSetSP.apply();
182 LOGF_INFO("Please change filter to %s then click Filter is set when done.", FilterNameTP[f - 1].getText());
183 return true;
184}
185
187{
189 MaxFiltersNP.save(fp);
190 return true;
191}
bool isConnected() const
INDI::PropertyText getText(const char *name) const
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
INDI::PropertyNumber FilterSlotNP
INDI::PropertyText FilterNameTP
void generateSampleFilters()
generateSampleFilters Generate sample 8-filter wheel and fill it sample filters
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 updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
void setState(IPState state)
void apply(const char *format,...) const ATTRIBUTE_FORMAT_PRINTF(2
bool load()
load Attempt to load property values from configuration file.
void save(FILE *f) const
save Save property to configuration file.
void resize(size_t size)
void setLabel(const char *label)
bool isNameMatch(const char *otherName) const
bool update(const double values[], const char *const names[], int n)
void fill(const char *device, const char *name, const char *label, const char *group, IPerm permission, double timeout, IPState state)
void fill(const char *device, const char *name, const char *label, const char *group, IPerm permission, ISRule rule, double timeout, IPState state)
Manual filter enables users from changing filter wheels manually.
bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
virtual bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool SelectFilter(int) override
Select a new filter position.
const char * getDefaultName() override
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
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 Disconnect() override
Disconnect from device.
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
ISState
Switch state.
Definition indiapi.h:148
@ ISS_OFF
Definition indiapi.h:149
@ IP_RW
Definition indiapi.h:184
@ IP_WO
Definition indiapi.h:183
@ IPS_BUSY
Definition indiapi.h:161
@ IPS_IDLE
Definition indiapi.h:159
@ IPS_OK
Definition indiapi.h:160
@ ISR_ATMOST1
Definition indiapi.h:172
#define LOGF_INFO(fmt,...)
Definition indilogger.h:82
#define getDeviceName