Instrument Neutral Distributed Interface INDI  2.0.2
simpleskeleton.cpp
Go to the documentation of this file.
1 #if 0
2 Simple Skeleton - Tutorial Four
3 Demonstration of libindi v0.7 capabilities.
4 
5 Copyright (C) 2010 Jasem Mutlaq (mutlaqja@ikarustech.com)
6 
7 This library is free software;
8 you can redistribute it and / or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation;
11 either
12 version 2.1 of the License, or (at your option) any later version.
13 
14 This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY;
16 without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library;
22 if not, write to the Free Software
23 Foundation, 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 */
49 std::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 ***************************************************************************************/
151 void SimpleSkeleton::ISGetProperties(const char *dev)
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
Definition: basedevice.cpp:520
INDI::PropertyNumber getNumber(const char *name) const
Definition: basedevice.cpp:89
bool buildSkeleton(const char *filename)
Build driver properties from a skeleton file.
Definition: basedevice.cpp:302
INDI::PropertySwitch getSwitch(const char *name) const
Definition: basedevice.cpp:99
INDI::PropertyBlob getBLOB(const char *name) const
Definition: basedevice.cpp:109
INDI::PropertyLight getLight(const char *name) const
Definition: basedevice.cpp:104
Properties getProperties()
Return a list of all properties in the device.
Definition: basedevice.cpp:158
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
Initilize 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:160
@ IPS_ALERT
Definition: indiapi.h:164
@ IPS_OK
Definition: indiapi.h:162
void IDLog(const char *fmt,...)
Definition: indicom.c:316
std::unique_ptr< SimpleSkeleton > simpleSkeleton(new SimpleSkeleton())
Construct a basic INDI CCD device that demonstrates ability to define properties from a skeleton file...