From 097f7e6a04b4870db3104243d6badb7356e57fb8 Mon Sep 17 00:00:00 2001 From: NatsuDrag9 Date: Wed, 4 Apr 2018 22:13:39 +0530 Subject: [PATCH 1/5] Added SPDT switch with complete funcitonality. UI for DSPT has been added. --- ldmicro/Makefile | 4 +- ldmicro/components/DPDT.cpp | 142 +++++++ ldmicro/components/DPST.cpp | 252 ++++++++++++ ldmicro/components/SPDT.cpp | 467 +++++++++++++++++++++ ldmicro/components/SPDT_Test.cpp | 524 ++++++++++++++++++++++++ ldmicro/components/componentfunctions.h | 18 + ldmicro/components/componentimages.h | 32 +- ldmicro/components/components.cpp | 46 +++ ldmicro/components/componentstructs.h | 38 ++ ldmicro/ldmicro.rc | 16 +- 10 files changed, 1522 insertions(+), 17 deletions(-) create mode 100644 ldmicro/components/DPDT.cpp create mode 100644 ldmicro/components/DPST.cpp create mode 100644 ldmicro/components/SPDT.cpp create mode 100644 ldmicro/components/SPDT_Test.cpp diff --git a/ldmicro/Makefile b/ldmicro/Makefile index 03d533b..82edc2c 100644 --- a/ldmicro/Makefile +++ b/ldmicro/Makefile @@ -44,7 +44,9 @@ LDOBJS = $(OBJDIR)\ldmicro.obj \ COMPOBJS = $(OBJDIR)\components.obj \ $(OBJDIR)\switch.obj \ - $(OBJDIR)\relay.obj + $(OBJDIR)\relay.obj \ + $(OBJDIR)\SPDT.obj \ + $(OBJDIR)\DPST.obj HELPOBJ = $(OBJDIR)\helptext.obj diff --git a/ldmicro/components/DPDT.cpp b/ldmicro/components/DPDT.cpp new file mode 100644 index 0000000..10a701c --- /dev/null +++ b/ldmicro/components/DPDT.cpp @@ -0,0 +1,142 @@ +#include +#include +//#include +#include +#include + +#include "componentstructs.h" +//#include "componentfunctions.h" +#include "componentimages.h" +#include "components.h" + +static HWND TemporaryRadio_dpdt; +static HWND LatchedRadio_dpdt; +static HWND ClosedRadio_dpdt; +static HWND OpenRadio_dpdt; +static HWND NameTextbox_dpdt; +static HWND InNameTextbox_dpdt; +static HWND OutNameTextbox_dpdt; +HWND* DPDTDialog; + +void DPDTStateChanged(SPDTStruct* SwitchData, void* ImageLocation); +void MakeSwitchControls() +{ + HWND ActionGrouper = CreateWindowEx(0, WC_BUTTON, ("Action"), + WS_CHILD | BS_GROUPBOX | WS_VISIBLE | WS_TABSTOP, + 7, 3, 120, 65, *DPDTDialog, NULL, NULL, NULL); + FontNice(ActionGrouper); + + LatchedRadio_dpdt = CreateWindowEx(0, WC_BUTTON, ("Latched"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE | WS_GROUP, + 16, 21, 100, 20, *DPDTDialog, NULL, NULL, NULL); + FontNice(LatchedRadio_dpdt); + + TemporaryRadio_dpdt = CreateWindowEx(0, WC_BUTTON, ("Temporary"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE, + 16, 41, 100, 20, *DPDTDialog, NULL, NULL, NULL); + FontNice(TemporaryRadio_dpdt); + + // SetOnlyRadio = CreateWindowEx(0, WC_BUTTON, _("(S) Set-Only"), + // WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE, + // 16, 61, 100, 20, CoilDialog, NULL, Instance, NULL); + // NiceFont(SetOnlyRadio); + + // ResetOnlyRadio = CreateWindowEx(0, WC_BUTTON, _("(R) Reset-Only"), + // WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE, + // 16, 81, 105, 20, CoilDialog, NULL, Instance, NULL); + // NiceFont(ResetOnlyRadio); + + HWND PositionGrouper = CreateWindowEx(0, WC_BUTTON, ("Position"), + WS_CHILD | BS_GROUPBOX | WS_VISIBLE, + 140, 3, 120, 65, *DPDTDialog, NULL, NULL, NULL); + FontNice(PositionGrouper); + + OpenRadio_dpdt = CreateWindowEx(0, WC_BUTTON, ("Open"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE | WS_GROUP | WS_TABSTOP, + 149, 21, 100, 20, *DPDTDialog, NULL, NULL, NULL); + FontNice(OpenRadio_dpdt); + + ClosedRadio_dpdt = CreateWindowEx(0, WC_BUTTON, ("Closed"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE | WS_TABSTOP, + 149, 41, 100, 20, *DPDTDialog, NULL, NULL, NULL); + FontNice(ClosedRadio_dpdt); + + HWND textLabel = CreateWindowEx(0, WC_STATIC, ("Name:"), + WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE | SS_RIGHT, + 100, 80, 50, 21, *DPDTDialog, NULL, NULL, NULL); + FontNice(textLabel); + + NameTextbox_dpdt = CreateWindowEx(WS_EX_CLIENTEDGE, WC_EDIT, "", + WS_CHILD | ES_AUTOHSCROLL | WS_TABSTOP | WS_CLIPSIBLINGS | WS_VISIBLE, + 155, 80, 155, 21, *DPDTDialog, NULL, NULL, NULL); + FontFixed(NameTextbox_dpdt); + + /*PrevNameProc = SetWindowLongPtr(NameTextbox, GWLP_WNDPROC, + (LONG_PTR)MyNameProc);*/ +} + +void DPDTLoadState(DPDTStruct* Data) +{ + if (Data->latch_1) + { + Button_SetCheck(LatchedRadio_dpdt, BST_CHECKED); + } + else + { + Button_SetCheck(TemporaryRadio_dpdt, BST_CHECKED); + } + if (Data->latch_2) + { + Button_SetCheck(LatchedRadio_dpdt, BST_CHECKED); + } + else + { + Button_SetCheck(TemporaryRadio_dpdt, BST_CHECKED); + } + if (Data->latch_3) + { + Button_SetCheck(LatchedRadio_dpdt, BST_CHECKED); + } + else + { + Button_SetCheck(TemporaryRadio_dpdt, BST_CHECKED); + } + if (Data->latch_3) + { + Button_SetCheck(LatchedRadio_dpdt, BST_CHECKED); + } + else + { + Button_SetCheck(TemporaryRadio_dpdt, BST_CHECKED); + } + if (Data->init_pos) + { + Button_SetCheck(OpenRadio_dpdt, BST_CHECKED); + } + else + { + Button_SetCheck(ClosedRadio_dpdt, BST_CHECKED); + } + //Edit_SetText(NameTextbox_spdt, Data->Name); +} + +void SetDPDTIds(int* id, void* ComponentAddress) +{ + DPDTStruct *s = (DPDTStruct*)ComponentAddress; + s->pinId[0] = *id++; + s->pinId[1] = *id++; + s->pinId[2] = *id++; + s->pinId[3] = *id++; + s->pinId[4] = *id++; + s->pinId[5] = *id; + /*char Debug[256]; + sprintf_s(Debug, "SetSwitchIds: \tAddr1: %p \tAddr2: %p \tPin0:%d \t Pin1:%d \tVolt0: %f Volt1: %f\n", + ComponentAddress, s, s->PinId[0], s->PinId[1],VoltRequest(s->PinId[0], ComponentAddress), + VoltRequest(s->PinId[0], ComponentAddress)); + OutputDebugString(Debug); + char vx[4],vy[4]; + _itoa(s->PinId[0],vy,10); + _itoa(s->PinId[1],vx,10); + MessageBox(NULL, + (vy), (vx), MB_OK | MB_ICONWARNING);*/ +} \ No newline at end of file diff --git a/ldmicro/components/DPST.cpp b/ldmicro/components/DPST.cpp new file mode 100644 index 0000000..9df7d44 --- /dev/null +++ b/ldmicro/components/DPST.cpp @@ -0,0 +1,252 @@ +#include +#include +//#include +#include +#include + +#include "componentstructs.h" +#include "componentfunctions.h" +#include "componentimages.h" +#include "components.h" + +//static HWND TemporaryRadio; +static HWND LatchedRadio_dpst; +static HWND ClosedRadio_dpst; +//static HWND ClosedRadio_2_dpst; +static HWND OpenRadio_dpst; +//static HWND NameTextbox_dpst; +//static HWND InNameTextbox_dpst; +static HWND OutNameTextbox_dpst; +HWND* DPSTDialog; + +void MakeDPSTControls() +{ + HWND ActionGrouper = CreateWindowEx(0, WC_BUTTON, ("Action"), + WS_CHILD | BS_GROUPBOX | WS_VISIBLE | WS_TABSTOP, + 7, 3, 120, 65, *DPSTDialog, NULL, NULL, NULL); + FontNice(ActionGrouper); + + LatchedRadio_dpst = CreateWindowEx(0, WC_BUTTON, ("Latched"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE | WS_GROUP, + 16, 21, 100, 20, *DPSTDialog, NULL, NULL, NULL); + FontNice(LatchedRadio_dpst); + + HWND PositionGrouper = CreateWindowEx(0, WC_BUTTON, ("Position"), + WS_CHILD | BS_GROUPBOX | WS_VISIBLE, + 140, 3, 120, 65, *DPSTDialog, NULL, NULL, NULL); + FontNice(PositionGrouper); + + ClosedRadio_dpst = CreateWindowEx(0, WC_BUTTON, ("Closed"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE | WS_GROUP | WS_TABSTOP, + 149, 21, 100, 20, *DPSTDialog, NULL, NULL, NULL); + FontNice(ClosedRadio_dpst); + + OpenRadio_dpst = CreateWindowEx(0, WC_BUTTON, ("Open"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE | WS_TABSTOP, + 149, 41, 100, 20, *DPSTDialog, NULL, NULL, NULL); + FontNice(OpenRadio_dpst); +} + +void DPSTLoadState(DPSTStruct* Data) +{ + if (Data->latched) + { + Button_SetCheck(LatchedRadio_dpst, BST_CHECKED); + } + if (!Data->init_pos) + { + Button_SetCheck(ClosedRadio_dpst, BST_CHECKED); + } + else + { + Button_SetCheck(OpenRadio_dpst, BST_CHECKED); + } + //Edit_SetText(NameTextbox_spdt, Data->Name); +} + +BOOL SaveDPSTDialog(DPSTStruct* Data) +{ + BOOL latched, init_pos; + if (Button_GetState(LatchedRadio_dpst) == BST_CHECKED) + { + latched = TRUE; + + } + else + { + MessageBox(*DPSTDialog, + ("Incomplete"), ("Warning"), MB_OK | MB_ICONWARNING); + return FALSE; + } + + if (Button_GetState(ClosedRadio_dpst) == BST_CHECKED) + { + init_pos = TRUE; + + } + else if (Button_GetState(OpenRadio_dpst) == BST_CHECKED) + { + init_pos = FALSE; + } + else + { + MessageBox(*DPSTDialog, + ("Incomplete"), ("Warning"), MB_OK | MB_ICONWARNING); + return FALSE; + } + Data->latched = latched; + Data->init_pos = init_pos; + //Data->Open = NOpen; + if (init_pos) + { + Data->image = DPST_switch_disconnected_1; + } + else + { + Data->image = DPST_switch_connected_1; + } + return TRUE; +} + +void ShowDPSTDialog(void* ComponentAddress) +{ + DPSTStruct* Data = (DPSTStruct*)ComponentAddress; + BOOL Canceled, Complete = TRUE; + DPSTDialog = CreateDialogWindow("DPST Dialog", 100, 100, 263, 145, STYLE_VERTICAL); + MakeDPSTControls(); + ShowDialogWindow(); + DPSTLoadState(Data); + Canceled = ProcessDialogWindow(); + while (Canceled == FALSE) + { + Complete = SaveDPSTDialog(Data); + if (Complete == TRUE) + { + // MessageBox(*SwitchDialog, + // ("Saved"), ("Mouse click"), MB_OK | MB_ICONWARNING); + break; + } + else + { + Complete = TRUE; + Canceled = ProcessDialogWindow(); + } + } + DestroyWindow(*DPSTDialog); +} + +int Init_DPST(void* ComponentAddress) +{ + DPSTStruct* temp = (DPSTStruct*)ComponentAddress; + temp->image = DPST_switch_disconnected_1; + temp->latched = TRUE; + temp->open = TRUE; + temp->init_pos = TRUE; + temp->volt[0] = V_OPEN; + temp->volt[1] = V_OPEN; + temp->volt[2] = V_OPEN; + temp->volt[3] = V_OPEN; + + return DPST_switch_disconnected_1; +} + +void HandleDPSTEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, + void* ImageLocation, UINT ImageId, HWND* h) +{ + DPSTStruct *temp = (DPSTStruct*)ComponentAddress; + + if (SimulationStarted) + { + switch (Event) { + case EVENT_MOUSE_UP: + /*if (temp->Latched) + { + temp->Open = !temp->Open; + /*MessageBox(NULL, + "Latched", "Test", MB_OK | MB_ICONWARNING); + }*/ + /*else + { + temp->Open = temp->NOpen; + // MessageBox(NULL, + // "Latched", "Test", MB_OK | MB_ICONWARNING); + }*/ + /*SwitchStateChanged(temp, ImageLocation); + UpdateValues(temp, ComponentAddress); + break;*/ + case EVENT_MOUSE_DOWN: + /*if (!temp->Latched) + { + temp->Open = !temp->NOpen; + SwitchStateChanged(temp, ImageLocation); + UpdateValues(temp, ComponentAddress); + } + */ + break; + } + + } + else + { + switch (Event) { + case EVENT_MOUSE_DBLCLICK: + ShowDPSTDialog(ComponentAddress); + SetImage(temp->image, ImageLocation); + RefreshImages(); + break; + } + } +} + +double DPSTVoltChanged(void* dpstData, BOOL SimulationStarted, int Index, + double Volt, int Source, void* ImageLocation) +{ + DPSTStruct* temp = (DPSTStruct*)dpstData; + if (SimulationStarted) + { + double v0,v1,v2,v3; + v0 = VoltRequest(temp->pinId[0], dpstData); + v1 = VoltRequest(temp->pinId[1], dpstData); + v2 = VoltRequest(temp->pinId[2], dpstData); + v3 = VoltRequest(temp->pinId[3], dpstData); + if (temp->open) + { + temp->volt[0] = VoltChange(temp->pinId[0], 0, dpstData, V_OPEN); + temp->volt[1] = VoltChange(temp->pinId[1], 1, dpstData, V_OPEN); + temp->volt[2] = VoltChange(temp->pinId[2], 2, dpstData, V_OPEN); + temp->volt[3] = VoltChange(temp->pinId[3], 3, dpstData, V_OPEN); + /*sprintf_s(Debug, "SwitchVoltChanged: \tAddress: %p \tVolt0:%f \t Volt1:%f\n", + SwitchData, Voltage, Volt); + OutputDebugString(Debug);*/ + return temp->volt[Index]; + } + if ((v0 > v1) && (v2 > v3)) + { + temp->volt[1] = VoltChange(temp->pinId[1], 1, dpstData, v0); + temp->volt[3] = VoltChange(temp->pinId[3], 3, dpstData, v2); + return temp->volt[Index]; + } + else + { + // temp->Volt[!Index] = VoltChange(temp->PinId[!Index], !Index, SwitchData, Volt); + temp->volt[0] = VoltChange(temp->pinId[0], 0, dpstData, v1); + temp->volt[2] = VoltChange(temp->pinId[2], 2, dpstData, v3); + return temp->volt[Index]; + } + } + else + { + temp->volt[Index] = Volt; + return Volt; + } + return 0; +} + +void SetDPSTIds(int* id, void* ComponentAddress) +{ + DPSTStruct *s = (DPSTStruct*)ComponentAddress; + s->pinId[0] = *id++; + s->pinId[1] = *id++; + s->pinId[2] = *id++; + s->pinId[3] = *id; +} \ No newline at end of file diff --git a/ldmicro/components/SPDT.cpp b/ldmicro/components/SPDT.cpp new file mode 100644 index 0000000..a773ae0 --- /dev/null +++ b/ldmicro/components/SPDT.cpp @@ -0,0 +1,467 @@ +#include +#include +//#include +#include +#include + +#include "componentstructs.h" +#include "componentfunctions.h" +#include "componentimages.h" +#include "components.h" + +//static HWND TemporaryRadio; +static HWND LatchedRadio_spdt; +static HWND ClosedRadio_1_spdt; +static HWND ClosedRadio_2_spdt; +//static HWND OpenRadio; +static HWND NameTextbox_spdt; +static HWND InNameTextbox_spdt; +static HWND OutNameTextbox_spdt; +HWND* SPDTDialog; + +void MakeSPDTControls() +{ + HWND ActionGrouper = CreateWindowEx(0, WC_BUTTON, ("Action"), + WS_CHILD | BS_GROUPBOX | WS_VISIBLE | WS_TABSTOP, + 7, 3, 120, 65, *SPDTDialog, NULL, NULL, NULL); + FontNice(ActionGrouper); + + LatchedRadio_spdt = CreateWindowEx(0, WC_BUTTON, ("Latched"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE | WS_GROUP, + 16, 21, 100, 20, *SPDTDialog, NULL, NULL, NULL); + FontNice(LatchedRadio_spdt); + + /*TemporaryRadio = CreateWindowEx(0, WC_BUTTON, ("Temporary"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE, + 16, 41, 100, 20, *SwitchDialog, NULL, NULL, NULL); + FontNice(TemporaryRadio);*/ + + // SetOnlyRadio = CreateWindowEx(0, WC_BUTTON, _("(S) Set-Only"), + // WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE, + // 16, 61, 100, 20, CoilDialog, NULL, Instance, NULL); + // NiceFont(SetOnlyRadio); + + // ResetOnlyRadio = CreateWindowEx(0, WC_BUTTON, _("(R) Reset-Only"), + // WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE, + // 16, 81, 105, 20, CoilDialog, NULL, Instance, NULL); + // NiceFont(ResetOnlyRadio); + + HWND PositionGrouper = CreateWindowEx(0, WC_BUTTON, ("Position"), + WS_CHILD | BS_GROUPBOX | WS_VISIBLE, + 140, 3, 120, 65, *SPDTDialog, NULL, NULL, NULL); + FontNice(PositionGrouper); + + ClosedRadio_1_spdt = CreateWindowEx(0, WC_BUTTON, ("Pin 1"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE | WS_GROUP | WS_TABSTOP, + 149, 21, 100, 20, *SPDTDialog, NULL, NULL, NULL); + FontNice(ClosedRadio_1_spdt); + + ClosedRadio_2_spdt = CreateWindowEx(0, WC_BUTTON, ("Pin 2"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE | WS_TABSTOP, + 149, 41, 100, 20, *SPDTDialog, NULL, NULL, NULL); + FontNice(ClosedRadio_2_spdt); + + /*NameTextbox_spdt = CreateWindowEx(WS_EX_CLIENTEDGE, WC_EDIT, "", + WS_CHILD | ES_AUTOHSCROLL | WS_TABSTOP | WS_CLIPSIBLINGS | WS_VISIBLE, + 155, 80, 155, 21, *SPDTDialog, NULL, NULL, NULL); + FontFixed(NameTextbox_spdt); + + /*PrevNameProc = SetWindowLongPtr(NameTextbox, GWLP_WNDPROC, + (LONG_PTR)MyNameProc);*/ +} + +void SPDTLoadState(SPDTStruct* Data) +{ + if (Data->latch_1) + { + Button_SetCheck(LatchedRadio_spdt, BST_CHECKED); + } + if (!Data->init_pos) + { + Button_SetCheck(ClosedRadio_2_spdt, BST_CHECKED); + } + else + { + Button_SetCheck(ClosedRadio_1_spdt, BST_CHECKED); + } + //Edit_SetText(NameTextbox_spdt, Data->Name); +} + +BOOL SaveSPDTDialog(SPDTStruct* Data) +{ + BOOL latch_1, init_pos; + if (Button_GetState(LatchedRadio_spdt) == BST_CHECKED) + { + latch_1 = TRUE; + + } + else + { + MessageBox(*SPDTDialog, + ("Incomplete"), ("Warning"), MB_OK | MB_ICONWARNING); + return FALSE; + } + + if (Button_GetState(ClosedRadio_1_spdt) == BST_CHECKED) + { + init_pos = TRUE; + + } + else if (Button_GetState(ClosedRadio_2_spdt) == BST_CHECKED) + { + init_pos = FALSE; + } + else + { + MessageBox(*SPDTDialog, + ("Incomplete"), ("Warning"), MB_OK | MB_ICONWARNING); + return FALSE; + } + Data->latch_1 = latch_1; + Data->init_pos = init_pos; + //Data->Open = NOpen; + if (init_pos) + { + Data->image = SPDT_switch_connected_1; + } + else + { + Data->image = SPDT_switch_connected_2; + } + return TRUE; +} + +void ShowSPDTDialog(void* ComponentAddress) +{ + SPDTStruct* Data = (SPDTStruct*)ComponentAddress; + BOOL Canceled, Complete = TRUE; + SPDTDialog = CreateDialogWindow("SPDT Dialog", 100, 100, 263, 145, STYLE_VERTICAL); + MakeSPDTControls(); + ShowDialogWindow(); + SPDTLoadState(Data); + Canceled = ProcessDialogWindow(); + while (Canceled == FALSE) + { + Complete = SaveSPDTDialog(Data); + if (Complete == TRUE) + { + // MessageBox(*SwitchDialog, + // ("Saved"), ("Mouse click"), MB_OK | MB_ICONWARNING); + break; + } + else + { + Complete = TRUE; + Canceled = ProcessDialogWindow(); + } + } + DestroyWindow(*SPDTDialog); +} + +void SPDTUpdateValues(SPDTStruct* spdt, void* ComponentAddress) +{ + SPDTStruct* temp = (SPDTStruct*)spdt; + double v0, v1, v2; + /*if (temp->latch_1 == TRUE) + { + temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, GND); + temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, V_OPEN); + } + else + { + temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, V_OPEN); + temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, GND); + }*/ + if (temp->latch_1 == TRUE) + { + v0 = VoltRequest(temp->pinId[0], ComponentAddress); + v1 = VoltRequest(temp->pinId[1], ComponentAddress); + if (v0 != GND && v1 != GND) { + if (v0 != V_OPEN && v1 != V_OPEN) { + if (v0 > v1) { + //temp->volt[Index] = Volt; + temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, v0); + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, V_OPEN); + //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); + //return temp->volt[Index]; + } + else { + //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, v1); + temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, V_OPEN); + //return (temp->volt[Index]); + } + } + else if (v1 == V_OPEN) { + temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, v0); + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, V_OPEN); + //return (temp->volt[Index]); + } + else if (v0 == V_OPEN) { + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, v1); + temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, V_OPEN); + //return (temp->volt[Index]); + } + } + else { + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, GND); + temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, GND); + temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, V_OPEN); + //return (temp->volt[Index]); + } + } + else if (temp->latch_1 == FALSE) { + v0 = VoltRequest(temp->pinId[0], ComponentAddress); + v2 = VoltRequest(temp->pinId[2], ComponentAddress); + if (v0 != GND && v2 != GND) { + if (v0 != V_OPEN && v2 != V_OPEN) { + if (v0 > v2) { + //temp->volt[Index] = Volt; + temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, v0); + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, V_OPEN); + //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); + //return temp->volt[2]; + } + else { + //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, v2); + temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, V_OPEN); + //return (temp->volt[Index]); + } + } + else if (v2 == V_OPEN) { + temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, v0); + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, V_OPEN); + //return (temp->volt[Index]); + } + else if (v0 == V_OPEN) { + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, v2); + temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, V_OPEN); + //return (temp->volt[Index]); + } + } + else { + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, GND); + temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, GND); + temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, V_OPEN); + //return (temp->volt[Index]); + } + } + + // Storing real time values in each pin + /*double v0,v1,v2; + v0 = VoltRequest(spdt->pinId[0], ComponentAddress); + spdt->volt[0] = v0; + v1 = VoltRequest(spdt->pinId[1], ComponentAddress); + spdt->volt[1] = v1; + v2 = VoltRequest(spdt->pinId[2], ComponentAddress); + spdt->volt[2] = v2; + if (spdt->latch_1 == TRUE) { + + if (spdt->volt[0] > spdt->volt[1]) + { + spdt->volt[1] = VoltChange(spdt->pinId[0], 0, + ComponentAddress, spdt->volt[0]); + } + else if (spdt->volt[1] > spdt->volt[0]) + { + spdt->volt[0] = VoltChange(spdt->pinId[1], 1, + ComponentAddress, spdt->volt[1]); + } + else if (spdt->latch_1 == FALSE) { + if (spdt->volt[0] > spdt->volt[2]) + { + spdt->volt[2] = VoltChange(spdt->pinId[0], 0, + ComponentAddress, spdt->volt[0]); + } + else if (spdt->volt[2] > spdt->volt[0]){ + spdt->volt[0] = VoltChange(spdt->pinId[2], 1, + ComponentAddress, spdt->volt[2]); + } + } + }*/ +} + +void HandleSPDTEvent(void * ComponentAddress, int Event, BOOL SimulationStarted, void * ImageLocation, UINT ImageId, HWND * h) +{ + SPDTStruct *temp = (SPDTStruct*)ComponentAddress; + + if (SimulationStarted) + { + switch (Event) { + case EVENT_MOUSE_CLICK: + temp->latch_1 = !temp->latch_1; + SetImage(temp->latch_1 ? SPDT_switch_connected_2 : SPDT_switch_connected_1, + ImageLocation); + RefreshImages(); + SPDTUpdateValues(temp, ComponentAddress); + break; + } + } + else + { + switch (Event) { + case EVENT_MOUSE_DBLCLICK: + ShowSPDTDialog(ComponentAddress); + SetImage(temp->image, ImageLocation); + RefreshImages(); + break; + } + } +} + +double SPDTVoltChanged(void* spdtData, BOOL SimulationStarted, int Index, + double Volt, int Source, void* ImageLocation) { + // char Debug[256]; + SPDTStruct* temp = (SPDTStruct*)spdtData; + if (SimulationStarted) + { + double v0, v1, v2; + if (temp->latch_1 == TRUE) { + v0 = VoltRequest(temp->pinId[0], spdtData); + v1 = VoltRequest(temp->pinId[1], spdtData); + if (v0 != GND && v1 != GND) { + if (v0 != V_OPEN && v1 != V_OPEN) { + if (v0 > v1) { + //temp->volt[Index] = Volt; + temp->volt[1] = VoltChange(temp->pinId[1], 1, spdtData, v0); + //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); + return temp->volt[Index]; + } + else { + //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); + temp->volt[0] = VoltChange(temp->pinId[0], 0, spdtData, v1); + return (temp->volt[Index]); + } + } + else if (v1 == V_OPEN) { + temp->volt[1] = VoltChange(temp->pinId[1], 1, spdtData, v0); + return (temp->volt[Index]); + } + else if (v0 == V_OPEN) { + temp->volt[0] = VoltChange(temp->pinId[0], 0, spdtData, v1); + return (temp->volt[Index]); + } + } + else { + temp->volt[0] = VoltChange(temp->pinId[0], 0, spdtData, GND); + temp->volt[1] = VoltChange(temp->pinId[1], 1, spdtData, GND); + return (temp->volt[Index]); + } + } + else if (temp->latch_1 == FALSE) { + v0 = VoltRequest(temp->pinId[0], spdtData); + v2 = VoltRequest(temp->pinId[2], spdtData); + if (v0 != GND && v2 != GND) { + if (v0 != V_OPEN && v2 != V_OPEN) { + if (v0 > v2) { + //temp->volt[Index] = Volt; + temp->volt[2] = VoltChange(temp->pinId[2], 2, spdtData, v0); + //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); + return temp->volt[2]; + } + else { + //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); + temp->volt[0] = VoltChange(temp->pinId[0], 0, spdtData, v2); + return (temp->volt[Index]); + } + } + else if (v2 == V_OPEN) { + temp->volt[2] = VoltChange(temp->pinId[2], 2, spdtData, v0); + return (temp->volt[Index]); + } + else if (v0 == V_OPEN) { + temp->volt[0] = VoltChange(temp->pinId[0], 0, spdtData, v2); + return (temp->volt[Index]); + } + } + else { + temp->volt[0] = VoltChange(temp->pinId[0], 0, spdtData, GND); + temp->volt[2] = VoltChange(temp->pinId[2], 2, spdtData, GND); + return (temp->volt[Index]); + } + /*temp->volt[0] = VoltChange(temp->pinId[0], 0, spdtData, GND); + temp->volt[1] = VoltChange(temp->pinId[1], 1, spdtData, GND); + return (temp->volt[0]);*/ + /*if (Index == 1) { + Voltage = VoltRequest(temp->pinId[Index-1], spdtData); + // Equalizing the voltage at pins + if (Voltage > Volt) { + //temp->volt[Index] = Volt; + temp->volt[Index] = VoltChange(temp->pinId[Index-1], Index-1, spdtData, Voltage); + //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); + return temp->volt[Index-1]; + } + else if (Voltage < Volt) { + //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); + temp->volt[Index-1] = Volt; + return (temp->volt[Index]); + } + else if (Voltage == GND || Volt == GND) { + temp->volt[Index] = VoltChange(temp->pinId[Index-1], Index-1, spdtData, GND); + temp->volt[Index-1] = VoltChange(temp->pinId[Index-1], Index-1, spdtData, GND); + } + else if (Voltage ) + } + else if (Index == 2) { + Voltage = VoltRequest(temp->pinId[Index-2], spdtData); + // Equalizing the voltage at pins + if (Voltage > Volt) { + //temp->volt[Index] = Volt; + temp->volt[Index] = VoltChange(temp->pinId[Index-2], Index-2, spdtData, Voltage); + //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); + return temp->volt[Index]; + } + else { + //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); + temp->volt[Index-2] = Volt; + return (temp->volt[Index]); + } + } + else { + if (temp->latch_1 == TRUE) { + Voltage = VoltRequest(temp->pinId[Index+1], spdtData); + if (Voltage > Volt) { + //temp->volt[Index] = Volt; + temp->volt[Index] = VoltChange(temp->pinId[Index+1], Index+1, spdtData, Voltage); + //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); + return temp->volt[Index]; + } + else { + //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); + temp->volt[Index+1] = Volt; + return (temp->volt[Index]); + } + } + }*/ + } + else + { + temp->volt[Index] = Volt; + return Volt; + } + return 0; + } +} + +int Init_SPDT(void * ComponentAddress) +{ + SPDTStruct* temp = (SPDTStruct*)ComponentAddress; + temp->image = SPDT_switch_connected_1; + temp->latch_1 = TRUE; + //temp->latch_2 = FALSE; + //temp->open = TRUE; + temp->init_pos = TRUE; + temp->volt[0] = V_OPEN; + temp->volt[1] = V_OPEN; + temp->volt[2] = V_OPEN; + return SPDT_switch_connected_1; +} + +void SetSPDTIds(int *id, void * ComponentAddress) +{ + SPDTStruct *s = (SPDTStruct*)ComponentAddress; + s->pinId[0] = *id++; + s->pinId[1] = *id++; + s->pinId[2] = *id; +} \ No newline at end of file diff --git a/ldmicro/components/SPDT_Test.cpp b/ldmicro/components/SPDT_Test.cpp new file mode 100644 index 0000000..09e89ff --- /dev/null +++ b/ldmicro/components/SPDT_Test.cpp @@ -0,0 +1,524 @@ +#include +#include +//#include +#include +#include + +#include "componentstructs.h" +#include "componentfunctions.h" +#include "componentimages.h" +#include "components.h" + +//static HWND TemporaryRadio_spdt; +static HWND LatchedRadio_spdt; +static HWND ClosedRadio_1_spdt; +static HWND ClosedRadio_2_spdt; +//static HWND OpenRadio_spdt; +static HWND NameTextbox_spdt; +static HWND InNameTextbox_spdt; +static HWND OutNameTextbox_spdt; +HWND* SPDTDialog; + +void SPDTStateChanged(SPDTStruct* SPDTData, void* ImageLocation); + +// Making switch controls +void MakeSPDTControls() +{ + + + HWND ActionGrouper = CreateWindowEx(0, WC_BUTTON, ("Action"), + WS_CHILD | BS_GROUPBOX | WS_VISIBLE | WS_TABSTOP, + 7, 3, 120, 65, *SPDTDialog, NULL, NULL, NULL); + FontNice(ActionGrouper); + + LatchedRadio_spdt = CreateWindowEx(0, WC_BUTTON, ("Latched"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE | WS_GROUP, + 16, 21, 100, 20, *SPDTDialog, NULL, NULL, NULL); + FontNice(LatchedRadio_spdt); + + /*TemporaryRadio_spdt = CreateWindowEx(0, WC_BUTTON, ("Temporary"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE, + 16, 41, 100, 20, *SPDTDialog, NULL, NULL, NULL); + FontNice(TemporaryRadio_spdt);*/ + + // SetOnlyRadio = CreateWindowEx(0, WC_BUTTON, _("(S) Set-Only"), + // WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE, + // 16, 61, 100, 20, CoilDialog, NULL, Instance, NULL); + // NiceFont(SetOnlyRadio); + + // ResetOnlyRadio = CreateWindowEx(0, WC_BUTTON, _("(R) Reset-Only"), + // WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE, + // 16, 81, 105, 20, CoilDialog, NULL, Instance, NULL); + // NiceFont(ResetOnlyRadio); + + HWND PositionGrouper = CreateWindowEx(0, WC_BUTTON, ("Position"), + WS_CHILD | BS_GROUPBOX | WS_VISIBLE, + 140, 3, 120, 65, *SPDTDialog, NULL, NULL, NULL); + FontNice(PositionGrouper); + + /*OpenRadio_spdt = CreateWindowEx(0, WC_BUTTON, ("Open"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE | WS_GROUP | WS_TABSTOP, + 149, 21, 100, 20, *SPDTDialog, NULL, NULL, NULL); + FontNice(OpenRadio_spdt);*/ + + + ClosedRadio_1_spdt = CreateWindowEx(0, WC_BUTTON, ("Open"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE | WS_GROUP | WS_TABSTOP, + 149, 21, 100, 20, *SPDTDialog, NULL, NULL, NULL); + FontNice(ClosedRadio_1_spdt); + + ClosedRadio_2_spdt = CreateWindowEx(0, WC_BUTTON, ("Closed"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE | WS_TABSTOP, + 149, 41, 100, 20, *SPDTDialog, NULL, NULL, NULL); + FontNice(ClosedRadio_2_spdt); + HWND textLabel = CreateWindowEx(0, WC_STATIC, ("Name:"), + WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE | SS_RIGHT, + 100, 80, 50, 21, *SPDTDialog, NULL, NULL, NULL); + FontNice(textLabel); + + NameTextbox_spdt = CreateWindowEx(WS_EX_CLIENTEDGE, WC_EDIT, "", + WS_CHILD | ES_AUTOHSCROLL | WS_TABSTOP | WS_CLIPSIBLINGS | WS_VISIBLE, + 155, 80, 155, 21, *SPDTDialog, NULL, NULL, NULL); + FontFixed(NameTextbox_spdt); + + /*PrevNameProc = SetWindowLongPtr(NameTextbox, GWLP_WNDPROC, + (LONG_PTR)MyNameProc);*/ +} + +void SPDTLoadState(SPDTStruct* Data) +{ + + Button_SetCheck(LatchedRadio_spdt, BST_CHECKED); + + if (Data->init_pos) + { + Button_SetCheck(ClosedRadio_1_spdt, BST_CHECKED); + } + else + { + Button_SetCheck(ClosedRadio_2_spdt, BST_CHECKED); + } + //Edit_SetText(NameTextbox_spdt, Data->Name); +} + +BOOL SaveSPDTDialog(SPDTStruct* Data) +{ + char temp[15]; + BOOL latch_1, latch_2, init_pos; + + // For pin 1 + if (Button_GetState(LatchedRadio_spdt) == BST_CHECKED) + { + if (Button_GetState(ClosedRadio_1_spdt) == BST_CHECKED) + { + init_pos = TRUE; + } + + else if (Button_GetState(ClosedRadio_2_spdt) == BST_CHECKED) + { + init_pos = FALSE; + } + + else + { + MessageBox(*SPDTDialog, + ("Incomplete"), ("Warning"), MB_OK | MB_ICONWARNING); + return FALSE; + } + } + + /*else if (Button_GetState(TemporaryRadio_spdt) == BST_CHECKED) + { + latch_1 = FALSE; + } + else + { + MessageBox(*SPDTDialog, + ("Incomplete"), ("Warning"), MB_OK | MB_ICONWARNING); + return FALSE; + } + + // For pin 2 + if (Button_GetState(LatchedRadio_spdt) == BST_CHECKED) + { + latch_2 = TRUE; + } + /*else if (Button_GetState(TemporaryRadio_spdt) == BST_CHECKED) + { + latch_2 = FALSE; + } + else + { + MessageBox(*SPDTDialog, + ("Incomplete"), ("Warning"), MB_OK | MB_ICONWARNING); + return FALSE; + } + */ + + if (Edit_GetText(NameTextbox_spdt, (LPSTR)&temp, 15) < 1) + { + MessageBox(*SPDTDialog, + ("Incomplete"), ("Warning"), MB_OK | MB_ICONWARNING); + return FALSE; + } + else + { + Data->latch_1 = latch_1; + Data->latch_2 = latch_2; + Data->init_pos = init_pos; + //Data->open = init_pos; + strcpy(Data->name, temp); + if (init_pos) + { + Data->image = SPDT_switch_connected_1; + } + else + { + Data->image = SPDT_switch_connected_2; + } + } + return TRUE; +} + +void ShowSPDTDialog(void* ComponentAddress) +{ + SPDTStruct* Data = (SPDTStruct*)ComponentAddress; + BOOL Canceled, Complete = TRUE; + SPDTDialog = CreateDialogWindow("Switch Dialog", 100, 100, 263, 145, STYLE_VERTICAL); + MakeSPDTControls(); + ShowDialogWindow(); + SPDTLoadState(Data); + Canceled = ProcessDialogWindow(); + while (Canceled == FALSE) + { + Complete = SaveSPDTDialog(Data); + if (Complete == TRUE) + { + // MessageBox(*SwitchDialog, + // ("Saved"), ("Mouse click"), MB_OK | MB_ICONWARNING); + break; + } + else + { + Complete = TRUE; + Canceled = ProcessDialogWindow(); + } + } + DestroyWindow(*SPDTDialog); +} + +int Init_SPDT(void* ComponentAddress) +{ + SPDTStruct* temp = (SPDTStruct*)ComponentAddress; + temp->image = SPDT_switch_connected_1; + temp->latch_1 = TRUE; + temp->latch_2 = FALSE; + //temp->Open = TRUE; + temp->init_pos = TRUE; + temp->volt[0] = V_OPEN; + temp->volt[1] = V_OPEN; + temp->volt[2] = V_OPEN; + return SPDT_switch_connected_1; +} + +void SPDTUpdateValues(SPDTStruct* spdt, void* ComponentAddress) +{ + /*char Debug[256]; + sprintf_s(Debug, "Clicked: %p\n", ComponentAddress); + OutputDebugString(Debug);*/ + if (spdt->latch_1) + /*{ + // Changing the initial state + spdt->volt[0] = + VoltRequest(spdt->pinId[0], ComponentAddress); + spdt->volt[1] = + VoltRequest(spdt->pinId[1], ComponentAddress); + + //spdt->volt[2] = + //VoltChange(spdt->pinId[2], 2, ComponentAddress, VOLT_5); + // char vx[3],vy[10]; + // _gcvt(Switch->Volt[0],8,vy); + // _itoa(Switch->PinId[0],vx,10); + // MessageBox(NULL, + // (vx),(vy), MB_OK | MB_ICONWARNING); + // Switch->Volt[1] = GlobalVoltChange(Switch->PinId[1], ComponentAddress, V_OPEN); + // MessageBox(NULL, + // "Open", "Test", MB_OK | MB_ICONWARNING); + } + else*/ + { + // Storing real time values in each pin + double Voltage; + Voltage = VoltRequest(spdt->pinId[0], ComponentAddress); + spdt->volt[0] = Voltage; + Voltage = VoltRequest(spdt->pinId[1], ComponentAddress); + spdt->volt[1] = Voltage; + Voltage = VoltRequest(spdt->pinId[2], ComponentAddress); + spdt->volt[2] = Voltage; + if (spdt->volt[0] > spdt->volt[1]) + { + /*sprintf_s(Debug, "Values2: Volt1:%f \t Volt2:%f \tAddr: %p \tPin0:%d \t Pin1:%d\n", + Switch->Volt[0], Switch->Volt[1], ComponentAddress, Switch->PinId[0], Switch->PinId[1]); + OutputDebugString(Debug);*/ + spdt->volt[0] = VoltChange(spdt->pinId[0], 0, + ComponentAddress, spdt->volt[1]); + } + else if (spdt->volt[1] > spdt->volt[0]) + { + /*sprintf_s(Debug, "Values3: Volt1:%f \t Volt2:%f \tAddr: %p \tPin0:%d \t Pin1:%d\n", + Switch->Volt[0], Switch->Volt[1], ComponentAddress, Switch->PinId[0], Switch->PinId[1]); + OutputDebugString(Debug);*/ + spdt->volt[1] = VoltChange(spdt->pinId[1], 1, + ComponentAddress, spdt->volt[0]); + } + } + /*sprintf_s(Debug, "Values: Volt1:%f \t Volt2:%f \tAddr: %p \tPin0:%d \t Pin1:%d\n", + Switch->Volt[0], Switch->Volt[1], ComponentAddress, Switch->PinId[0], Switch->PinId[1]); + OutputDebugString(Debug);*/ + else if (spdt->latch_2) { + double Voltage; + Voltage = VoltRequest(spdt->pinId[0], ComponentAddress); + spdt->volt[0] = Voltage; + Voltage = VoltRequest(spdt->pinId[1], ComponentAddress); + spdt->volt[1] = Voltage; + Voltage = VoltRequest(spdt->pinId[2], ComponentAddress); + spdt->volt[2] = Voltage; + if (spdt->volt[0] > spdt->volt[2]) + { + /*sprintf_s(Debug, "Values2: Volt1:%f \t Volt2:%f \tAddr: %p \tPin0:%d \t Pin1:%d\n", + Switch->Volt[0], Switch->Volt[1], ComponentAddress, Switch->PinId[0], Switch->PinId[1]); + OutputDebugString(Debug);*/ + spdt->volt[0] = VoltChange(spdt->pinId[0], 0, + ComponentAddress, spdt->volt[2]); + } + else if (spdt->volt[2] > spdt->volt[0]) + { + /*sprintf_s(Debug, "Values3: Volt1:%f \t Volt2:%f \tAddr: %p \tPin0:%d \t Pin1:%d\n", + Switch->Volt[0], Switch->Volt[1], ComponentAddress, Switch->PinId[0], Switch->PinId[1]); + OutputDebugString(Debug);*/ + spdt->volt[2] = VoltChange(spdt->pinId[2], 2, + ComponentAddress, spdt->volt[0]); + } + } +} + +void HandleSPDTEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, + void* ImageLocation, UINT ImageId, HWND* h) +{ + SPDTStruct *temp = (SPDTStruct*)ComponentAddress; + + if (SimulationStarted) + { + switch (Event) { + case EVENT_MOUSE_UP: + //if (temp->latch_1) + //{ + temp->latch_1 = !temp->latch_1; + temp->latch_2 = !temp->latch_2; + /* /*MessageBox(NULL, + "Latched", "Test", MB_OK | MB_ICONWARNING); + } + else + { + temp->latch_1 = !temp->latch_1; + temp->latch_2 = !temp->latch_2; + // MessageBox(NULL, + // "Latched", "Test", MB_OK | MB_ICONWARNING); + }*/ + SPDTStateChanged(temp, ImageLocation); + SPDTUpdateValues(temp, ComponentAddress); + break; + case EVENT_MOUSE_DOWN: + /*if (!temp->latch_2) + { + temp->latch_2 = !temp->init_pos; + SPDTStateChanged(temp, ImageLocation); + SPDTUpdateValues(temp, ComponentAddress); + }*/ + break; + } + + } + else + { + switch (Event) { + case EVENT_MOUSE_DBLCLICK: + ShowSPDTDialog(ComponentAddress); + SetImage(temp->image, ImageLocation); + RefreshImages(); + break; + } + } +} + +void SPDTStateChanged(SPDTStruct* spdtData, void* ImageLocation) +{ + if (spdtData->latch_1 == TRUE) { + SetImage(spdtData->latch_1 ? SPDT_switch_connected_2 : SPDT_switch_connected_1, + ImageLocation); + RefreshImages(); + } + else if (spdtData->latch_2 == TRUE) { + SetImage(spdtData->latch_2 ? SPDT_switch_connected_1 : SPDT_switch_connected_2, + ImageLocation); + RefreshImages(); + } + +} + +/*double SPDTVoltChanged(void* spdtData, BOOL SimulationStarted, int Index, + double Volt, int Source, void* ImageLocation) +{ + // char Debug[256]; + SPDTStruct* temp = (SPDTStruct*)spdtData; + if (SimulationStarted) + { + double Voltage; + Voltage = VoltRequest(temp->pinId[!Index], spdtData); + if (temp->latch_1) { + if (Index == 0) + { + temp->volt[Index] = VoltChange(temp->pinId[Index], Index, spdtData, Volt); + temp->volt[Index + 1] = VoltChange(temp->pinId[Index - 1], Index - 1, spdtData, Volt); + temp->volt[Index + 2] = V_OPEN; + /*sprintf_s(Debug, "SwitchVoltChanged: \tAddress: %p \tVolt0:%f \t Volt1:%f\n", + SwitchData, Voltage, Volt); + OutputDebugString(Debug); + + if (Voltage > Volt) + { + temp->volt[Index] = VoltChange(temp->pinId[Index], Index, spdtData, Volt); + temp->volt[Index + 1] = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); + temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, V_OPEN); + /*sprintf_s(Debug, "SwitchVoltChanged2: \tAddress: %p \tVolt0:%f \t Volt1:%f\n", + SwitchData, Voltage, Volt); + OutputDebugString(Debug); + return temp->volt[Index]; + } + return temp->volt[Index]; + } + else if (Index == 1) + { + temp->volt[Index] = VoltChange(temp->pinId[Index - 1], Index - 1, spdtData, Volt); + temp->volt[Index + 1] = VoltChange(temp->pinId[Index], Index, spdtData, Volt); + temp->volt[Index + 2] = V_OPEN; + /*sprintf_s(Debug, "SwitchVoltChanged: \tAddress: %p \tVolt0:%f \t Volt1:%f\n", + SwitchData, Voltage, Volt); + OutputDebugString(Debug); + + if (Voltage > Volt) + { + temp->volt[Index] = VoltChange(temp->pinId[Index], Index, spdtData, Volt); + temp->volt[Index + 1] = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); + temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, V_OPEN); + /*sprintf_s(Debug, "SwitchVoltChanged2: \tAddress: %p \tVolt0:%f \t Volt1:%f\n", + SwitchData, Voltage, Volt); + OutputDebugString(Debug); + return temp->volt[Index]; + } + return temp->volt[Index]; + } + else if (Index == 2) + { + temp->volt[Index] = VoltChange(temp->pinId[Index - 2], Index - 2, spdtData, Volt); + temp->volt[Index + 1] = VoltChange(temp->pinId[Index - 1], Index - 1, spdtData, Volt); + temp->volt[Index + 2] = V_OPEN; + /*sprintf_s(Debug, "SwitchVoltChanged: \tAddress: %p \tVolt0:%f \t Volt1:%f\n", + SwitchData, Voltage, Volt); + OutputDebugString(Debug); + + if (Voltage > Volt) + { + temp->volt[Index] = VoltChange(temp->pinId[Index], Index, spdtData, Volt); + temp->volt[Index + 1] = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); + temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, V_OPEN); + /*sprintf_s(Debug, "SwitchVoltChanged2: \tAddress: %p \tVolt0:%f \t Volt1:%f\n", + SwitchData, Voltage, Volt); + OutputDebugString(Debug); + return temp->volt[Index]; + } + return temp->volt[Index]; + } + } + else if (temp->latch_2) { + if (Index == 0) { + // temp->Volt[!Index] = VoltChange(temp->PinId[!Index], !Index, SwitchData, Volt); + + //temp->volt[0] = VoltChange(temp->pinId[!Index-1], Index-1, spdtData, V_OPEN); + Voltage = VoltChange(temp->pinId[Index], Index, spdtData, Volt); + temp->volt[Index] = Voltage; + + // VoltChange(temp->PinId[!Index], !Index, SwitchData, Volt); + return Voltage; + } + } + else + { + temp->volt[Index] = Volt; + return Volt; + } + } + return 0; +}*/ + +double SwitchVoltChanged(void* spdtData, BOOL SimulationStarted, int Index, + double Volt, int Source, void* ImageLocation) +{ + SPDTStruct* temp = (SPDTStruct*)spdtData; +if (SimulationStarted) +{ + double Voltage; + Voltage = VoltRequest(temp->pinId[!Index], spdtData); + if (temp->latch_1) + { + temp->volt[!Index] = VoltChange(temp->pinId[!Index], !Index, spdtData, V_OPEN); + temp->volt[Index] = V_OPEN; + /*sprintf_s(Debug, "SwitchVoltChanged: \tAddress: %p \tVolt0:%f \t Volt1:%f\n", + SwitchData, Voltage, Volt); + OutputDebugString(Debug);*/ + return temp->volt[Index]; + } + if (Voltage > Volt) + { + temp->volt[Index] = Volt; + temp->volt[!Index] = VoltChange(temp->pinId[!Index], !Index, spdtData, Volt); + /*sprintf_s(Debug, "SwitchVoltChanged2: \tAddress: %p \tVolt0:%f \t Volt1:%f\n", + SwitchData, Voltage, Volt); + OutputDebugString(Debug);*/ + return temp->volt[Index]; + } + else + { + // temp->Volt[!Index] = VoltChange(temp->PinId[!Index], !Index, SwitchData, Volt); + Voltage = VoltChange(temp->pinId[!Index], !Index, spdtData, Volt); + temp->volt[Index] = Voltage; + // VoltChange(temp->PinId[!Index], !Index, SwitchData, Volt); + return Voltage; + } +} +else +{ + temp->volt[Index] = Volt; + return Volt; +} +return 0; +} + +// Setting pin Ids for each pin +void SetSPDTIds(int* id, void* ComponentAddress) +{ + SPDTStruct *s = (SPDTStruct*)ComponentAddress; + s->pinId[0] = *id++; + s->pinId[1] = *id++; + s->pinId[2] = *id; + /*char Debug[256]; + sprintf_s(Debug, "SetSwitchIds: \tAddr1: %p \tAddr2: %p \tPin0:%d \t Pin1:%d \tVolt0: %f Volt1: %f\n", + ComponentAddress, s, s->PinId[0], s->PinId[1],VoltRequest(s->PinId[0], ComponentAddress), + VoltRequest(s->PinId[0], ComponentAddress)); + OutputDebugString(Debug); + char vx[4],vy[4]; + _itoa(s->PinId[0],vy,10); + _itoa(s->PinId[1],vx,10); + MessageBox(NULL, + (vy), (vx), MB_OK | MB_ICONWARNING);*/ +} \ No newline at end of file diff --git a/ldmicro/components/componentfunctions.h b/ldmicro/components/componentfunctions.h index 9da4bfb..e068bef 100644 --- a/ldmicro/components/componentfunctions.h +++ b/ldmicro/components/componentfunctions.h @@ -23,22 +23,40 @@ BOOL ProcessDialogWindow(void); /*Initialization Functions*/ int InitSwitch(void* ComponentAddress); int InitRelay(void* ComponentAddress); +int Init_SPDT(void* ComponentAddress); +int Init_DPST(void* CompponentAddress); +//int Init_DPDT(void* CompponentAddress); /*Event Handlers*/ void HandleSwitchEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, void* ImageLocation, UINT ImageId, HWND* h); void HandleRelayEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, void* ImageLocation, UINT ImageId, HWND* h); +void HandleSPDTEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, + void* ImageLocation, UINT ImageId, HWND* h); +void HandleDPSTEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, + void* ImageLocation, UINT ImageId, HWND* h); +/*void HandleDPDTEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, + void* ImageLocation, UINT ImageId, HWND* h);*/ /*Request Handlers*/ double SwitchVoltChanged(void* ComponentAddress, BOOL SimulationStarted, int index, double Volt, int Source, void* ImageLocation); double RelayVoltChanged(void* ComponentAddress, BOOL SimulationStarted, int index, double Volt, int Source, void* ImageLocation); +double SPDTVoltChanged(void* ComponentAddress, BOOL SimulationStarted, int index, + double Volt, int Source, void* ImageLocation); +double DPSTVoltChanged(void* ComponentAddress, BOOL SimulationStarted, int index, + double Volt, int Source, void* ImageLocation); +/*double DPDTVoltChanged(void* ComponentAddress, BOOL SimulationStarted, int index, + double Volt, int Source, void* ImageLocation);*/ /*Program Reference Functions*/ void SetSwitchIds(int*, void*); void SetRelayIds(int*, void*); +void SetSPDTIds(int*, void*); +void SetDPSTIds(int*, void*); +//void SetDPDTIds(int*, void*); // Relay Functions diff --git a/ldmicro/components/componentimages.h b/ldmicro/components/componentimages.h index 5c71ae6..70960cb 100644 --- a/ldmicro/components/componentimages.h +++ b/ldmicro/components/componentimages.h @@ -1,23 +1,30 @@ #ifndef __COMPONENTIMAGES_H #define __COMPONENTIMAGES_H -#define SWITCH_CONNECTED 8000 -#define SWITCH_DISCONNECTED 8001 -#define RELAY_NC 8002 -#define RELAY_NO 8003 +#define SWITCH_CONNECTED 8000 +#define SWITCH_DISCONNECTED 8001 +#define RELAY_NC 8002 +#define RELAY_NO 8003 +#define SPDT_switch_connected_1 8004 +#define SPDT_switch_connected_2 8005 +#define DPST_switch_connected_1 8006 +#define DPST_switch_disconnected_1 8007 +#define DPDT_switch_connected_1 8008 +#define DPDT_switch_connected_2 8009 #ifndef RC_INVOKED //Used to hide code from resource file(Guess) -#define TOTAL_COMPONENTS 2 +#define TOTAL_COMPONENTS 5 #define COMPONENT_NAME_MAX_LENGTH 50 // Try to keep ComponentID's between 6000 - 6999 -#define COMPONENT_SWITCH 6000 -#define COMPONENT_RELAY 6001 - - -#define MAX_PIN_COUNT 10 +#define COMPONENT_SWITCH 6000 +#define COMPONENT_RELAY 6001 +#define COMPONENT_SPDT_switch 6002 +#define COMPONENT_DPST_switch 6003 +#define COMPONENT_DPDT_switch 6004 +#define MAX_PIN_COUNT 10 typedef struct ComponentDataTag{ int Index; @@ -32,7 +39,10 @@ void SetImage(int Component, void *il); static ComponentData rgCompData[TOTAL_COMPONENTS] = { {0, COMPONENT_SWITCH, TEXT("Switch"), 2, {"Input:", "Output:"}}, - {1, COMPONENT_RELAY, TEXT("Relay"), 5, {"Coil1:", "Coil2:", "NO:", "COM:", "NC:"}} + {1, COMPONENT_RELAY, TEXT("Relay"), 5, {"Coil1:", "Coil2:", "NO:", "COM:", "NC:"}}, + {2, COMPONENT_SPDT_switch, TEXT("SPDT Switch"), 3, {"Input : ", "Output 1 : ", "Output 2 : "}}, + {3, COMPONENT_DPST_switch, TEXT("DPST Switch"), 4, {"Input 1 : ", "Input 2 : ", "Output 1 : ", "Output 2 : "}}, + {4, COMPONENT_DPDT_switch, TEXT("DPDT Switch"), 6, {"Input 1 : ", "Input 2 : ", "Output 1 : ", "Output 2 : ", "Output 3 : ", "Output 4 : "}}, }; #endif diff --git a/ldmicro/components/components.cpp b/ldmicro/components/components.cpp index 7a27823..96fbdda 100644 --- a/ldmicro/components/components.cpp +++ b/ldmicro/components/components.cpp @@ -49,6 +49,15 @@ size_t GetStructSize(int ComponentId) case COMPONENT_RELAY: return sizeof(RelayStruct); break; + case COMPONENT_SPDT_switch: + return sizeof(SPDTStruct); + break; + case COMPONENT_DPST_switch: + return sizeof(DPSTStruct); + break; + case COMPONENT_DPDT_switch: + return sizeof(DPDTStruct); + break; } return (size_t)-1; } @@ -64,6 +73,15 @@ int InitializeComponentProperties(void *ComponentAddress, int ComponentId) case COMPONENT_RELAY: return InitRelay(ComponentAddress); break; + case COMPONENT_SPDT_switch: + return Init_SPDT(ComponentAddress); + break; + case COMPONENT_DPST_switch: + return Init_DPST(ComponentAddress); + break; + /*case COMPONENT_DPDT_switch: + return Init_DPDT(ComponentAddress); + break;*/ } return 0; } @@ -81,6 +99,15 @@ double VoltSet(void* ComponentAddress, BOOL SimulationStarted, int ImageType, in break; case COMPONENT_RELAY: return RelayVoltChanged(ComponentAddress, SimulationStarted, Index, Volt, Source, ImageLocation); + case COMPONENT_SPDT_switch: + return SPDTVoltChanged(ComponentAddress, SimulationStarted, Index, Volt, Source, ImageLocation); + break; + case COMPONENT_DPST_switch: + return DPSTVoltChanged(ComponentAddress, SimulationStarted, Index, Volt, Source, ImageLocation); + break; + /*case COMPONENT_DPDT_switch: + return DPDTVoltChanged(ComponentAddress, SimulationStarted, Index, Volt, Source, ImageLocation); + break;*/ } return Volt; } @@ -95,6 +122,16 @@ void SetPinIds(int Index, void *PinName,int ComponentId, void *ComponentAddres case COMPONENT_SWITCH: SetSwitchIds(PinIds, ComponentAddress); break; + case COMPONENT_SPDT_switch: + SetSPDTIds(PinIds, ComponentAddress); + break; + case COMPONENT_DPST_switch: + SetDPSTIds(PinIds, ComponentAddress); + break; + /*case COMPONENT_DPDT_switch: + SetDPDTIds(PinIds, ComponentAddress); + break; + */ case COMPONENT_RELAY: SetRelayIds(PinIds,ComponentAddress); } @@ -125,6 +162,15 @@ int NotifyComponent(void *ComponentAddress, void *PinName, int ComponentId, HandleRelayEvent(ComponentAddress, Event, SimulationStarted, ImageLocation, ImageId, h); // return InitRelay(ComponentAddress); break; + case COMPONENT_SPDT_switch : + HandleSPDTEvent(ComponentAddress, Event, SimulationStarted, ImageLocation, ImageId, h); + break; + case COMPONENT_DPST_switch: + HandleDPSTEvent(ComponentAddress, Event, SimulationStarted, ImageLocation, ImageId, h); + break; + /*case COMPONENT_DPDT_switch: + HandleDPDTEvent(ComponentAddress, Event, SimulationStarted, ImageLocation, ImageId, h); + break;*/ } // return voltage return 0; diff --git a/ldmicro/components/componentstructs.h b/ldmicro/components/componentstructs.h index a7baca5..abbabdb 100644 --- a/ldmicro/components/componentstructs.h +++ b/ldmicro/components/componentstructs.h @@ -29,4 +29,42 @@ typedef struct RelayStructTag }RelayStruct; +typedef struct SPDT_switch_struct_Tag +{ + int id; + int image; // Image of the pin + int pinId[3]; // Pin IDs to store 3 pins + BOOL init_pos; // Initial open/closed position + BOOL latch_1; // Latched to the output pin 1 + //BOOL latch_2; // Latched to the output pin 2 + double volt[3]; // Voltage at each pin + char name[15]; +}SPDTStruct; + +typedef struct DPST_switch_struct_Tag +{ + int id; + int image; // Image of the pin + int pinId[4]; // Pin IDs to store 4 pins + BOOL init_pos; // Initial open/closed position + BOOL closed; // Closed + BOOL open; // Open + double volt[4]; // Voltage at each pin + BOOL latched; +}DPSTStruct; + +typedef struct DPDT_switch_struct_Tag +{ + int id; + int image; // Image of the pin + int pinId[6]; // Pin IDs to store 6 pins + BOOL init_pos_1; // Initial open/closed position of switch 1 + BOOL init_pos_2; // Initial open/closed position of switch 2 + BOOL latch_1; // Latched to the output pin 1 + BOOL latch_2; // Latched to the output pin 2 + BOOL latch_3; // Latched to the output pin 3 + BOOL latch_4; // Latched to the output pin 4 + //BOOL open; + double volt[6]; // Voltage at each pin +}DPDTStruct; #endif diff --git a/ldmicro/ldmicro.rc b/ldmicro/ldmicro.rc index 46c3de1..47eb087 100644 --- a/ldmicro/ldmicro.rc +++ b/ldmicro/ldmicro.rc @@ -6,8 +6,14 @@ 1 24 "ldmicro.exe.manifest" -4000 ICON "ldmicro.ico" -RELAY_NC PNG "img\\relay_nc.png" -RELAY_NO PNG "img\\relay_no.png" -SWITCH_CONNECTED PNG "img\\switch_connected.png" -SWITCH_DISCONNECTED PNG "img\\switch_disconnected.png" \ No newline at end of file +4000 ICON "ldmicro.ico" +RELAY_NC PNG "img\\relay_nc.png" +RELAY_NO PNG "img\\relay_no.png" +SWITCH_CONNECTED PNG "img\\switch_connected.png" +SWITCH_DISCONNECTED PNG "img\\switch_disconnected.png" +SPDT_switch_connected_1 PNG "img\\spdt_switch_1.png" +SPDT_switch_connected_2 PNG "img\\spdt_switch_2.png" +DPST_switch_connected_1 PNG "img\\dpst_switch_1.png" +DPST_switch_disconnected_1 PNG "img\\dpst_switch_2.png" +DPDT_switch_connected_1 PNG "img\\dpdt_switch_1.png" +DPDT_switch_connected_2 PNG "img\\dpdt_switch_2.png" \ No newline at end of file From 5d05e8d7427ceb8fd807367cc832e50fdb37d43c Mon Sep 17 00:00:00 2001 From: NatsuDrag9 Date: Thu, 5 Apr 2018 09:15:17 +0530 Subject: [PATCH 2/5] Added DPST switch. --- ldmicro/components/DPST.cpp | 185 +++++++++++++++++++------- ldmicro/components/SPDT.cpp | 2 +- ldmicro/components/componentstructs.h | 1 - 3 files changed, 135 insertions(+), 53 deletions(-) diff --git a/ldmicro/components/DPST.cpp b/ldmicro/components/DPST.cpp index 9df7d44..0b399c1 100644 --- a/ldmicro/components/DPST.cpp +++ b/ldmicro/components/DPST.cpp @@ -9,16 +9,15 @@ #include "componentimages.h" #include "components.h" -//static HWND TemporaryRadio; +// Declaring the required components in UI. +static HWND TemporaryRadio_dpst; static HWND LatchedRadio_dpst; static HWND ClosedRadio_dpst; -//static HWND ClosedRadio_2_dpst; static HWND OpenRadio_dpst; -//static HWND NameTextbox_dpst; -//static HWND InNameTextbox_dpst; static HWND OutNameTextbox_dpst; HWND* DPSTDialog; +// UI for the DPST switch. void MakeDPSTControls() { HWND ActionGrouper = CreateWindowEx(0, WC_BUTTON, ("Action"), @@ -31,6 +30,11 @@ void MakeDPSTControls() 16, 21, 100, 20, *DPSTDialog, NULL, NULL, NULL); FontNice(LatchedRadio_dpst); + TemporaryRadio_dpst = CreateWindowEx(0, WC_BUTTON, ("Temporary"), + WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE, + 16, 41, 100, 20, *DPSTDialog, NULL, NULL, NULL); + FontNice(TemporaryRadio_dpst); + HWND PositionGrouper = CreateWindowEx(0, WC_BUTTON, ("Position"), WS_CHILD | BS_GROUPBOX | WS_VISIBLE, 140, 3, 120, 65, *DPSTDialog, NULL, NULL, NULL); @@ -53,7 +57,11 @@ void DPSTLoadState(DPSTStruct* Data) { Button_SetCheck(LatchedRadio_dpst, BST_CHECKED); } - if (!Data->init_pos) + /*else + { + Button_SetCheck(TemporaryRadio_dpst, BST_CHECKED); + }*/ + if (Data->init_pos) { Button_SetCheck(ClosedRadio_dpst, BST_CHECKED); } @@ -61,7 +69,6 @@ void DPSTLoadState(DPSTStruct* Data) { Button_SetCheck(OpenRadio_dpst, BST_CHECKED); } - //Edit_SetText(NameTextbox_spdt, Data->Name); } BOOL SaveDPSTDialog(DPSTStruct* Data) @@ -70,8 +77,11 @@ BOOL SaveDPSTDialog(DPSTStruct* Data) if (Button_GetState(LatchedRadio_dpst) == BST_CHECKED) { latched = TRUE; - } + /*else if (Button_GetState(TemporaryRadio_dpst) == BST_CHECKED) + { + latched = FALSE; + }*/ else { MessageBox(*DPSTDialog, @@ -82,7 +92,6 @@ BOOL SaveDPSTDialog(DPSTStruct* Data) if (Button_GetState(ClosedRadio_dpst) == BST_CHECKED) { init_pos = TRUE; - } else if (Button_GetState(OpenRadio_dpst) == BST_CHECKED) { @@ -96,14 +105,13 @@ BOOL SaveDPSTDialog(DPSTStruct* Data) } Data->latched = latched; Data->init_pos = init_pos; - //Data->Open = NOpen; if (init_pos) { - Data->image = DPST_switch_disconnected_1; + Data->image = DPST_switch_connected_1; } else { - Data->image = DPST_switch_connected_1; + Data->image = DPST_switch_disconnected_1; } return TRUE; } @@ -135,11 +143,12 @@ void ShowDPSTDialog(void* ComponentAddress) DestroyWindow(*DPSTDialog); } +// Initializing the required variables. int Init_DPST(void* ComponentAddress) { DPSTStruct* temp = (DPSTStruct*)ComponentAddress; temp->image = DPST_switch_disconnected_1; - temp->latched = TRUE; + temp->latched = TRUE; // Varibale for latching action temp->open = TRUE; temp->init_pos = TRUE; temp->volt[0] = V_OPEN; @@ -150,41 +159,92 @@ int Init_DPST(void* ComponentAddress) return DPST_switch_disconnected_1; } +// Updating the voltages at each pin. +void DPSTUpdateValues(DPSTStruct* dpstData, void* ComponentAddress) +{ + DPSTStruct* temp = (DPSTStruct*)dpstData; + double v0, v1, v2, v3; + v0 = VoltRequest(temp->pinId[0], ComponentAddress); + v1 = VoltRequest(temp->pinId[1], ComponentAddress); + v2 = VoltRequest(temp->pinId[2], ComponentAddress); + v3 = VoltRequest(temp->pinId[3], ComponentAddress); + if (temp->open) // Open condition. + { + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, V_OPEN); + temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, V_OPEN); + temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, V_OPEN); + temp->volt[3] = VoltChange(temp->pinId[3], 3, ComponentAddress, V_OPEN); + } + // Closed condition + else if (!temp->open) { + /*if ((v0 > v1) && (v2 > v3)) + { + temp->volt[1] = VoltChange(temp->pinId[1], 1, dpstData, v0); + temp->volt[3] = VoltChange(temp->pinId[3], 3, dpstData, v2); + temp->volt[2] = VoltChange(temp->pinId[2], 2, dpstData, V_OPEN); + temp->volt[0] = VoltChange(temp->pinId[0], 0, dpstData, V_OPEN); + } + else + { + temp->volt[0] = VoltChange(temp->pinId[0], 0, dpstData, v1); + temp->volt[2] = VoltChange(temp->pinId[2], 2, dpstData, v3); + temp->volt[3] = VoltChange(temp->pinId[3], 3, dpstData, V_OPEN); + temp->volt[1] = VoltChange(temp->pinId[1], 1, dpstData, V_OPEN); + }*/ + if ((v0 != GND && v1 != GND) && (v2 != GND && v3 != GND)) { + if ((v0 != V_OPEN && v1 != V_OPEN) && (v2 != V_OPEN && v3 != V_OPEN)) { + if ((v0 > v1) && (v2 > v3)) + { + temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, v0); + temp->volt[3] = VoltChange(temp->pinId[3], 3, ComponentAddress, v2); + } + else + { + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, v1); + temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, v3); + } + } + else if (v1 == V_OPEN && v3 == V_OPEN) { + temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, v0); + temp->volt[3] = VoltChange(temp->pinId[3], 3, ComponentAddress, v2); + } + else if (v0 == V_OPEN && v2 == V_OPEN) { + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, v1); + temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, v3); + } + } + else { + temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, GND); + temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, GND); + temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, GND); + temp->volt[3] = VoltChange(temp->pinId[3], 3, ComponentAddress, GND); + } + } +} + void HandleDPSTEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, void* ImageLocation, UINT ImageId, HWND* h) { DPSTStruct *temp = (DPSTStruct*)ComponentAddress; - if (SimulationStarted) { switch (Event) { - case EVENT_MOUSE_UP: - /*if (temp->Latched) + case EVENT_MOUSE_CLICK: + if (temp->latched) { - temp->Open = !temp->Open; - /*MessageBox(NULL, - "Latched", "Test", MB_OK | MB_ICONWARNING); - }*/ - /*else - { - temp->Open = temp->NOpen; - // MessageBox(NULL, - // "Latched", "Test", MB_OK | MB_ICONWARNING); - }*/ - /*SwitchStateChanged(temp, ImageLocation); - UpdateValues(temp, ComponentAddress); - break;*/ - case EVENT_MOUSE_DOWN: - /*if (!temp->Latched) + temp->open = !temp->open; + } + else { - temp->Open = !temp->NOpen; - SwitchStateChanged(temp, ImageLocation); - UpdateValues(temp, ComponentAddress); + temp->open = temp->init_pos; } - */ + // Setting the image according to the click. + SetImage(!temp->open ? DPST_switch_disconnected_1 : DPST_switch_connected_1, + ImageLocation); + RefreshImages(); + DPSTUpdateValues(temp, ComponentAddress); break; } - } else { @@ -198,40 +258,62 @@ void HandleDPSTEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, } } +// Voltage change at each pin double DPSTVoltChanged(void* dpstData, BOOL SimulationStarted, int Index, double Volt, int Source, void* ImageLocation) { DPSTStruct* temp = (DPSTStruct*)dpstData; if (SimulationStarted) { + // Fetching the voltages at each pin. double v0,v1,v2,v3; v0 = VoltRequest(temp->pinId[0], dpstData); v1 = VoltRequest(temp->pinId[1], dpstData); v2 = VoltRequest(temp->pinId[2], dpstData); v3 = VoltRequest(temp->pinId[3], dpstData); - if (temp->open) + if (temp->open) // Open condition. { temp->volt[0] = VoltChange(temp->pinId[0], 0, dpstData, V_OPEN); temp->volt[1] = VoltChange(temp->pinId[1], 1, dpstData, V_OPEN); temp->volt[2] = VoltChange(temp->pinId[2], 2, dpstData, V_OPEN); temp->volt[3] = VoltChange(temp->pinId[3], 3, dpstData, V_OPEN); - /*sprintf_s(Debug, "SwitchVoltChanged: \tAddress: %p \tVolt0:%f \t Volt1:%f\n", - SwitchData, Voltage, Volt); - OutputDebugString(Debug);*/ - return temp->volt[Index]; - } - if ((v0 > v1) && (v2 > v3)) - { - temp->volt[1] = VoltChange(temp->pinId[1], 1, dpstData, v0); - temp->volt[3] = VoltChange(temp->pinId[3], 3, dpstData, v2); return temp->volt[Index]; } - else - { - // temp->Volt[!Index] = VoltChange(temp->PinId[!Index], !Index, SwitchData, Volt); - temp->volt[0] = VoltChange(temp->pinId[0], 0, dpstData, v1); - temp->volt[2] = VoltChange(temp->pinId[2], 2, dpstData, v3); - return temp->volt[Index]; + // Closed condition + else if (!temp->open) { + if ((v0 != GND && v1 != GND) && (v2 != GND && v3 != GND)) { + if ((v0 != V_OPEN && v1 != V_OPEN) && (v2 != V_OPEN && v3 != V_OPEN)) { + if ((v0 > v1) && (v2 > v3)) + { + temp->volt[1] = VoltChange(temp->pinId[1], 1, dpstData, v0); + temp->volt[3] = VoltChange(temp->pinId[3], 3, dpstData, v2); + return temp->volt[Index]; + } + else + { + temp->volt[0] = VoltChange(temp->pinId[0], 0, dpstData, v1); + temp->volt[2] = VoltChange(temp->pinId[2], 2, dpstData, v3); + return temp->volt[Index]; + } + } + else if (v1 == V_OPEN && v3 == V_OPEN) { + temp->volt[1] = VoltChange(temp->pinId[1], 1, dpstData, v0); + temp->volt[3] = VoltChange(temp->pinId[3], 3, dpstData, v2); + return (temp->volt[Index]); + } + else if (v0 == V_OPEN && v2 == V_OPEN) { + temp->volt[0] = VoltChange(temp->pinId[0], 0, dpstData, v1); + temp->volt[2] = VoltChange(temp->pinId[2], 2, dpstData, v3); + return (temp->volt[Index]); + } + } + else { + temp->volt[0] = VoltChange(temp->pinId[0], 0, dpstData, GND); + temp->volt[1] = VoltChange(temp->pinId[1], 1, dpstData, GND); + temp->volt[2] = VoltChange(temp->pinId[2], 2, dpstData, GND); + temp->volt[3] = VoltChange(temp->pinId[3], 3, dpstData, GND); + return (temp->volt[Index]); + } } } else @@ -242,6 +324,7 @@ double DPSTVoltChanged(void* dpstData, BOOL SimulationStarted, int Index, return 0; } +// Setting the pin Ids of each pin. void SetDPSTIds(int* id, void* ComponentAddress) { DPSTStruct *s = (DPSTStruct*)ComponentAddress; diff --git a/ldmicro/components/SPDT.cpp b/ldmicro/components/SPDT.cpp index a773ae0..1e22d44 100644 --- a/ldmicro/components/SPDT.cpp +++ b/ldmicro/components/SPDT.cpp @@ -330,7 +330,7 @@ double SPDTVoltChanged(void* spdtData, BOOL SimulationStarted, int Index, } else { //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); - temp->volt[0] = VoltChange(temp->pinId[0], 0, spdtData, v1); + temp->volt[1] = VoltChange(temp->pinId[1], 1, spdtData, v0); return (temp->volt[Index]); } } diff --git a/ldmicro/components/componentstructs.h b/ldmicro/components/componentstructs.h index abbabdb..9769e61 100644 --- a/ldmicro/components/componentstructs.h +++ b/ldmicro/components/componentstructs.h @@ -47,7 +47,6 @@ typedef struct DPST_switch_struct_Tag int image; // Image of the pin int pinId[4]; // Pin IDs to store 4 pins BOOL init_pos; // Initial open/closed position - BOOL closed; // Closed BOOL open; // Open double volt[4]; // Voltage at each pin BOOL latched; From 75a0d33365ada2d2ff40f3e3c6246eb2f3b3c0c3 Mon Sep 17 00:00:00 2001 From: NatsuDrag9 Date: Thu, 5 Apr 2018 13:50:57 +0530 Subject: [PATCH 3/5] Added temporary functionality in DPDT switch. Fixed minor bugs. --- ldmicro/components/DPDT.cpp | 4 +-- ldmicro/components/DPST.cpp | 50 +++++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/ldmicro/components/DPDT.cpp b/ldmicro/components/DPDT.cpp index 10a701c..a886e1b 100644 --- a/ldmicro/components/DPDT.cpp +++ b/ldmicro/components/DPDT.cpp @@ -5,7 +5,7 @@ #include #include "componentstructs.h" -//#include "componentfunctions.h" +#include "componentfunctions.h" #include "componentimages.h" #include "components.h" @@ -18,7 +18,7 @@ static HWND InNameTextbox_dpdt; static HWND OutNameTextbox_dpdt; HWND* DPDTDialog; -void DPDTStateChanged(SPDTStruct* SwitchData, void* ImageLocation); +void DPDTStateChanged(DPDTStruct* dpdtData, void* ImageLocation); void MakeSwitchControls() { HWND ActionGrouper = CreateWindowEx(0, WC_BUTTON, ("Action"), diff --git a/ldmicro/components/DPST.cpp b/ldmicro/components/DPST.cpp index 0b399c1..5178e9c 100644 --- a/ldmicro/components/DPST.cpp +++ b/ldmicro/components/DPST.cpp @@ -57,10 +57,10 @@ void DPSTLoadState(DPSTStruct* Data) { Button_SetCheck(LatchedRadio_dpst, BST_CHECKED); } - /*else + else { Button_SetCheck(TemporaryRadio_dpst, BST_CHECKED); - }*/ + } if (Data->init_pos) { Button_SetCheck(ClosedRadio_dpst, BST_CHECKED); @@ -78,10 +78,10 @@ BOOL SaveDPSTDialog(DPSTStruct* Data) { latched = TRUE; } - /*else if (Button_GetState(TemporaryRadio_dpst) == BST_CHECKED) + else if (Button_GetState(TemporaryRadio_dpst) == BST_CHECKED) { latched = FALSE; - }*/ + } else { MessageBox(*DPSTDialog, @@ -176,7 +176,7 @@ void DPSTUpdateValues(DPSTStruct* dpstData, void* ComponentAddress) temp->volt[3] = VoltChange(temp->pinId[3], 3, ComponentAddress, V_OPEN); } // Closed condition - else if (!temp->open) { + else { /*if ((v0 > v1) && (v2 > v3)) { temp->volt[1] = VoltChange(temp->pinId[1], 1, dpstData, v0); @@ -204,14 +204,14 @@ void DPSTUpdateValues(DPSTStruct* dpstData, void* ComponentAddress) temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, v3); } } - else if (v1 == V_OPEN && v3 == V_OPEN) { + /*else if (v1 == V_OPEN && v3 == V_OPEN) { temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, v0); temp->volt[3] = VoltChange(temp->pinId[3], 3, ComponentAddress, v2); } else if (v0 == V_OPEN && v2 == V_OPEN) { temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, v1); temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, v3); - } + }*/ } else { temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, GND); @@ -242,8 +242,44 @@ void HandleDPSTEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, SetImage(!temp->open ? DPST_switch_disconnected_1 : DPST_switch_connected_1, ImageLocation); RefreshImages(); + + // Updating the values according to the click. DPSTUpdateValues(temp, ComponentAddress); break; + + case EVENT_MOUSE_UP: + if (!temp->latched) + { + temp->open = temp->init_pos; + //temp->open = !temp->open; + /*else + { + temp->open = temp->init_pos; + }*/ + // Setting the image according to the click. + SetImage(!temp->open ? DPST_switch_disconnected_1 : DPST_switch_connected_1, + ImageLocation); + RefreshImages(); + } + + // Updating the values according to the click. + DPSTUpdateValues(temp, ComponentAddress); + break; + + case EVENT_MOUSE_DOWN: + if (!temp->latched) + { + temp->open = !temp->init_pos; + + // Updating the values according to the click. + DPSTUpdateValues(temp, ComponentAddress); + + // Setting the image according to the click. + SetImage(!temp->open ? DPST_switch_disconnected_1 : DPST_switch_connected_1, + ImageLocation); + RefreshImages(); + } + break; } } else From 767b55c0d0408bdbc3278f833075b87db3064c1b Mon Sep 17 00:00:00 2001 From: NatsuDrag9 Date: Thu, 5 Apr 2018 20:19:48 +0530 Subject: [PATCH 4/5] Updated the SPDT switch and fixed a few bugs. --- ldmicro/components/DPST.cpp | 30 +++- ldmicro/components/SPDT.cpp | 221 +++++++------------------- ldmicro/components/componentstructs.h | 2 +- 3 files changed, 89 insertions(+), 164 deletions(-) diff --git a/ldmicro/components/DPST.cpp b/ldmicro/components/DPST.cpp index 5178e9c..285013d 100644 --- a/ldmicro/components/DPST.cpp +++ b/ldmicro/components/DPST.cpp @@ -229,7 +229,31 @@ void HandleDPSTEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, if (SimulationStarted) { switch (Event) { - case EVENT_MOUSE_CLICK: + case EVENT_MOUSE_UP: + if (temp->latched) + { + temp->open = !temp->open; + } + else + { + temp->open = temp->init_pos; + } + SetImage(temp->open ? DPST_switch_disconnected_1 : DPST_switch_connected_1, + ImageLocation); + RefreshImages(); + DPSTUpdateValues(temp, ComponentAddress); + break; + case EVENT_MOUSE_DOWN: + if (!temp->latched) + { + temp->open = !temp->init_pos; + SetImage(temp->open ? DPST_switch_disconnected_1 : DPST_switch_connected_1, + ImageLocation); + RefreshImages(); + DPSTUpdateValues(temp, ComponentAddress); + } + break; + /*case EVENT_MOUSE_CLICK: if (temp->latched) { temp->open = !temp->open; @@ -255,7 +279,7 @@ void HandleDPSTEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, /*else { temp->open = temp->init_pos; - }*/ + } //End multicomment of else // Setting the image according to the click. SetImage(!temp->open ? DPST_switch_disconnected_1 : DPST_switch_connected_1, ImageLocation); @@ -279,7 +303,7 @@ void HandleDPSTEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, ImageLocation); RefreshImages(); } - break; + break;*/ } } else diff --git a/ldmicro/components/SPDT.cpp b/ldmicro/components/SPDT.cpp index 1e22d44..6dc7659 100644 --- a/ldmicro/components/SPDT.cpp +++ b/ldmicro/components/SPDT.cpp @@ -9,11 +9,10 @@ #include "componentimages.h" #include "components.h" -//static HWND TemporaryRadio; +static HWND TemporaryRadio_spdt; static HWND LatchedRadio_spdt; static HWND ClosedRadio_1_spdt; static HWND ClosedRadio_2_spdt; -//static HWND OpenRadio; static HWND NameTextbox_spdt; static HWND InNameTextbox_spdt; static HWND OutNameTextbox_spdt; @@ -31,20 +30,10 @@ void MakeSPDTControls() 16, 21, 100, 20, *SPDTDialog, NULL, NULL, NULL); FontNice(LatchedRadio_spdt); - /*TemporaryRadio = CreateWindowEx(0, WC_BUTTON, ("Temporary"), + TemporaryRadio_spdt = CreateWindowEx(0, WC_BUTTON, ("Temporary"), WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE, - 16, 41, 100, 20, *SwitchDialog, NULL, NULL, NULL); - FontNice(TemporaryRadio);*/ - - // SetOnlyRadio = CreateWindowEx(0, WC_BUTTON, _("(S) Set-Only"), - // WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE, - // 16, 61, 100, 20, CoilDialog, NULL, Instance, NULL); - // NiceFont(SetOnlyRadio); - - // ResetOnlyRadio = CreateWindowEx(0, WC_BUTTON, _("(R) Reset-Only"), - // WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE, - // 16, 81, 105, 20, CoilDialog, NULL, Instance, NULL); - // NiceFont(ResetOnlyRadio); + 16, 41, 100, 20, *SPDTDialog, NULL, NULL, NULL); + FontNice(TemporaryRadio_spdt); HWND PositionGrouper = CreateWindowEx(0, WC_BUTTON, ("Position"), WS_CHILD | BS_GROUPBOX | WS_VISIBLE, @@ -60,22 +49,18 @@ void MakeSPDTControls() WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE | WS_TABSTOP, 149, 41, 100, 20, *SPDTDialog, NULL, NULL, NULL); FontNice(ClosedRadio_2_spdt); - - /*NameTextbox_spdt = CreateWindowEx(WS_EX_CLIENTEDGE, WC_EDIT, "", - WS_CHILD | ES_AUTOHSCROLL | WS_TABSTOP | WS_CLIPSIBLINGS | WS_VISIBLE, - 155, 80, 155, 21, *SPDTDialog, NULL, NULL, NULL); - FontFixed(NameTextbox_spdt); - - /*PrevNameProc = SetWindowLongPtr(NameTextbox, GWLP_WNDPROC, - (LONG_PTR)MyNameProc);*/ } void SPDTLoadState(SPDTStruct* Data) { - if (Data->latch_1) + if (Data->latched) { Button_SetCheck(LatchedRadio_spdt, BST_CHECKED); } + else + { + Button_SetCheck(TemporaryRadio_spdt, BST_CHECKED); + } if (!Data->init_pos) { Button_SetCheck(ClosedRadio_2_spdt, BST_CHECKED); @@ -84,16 +69,18 @@ void SPDTLoadState(SPDTStruct* Data) { Button_SetCheck(ClosedRadio_1_spdt, BST_CHECKED); } - //Edit_SetText(NameTextbox_spdt, Data->Name); } BOOL SaveSPDTDialog(SPDTStruct* Data) { - BOOL latch_1, init_pos; + BOOL latched, init_pos; if (Button_GetState(LatchedRadio_spdt) == BST_CHECKED) { - latch_1 = TRUE; - + latched = TRUE; + } + else if (Button_GetState(TemporaryRadio_spdt) == BST_CHECKED) + { + latched = FALSE; } else { @@ -105,7 +92,6 @@ BOOL SaveSPDTDialog(SPDTStruct* Data) if (Button_GetState(ClosedRadio_1_spdt) == BST_CHECKED) { init_pos = TRUE; - } else if (Button_GetState(ClosedRadio_2_spdt) == BST_CHECKED) { @@ -117,9 +103,8 @@ BOOL SaveSPDTDialog(SPDTStruct* Data) ("Incomplete"), ("Warning"), MB_OK | MB_ICONWARNING); return FALSE; } - Data->latch_1 = latch_1; + Data->latched = latched; Data->init_pos = init_pos; - //Data->Open = NOpen; if (init_pos) { Data->image = SPDT_switch_connected_1; @@ -162,52 +147,36 @@ void SPDTUpdateValues(SPDTStruct* spdt, void* ComponentAddress) { SPDTStruct* temp = (SPDTStruct*)spdt; double v0, v1, v2; - /*if (temp->latch_1 == TRUE) - { - temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, GND); - temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, V_OPEN); - } - else - { - temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, V_OPEN); - temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, GND); - }*/ if (temp->latch_1 == TRUE) { v0 = VoltRequest(temp->pinId[0], ComponentAddress); v1 = VoltRequest(temp->pinId[1], ComponentAddress); + + // Updating the voltages according to priority order mentioned in the documentation. if (v0 != GND && v1 != GND) { if (v0 != V_OPEN && v1 != V_OPEN) { if (v0 > v1) { - //temp->volt[Index] = Volt; temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, v0); temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, V_OPEN); - //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); - //return temp->volt[Index]; } else { - //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, v1); temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, V_OPEN); - //return (temp->volt[Index]); } } else if (v1 == V_OPEN) { temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, v0); temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, V_OPEN); - //return (temp->volt[Index]); } else if (v0 == V_OPEN) { temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, v1); temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, V_OPEN); - //return (temp->volt[Index]); } } else { temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, GND); temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, GND); temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, V_OPEN); - //return (temp->volt[Index]); } } else if (temp->latch_1 == FALSE) { @@ -216,70 +185,29 @@ void SPDTUpdateValues(SPDTStruct* spdt, void* ComponentAddress) if (v0 != GND && v2 != GND) { if (v0 != V_OPEN && v2 != V_OPEN) { if (v0 > v2) { - //temp->volt[Index] = Volt; temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, v0); temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, V_OPEN); - //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); - //return temp->volt[2]; } else { - //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, v2); temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, V_OPEN); - //return (temp->volt[Index]); } } else if (v2 == V_OPEN) { temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, v0); temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, V_OPEN); - //return (temp->volt[Index]); } else if (v0 == V_OPEN) { temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, v2); temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, V_OPEN); - //return (temp->volt[Index]); } } else { temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, GND); temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, GND); temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, V_OPEN); - //return (temp->volt[Index]); } } - - // Storing real time values in each pin - /*double v0,v1,v2; - v0 = VoltRequest(spdt->pinId[0], ComponentAddress); - spdt->volt[0] = v0; - v1 = VoltRequest(spdt->pinId[1], ComponentAddress); - spdt->volt[1] = v1; - v2 = VoltRequest(spdt->pinId[2], ComponentAddress); - spdt->volt[2] = v2; - if (spdt->latch_1 == TRUE) { - - if (spdt->volt[0] > spdt->volt[1]) - { - spdt->volt[1] = VoltChange(spdt->pinId[0], 0, - ComponentAddress, spdt->volt[0]); - } - else if (spdt->volt[1] > spdt->volt[0]) - { - spdt->volt[0] = VoltChange(spdt->pinId[1], 1, - ComponentAddress, spdt->volt[1]); - } - else if (spdt->latch_1 == FALSE) { - if (spdt->volt[0] > spdt->volt[2]) - { - spdt->volt[2] = VoltChange(spdt->pinId[0], 0, - ComponentAddress, spdt->volt[0]); - } - else if (spdt->volt[2] > spdt->volt[0]){ - spdt->volt[0] = VoltChange(spdt->pinId[2], 1, - ComponentAddress, spdt->volt[2]); - } - } - }*/ } void HandleSPDTEvent(void * ComponentAddress, int Event, BOOL SimulationStarted, void * ImageLocation, UINT ImageId, HWND * h) @@ -288,14 +216,46 @@ void HandleSPDTEvent(void * ComponentAddress, int Event, BOOL SimulationStarted, if (SimulationStarted) { - switch (Event) { - case EVENT_MOUSE_CLICK: - temp->latch_1 = !temp->latch_1; - SetImage(temp->latch_1 ? SPDT_switch_connected_2 : SPDT_switch_connected_1, - ImageLocation); - RefreshImages(); - SPDTUpdateValues(temp, ComponentAddress); - break; + if (temp->latched) + { + switch (Event) { + case EVENT_MOUSE_UP: + temp->latch_1 = !temp->latch_1; + SetImage(temp->latch_1 ? SPDT_switch_connected_2 : SPDT_switch_connected_1, + ImageLocation); + RefreshImages(); + SPDTUpdateValues(temp, ComponentAddress); + break; + } + } + else + { + switch (Event) + { + case EVENT_MOUSE_DOWN: + temp->latch_1 = !temp->init_pos; + + // Updating the values according to the click. + SPDTUpdateValues(temp, ComponentAddress); + + // Setting the image according to the click. + SetImage(!temp->latch_1 ? SPDT_switch_connected_2 : SPDT_switch_connected_1, + ImageLocation); + RefreshImages(); + break; + + case EVENT_MOUSE_UP: + temp->latch_1 = temp->init_pos; + + // Setting the image according to the click. + SetImage(!temp->latch_1 ? SPDT_switch_connected_2 : SPDT_switch_connected_1, + ImageLocation); + RefreshImages(); + + // Updating the values according to the click. + SPDTUpdateValues(temp, ComponentAddress); + break; + } } } else @@ -320,16 +280,15 @@ double SPDTVoltChanged(void* spdtData, BOOL SimulationStarted, int Index, if (temp->latch_1 == TRUE) { v0 = VoltRequest(temp->pinId[0], spdtData); v1 = VoltRequest(temp->pinId[1], spdtData); + + // Changing the voltages according to the priority order mentioned. if (v0 != GND && v1 != GND) { if (v0 != V_OPEN && v1 != V_OPEN) { if (v0 > v1) { - //temp->volt[Index] = Volt; temp->volt[1] = VoltChange(temp->pinId[1], 1, spdtData, v0); - //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); return temp->volt[Index]; } else { - //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); temp->volt[1] = VoltChange(temp->pinId[1], 1, spdtData, v0); return (temp->volt[Index]); } @@ -355,13 +314,10 @@ double SPDTVoltChanged(void* spdtData, BOOL SimulationStarted, int Index, if (v0 != GND && v2 != GND) { if (v0 != V_OPEN && v2 != V_OPEN) { if (v0 > v2) { - //temp->volt[Index] = Volt; temp->volt[2] = VoltChange(temp->pinId[2], 2, spdtData, v0); - //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); return temp->volt[2]; } else { - //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); temp->volt[0] = VoltChange(temp->pinId[0], 0, spdtData, v2); return (temp->volt[Index]); } @@ -380,60 +336,6 @@ double SPDTVoltChanged(void* spdtData, BOOL SimulationStarted, int Index, temp->volt[2] = VoltChange(temp->pinId[2], 2, spdtData, GND); return (temp->volt[Index]); } - /*temp->volt[0] = VoltChange(temp->pinId[0], 0, spdtData, GND); - temp->volt[1] = VoltChange(temp->pinId[1], 1, spdtData, GND); - return (temp->volt[0]);*/ - /*if (Index == 1) { - Voltage = VoltRequest(temp->pinId[Index-1], spdtData); - // Equalizing the voltage at pins - if (Voltage > Volt) { - //temp->volt[Index] = Volt; - temp->volt[Index] = VoltChange(temp->pinId[Index-1], Index-1, spdtData, Voltage); - //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); - return temp->volt[Index-1]; - } - else if (Voltage < Volt) { - //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); - temp->volt[Index-1] = Volt; - return (temp->volt[Index]); - } - else if (Voltage == GND || Volt == GND) { - temp->volt[Index] = VoltChange(temp->pinId[Index-1], Index-1, spdtData, GND); - temp->volt[Index-1] = VoltChange(temp->pinId[Index-1], Index-1, spdtData, GND); - } - else if (Voltage ) - } - else if (Index == 2) { - Voltage = VoltRequest(temp->pinId[Index-2], spdtData); - // Equalizing the voltage at pins - if (Voltage > Volt) { - //temp->volt[Index] = Volt; - temp->volt[Index] = VoltChange(temp->pinId[Index-2], Index-2, spdtData, Voltage); - //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); - return temp->volt[Index]; - } - else { - //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); - temp->volt[Index-2] = Volt; - return (temp->volt[Index]); - } - } - else { - if (temp->latch_1 == TRUE) { - Voltage = VoltRequest(temp->pinId[Index+1], spdtData); - if (Voltage > Volt) { - //temp->volt[Index] = Volt; - temp->volt[Index] = VoltChange(temp->pinId[Index+1], Index+1, spdtData, Voltage); - //temp->volt[Index + 2] = VoltChange(temp->pinId[Index + 2], Index + 2, spdtData, Volt); - return temp->volt[Index]; - } - else { - //Voltage = VoltChange(temp->pinId[Index + 1], Index + 1, spdtData, Volt); - temp->volt[Index+1] = Volt; - return (temp->volt[Index]); - } - } - }*/ } else { @@ -449,8 +351,7 @@ int Init_SPDT(void * ComponentAddress) SPDTStruct* temp = (SPDTStruct*)ComponentAddress; temp->image = SPDT_switch_connected_1; temp->latch_1 = TRUE; - //temp->latch_2 = FALSE; - //temp->open = TRUE; + temp->latched = TRUE; temp->init_pos = TRUE; temp->volt[0] = V_OPEN; temp->volt[1] = V_OPEN; diff --git a/ldmicro/components/componentstructs.h b/ldmicro/components/componentstructs.h index 9769e61..3888e9b 100644 --- a/ldmicro/components/componentstructs.h +++ b/ldmicro/components/componentstructs.h @@ -36,7 +36,7 @@ typedef struct SPDT_switch_struct_Tag int pinId[3]; // Pin IDs to store 3 pins BOOL init_pos; // Initial open/closed position BOOL latch_1; // Latched to the output pin 1 - //BOOL latch_2; // Latched to the output pin 2 + BOOL latched; // Latching/temporary action double volt[3]; // Voltage at each pin char name[15]; }SPDTStruct; From a77980a1e291c75f1b52127823d6d6338fe95159 Mon Sep 17 00:00:00 2001 From: NatsuDrag9 Date: Thu, 5 Apr 2018 20:37:14 +0530 Subject: [PATCH 5/5] Updated DPST and SPDT switches and finalized them. --- ldmicro/components/DPST.cpp | 106 +++++++------------------- ldmicro/components/SPDT.cpp | 32 ++++---- ldmicro/components/componentstructs.h | 4 +- 3 files changed, 48 insertions(+), 94 deletions(-) diff --git a/ldmicro/components/DPST.cpp b/ldmicro/components/DPST.cpp index 285013d..585db19 100644 --- a/ldmicro/components/DPST.cpp +++ b/ldmicro/components/DPST.cpp @@ -164,10 +164,14 @@ void DPSTUpdateValues(DPSTStruct* dpstData, void* ComponentAddress) { DPSTStruct* temp = (DPSTStruct*)dpstData; double v0, v1, v2, v3; + + // Fetching the voltages at each pin. v0 = VoltRequest(temp->pinId[0], ComponentAddress); v1 = VoltRequest(temp->pinId[1], ComponentAddress); v2 = VoltRequest(temp->pinId[2], ComponentAddress); v3 = VoltRequest(temp->pinId[3], ComponentAddress); + + // Updating the voltages according to priority order mentioned in the documentation. if (temp->open) // Open condition. { temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, V_OPEN); @@ -177,20 +181,6 @@ void DPSTUpdateValues(DPSTStruct* dpstData, void* ComponentAddress) } // Closed condition else { - /*if ((v0 > v1) && (v2 > v3)) - { - temp->volt[1] = VoltChange(temp->pinId[1], 1, dpstData, v0); - temp->volt[3] = VoltChange(temp->pinId[3], 3, dpstData, v2); - temp->volt[2] = VoltChange(temp->pinId[2], 2, dpstData, V_OPEN); - temp->volt[0] = VoltChange(temp->pinId[0], 0, dpstData, V_OPEN); - } - else - { - temp->volt[0] = VoltChange(temp->pinId[0], 0, dpstData, v1); - temp->volt[2] = VoltChange(temp->pinId[2], 2, dpstData, v3); - temp->volt[3] = VoltChange(temp->pinId[3], 3, dpstData, V_OPEN); - temp->volt[1] = VoltChange(temp->pinId[1], 1, dpstData, V_OPEN); - }*/ if ((v0 != GND && v1 != GND) && (v2 != GND && v3 != GND)) { if ((v0 != V_OPEN && v1 != V_OPEN) && (v2 != V_OPEN && v3 != V_OPEN)) { if ((v0 > v1) && (v2 > v3)) @@ -204,14 +194,6 @@ void DPSTUpdateValues(DPSTStruct* dpstData, void* ComponentAddress) temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, v3); } } - /*else if (v1 == V_OPEN && v3 == V_OPEN) { - temp->volt[1] = VoltChange(temp->pinId[1], 1, ComponentAddress, v0); - temp->volt[3] = VoltChange(temp->pinId[3], 3, ComponentAddress, v2); - } - else if (v0 == V_OPEN && v2 == V_OPEN) { - temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, v1); - temp->volt[2] = VoltChange(temp->pinId[2], 2, ComponentAddress, v3); - }*/ } else { temp->volt[0] = VoltChange(temp->pinId[0], 0, ComponentAddress, GND); @@ -228,82 +210,46 @@ void HandleDPSTEvent(void* ComponentAddress, int Event, BOOL SimulationStarted, DPSTStruct *temp = (DPSTStruct*)ComponentAddress; if (SimulationStarted) { - switch (Event) { - case EVENT_MOUSE_UP: - if (temp->latched) - { + if (temp->latched) + { + switch (Event) { + case EVENT_MOUSE_UP: temp->open = !temp->open; - } - else - { - temp->open = temp->init_pos; - } - SetImage(temp->open ? DPST_switch_disconnected_1 : DPST_switch_connected_1, - ImageLocation); - RefreshImages(); - DPSTUpdateValues(temp, ComponentAddress); - break; - case EVENT_MOUSE_DOWN: - if (!temp->latched) - { - temp->open = !temp->init_pos; SetImage(temp->open ? DPST_switch_disconnected_1 : DPST_switch_connected_1, ImageLocation); RefreshImages(); DPSTUpdateValues(temp, ComponentAddress); + break; } - break; - /*case EVENT_MOUSE_CLICK: - if (temp->latched) - { - temp->open = !temp->open; - } - else + } + else + { + switch (Event) { - temp->open = temp->init_pos; - } - // Setting the image according to the click. - SetImage(!temp->open ? DPST_switch_disconnected_1 : DPST_switch_connected_1, - ImageLocation); - RefreshImages(); + case EVENT_MOUSE_DOWN: + temp->open = !temp->init_pos; - // Updating the values according to the click. - DPSTUpdateValues(temp, ComponentAddress); - break; + // Updating the values according to the click. + DPSTUpdateValues(temp, ComponentAddress); - case EVENT_MOUSE_UP: - if (!temp->latched) - { - temp->open = temp->init_pos; - //temp->open = !temp->open; - /*else - { - temp->open = temp->init_pos; - } //End multicomment of else - // Setting the image according to the click. + // Setting the image according to the click. SetImage(!temp->open ? DPST_switch_disconnected_1 : DPST_switch_connected_1, ImageLocation); RefreshImages(); - } - - // Updating the values according to the click. - DPSTUpdateValues(temp, ComponentAddress); - break; - - case EVENT_MOUSE_DOWN: - if (!temp->latched) - { - temp->open = !temp->init_pos; + break; - // Updating the values according to the click. - DPSTUpdateValues(temp, ComponentAddress); + case EVENT_MOUSE_UP: + temp->open = temp->init_pos; // Setting the image according to the click. SetImage(!temp->open ? DPST_switch_disconnected_1 : DPST_switch_connected_1, ImageLocation); RefreshImages(); + + // Updating the values according to the click. + DPSTUpdateValues(temp, ComponentAddress); + break; } - break;*/ } } else @@ -331,6 +277,8 @@ double DPSTVoltChanged(void* dpstData, BOOL SimulationStarted, int Index, v1 = VoltRequest(temp->pinId[1], dpstData); v2 = VoltRequest(temp->pinId[2], dpstData); v3 = VoltRequest(temp->pinId[3], dpstData); + + // Changing the voltages according to the priority order mentioned. if (temp->open) // Open condition. { temp->volt[0] = VoltChange(temp->pinId[0], 0, dpstData, V_OPEN); diff --git a/ldmicro/components/SPDT.cpp b/ldmicro/components/SPDT.cpp index 6dc7659..fd5b809 100644 --- a/ldmicro/components/SPDT.cpp +++ b/ldmicro/components/SPDT.cpp @@ -9,6 +9,7 @@ #include "componentimages.h" #include "components.h" +// Declaring the required components in UI. static HWND TemporaryRadio_spdt; static HWND LatchedRadio_spdt; static HWND ClosedRadio_1_spdt; @@ -18,6 +19,7 @@ static HWND InNameTextbox_spdt; static HWND OutNameTextbox_spdt; HWND* SPDTDialog; +// UI for the SPDT switch. void MakeSPDTControls() { HWND ActionGrouper = CreateWindowEx(0, WC_BUTTON, ("Action"), @@ -116,6 +118,20 @@ BOOL SaveSPDTDialog(SPDTStruct* Data) return TRUE; } +// Initializing the required variables. +int Init_SPDT(void * ComponentAddress) +{ + SPDTStruct* temp = (SPDTStruct*)ComponentAddress; + temp->image = SPDT_switch_connected_1; + temp->latch_1 = TRUE; // Connection between pin 1 and common pin + temp->latched = TRUE; // Varibale for latching action + temp->init_pos = TRUE; + temp->volt[0] = V_OPEN; + temp->volt[1] = V_OPEN; + temp->volt[2] = V_OPEN; + return SPDT_switch_connected_1; +} + void ShowSPDTDialog(void* ComponentAddress) { SPDTStruct* Data = (SPDTStruct*)ComponentAddress; @@ -149,6 +165,7 @@ void SPDTUpdateValues(SPDTStruct* spdt, void* ComponentAddress) double v0, v1, v2; if (temp->latch_1 == TRUE) { + // Fetching the voltages at each pin. v0 = VoltRequest(temp->pinId[0], ComponentAddress); v1 = VoltRequest(temp->pinId[1], ComponentAddress); @@ -278,6 +295,8 @@ double SPDTVoltChanged(void* spdtData, BOOL SimulationStarted, int Index, { double v0, v1, v2; if (temp->latch_1 == TRUE) { + + // Fetching the voltages at each pin. v0 = VoltRequest(temp->pinId[0], spdtData); v1 = VoltRequest(temp->pinId[1], spdtData); @@ -346,19 +365,6 @@ double SPDTVoltChanged(void* spdtData, BOOL SimulationStarted, int Index, } } -int Init_SPDT(void * ComponentAddress) -{ - SPDTStruct* temp = (SPDTStruct*)ComponentAddress; - temp->image = SPDT_switch_connected_1; - temp->latch_1 = TRUE; - temp->latched = TRUE; - temp->init_pos = TRUE; - temp->volt[0] = V_OPEN; - temp->volt[1] = V_OPEN; - temp->volt[2] = V_OPEN; - return SPDT_switch_connected_1; -} - void SetSPDTIds(int *id, void * ComponentAddress) { SPDTStruct *s = (SPDTStruct*)ComponentAddress; diff --git a/ldmicro/components/componentstructs.h b/ldmicro/components/componentstructs.h index 3888e9b..a746639 100644 --- a/ldmicro/components/componentstructs.h +++ b/ldmicro/components/componentstructs.h @@ -35,8 +35,8 @@ typedef struct SPDT_switch_struct_Tag int image; // Image of the pin int pinId[3]; // Pin IDs to store 3 pins BOOL init_pos; // Initial open/closed position - BOOL latch_1; // Latched to the output pin 1 - BOOL latched; // Latching/temporary action + BOOL latch_1; // Connection between pin 1 and common pin + BOOL latched; // Varibale for latching/temporary action double volt[3]; // Voltage at each pin char name[15]; }SPDTStruct;