Instrument Neutral Distributed Interface INDI  2.0.2
gpusb.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright(c) 2012-2019 Jasem Mutlaq. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License as published by the Free
6  Software Foundation; either version 2 of the License, or (at your option)
7  any later version.
8 
9  This program is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 
19  The full GNU General Public License is included in this distribution in the
20  file called LICENSE.
21 *******************************************************************************/
22 
23 #include "gpusb.h"
24 
25 #include "gpdriver.h"
26 
27 #include <memory>
28 #include <cstring>
29 #include <unistd.h>
30 
31 // We declare an auto pointer to gpGuide.
32 static std::unique_ptr<GPUSB> gpGuide(new GPUSB());
33 
35 {
36  driver = new GPUSBDriver();
37 }
38 
40 {
41  delete (driver);
42 }
43 
44 const char *GPUSB::getDefaultName()
45 {
46  return "GPUSB";
47 }
48 
50 {
51  driver->setDebug(isDebug());
52 
53  bool rc = driver->Connect();
54 
55  if (rc)
56  LOG_INFO("GPUSB is online.");
57  else
58  LOG_ERROR("Error: cannot find GPUSB device.");
59 
60  return rc;
61 }
62 
64 {
65  LOG_INFO("GPSUSB is offline.");
66 
67  return driver->Disconnect();
68 }
69 
71 {
73 
75 
77 
79 
81 
82  return true;
83 }
84 
86 {
88 
89  if (isConnected())
90  {
93  }
94  else
95  {
98  }
99 
100  return true;
101 }
102 
103 bool GPUSB::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
104 {
105  if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
106  {
107  if (!strcmp(name, GuideNSNP.name) || !strcmp(name, GuideWENP.name))
108  {
109  processGuiderProperties(name, values, names, n);
110  return true;
111  }
112  }
113 
114  return INDI::DefaultDevice::ISNewNumber(dev, name, values, names, n);
115 }
116 
117 void GPUSB::debugTriggered(bool enable)
118 {
119  driver->setDebug(enable);
120 }
121 
122 //float GPUSB::CalcWEPulseTimeLeft()
123 //{
124 // double timesince;
125 // double timeleft;
126 // struct timeval now
127 // {
128 // 0, 0
129 // };
130 // gettimeofday(&now, nullptr);
131 
132 // timesince = (double)(now.tv_sec * 1000.0 + now.tv_usec / 1000) -
133 // (double)(WEPulseStart.tv_sec * 1000.0 + WEPulseStart.tv_usec / 1000);
134 // timesince = timesince / 1000;
135 
136 // timeleft = WEPulseRequest - timesince;
137 // return timeleft;
138 //}
139 
140 //float GPUSB::CalcNSPulseTimeLeft()
141 //{
142 // double timesince;
143 // double timeleft;
144 // struct timeval now
145 // {
146 // 0, 0
147 // };
148 // gettimeofday(&now, nullptr);
149 
150 // timesince = (double)(now.tv_sec * 1000.0 + now.tv_usec / 1000) -
151 // (double)(NSPulseStart.tv_sec * 1000.0 + NSPulseStart.tv_usec / 1000);
152 // timesince = timesince / 1000;
153 
154 // timeleft = NSPulseRequest - timesince;
155 // return timeleft;
156 //}
157 
158 
160 {
161  RemoveTimer(NSTimerID);
162 
163  driver->startPulse(GPUSB_NORTH);
164 
165  NSDirection = GPUSB_NORTH;
166 
167  LOG_DEBUG("Starting NORTH guide");
168 
169  NSPulseRequest = ms;
170 
171  NSGuideTS = std::chrono::system_clock::now();
172 
173  NSTimerID = IEAddTimer(ms, &GPUSB::NSTimerHelper, this);
174 
175  return IPS_BUSY;
176 }
177 
179 {
180  RemoveTimer(NSTimerID);
181 
182  driver->startPulse(GPUSB_SOUTH);
183 
184  NSDirection = GPUSB_SOUTH;
185 
186  LOG_DEBUG("Starting SOUTH guide");
187 
188  NSPulseRequest = ms;
189 
190  NSGuideTS = std::chrono::system_clock::now();
191 
192  NSTimerID = IEAddTimer(ms, &GPUSB::NSTimerHelper, this);
193 
194  return IPS_BUSY;
195 }
196 
198 {
199  RemoveTimer(WETimerID);
200 
201  driver->startPulse(GPUSB_EAST);
202 
203  WEDirection = GPUSB_EAST;
204 
205  LOG_DEBUG("Starting EAST guide");
206 
207  WEPulseRequest = ms;
208 
209  WEGuideTS = std::chrono::system_clock::now();
210 
211  WETimerID = IEAddTimer(ms, &GPUSB::WETimerHelper, this);
212 
213  return IPS_BUSY;
214 }
215 
217 {
218  RemoveTimer(WETimerID);
219 
220  driver->startPulse(GPUSB_WEST);
221 
222  WEDirection = GPUSB_WEST;
223 
224  LOG_DEBUG("Starting WEST guide");
225 
226  WEPulseRequest = ms;
227 
228  WEGuideTS = std::chrono::system_clock::now();
229 
230  WETimerID = IEAddTimer(ms, &GPUSB::WETimerHelper, this);
231 
232  return IPS_BUSY;
233 }
234 
235 void GPUSB::NSTimerHelper(void *context)
236 {
237  static_cast<GPUSB*>(context)->NSTimerCallback();
238 }
239 
240 void GPUSB::WETimerHelper(void *context)
241 {
242  static_cast<GPUSB*>(context)->WETimerCallback();
243 }
244 
245 void GPUSB::NSTimerCallback()
246 {
247  driver->stopPulse(NSDirection);
249 }
250 
251 void GPUSB::WETimerCallback()
252 {
253  driver->stopPulse(WEDirection);
255 }
256 
void setDebug(bool enable)
Definition: gpdriver.h:52
bool Connect()
Definition: gpdriver.cpp:38
bool Disconnect()
Definition: gpdriver.cpp:53
bool stopPulse(int direction)
Definition: gpdriver.cpp:99
bool startPulse(int direction)
Definition: gpdriver.cpp:59
Definition: gpusb.h:33
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
Definition: gpusb.cpp:49
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
Definition: gpusb.cpp:103
virtual IPState GuideEast(uint32_t ms) override
Guide east for ms milliseconds. East is defined as RA+.
Definition: gpusb.cpp:197
virtual IPState GuideWest(uint32_t ms) override
Guide west for ms milliseconds. West is defined as RA-.
Definition: gpusb.cpp:216
virtual IPState GuideNorth(uint32_t ms) override
Guide north for ms milliseconds. North is defined as DEC+.
Definition: gpusb.cpp:159
const char * getDefaultName() override
Definition: gpusb.cpp:44
bool Disconnect() override
Disconnect from device.
Definition: gpusb.cpp:63
GPUSB()
Definition: gpusb.cpp:34
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: gpusb.cpp:70
static void NSTimerHelper(void *context)
Definition: gpusb.cpp:235
virtual ~GPUSB()
Definition: gpusb.cpp:39
virtual IPState GuideSouth(uint32_t ms) override
Guide south for ms milliseconds. South is defined as DEC-.
Definition: gpusb.cpp:178
static void WETimerHelper(void *context)
Definition: gpusb.cpp:240
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: gpusb.cpp:85
void debugTriggered(bool enable) override
Inform driver that the debug option was triggered. This function is called after setDebug is triggere...
Definition: gpusb.cpp:117
bool isConnected() const
Definition: basedevice.cpp:520
const char * getDeviceName() const
Definition: basedevice.cpp:821
virtual bool updateProperties()
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
void setDefaultPollingPeriod(uint32_t msec)
setDefaultPollingPeriod Change the default polling period to call TimerHit() function in the driver.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
virtual bool initProperties()
Initilize properties initial state and value. The child class must implement this function.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process the client newNumber command.
void setDriverInterface(uint16_t value)
setInterface Set driver interface. By default the driver interface is set to GENERAL_DEVICE....
void RemoveTimer(int id)
Remove timer added with SetTimer.
void addDebugControl()
Add Debug control to the driver.
virtual void GuideComplete(INDI_EQ_AXIS axis)
Call GuideComplete once the guiding pulse is complete.
INumberVectorProperty GuideNSNP
void initGuiderProperties(const char *deviceName, const char *groupName)
Initilize guider properties. It is recommended to call this function within initProperties() of your ...
INumberVectorProperty GuideWENP
void processGuiderProperties(const char *name, double values[], char *names[], int n)
Call this function whenever client updates GuideNSNP or GuideWSP properties in the primary device....
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
int IEAddTimer(int millisecs, IE_TCF *fp, void *p)
Register a new single-shot timer function, fp, to be called with ud as argument after ms.
Definition: eventloop.c:582
@ GPUSB_SOUTH
Definition: gpdriver.h:30
@ GPUSB_WEST
Definition: gpdriver.h:32
@ GPUSB_NORTH
Definition: gpdriver.h:29
@ GPUSB_EAST
Definition: gpdriver.h:31
IPState
Property state.
Definition: indiapi.h:160
@ IPS_BUSY
Definition: indiapi.h:163
@ AXIS_DE
Definition: indibasetypes.h:36
@ AXIS_RA
Definition: indibasetypes.h:35
#define LOG_DEBUG(txt)
Definition: indilogger.h:75
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
Definition: indilogger.h:72
#define LOG_INFO(txt)
Definition: indilogger.h:74
char name[MAXINDINAME]
Definition: indiapi.h:323