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
|
/* DPST component file
* Code version: 2.1
* Version description: Fixes minor bugs in v2.0
* Version steability:
* GUI -> Stable
* Functionality -> Bugs found.
* Bugs:
* 1. Voltages dont load properly during simulation when the "Switch" component controls input pin of the DPST component.
*
*/
///Includes
#include <wincodec.h>
#include <stdio.h>
//#include <string.h>
#include <commctrl.h>
#include <Windowsx.h>
#include "componentstructs.h"
#include "componentfunctions.h"
#include "componentimages.h"
#include "components.h"
///Window handles
static HWND State1;
static HWND State2;
static HWND ModeLatchedDPST;
static HWND ModeTempDPST;
HWND* SettingsDialogDPST;
///Global variables
enum DPST_Pins { in1 = 0, in2, out1, out2 };
///Function definitions
int InitDpst(void * ComponentAddress)
{
DpstStruct* d = (DpstStruct*)ComponentAddress;
d->image = DPST_1;
d->NO = FALSE;
d->latched = TRUE;
d->Volt[in1] = V_OPEN;
d->Volt[in2] = V_OPEN;
d->Volt[out1] = V_OPEN;
d->Volt[out2] = V_OPEN;
return DPST_1;
}
void SetDpstIds(int* id, void* ComponentAddress)
{
DpstStruct* d = (DpstStruct*)ComponentAddress;
d->PinId[in1] = *id++;
d->PinId[in2] = *id++;
d->PinId[out1] = *id++;
d->PinId[out2] = *id++;
}
void MakeSettingsDialogDPST()
{
///Switch action mode
HWND InitLatched = CreateWindowEx(0, WC_BUTTON, ("Action mode"),
WS_CHILD | BS_GROUPBOX | WS_VISIBLE | WS_TABSTOP,
7, 3, 120, 65, *SettingsDialogDPST, NULL, NULL, NULL);
FontNice(InitLatched);
ModeLatchedDPST = CreateWindowEx(0, WC_BUTTON, ("Latched"),
WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE | WS_GROUP,
16, 21, 100, 20, *SettingsDialogDPST, NULL, NULL, NULL);
FontNice(ModeLatchedDPST);
ModeTempDPST = CreateWindowEx(0, WC_BUTTON, ("Temporary"),
WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE,
16, 41, 100, 20, *SettingsDialogDPST, NULL, NULL, NULL);
FontNice(ModeTempDPST);
///Switch initial status
HWND InitOut = CreateWindowEx(0, WC_BUTTON, ("Initial output state"),
WS_CHILD | BS_GROUPBOX | WS_VISIBLE | WS_TABSTOP,
140, 3, 120, 65, *SettingsDialogDPST, NULL, NULL, NULL);
FontNice(InitOut);
State1 = CreateWindowEx(0, WC_BUTTON, ("State 1"),
WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE | WS_GROUP,
149, 21, 100, 20, *SettingsDialogDPST, NULL, NULL, NULL);
FontNice(State1);
State2 = CreateWindowEx(0, WC_BUTTON, ("State 2"),
WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE,
149, 41, 100, 20, *SettingsDialogDPST, NULL, NULL, NULL);
FontNice(State2);
}
void LoadSettings(DpstStruct* d)
{
if (d->latched)
Button_SetCheck(ModeLatchedDPST, BST_CHECKED);
else
Button_SetCheck(ModeTempDPST, BST_CHECKED);
if (!d->NO)
Button_SetCheck(State1, BST_CHECKED);
else
Button_SetCheck(State2, BST_CHECKED);
}
BOOL SaveSettings(DpstStruct* d, void* ImageLocation)
{
if (Button_GetState(ModeLatchedDPST) == BST_CHECKED)
d->latched = TRUE;
else if (Button_GetState(ModeTempDPST) == BST_CHECKED)
d->latched = FALSE;
else
{
MessageBox(*SettingsDialogDPST,
("Incomplete"), ("Warning"), MB_OK | MB_ICONWARNING);
return FALSE;
}
if (Button_GetState(State1) == BST_CHECKED)
d->NO = FALSE;
else if (Button_GetState(State2) == BST_CHECKED)
d->NO = TRUE;
else
{
MessageBox(*SettingsDialogDPST,
("Incomplete"), ("Warning"), MB_OK | MB_ICONWARNING);
return FALSE;
}
if (!d->NO)
d->image = DPST_1;
else
d->image = DPST_2;
SetImage(d->image, ImageLocation);
RefreshImages();
return TRUE;
}
void DpstSettingsDialog(void* ComponentAddress, void* ImageLocation)
{
DpstStruct* d = (DpstStruct*)ComponentAddress;
BOOL exitStatus;
//Create dialog window instance
SettingsDialogDPST = CreateDialogWindow("SPDT Settings Dialog", 100, 100, 263, 145, STYLE_VERTICAL);
//Make the settings dialog
MakeSettingsDialogDPST();
//Load settings
LoadSettings(d);
//Show dialog window
ShowDialogWindow();
exitStatus = ProcessDialogWindow();
while (exitStatus == FALSE)
{
exitStatus = SaveSettings(d, ImageLocation);
if (exitStatus == TRUE)
break;
else
{
exitStatus = TRUE;
exitStatus = ProcessDialogWindow();
}
}
DestroyWindow(*SettingsDialogDPST);
}
//Dynamically check and equalise the voltage on all pins that are connected to DPST at runtime
double EqualiseRuntimeVoltageDPST(void* ComponentAdderss, int index, double volt)
{
DpstStruct* d = (DpstStruct*)ComponentAdderss;
///Check if the switch is open
if (d->NO)
{
///If switch is open then all terminals will be floating/open
switch (index)
{
case in1:
d->Volt[in1] = V_OPEN;
d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, V_OPEN);
d->Volt[out1] = VoltChange(d->PinId[out1], out1, ComponentAdderss, V_OPEN);
d->Volt[out2] = VoltChange(d->PinId[out2], out2, ComponentAdderss, V_OPEN);
break;
case in2:
d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, V_OPEN);
d->Volt[in2] = V_OPEN;
d->Volt[out1] = VoltChange(d->PinId[out1], out1, ComponentAdderss, V_OPEN);
d->Volt[out2] = VoltChange(d->PinId[out2], out2, ComponentAdderss, V_OPEN);
case out1:
d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, V_OPEN);
d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, V_OPEN);
d->Volt[out1] = V_OPEN;
d->Volt[out2] = VoltChange(d->PinId[out2], out2, ComponentAdderss, V_OPEN);
break;
case out2:
d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, V_OPEN);
d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, V_OPEN);
d->Volt[out1] = VoltChange(d->PinId[out1], out1, ComponentAdderss, V_OPEN);
d->Volt[out2] = V_OPEN;
break;
default:
d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, V_OPEN);
d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, V_OPEN);
d->Volt[out1] = VoltChange(d->PinId[out1], out1, ComponentAdderss, V_OPEN);
d->Volt[out2] = VoltChange(d->PinId[out2], out2, ComponentAdderss, V_OPEN);
break;
}
}
///If the switch is connected
else
{
double Vin;
double Vout;
///Get voltages at the connected pins
if (index == in1)
{
Vin = volt;
Vout = VoltRequest(d->PinId[out1], ComponentAdderss);
}
else if (index == out1)
{
Vin = VoltRequest(d->PinId[in1], ComponentAdderss);
Vout = volt;
}
else
{
Vin = VoltRequest(d->PinId[in1], ComponentAdderss);
Vout = VoltRequest(d->PinId[out1], ComponentAdderss);
}
///Equalise voltage for input 1 output 1 pair
///If either pin is grounded then all pins are set to GND (Static event)
if (Vin == GND || Vout == GND)
{
d->Volt[out1] = VoltChange(d->PinId[out1], out1, ComponentAdderss, GND);
d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, GND);
}
///If Vin is set as open
else if (Vin == V_OPEN)
d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, Vout);
///If Vout is set as open
else if (Vout == V_OPEN)
d->Volt[out1] = VoltChange(d->PinId[out1], out1, ComponentAdderss, Vin);
///If no pin is grounded then all pins are set to the max voltage of the pins (Dynamic event)
else
{
d->Volt[out1] = VoltChange(d->PinId[out1], out1, ComponentAdderss, max(Vin, Vout));
d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, max(Vin, Vout));
}
///Get voltages at the connected pins
if (index == in2)
{
Vin = volt;
Vout = VoltRequest(d->PinId[out2], ComponentAdderss);
}
else if (index == out2)
{
Vin = VoltRequest(d->PinId[in2], ComponentAdderss);
Vout = volt;
}
else
{
Vin = VoltRequest(d->PinId[in2], ComponentAdderss);
Vout = VoltRequest(d->PinId[out2], ComponentAdderss);
}
///Equalise voltage for input 2 output 2 pair
///If either pin is grounded then all pins are set to GND (Static event)
if (Vin == GND || Vout == GND)
{
d->Volt[out2] = VoltChange(d->PinId[out2], out2, ComponentAdderss, GND);
d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, GND);
}
///If Vin is set as open
else if (Vin == V_OPEN)
d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, Vout);
///If Vout is set as open
else if (Vout == V_OPEN)
d->Volt[out2] = VoltChange(d->PinId[out2], out2, ComponentAdderss, Vin);
///If no pin is grounded then all pins are set to the max voltage of the pins (Dynamic event)
else
{
d->Volt[out2] = VoltChange(d->PinId[out2], out2, ComponentAdderss, max(Vin, Vout));
d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, max(Vin, Vout));
}
}
return d->Volt[index];
}
//Perform a static check and equalise the voltage on all pins that are connected to DPST at runtime
void EqualiseStaticVoltageDPST(void* ComponentAdderss)
{
DpstStruct* d = (DpstStruct*)ComponentAdderss;
///Check if the switch is open
if (d->NO)
{
///If switch is open then all terminals will be floating/open
d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, V_OPEN);
d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, V_OPEN);
d->Volt[out1] = VoltChange(d->PinId[out1], out1, ComponentAdderss, V_OPEN);
d->Volt[out2] = VoltChange(d->PinId[out2], out2, ComponentAdderss, V_OPEN);
}
///If the switch is connected
else
{
double Vin = VoltRequest(d->PinId[in1], ComponentAdderss);
double Vout = VoltRequest(d->PinId[out1], ComponentAdderss);
///Equalise voltage for input 1 output 1 pair
///If either pin is grounded then all pins are set to GND (Static event)
if (Vin == GND || Vout == GND)
{
d->Volt[out1] = VoltChange(d->PinId[out1], out1, ComponentAdderss, GND);
d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, GND);
}
///If Vin is set as open
else if (Vin == V_OPEN)
d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, Vout);
///If Vout is set as open
else if (Vout == V_OPEN)
d->Volt[out1] = VoltChange(d->PinId[out1], out1, ComponentAdderss, Vin);
///If no pin is grounded then all pins are set to the max voltage of the pins (Dynamic event)
else
{
d->Volt[out1] = VoltChange(d->PinId[out1], out1, ComponentAdderss, max(Vin, Vout));
d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, max(Vin, Vout));
}
Vin = VoltRequest(d->PinId[in2], ComponentAdderss);
Vout = VoltRequest(d->PinId[out2], ComponentAdderss);
///Equalise voltage for input 2 output 2 pair
///If either pin is grounded then all pins are set to GND (Static event)
if (Vin == GND || Vout == GND)
{
d->Volt[out2] = VoltChange(d->PinId[out2], out2, ComponentAdderss, GND);
d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, GND);
}
///If Vin is set as open
else if (Vin == V_OPEN)
d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, Vout);
///If Vout is set as open
else if (Vout == V_OPEN)
d->Volt[out2] = VoltChange(d->PinId[out2], out2, ComponentAdderss, Vin);
///If no pin is grounded then all pins are set to the max voltage of the pins (Dynamic event)
else
{
d->Volt[out2] = VoltChange(d->PinId[out2], out2, ComponentAdderss, max(Vin, Vout));
d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, max(Vin, Vout));
}
}
}
void ToggleState(DpstStruct* d, void* ImageLocation)
{
d->image = (d->image == DPST_1) ? DPST_2 : DPST_1;
d->NO = (d->NO == TRUE) ? FALSE : TRUE;
SetImage(d->image, ImageLocation);
RefreshImages();
}
void HandleDpstEvent(void * ComponentAddress, int Event, BOOL SimulationStarted, void * ImageLocation, UINT ImageId, HWND * h)
{
DpstStruct* d = (DpstStruct*)ComponentAddress;
if (SimulationStarted)
{
switch (Event)
{
case EVENT_MOUSE_DOWN:
ToggleState(d, ImageLocation);
EqualiseStaticVoltageDPST(ComponentAddress);
break;
case EVENT_MOUSE_UP:
if (!d->latched)
{
ToggleState(d, ImageLocation);
EqualiseStaticVoltageDPST(ComponentAddress);
}
break;
default:
break;
}
}
else
{
switch (Event)
{
case EVENT_MOUSE_DBLCLICK:
DpstSettingsDialog(ComponentAddress, ImageLocation);
break;
default:
break;
}
}
}
double DpstVoltChanged(void * ComponentAddress, BOOL SimulationStarted, int index, double Volt, int Source, void * ImageLocation)
{
if (SimulationStarted)
return EqualiseRuntimeVoltageDPST(ComponentAddress, index, Volt);
return Volt;
}
|