Instrument Neutral Distributed Interface INDI  2.0.2
skycommander.c
Go to the documentation of this file.
1 #if 0
2  Sky Commander INDI driver
3  Copyright (C) 2005 Jasem Mutlaq (mutlaqja@ikarustech.com)
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #endif
20 
21 #include "lx200driver.h"
22 
23 #include "indidevapi.h"
24 #include "indicom.h"
25 
26 #ifndef _WIN32
27 #include <termios.h>
28 #endif
29 
30 #define mydev "Sky Commander"
31 #define BASIC_GROUP "Main Control"
32 #define POLLMS_OVERRIDE 1000
33 #define currentRA eq[0].value
34 #define currentDEC eq[1].value
35 #define SKYCOMMANDER_TIMEOUT 5
36 
37 static void ISPoll(void *);
38 static void ISInit(void);
39 static void connectTelescope(void);
40 
41 int fd;
42 
43 static ISwitch PowerS[] = { { "CONNECT", "Connect", ISS_OFF, 0, 0 }, { "DISCONNECT", "Disconnect", ISS_ON, 0, 0 } };
44 ISwitchVectorProperty PowerSP = { mydev, "CONNECTION", "Connection", BASIC_GROUP, IP_RW, ISR_1OFMANY,
45  0, IPS_IDLE, PowerS, NARRAY(PowerS), "", 0 };
46 
47 static IText PortT[] = { { "PORT", "Port", 0, 0, 0, 0 } };
48 static ITextVectorProperty PortTP = { mydev, "DEVICE_PORT", "Ports", BASIC_GROUP, IP_RW, 0,
49  IPS_IDLE, PortT, NARRAY(PortT), "", 0 };
50 
51 /* equatorial position */
52 INumber eq[] = {
53  { "RA", "RA H:M:S", "%10.6m", 0., 24., 0., 0., 0, 0, 0 },
54  { "DEC", "Dec D:M:S", "%10.6m", -90., 90., 0., 0., 0, 0, 0 },
55 };
57  mydev, "EQUATORIAL_EOD_COORD", "Equatorial JNow", BASIC_GROUP, IP_RO, 0, IPS_IDLE, eq, NARRAY(eq), "", 0
58 };
59 
60 void ISInit(void)
61 {
62  static int isInit = 0;
63 
64  if (isInit)
65  return;
66 
67  isInit = 1;
68  fd = -1;
69 
70  IEAddTimer(POLLMS_OVERRIDE, ISPoll, NULL);
71 }
72 
73 void ISGetProperties(const char *dev)
74 {
75  ISInit();
76 
77  dev = dev;
78 
79  IDDefSwitch(&PowerSP, NULL);
80  IDDefText(&PortTP, NULL);
81  IDDefNumber(&eqNum, NULL);
82 }
83 
84 void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
85 {
86  ISInit();
87 
88  dev = dev;
89 
90  if (!strcmp(name, PowerSP.name))
91  {
93  IUUpdateSwitch(&PowerSP, states, names, n);
94  connectTelescope();
95  return;
96  }
97 }
98 
99 void ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
100 {
101  ISInit();
102 
103  dev = dev;
104  names = names;
105  n = n;
106 
107  if (!strcmp(name, PortTP.name))
108  {
109  IUSaveText(&PortT[0], texts[0]);
110  PortTP.s = IPS_OK;
111  IDSetText(&PortTP, NULL);
112  return;
113  }
114 }
115 void ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
116 {
117  dev = dev;
118  name = name;
119  values = values;
120  names = names;
121  n = n;
122 }
123 
124 void ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[],
125  char *names[], int n)
126 {
127  INDI_UNUSED(dev);
128  INDI_UNUSED(name);
129  INDI_UNUSED(sizes);
130  INDI_UNUSED(blobsizes);
131  INDI_UNUSED(blobs);
132  INDI_UNUSED(formats);
133  INDI_UNUSED(names);
134  INDI_UNUSED(n);
135 }
137 {
138  INDI_UNUSED(root);
139 }
140 
141 int updateSkyCommanderCoord(int fd, double *ra, double *dec)
142 {
143  char coords[16];
144  char CR[1] = { (char)0x0D };
145  float RA = 0.0, DEC = 0.0;
146  int error_type;
147  int nbytes_read = 0;
148 
149  error_type = write(fd, CR, 1);
150 
151  error_type = tty_read(fd, coords, 16, SKYCOMMANDER_TIMEOUT, &nbytes_read);
152  /*read_ret = portRead(coords, 16, LX200_TIMEOUT);*/
153  tcflush(fd, TCIFLUSH);
154 
155  nbytes_read = sscanf(coords, " %g %g", &RA, &DEC);
156 
157  if (nbytes_read < 2)
158  {
159  IDLog("Error in Sky commander number format [%s], exiting.\n", coords);
160  return error_type;
161  }
162 
163  *ra = RA;
164  *dec = DEC;
165 
166  return 0;
167 }
168 
169 void ISPoll(void *p)
170 {
171  p = p;
172 
173  if (PowerS[0].s == ISS_ON)
174  {
175  switch (eqNum.s)
176  {
177  case IPS_IDLE:
178  case IPS_OK:
179  case IPS_BUSY:
181  {
182  eqNum.s = IPS_ALERT;
183  IDSetNumber(&eqNum, "Unknown error while reading telescope coordinates");
184  IDLog("Unknown error while reading telescope coordinates\n");
185  break;
186  }
187 
188  IDSetNumber(&eqNum, NULL);
189  break;
190 
191  case IPS_ALERT:
192  break;
193  }
194  }
195 
196  IEAddTimer(POLLMS_OVERRIDE, ISPoll, NULL);
197 }
198 
199 void connectTelescope(void)
200 {
201  switch (PowerS[0].s)
202  {
203  case ISS_ON:
204  if (tty_connect(PortT[0].text, 9600, 8, 0, 1, &fd) != TTY_OK)
205  {
206  PowerSP.s = IPS_ALERT;
208  IDSetSwitch(&PowerSP, "Error connecting to port %s", PortT[0].text);
209  return;
210  }
211 
212  PowerSP.s = IPS_OK;
213  IDSetSwitch(&PowerSP, "Sky Commander is online.");
214  break;
215 
216  case ISS_OFF:
219  eqNum.s = PortTP.s = PowerSP.s = IPS_IDLE;
220  IDSetSwitch(&PowerSP, "Sky Commander is offline.");
221  IDSetText(&PortTP, NULL);
222  IDSetNumber(&eqNum, NULL);
223  break;
224  }
225 }
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
void ISInit(void)
Definition: fli_wheel.c:121
void ISPoll(void *)
Definition: fli_wheel.c:393
double ra
double dec
ISState
Switch state.
Definition: indiapi.h:150
@ ISS_OFF
Definition: indiapi.h:151
@ ISS_ON
Definition: indiapi.h:152
#define NARRAY(a)
Handy macro to find the number of elements in array a[]. Must be used with actual array,...
Definition: indiapi.h:500
@ IP_RW
Definition: indiapi.h:186
@ IP_RO
Definition: indiapi.h:184
@ IPS_BUSY
Definition: indiapi.h:163
@ IPS_ALERT
Definition: indiapi.h:164
@ IPS_IDLE
Definition: indiapi.h:161
@ IPS_OK
Definition: indiapi.h:162
@ ISR_1OFMANY
Definition: indiapi.h:173
int tty_connect(const char *device, int bit_rate, int word_size, int parity, int stop_bits, int *fd)
Establishes a tty connection to a terminal device.
Definition: indicom.c:946
int tty_disconnect(int fd)
Closes a tty connection and flushes the bus.
Definition: indicom.c:1148
void IDLog(const char *fmt,...)
Definition: indicom.c:316
int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read)
read buffer from terminal
Definition: indicom.c:482
Implementations for common driver routines.
@ TTY_OK
Definition: indicom.h:150
void IUResetSwitch(ISwitchVectorProperty *svp)
Reset all switches in a switch vector property to OFF.
Definition: indidevapi.c:148
void IUSaveText(IText *tp, const char *newtext)
Function to reliably save new text in a IText.
Definition: indidevapi.c:36
Interface to the reference INDI C API device implementation on the Device Driver side.
#define INDI_UNUSED(x)
Definition: indidevapi.h:131
int IUUpdateSwitch(ISwitchVectorProperty *svp, ISState *states, char *names[], int n)
Update all switches in a switch vector property.
Definition: indidriver.c:1308
void IDSetNumber(const INumberVectorProperty *nvp, const char *fmt,...)
Definition: indidriver.c:1211
void IDSetSwitch(const ISwitchVectorProperty *svp, const char *fmt,...)
Definition: indidriver.c:1231
void IDDefNumber(const INumberVectorProperty *nvp, const char *fmt,...)
Definition: indidriver.c:1104
void IDDefText(const ITextVectorProperty *tvp, const char *fmt,...)
Definition: indidriver.c:1081
void IDSetText(const ITextVectorProperty *tvp, const char *fmt,...)
Definition: indidriver.c:1191
void IDDefSwitch(const ISwitchVectorProperty *svp, const char *fmt,...)
Definition: indidriver.c:1127
ITextVectorProperty PortTP
Definition: magellan1.cpp:65
Namespace to encapsulate INDI client, drivers, and mediator classes.
#define currentDEC
Definition: skycommander.c:34
#define SKYCOMMANDER_TIMEOUT
Definition: skycommander.c:35
void ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n)
Update data of an existing blob vector property.
Definition: skycommander.c:124
void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Update the value of an existing switch vector property.
Definition: skycommander.c:84
void ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
Update the value of an existing text vector property.
Definition: skycommander.c:99
int fd
Definition: skycommander.c:41
#define BASIC_GROUP
Definition: skycommander.c:31
#define POLLMS_OVERRIDE
Definition: skycommander.c:32
#define currentRA
Definition: skycommander.c:33
void ISGetProperties(const char *dev)
Get Device Properties.
Definition: skycommander.c:73
int updateSkyCommanderCoord(int fd, double *ra, double *dec)
Definition: skycommander.c:141
INumberVectorProperty eqNum
Definition: skycommander.c:56
#define mydev
Definition: skycommander.c:30
void ISSnoopDevice(XMLEle *root)
Function defined by Drivers that is called when another Driver it is snooping (by having previously c...
Definition: skycommander.c:136
void ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
Definition: skycommander.c:115
INumber eq[]
Definition: skycommander.c:52
ISwitchVectorProperty PowerSP
Definition: skycommander.c:44
One number descriptor.
One switch descriptor.
One text descriptor.
Number vector property descriptor.
Definition: indiapi.h:319
Switch vector property descriptor.
Definition: indiapi.h:367
char name[MAXINDINAME]
Definition: indiapi.h:371
Text vector property descriptor.
Definition: indiapi.h:246
char name[MAXINDINAME]
Definition: indiapi.h:250