-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.cs
652 lines (574 loc) · 21.9 KB
/
Driver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
//tabs=4
// --------------------------------------------------------------------------------
// TODO fill in this information for your driver, then remove this line!
//
// ASCOM Dome driver for CVTDome
//
// Description: Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
// nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
// erat, sed diam voluptua. At vero eos et accusam et justo duo
// dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
// sanctus est Lorem ipsum dolor sit amet.
//
// Implements: ASCOM Dome interface version: <To be completed by driver developer>
// Author: (XXX) Your N. Here <[email protected]>
//
// Edit Log:
//
// Date Who Vers Description
// ----------- --- ----- -------------------------------------------------------
// dd-mmm-yyyy XXX 6.0.0 Initial edit, created from ASCOM driver template
// --------------------------------------------------------------------------------
//
// This is used to define code in the template that is specific to one class implementation
// unused code canbe deleted and this definition removed.
#define Dome
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;
using ASCOM;
using ASCOM.Astrometry;
using ASCOM.Astrometry.AstroUtils;
using ASCOM.Utilities;
using ASCOM.DeviceInterface;
using System.Globalization;
using System.Collections;
namespace ASCOM.CVTDome
{
//
// Your driver's DeviceID is ASCOM.CVTDome.Dome
//
// The Guid attribute sets the CLSID for ASCOM.CVTDome.Dome
// The ClassInterface/None addribute prevents an empty interface called
// _CVTDome from being created and used as the [default] interface
//
// TODO Replace the not implemented exceptions with code to implement the function or
// throw the appropriate ASCOM exception.
//
/// <summary>
/// ASCOM Dome Driver for CVTDome.
/// </summary>
[Guid("257151c7-fa4c-45ec-b381-23495db65b50")]
[ClassInterface(ClassInterfaceType.None)]
public class Dome : IDomeV2
{
/// <summary>
/// ASCOM DeviceID (COM ProgID) for this driver.
/// The DeviceID is used by ASCOM applications to load the driver at runtime.
/// </summary>
internal static string driverID = "ASCOM.CVTDome.Dome";
// TODO Change the descriptive string for your driver then remove this line
/// <summary>
/// Driver description that displays in the ASCOM Chooser.
/// </summary>
private static string driverDescription = "CVTDome";
internal static string comPortProfileName = "COM Port"; // Constants used for Profile persistence
internal static string comPortDefault = "COM1";
internal static string traceStateProfileName = "Trace Level";
internal static string traceStateDefault = "false";
internal static string comPort; // Variables to hold the currrent device configuration
/// <summary>
/// Private variable to hold the connected state
/// </summary>
private bool connectedState;
/// <summary>
/// Private variable to hold an ASCOM Utilities object
/// </summary>
private Util utilities;
/// <summary>
/// Private variable to hold an ASCOM AstroUtilities object to provide the Range method
/// </summary>
private AstroUtils astroUtilities;
/// <summary>
/// Serial connection to the Arduino
/// </summary>
private Serial serialConnection;
/// <summary>
/// Variable to hold the trace logger object (creates a diagnostic log file with information that you specify)
/// </summary>
internal static TraceLogger tl;
/// <summary>
/// Initializes a new instance of the <see cref="CVTDome"/> class.
/// Must be public for COM registration.
/// </summary>
public Dome()
{
tl = new TraceLogger("", "CVTDome");
ReadProfile(); // Read device configuration from the ASCOM Profile store
tl.LogMessage("Dome", "Starting initialisation");
connectedState = false; // Initialise connected to false
utilities = new Util(); //Initialise util object
astroUtilities = new AstroUtils(); // Initialise astro utilities object
//TODO: Implement your additional construction here
serialConnection = new Serial();
tl.LogMessage("Dome", "Completed initialisation");
}
//
// PUBLIC COM INTERFACE IDomeV2 IMPLEMENTATION
//
#region Common properties and methods.
/// <summary>
/// Displays the Setup Dialog form.
/// If the user clicks the OK button to dismiss the form, then
/// the new settings are saved, otherwise the old values are reloaded.
/// THIS IS THE ONLY PLACE WHERE SHOWING USER INTERFACE IS ALLOWED!
/// </summary>
public void SetupDialog()
{
// consider only showing the setup dialog if not connected
// or call a different dialog if connected
if (IsConnected)
System.Windows.Forms.MessageBox.Show("Already connected, just press OK");
using (SetupDialogForm F = new SetupDialogForm())
{
var result = F.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
WriteProfile(); // Persist device configuration values to the ASCOM Profile store
}
}
}
public ArrayList SupportedActions
{
get
{
tl.LogMessage("SupportedActions Get", "Returning empty arraylist");
return new ArrayList();
}
}
public string Action(string actionName, string actionParameters)
{
LogMessage("", "Action {0}, parameters {1} not implemented", actionName, actionParameters);
throw new ASCOM.ActionNotImplementedException("Action " + actionName + " is not implemented by this driver");
}
public void CommandBlind(string command, bool raw)
{
CheckConnected("CommandBlind");
// Call CommandString and return as soon as it finishes
this.CommandString(command, raw);
// or
throw new ASCOM.MethodNotImplementedException("CommandBlind");
// DO NOT have both these sections! One or the other
}
public bool CommandBool(string command, bool raw)
{
CheckConnected("CommandBool");
string ret = CommandString(command, raw);
// TODO decode the return string and return true or false
// or
throw new ASCOM.MethodNotImplementedException("CommandBool");
// DO NOT have both these sections! One or the other
}
public string CommandString(string command, bool raw)
{
CheckConnected("CommandString");
// it's a good idea to put all the low level communication with the device here,
// then all communication calls this function
// you need something to ensure that only one command is in progress at a time
throw new ASCOM.MethodNotImplementedException("CommandString");
}
public void Dispose()
{
// Clean up the tracelogger and util objects
tl.Enabled = false;
tl.Dispose();
tl = null;
utilities.Dispose();
utilities = null;
astroUtilities.Dispose();
astroUtilities = null;
}
public bool Connected
{
get
{
LogMessage("Connected", "Get {0}", IsConnected);
return IsConnected;
}
set
{
tl.LogMessage("Connected", "Set {0}", value);
if (value == IsConnected)
return;
if (value)
{
connectedState = true;
LogMessage("Connected Set", "Connecting to port {0}", comPort);
// TODO connect to the device
serialConnection.PortName = comPort;
serialConnection.Speed = SerialSpeed.ps9600;
serialConnection.Connected = true;
}
else
{
connectedState = false;
LogMessage("Connected Set", "Disconnecting from port {0}", comPort);
// TODO disconnect from the device
serialConnection.Connected = false;
}
}
}
public string Description
{
// TODO customise this device description
get
{
tl.LogMessage("Description Get", driverDescription);
return driverDescription;
}
}
public string DriverInfo
{
get
{
Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
// TODO customise this driver description
string driverInfo = "Information about the driver itself. Version: " + String.Format(CultureInfo.InvariantCulture, "{0}.{1}", version.Major, version.Minor);
tl.LogMessage("DriverInfo Get", driverInfo);
return driverInfo;
}
}
public string DriverVersion
{
get
{
Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
string driverVersion = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", version.Major, version.Minor);
tl.LogMessage("DriverVersion Get", driverVersion);
return driverVersion;
}
}
public short InterfaceVersion
{
// set by the driver wizard
get
{
LogMessage("InterfaceVersion Get", "2");
return Convert.ToInt16("2");
}
}
public string Name
{
get
{
string name = "Short driver name - please customise";
tl.LogMessage("Name Get", name);
return name;
}
}
#endregion
#region IDome Implementation
public void AbortSlew()
{
// This is a mandatory parameter but we have no action to take in this simple driver
tl.LogMessage("AbortSlew", "Completed");
}
public double Altitude
{
get
{
tl.LogMessage("Altitude Get", "Not implemented");
throw new ASCOM.PropertyNotImplementedException("Altitude", false);
}
}
public bool AtHome
{
get
{
tl.LogMessage("AtHome Get", "Not implemented");
throw new ASCOM.PropertyNotImplementedException("AtHome", false);
}
}
public bool AtPark
{
get
{
tl.LogMessage("AtPark Get", "Not implemented");
throw new ASCOM.PropertyNotImplementedException("AtPark", false);
}
}
public double Azimuth
{
get
{
tl.LogMessage("Azimuth Get", "Retrieving Azimuth...");
double azimuth = 0.0;
// Send the command to the arduino that will tell it to reply with the position
serialConnection.Transmit("+G;");
// Wait for the response ending in ';' and starting with '#' to know that we have a valid response
string response = serialConnection.ReceiveTerminated(";");
if(response[0] == '#')
{
azimuth = Convert.ToDouble(response.Substring(1, response.Length - 2));
}
return azimuth;
}
}
public bool CanFindHome
{
get
{
tl.LogMessage("CanFindHome Get", true.ToString());
return true;
}
}
public bool CanPark
{
get
{
tl.LogMessage("CanPark Get", true.ToString());
return true;
}
}
public bool CanSetAltitude
{
get
{
tl.LogMessage("CanSetAltitude Get", false.ToString());
return false;
}
}
public bool CanSetAzimuth
{
get
{
tl.LogMessage("CanSetAzimuth Get", true.ToString());
return true;
}
}
public bool CanSetPark
{
get
{
tl.LogMessage("CanSetPark Get", false.ToString());
return false;
}
}
public bool CanSetShutter
{
get
{
tl.LogMessage("CanSetShutter Get", false.ToString());
return false;
}
}
public bool CanSlave
{
get
{
tl.LogMessage("CanSlave Get", false.ToString());
return false;
}
}
public bool CanSyncAzimuth
{
get
{
tl.LogMessage("CanSyncAzimuth Get", false.ToString());
return false;
}
}
public void CloseShutter()
{
tl.LogMessage("CloseShutter", "Not implemented");
throw new ASCOM.MethodNotImplementedException("CloseShutter");
}
public void FindHome()
{
tl.LogMessage("FindHome", "Not implemented");
throw new ASCOM.MethodNotImplementedException("FindHome");
}
public void OpenShutter()
{
tl.LogMessage("OpenShutter", "Not Implemented");
throw new ASCOM.MethodNotImplementedException("OpenShutter");
}
public void Park()
{
tl.LogMessage("Park", "Park - Finding Home");
FindHome();
}
public void SetPark()
{
tl.LogMessage("SetPark", "Not implemented");
throw new ASCOM.MethodNotImplementedException("SetPark");
}
public ShutterState ShutterStatus
{
get
{
tl.LogMessage("ShutterStatus Get", false.ToString());
return ShutterState.shutterError;
}
}
public bool Slaved
{
get
{
tl.LogMessage("Slaved Get", false.ToString());
return false;
}
set
{
tl.LogMessage("Slaved Set", "not implemented");
throw new ASCOM.PropertyNotImplementedException("Slaved", true);
}
}
public void SlewToAltitude(double Altitude)
{
tl.LogMessage("SlewToAltitude", "Not implemented");
throw new ASCOM.MethodNotImplementedException("SlewToAltitude");
}
public void SlewToAzimuth(double Azimuth)
{
tl.LogMessage("SlewToAzimuth", "Slewing to Position...");
serialConnection.Transmit("+MA" + Convert.ToInt32(Azimuth) + ";");
}
public bool Slewing
{
get
{
tl.LogMessage("Slewing Get", false.ToString());
return false;
}
}
public void SyncToAzimuth(double Azimuth)
{
tl.LogMessage("SyncToAzimuth", "Not implemented");
throw new ASCOM.MethodNotImplementedException("SyncToAzimuth");
}
#endregion
#region Private properties and methods
// here are some useful properties and methods that can be used as required
// to help with driver development
#region ASCOM Registration
// Register or unregister driver for ASCOM. This is harmless if already
// registered or unregistered.
//
/// <summary>
/// Register or unregister the driver with the ASCOM Platform.
/// This is harmless if the driver is already registered/unregistered.
/// </summary>
/// <param name="bRegister">If <c>true</c>, registers the driver, otherwise unregisters it.</param>
private static void RegUnregASCOM(bool bRegister)
{
using (var P = new ASCOM.Utilities.Profile())
{
P.DeviceType = "Dome";
if (bRegister)
{
P.Register(driverID, driverDescription);
}
else
{
P.Unregister(driverID);
}
}
}
/// <summary>
/// This function registers the driver with the ASCOM Chooser and
/// is called automatically whenever this class is registered for COM Interop.
/// </summary>
/// <param name="t">Type of the class being registered, not used.</param>
/// <remarks>
/// This method typically runs in two distinct situations:
/// <list type="numbered">
/// <item>
/// In Visual Studio, when the project is successfully built.
/// For this to work correctly, the option <c>Register for COM Interop</c>
/// must be enabled in the project settings.
/// </item>
/// <item>During setup, when the installer registers the assembly for COM Interop.</item>
/// </list>
/// This technique should mean that it is never necessary to manually register a driver with ASCOM.
/// </remarks>
[ComRegisterFunction]
public static void RegisterASCOM(Type t)
{
RegUnregASCOM(true);
}
/// <summary>
/// This function unregisters the driver from the ASCOM Chooser and
/// is called automatically whenever this class is unregistered from COM Interop.
/// </summary>
/// <param name="t">Type of the class being registered, not used.</param>
/// <remarks>
/// This method typically runs in two distinct situations:
/// <list type="numbered">
/// <item>
/// In Visual Studio, when the project is cleaned or prior to rebuilding.
/// For this to work correctly, the option <c>Register for COM Interop</c>
/// must be enabled in the project settings.
/// </item>
/// <item>During uninstall, when the installer unregisters the assembly from COM Interop.</item>
/// </list>
/// This technique should mean that it is never necessary to manually unregister a driver from ASCOM.
/// </remarks>
[ComUnregisterFunction]
public static void UnregisterASCOM(Type t)
{
RegUnregASCOM(false);
}
#endregion
/// <summary>
/// Returns true if there is a valid connection to the driver hardware
/// </summary>
private bool IsConnected
{
get
{
// TODO check that the driver hardware connection exists and is connected to the hardware
return connectedState;
}
}
/// <summary>
/// Use this function to throw an exception if we aren't connected to the hardware
/// </summary>
/// <param name="message"></param>
private void CheckConnected(string message)
{
if (!IsConnected)
{
throw new ASCOM.NotConnectedException(message);
}
}
/// <summary>
/// Read the device configuration from the ASCOM Profile store
/// </summary>
internal void ReadProfile()
{
using (Profile driverProfile = new Profile())
{
driverProfile.DeviceType = "Dome";
tl.Enabled = Convert.ToBoolean(driverProfile.GetValue(driverID, traceStateProfileName, string.Empty, traceStateDefault));
comPort = driverProfile.GetValue(driverID, comPortProfileName, string.Empty, comPortDefault);
}
}
/// <summary>
/// Write the device configuration to the ASCOM Profile store
/// </summary>
internal void WriteProfile()
{
using (Profile driverProfile = new Profile())
{
driverProfile.DeviceType = "Dome";
driverProfile.WriteValue(driverID, traceStateProfileName, tl.Enabled.ToString());
driverProfile.WriteValue(driverID, comPortProfileName, comPort.ToString());
}
}
/// <summary>
/// Log helper function that takes formatted strings and arguments
/// </summary>
/// <param name="identifier"></param>
/// <param name="message"></param>
/// <param name="args"></param>
internal static void LogMessage(string identifier, string message, params object[] args)
{
var msg = string.Format(message, args);
tl.LogMessage(identifier, msg);
}
#endregion
}
}