Instrument Neutral Distributed Interface INDI 2.2.5
simpleskeleton.cpp
Go to the documentation of this file.
1#if 0
2Simple Skeleton - Tutorial Four
3Demonstration of libindi v0.7 capabilities.
4
5Copyright (C) 2010 Jasem Mutlaq ([email protected])
6
7This library is free software;
8you can redistribute it and / or
9modify it under the terms of the GNU Lesser General Public
10License as published by the Free Software Foundation;
11either
12version 2.1 of the License, or (at your option) any later version.
13
14This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY;
16without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18Lesser General Public License for more details.
19
20You should have received a copy of the GNU Lesser General Public
21License along with this library;
22if not, write to the Free Software
23Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA
24
25#endif
26
37#include "simpleskeleton.h"
38#include <indipropertyswitch.h>
39#include <indipropertynumber.h>
40#include <indipropertyblob.h>
41
42#include <cstdlib>
43#include <cstring>
44#include <memory>
45
46#include <sys/stat.h>
47
48/* Our simpleSkeleton auto pointer */
49std::unique_ptr<SimpleSkeleton> simpleSkeleton(new SimpleSkeleton());
50
51//const int POLLMS = 1000; // Period of update, 1 second.
52
53/**************************************************************************************
54** Initialize all properties & set default values.
55**************************************************************************************/
57{
58 DefaultDevice::initProperties();
59
60 // This is the default driver skeleton file location
61 // Convention is: drivername_sk_xml
62 // Default location is /usr/share/indi
63 const char *skelFileName = "/usr/share/indi/tutorial_four_sk.xml";
64 struct stat st;
65
66 char *skel = getenv("INDISKEL");
67
68 if (skel != nullptr)
69 buildSkeleton(skel);
70 else if (stat(skelFileName, &st) == 0)
71 buildSkeleton(skelFileName);
72 else
73 IDLog(
74 "No skeleton file was specified. Set environment variable INDISKEL to the skeleton path and try again.\n");
75
76 // Optional: Add aux controls for configuration, debug & simulation that get added in the Options tab
77 // of the driver.
79
80 // Let's print a list of all device properties
81 int i = 0;
82 for(const auto &oneProperty : getProperties())
83 IDLog("Property #%d: %s\n", i++, oneProperty.getName());
84
85 // Set the green light (IPS_OK) for a "Number Property" if changed
86 INDI::PropertyNumber number = getNumber("Number Property");
87 number.onUpdate([number, this]() mutable
88 {
89 if (!isConnected())
90 {
91 number.setState(IPS_ALERT);
92 number.apply("Cannot change property while device is disconnected.");
93 return;
94 }
95 number.setState(IPS_OK);
96 number.apply();
97 });
98
99 // Set random light state for selected switch index
100 INDI::PropertySwitch menu = getSwitch("Menu");
101 menu.onUpdate([menu, this]() mutable
102 {
103 if (!isConnected())
104 {
105 menu.setState(IPS_ALERT);
106 menu.apply("Cannot change property while device is disconnected.");
107 return;
108 }
109 auto index = menu.findOnSwitchIndex();
110 if (index < 0)
111 return;
112
113 menu.setState(IPS_OK);
114
115 INDI::PropertyLight light = getLight("Light Property");
116 light[index].setState(static_cast<IPState>(rand() % 4));
117 light.setState(IPS_OK);
118 light.apply();
119 });
120
121 // Show blob if changed
122 INDI::PropertyBlob blob = getBLOB("BLOB Test");
123 blob.onUpdate([blob, this]() mutable
124 {
125 if (!isConnected())
126 {
127 blob.setState(IPS_ALERT);
128 blob.apply("Cannot change property while device is disconnected.");
129 return;
130 }
131 IDLog("Received BLOB with name %s, format %s, and size %d, and bloblen %d\n",
132 blob[0].getName(), blob[0].getFormat(), blob[0].getSize(), blob[0].getBlobLen());
133
134 IDLog("BLOB Content:\n"
135 "##################################\n"
136 "%s\n"
137 "##################################\n",
138 blob[0].getBlobAsString().c_str());
139
140 blob[0].setSize(0);
141 blob.setState(IPS_OK);
142 blob.apply();
143 });
144
145 return true;
146}
147
148/**************************************************************************************
149** Define Basic properties to clients.
150***************************************************************************************/
152{
153 static int configLoaded = 0;
154
155 // Ask the default driver first to send properties.
157
158 // If no configuration is load before, then load it now.
159 if (configLoaded == 0)
160 {
161 loadConfig();
162 configLoaded = 1;
163 }
164}
165
166/**************************************************************************************
167**
168***************************************************************************************/
170{
171 return true;
172}
173
175{
176 return true;
177}
178
180{
181 return "Simple Skeleton";
182}
bool isConnected() const
INDI::PropertyNumber getNumber(const char *name) const
bool buildSkeleton(const char *filename)
Build driver properties from a skeleton file.
INDI::PropertySwitch getSwitch(const char *name) const
INDI::PropertyBlob getBLOB(const char *name) const
INDI::PropertyLight getLight(const char *name) const
Properties getProperties()
Return a list of all properties in the device.
virtual void ISGetProperties(const char *dev)
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
virtual bool loadConfig(bool silent=false, const char *property=nullptr)
Load the last saved configuration file.
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
void setState(IPState state)
void apply(const char *format,...) const ATTRIBUTE_FORMAT_PRINTF(2
void onUpdate(const std::function< void()> &callback)
bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
const char * getDefaultName() override
bool Disconnect() override
Disconnect from device.
void ISGetProperties(const char *dev) override
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
IPState
Property state.
Definition indiapi.h:158
@ IPS_ALERT
Definition indiapi.h:162
@ IPS_OK
Definition indiapi.h:160
void IDLog(const char *fmt,...)
Definition indicom.c:305
std::unique_ptr< SimpleSkeleton > simpleSkeleton(new SimpleSkeleton())
Construct a basic INDI CCD device that demonstrates ability to define properties from a skeleton file...