Instrument Neutral Distributed Interface INDI  1.9.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 
GPUSBDriver::setDebug
void setDebug(bool enable)
Definition: gpdriver.h:52
GPUSB_NORTH
@ GPUSB_NORTH
Definition: gpdriver.h:49
GPUSB::getDefaultName
const char * getDefaultName() override
Definition: gpusb.cpp:44
IPState
IPState
Property state.
Definition: indiapi.h:158
GPUSB::initProperties
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: gpusb.cpp:70
INDI::BaseDevice::GUIDER_INTERFACE
@ GUIDER_INTERFACE
Definition: basedevice.h:74
GPUSB::GuideWest
virtual IPState GuideWest(uint32_t ms) override
Guide west for ms milliseconds. West is defined as RA-.
Definition: gpusb.cpp:216
INDI::GuiderInterface::GuideWENP
INumberVectorProperty GuideWENP
Definition: indiguiderinterface.h:113
INDI::DefaultDevice::defineProperty
void defineProperty(INumberVectorProperty *property)
Definition: defaultdevice.cpp:997
MAIN_CONTROL_TAB
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
Definition: defaultdevice.cpp:34
GPUSB::Disconnect
bool Disconnect() override
Disconnect from device.
Definition: gpusb.cpp:63
GPUSB::GuideEast
virtual IPState GuideEast(uint32_t ms) override
Guide east for ms milliseconds. East is defined as RA+.
Definition: gpusb.cpp:197
GPUSB
Definition: gpusb.h:32
GPUSB::GuideNorth
virtual IPState GuideNorth(uint32_t ms) override
Guide north for ms milliseconds. North is defined as DEC+.
Definition: gpusb.cpp:159
INDI::DefaultDevice::setDefaultPollingPeriod
void setDefaultPollingPeriod(uint32_t msec)
setDefaultPollingPeriod Change the default polling period to call TimerHit() function in the driver.
Definition: defaultdevice.cpp:1157
GPUSBDriver::startPulse
bool startPulse(int direction)
Definition: gpdriver.cpp:59
INDI::BaseDevice::getDeviceName
const char * getDeviceName() const
Definition: basedevice.cpp:799
gpusb.h
GPUSB::GuideSouth
virtual IPState GuideSouth(uint32_t ms) override
Guide south for ms milliseconds. South is defined as DEC-.
Definition: gpusb.cpp:178
GPUSB::Connect
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
Definition: gpusb.cpp:49
INDI::GuiderInterface::initGuiderProperties
void initGuiderProperties(const char *deviceName, const char *groupName)
Initilize guider properties. It is recommended to call this function within initProperties() of your ...
Definition: indiguiderinterface.cpp:36
GPUSBDriver::Connect
bool Connect()
Definition: gpdriver.cpp:38
GPUSBDriver::stopPulse
bool stopPulse(int direction)
Definition: gpdriver.cpp:99
GPUSB::~GPUSB
virtual ~GPUSB()
Definition: gpusb.cpp:39
LOG_INFO
#define LOG_INFO(txt)
Definition: indilogger.h:74
INDI::DefaultDevice::initProperties
virtual bool initProperties()
Initilize properties initial state and value. The child class must implement this function.
Definition: defaultdevice.cpp:917
INDI::GuiderInterface::GuideNSNP
INumberVectorProperty GuideNSNP
Definition: indiguiderinterface.h:111
IEAddTimer
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:525
GPUSB::GPUSB
GPUSB()
Definition: gpusb.cpp:34
INDI::DefaultDevice::ISNewNumber
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process the client newNumber command.
Definition: defaultdevice.cpp:593
gpdriver.h
IPS_BUSY
@ IPS_BUSY
Definition: indiapi.h:162
GPUSB::debugTriggered
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
GPUSB::NSTimerHelper
static void NSTimerHelper(void *context)
Definition: gpusb.cpp:235
_INumberVectorProperty::name
char name[MAXINDINAME]
Definition: indiapi.h:322
INDI::DefaultDevice::isDebug
bool isDebug() const
Definition: defaultdevice.cpp:728
INDI::BaseDevice::isConnected
bool isConnected() const
Definition: basedevice.cpp:518
LOG_DEBUG
#define LOG_DEBUG(txt)
Definition: indilogger.h:75
LOG_ERROR
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
Definition: indilogger.h:72
GPUSBDriver::Disconnect
bool Disconnect()
Definition: gpdriver.cpp:53
INDI::GuiderInterface::GuideComplete
virtual void GuideComplete(INDI_EQ_AXIS axis)
Call GuideComplete once the guiding pulse is complete.
Definition: indiguiderinterface.cpp:86
AXIS_RA
@ AXIS_RA
Definition: indibasetypes.h:33
GPUSB_EAST
@ GPUSB_EAST
Definition: gpdriver.h:51
GPUSB::updateProperties
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: gpusb.cpp:85
INDI::DefaultDevice::RemoveTimer
void RemoveTimer(int id)
Remove timer added with SetTimer.
Definition: defaultdevice.cpp:874
INDI::BaseDevice::AUX_INTERFACE
@ AUX_INTERFACE
Definition: basedevice.h:87
GPUSB::WETimerHelper
static void WETimerHelper(void *context)
Definition: gpusb.cpp:240
INDI::DefaultDevice::addDebugControl
void addDebugControl()
Add Debug control to the driver.
Definition: defaultdevice.cpp:639
GPUSB_WEST
@ GPUSB_WEST
Definition: gpdriver.h:52
GPUSB::ISNewNumber
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
INDI::DefaultDevice::deleteProperty
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
Definition: defaultdevice.cpp:965
GPUSB_SOUTH
@ GPUSB_SOUTH
Definition: gpdriver.h:50
GPUSBDriver
Definition: gpdriver.h:39
INDI::DefaultDevice::setDriverInterface
void setDriverInterface(uint16_t value)
setInterface Set driver interface. By default the driver interface is set to GENERAL_DEVICE....
Definition: defaultdevice.cpp:902
AXIS_DE
@ AXIS_DE
Definition: indibasetypes.h:34
INDI::DefaultDevice::updateProperties
virtual bool updateProperties()
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: defaultdevice.cpp:890
INDI::GuiderInterface::processGuiderProperties
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....
Definition: indiguiderinterface.cpp:49