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..4695f09 --- /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);*/ +} diff --git a/ldmicro/components/DPST.cpp b/ldmicro/components/DPST.cpp new file mode 100644 index 0000000..e0e6257 --- /dev/null +++ b/ldmicro/components/DPST.cpp @@ -0,0 +1,335 @@ +#include +#include +//#include +#include +#include + +#include "componentstructs.h" +#include "componentfunctions.h" +#include "componentimages.h" +#include "components.h" + +// Declaring the required components in UI. +static HWND TemporaryRadio_dpst; +static HWND LatchedRadio_dpst; +static HWND ClosedRadio_dpst; +static HWND OpenRadio_dpst; +static HWND OutNameTextbox_dpst; +HWND* DPSTDialog; + +// UI for the DPST switch. +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); + + 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); + 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); + } + /*else + { + Button_SetCheck(TemporaryRadio_dpst, BST_CHECKED); + }*/ + if (Data->init_pos) + { + Button_SetCheck(ClosedRadio_dpst, BST_CHECKED); + } + else + { + Button_SetCheck(OpenRadio_dpst, BST_CHECKED); + } +} + +BOOL SaveDPSTDialog(DPSTStruct* Data) +{ + BOOL latched, init_pos; + if (Button_GetState(LatchedRadio_dpst) == BST_CHECKED) + { + latched = TRUE; + } + /*else if (Button_GetState(TemporaryRadio_dpst) == BST_CHECKED) + { + latched = FALSE; + }*/ + 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; + if (init_pos) + { + Data->image = DPST_switch_connected_1; + } + else + { + Data->image = DPST_switch_disconnected_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); +} + +// Initializing the required variables. +int Init_DPST(void* ComponentAddress) +{ + DPSTStruct* temp = (DPSTStruct*)ComponentAddress; + temp->image = DPST_switch_disconnected_1; + temp->latched = TRUE; // Varibale for latching action + 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; +} + +// 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_CLICK: + if (temp->latched) + { + 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(); + DPSTUpdateValues(temp, ComponentAddress); + break; + } + } + else + { + switch (Event) { + case EVENT_MOUSE_DBLCLICK: + ShowDPSTDialog(ComponentAddress); + SetImage(temp->image, ImageLocation); + RefreshImages(); + break; + } + } +} + +// 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) // 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); + 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 + { + temp->volt[Index] = Volt; + return Volt; + } + return 0; +} + +// Setting the pin Ids of each pin. +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; +} diff --git a/ldmicro/components/SPDT.cpp b/ldmicro/components/SPDT.cpp new file mode 100644 index 0000000..e5091b6 --- /dev/null +++ b/ldmicro/components/SPDT.cpp @@ -0,0 +1,389 @@ +#include +#include +//#include +#include +#include + +#include "componentstructs.h" +#include "componentfunctions.h" +#include "componentimages.h" +#include "components.h" + +// Declaring the required components in UI. +static HWND TemporaryRadio_dpst; +static HWND LatchedRadio_dpst; +static HWND ClosedRadio_dpst; +static HWND OpenRadio_dpst; +static HWND OutNameTextbox_dpst; +HWND* DPSTDialog; + +// UI for the DPST switch. +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); + + 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); + 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); + } + else + { + Button_SetCheck(TemporaryRadio_dpst, BST_CHECKED); + } + if (Data->init_pos) + { + Button_SetCheck(ClosedRadio_dpst, BST_CHECKED); + } + else + { + Button_SetCheck(OpenRadio_dpst, BST_CHECKED); + } +} + +BOOL SaveDPSTDialog(DPSTStruct* Data) +{ + BOOL latched, init_pos; + if (Button_GetState(LatchedRadio_dpst) == BST_CHECKED) + { + latched = TRUE; + } + else if (Button_GetState(TemporaryRadio_dpst) == BST_CHECKED) + { + latched = FALSE; + } + 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; + if (init_pos) + { + Data->image = DPST_switch_connected_1; + } + else + { + Data->image = DPST_switch_disconnected_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); +} + +// Initializing the required variables. +int Init_DPST(void* ComponentAddress) +{ + DPSTStruct* temp = (DPSTStruct*)ComponentAddress; + temp->image = DPST_switch_disconnected_1; + temp->latched = TRUE; // Varibale for latching action + 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; +} + +// 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 ((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) + { + 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; + } + 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_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. + 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 + { + switch (Event) { + case EVENT_MOUSE_DBLCLICK: + ShowDPSTDialog(ComponentAddress); + SetImage(temp->image, ImageLocation); + RefreshImages(); + break; + } + } +} + +// 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) // 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); + 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 + { + temp->volt[Index] = Volt; + return Volt; + } + return 0; +} + +// Setting the pin Ids of each pin. +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; +} diff --git a/ldmicro/components/SPDT_Test.cpp b/ldmicro/components/SPDT_Test.cpp new file mode 100644 index 0000000..7c4551c --- /dev/null +++ b/ldmicro/components/SPDT_Test.cpp @@ -0,0 +1,518 @@ +#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);*/ +} 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..40d7374 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; @@ -456,4 +502,4 @@ double VoltRequest(int PinId, void* ComponentAddress) double VoltChange(int PinId, int Index, void* ComponentAddress, double Volt) { return RequestVoltChange(PinId, Index, ComponentAddress, Volt); -} \ No newline at end of file +} diff --git a/ldmicro/components/componentstructs.h b/ldmicro/components/componentstructs.h index a7baca5..a746639 100644 --- a/ldmicro/components/componentstructs.h +++ b/ldmicro/components/componentstructs.h @@ -29,4 +29,41 @@ 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; // 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; + +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 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..649549a 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"