-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMainWindow.cs
271 lines (223 loc) · 8.24 KB
/
MainWindow.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
namespace ASCOM.EQFocuser
{
public partial class MainWindow : Form
{
ASCOM.EQFocuser.Focuser focuser;
public MainWindow(Focuser focuser)
{
this.focuser = focuser;
this.focuser.FocuserValueChanged += FocuserValueChanged;
this.focuser.FocuserStateChanged += FocuserStateChanged;
this.focuser.FocuserHumidityChanged += FocuserHumidityChanged;
this.focuser.FocuserTemperatureChanged += FocuserTemperatureChanged;
this.focuser.FocuserMotorChanged += FocuserMotorChanged;
InitializeComponent();
InitControls();
this.FormBorderStyle = FormBorderStyle.FixedSingle;
}
delegate void SetCurrentPositionCallBack(int position);
delegate void SetCurrentStateCallBack(bool isMoving);
delegate void SetCurrentTemperatureCallBack(string temperature);
delegate void SetCurrentHumidityCallBack(string humidity);
delegate void SetCurrentMotorCallBack(int motorNumber);
private void SetCurrentTemperature(string temperature)
{
txtBoxTemperature.InvokeIfRequired(txtBoxTemperature => { txtBoxTemperature.Text = temperature; });
}
private void SetCurrentHumidity(string humidity)
{
txtBoxHumidity.InvokeIfRequired(txtBoxHumidity => { txtBoxHumidity.Text = humidity; });
}
private void SetCurrentPosition(int position)
{
textBoxCurrentPosition.InvokeIfRequired(textBoxCurrentPosition => { textBoxCurrentPosition.Text = position.ToString(); });
}
private void FocuserValueChanged(object sender, FocuserValueChangedEventArgs e)
{
//this.textBoxCurrentPosition.Text = e.NewValue.ToString();
SetCurrentPosition(e.NewValue);
}
private void SetCurrentState(bool isMoving)
{
if (lblAction.InvokeRequired)
{
SetCurrentStateCallBack d = new SetCurrentStateCallBack(SetCurrentState);
this.Invoke(d, new Object[] { isMoving });
}
else
{
if (isMoving)
{
lblAction.Text = "MOVING...";
}
else
{
lblAction.Text = "READY...";
}
}
}
private void SetCurrentMotor(int motorNumber)
{
if (checkBox1.InvokeRequired || checkBox2.InvokeRequired)
{
SetCurrentMotorCallBack d = new SetCurrentMotorCallBack(SetCurrentMotor);
this.Invoke(d, new Object[] { motorNumber });
}
else
{
if (motorNumber == 0)
{
checkBox1.Checked = true;
checkBox2.Checked = false;
}
if (motorNumber == 1)
{
checkBox1.Checked = false;
checkBox2.Checked = true;
}
}
}
private void FocuserHumidityChanged(object sender, FocuserHumidityChangedEventArgs e)
{
SetCurrentHumidity(e.Humidity);
}
private void FocuserTemperatureChanged(object sender, FocuserTemperatureChangedEventArgs e)
{
SetCurrentTemperature(e.Temperature);
}
private void FocuserStateChanged(object sender, FocuserStateChangedEventArgs e)
{
SetCurrentState(e.IsMoving);
}
private void FocuserMotorChanged(object sender, FocuserMotorChangedEventArgs e)
{
SetCurrentMotor(e.Motor);
}
private void btnFastReverse_Click(object sender, EventArgs e)
{
focuser.CommandString("A", true);
}
private void InitControls()
{
textBoxCurrentPosition.Text = focuser.Position.ToString();
checkBox1.Checked = true;
}
private void btnReverse_Click(object sender, EventArgs e)
{
focuser.CommandString("B", true);
}
private void btnForward_Click(object sender, EventArgs e)
{
focuser.CommandString("C", true);
}
private void btnFastForward_Click(object sender, EventArgs e)
{
focuser.CommandString("D", true);
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
focuser.StepSize = Convert.ToDouble(numericUpDown1.Value);
}
private void btnMoveTo_Click(object sender, EventArgs e)
{
focuser.Move(Convert.ToInt16(textBoxMoveToPosition.Text));
}
private void btnReset_Click(object sender, EventArgs e)
{
focuser.CommandString("G", true);
SetCurrentPosition(0);
}
private void textBoxCurrentPosition_TextChanged(object sender, EventArgs e)
{
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
// acceleration
focuser.Action("H", numericUpDown2.Value.ToString());
}
private void numericUpDown3_ValueChanged(object sender, EventArgs e)
{
// max speed
focuser.Action("J", numericUpDown3.Value.ToString());
}
private void numericUpDown4_ValueChanged(object sender, EventArgs e)
{
// speed
focuser.Action("I", numericUpDown4.Value.ToString());
}
private void timer1_Tick(object sender, EventArgs e)
{
// do not disrupt the motor when it is moving
if (!focuser.IsMoving) {
System.Diagnostics.Debug.WriteLine("Getting Temperature");
focuser.Action("k", "");
}
}
private void btnStop_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Stopping the motor");
focuser.Halt();
}
private void MainWindow_Load(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Showing Main Window");
System.Diagnostics.Debug.WriteLine("Height " + this.Height);
Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
this.Text = "EQFocuser v" + String.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}", version.Major, version.Minor, version.Revision);
}
private void MainWindow_FormClosed(object sender, FormClosedEventArgs e)
{
//focuser.SerialPort.Close();
}
private void numericUpDownBacklash_ValueChanged(object sender, EventArgs e)
{
focuser.Action("L", numericUpDownBacklash.Value.ToString());
}
private void btnShowAdvanced_Click(object sender, EventArgs e)
{
//panel1.Visible = !panel1.Visible;
//if (panel1.Visible)
//{
// this.Height = 665;
//}
//else
//{
// this.Height = 376;
//}
txtBoxTemperature.Text = this.Height.ToString();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox1_Click(object sender, EventArgs e)
{
checkBox2.Checked = !checkBox1.Checked;
focuser.Action("M", "0");
}
private void checkBox2_Click(object sender, EventArgs e)
{
checkBox1.Checked = !checkBox2.Checked;
focuser.Action("M", "1");
}
private void lblAction_MouseClick(object sender, MouseEventArgs e)
{
System.Diagnostics.Debug.WriteLine(this.Height.ToString());
}
private void checkBoxReverse_CheckedChanged(object sender, EventArgs e)
{
focuser.CommandBool("Y", checkBoxReverse.Checked);
}
}
}