Skip to content

Conversation

@vj0305
Copy link

@vj0305 vj0305 commented Apr 6, 2018

#include <wincodec.h>
#include <stdio.h>

#include <Windowsx.h>

#include "componentstructs.h"
#include "componentfunctions.h"
#include "componentimages.h"
#include "components.h"

static HWND DPDTState1;
static HWND DPDTState2;
static HWND ModeLatchedDPDT;
static HWND ModeTempDPDT;
HWND* SettingsDialogDPDT;

enum DPDT_Pins { in1 = 0, in2, out11, out12, out21, out22 };

int InitDpdt(void * ComponentAddress)
{
DpdtStruct* d = (DpdtStruct*)ComponentAddress;
d->image = DPDT_1;
d->NS1 = TRUE;
d->latched = TRUE;
d->Volt[in1] = V_OPEN;
d->Volt[in2] = V_OPEN;
d->Volt[out11] = V_OPEN;
d->Volt[out12] = V_OPEN;
d->Volt[out21] = V_OPEN;
d->Volt[out22] = V_OPEN;

return DPDT_1;

}

void SetDpdtIds(int* id, void* ComponentAddress)
{
DpdtStruct* d = (DpdtStruct*)ComponentAddress;
d->PinId[in1] = *id++;
d->PinId[in2] = *id++;
d->PinId[out11] = *id++;
d->PinId[out12] = *id++;
d->PinId[out21] = *id++;
d->PinId[out22] = *id++;
}

void MakeSettingsDialogDPDT()
{

HWND InitLatched = CreateWindowEx(0, WC_BUTTON, ("Action mode"),
	WS_CHILD | BS_GROUPBOX | WS_VISIBLE | WS_TABSTOP,
	7, 3, 120, 65, *SettingsDialogDPDT, NULL, NULL, NULL);
FontNice(InitLatched);

ModeLatchedDPDT = CreateWindowEx(0, WC_BUTTON, ("Latched"),
	WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE | WS_GROUP,
	16, 21, 100, 20, *SettingsDialogDPDT, NULL, NULL, NULL);
FontNice(ModeLatchedDPDT);

ModeTempDPDT = CreateWindowEx(0, WC_BUTTON, ("Temporary"),
	WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE,
	16, 41, 100, 20, *SettingsDialogDPDT, NULL, NULL, NULL);
FontNice(ModeTempDPDT);


HWND InitOut = CreateWindowEx(0, WC_BUTTON, ("Initial output state"),
	WS_CHILD | BS_GROUPBOX | WS_VISIBLE | WS_TABSTOP,
	140, 3, 120, 65, *SettingsDialogDPDT, NULL, NULL, NULL);
FontNice(InitOut);

DPDTState1 = CreateWindowEx(0, WC_BUTTON, ("State 1"),
	WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE | WS_GROUP,
	149, 21, 100, 20, *SettingsDialogDPDT, NULL, NULL, NULL);
FontNice(DPDTState1);

DPDTState2 = CreateWindowEx(0, WC_BUTTON, ("State 2"),
	WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP | WS_VISIBLE,
	149, 41, 100, 20, *SettingsDialogDPDT, NULL, NULL, NULL);
FontNice(DPDTState2);

}

void LoadSettings(DpdtStruct* d)
{
if (d->latched)
Button_SetCheck(ModeLatchedDPDT, BST_CHECKED);
else
Button_SetCheck(ModeTempDPDT, BST_CHECKED);
if (d->NS1)
Button_SetCheck(DPDTState1, BST_CHECKED);
else
Button_SetCheck(DPDTState2, BST_CHECKED);
}

BOOL SaveSettings(DpdtStruct* d, void* ImageLocation)
{
if (Button_GetState(ModeLatchedDPDT) == BST_CHECKED)
d->latched = TRUE;
else
d->latched = FALSE;

if (Button_GetState(DPDTState1) == BST_CHECKED)
	d->NS1 = TRUE;
else if (Button_GetState(DPDTState2) == BST_CHECKED)
	d->NS1 = FALSE;
else
{
	MessageBox(*SettingsDialogDPDT,
		("Incomplete"), ("Warning"), MB_OK | MB_ICONWARNING);
	return FALSE;
}

if (d->NS1)
	d->image = DPDT_1;
else
	d->image = DPDT_2;

SetImage(d->image, ImageLocation);
int RefreshImages();

return TRUE;

}

void DpdtSettingsDialog(void* ComponentAddress, void* ImageLocation)
{
DpdtStruct* d = (DpdtStruct*)ComponentAddress;
BOOL exitStatus;

SettingsDialogDPDT = CreateDialogWindow("SPDT Settings Dialog", 100, 100, 263, 145, STYLE_VERTICAL);


MakeSettingsDialogDPDT();


LoadSettings(d);

ShowDialogWindow();

exitStatus = ProcessDialogWindow();
while (exitStatus == FALSE)
{
	exitStatus = SaveSettings(d, ImageLocation);
	if (exitStatus == TRUE)
		break;
	else
	{
		exitStatus = TRUE;
		exitStatus = ProcessDialogWindow();
	}
}

DestroyWindow(*SettingsDialogDPDT);

}

double EqualiseRuntimeVoltageDPDT(void* ComponentAdderss, int index, double volt)
{
DpdtStruct* d = (DpdtStruct*)ComponentAdderss;

if (d->NS1)
{

	if (index == out12)
	{
		d->Volt[out12] = V_OPEN;
		d->Volt[out22] = VoltChange(d->PinId[out22], out22, ComponentAdderss, V_OPEN);
	}
	else if (index == out22)
	{
		d->Volt[out12] = VoltChange(d->PinId[out12], out12, ComponentAdderss, V_OPEN);
		d->Volt[out22] = V_OPEN;
	}
	else
	{
		d->Volt[out12] = VoltChange(d->PinId[out12], out12, ComponentAdderss, V_OPEN);
		d->Volt[out22] = VoltChange(d->PinId[out22], out22, ComponentAdderss, V_OPEN);
	}

	double Vin;
	double Vout;


	if (index == in1)
	{
		Vin = volt;
		Vout = VoltRequest(d->PinId[out11], ComponentAdderss);
	}
	else if (index == out11)
	{
		Vin = VoltRequest(d->PinId[in1], ComponentAdderss);
		Vout = volt;
	}
	else
	{
		Vin = VoltRequest(d->PinId[in1], ComponentAdderss);
		Vout = VoltRequest(d->PinId[out11], ComponentAdderss);
	}


	if (Vin == GND || Vout == GND)
	{
		d->Volt[out11] = VoltChange(d->PinId[out11], out11, ComponentAdderss, GND);
		d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, GND);
	}

	else if (Vin == V_OPEN)
		d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, Vout);

	else if (Vout == V_OPEN)
		d->Volt[out11] = VoltChange(d->PinId[out11], out11, ComponentAdderss, Vin);

	else
	{
		d->Volt[out11] = VoltChange(d->PinId[out11], out11, ComponentAdderss, max(Vin, Vout));
		d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, max(Vin, Vout));
	}


	if (index == in2)
	{
		Vin = volt;
		Vout = VoltRequest(d->PinId[out21], ComponentAdderss);
	}
	else if (index == out21)
	{
		Vin = VoltRequest(d->PinId[in2], ComponentAdderss);
		Vout = volt;
	}
	else
	{
		Vin = VoltRequest(d->PinId[in2], ComponentAdderss);
		Vout = VoltRequest(d->PinId[out21], ComponentAdderss);
	}


	if (Vin == GND || Vout == GND)
	{
		d->Volt[out21] = VoltChange(d->PinId[out21], out21, ComponentAdderss, GND);
		d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, GND);
	}

	else if (Vin == V_OPEN)
		d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, Vout);

	else if (Vout == V_OPEN)
		d->Volt[out21] = VoltChange(d->PinId[out21], out21, ComponentAdderss, Vin);

	else
	{
		d->Volt[out21] = VoltChange(d->PinId[out21], out21, ComponentAdderss, max(Vin, Vout));
		d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, max(Vin, Vout));
	}
}

else
{

	if (index == out11)
	{
		d->Volt[out11] = V_OPEN;
		d->Volt[out21] = VoltChange(d->PinId[out21], out21, ComponentAdderss, V_OPEN);
	}
	else if (index == out21)
	{
		d->Volt[out11] = VoltChange(d->PinId[out11], out11, ComponentAdderss, V_OPEN);
		d->Volt[out21] = V_OPEN;
	}
	else
	{
		d->Volt[out11] = VoltChange(d->PinId[out11], out11, ComponentAdderss, V_OPEN);
		d->Volt[out21] = VoltChange(d->PinId[out21], out21, ComponentAdderss, V_OPEN);
	}

	double Vin;
	double Vout;


	if (index == in1)
	{
		Vin = volt;
		Vout = VoltRequest(d->PinId[out12], ComponentAdderss);
	}
	else if (index == out12)
	{
		Vin = VoltRequest(d->PinId[in1], ComponentAdderss);
		Vout = volt;
	}
	else
	{
		Vin = VoltRequest(d->PinId[in1], ComponentAdderss);
		Vout = VoltRequest(d->PinId[out12], ComponentAdderss);
	}


	if (Vin == GND || Vout == GND)
	{
		d->Volt[out12] = VoltChange(d->PinId[out12], out12, ComponentAdderss, GND);
		d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, GND);
	}

	else if (Vin == V_OPEN)
		d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, Vout);

	else if (Vout == V_OPEN)
		d->Volt[out12] = VoltChange(d->PinId[out12], out12, ComponentAdderss, Vin);

	else
	{
		d->Volt[out12] = VoltChange(d->PinId[out12], out12, ComponentAdderss, max(Vin, Vout));
		d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, max(Vin, Vout));
	}

	if (index == in2)
	{
		Vin = volt;
		Vout = VoltRequest(d->PinId[out22], ComponentAdderss);
	}
	else if (index == out22)
	{
		Vin = VoltRequest(d->PinId[in2], ComponentAdderss);
		Vout = volt;
	}
	else
	{
		Vin = VoltRequest(d->PinId[in2], ComponentAdderss);
		Vout = VoltRequest(d->PinId[out22], ComponentAdderss);
	}
	

	if (Vin == GND || Vout == GND)
	{
		d->Volt[out22] = VoltChange(d->PinId[out22], out22, ComponentAdderss, GND);
		d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, GND);
	}

	else if (Vin == V_OPEN)
		d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, Vout);

	else if (Vout == V_OPEN)
		d->Volt[out22] = VoltChange(d->PinId[out22], out22, ComponentAdderss, Vin);

	else
	{
		d->Volt[out22] = VoltChange(d->PinId[out22], out22, ComponentAdderss, max(Vin, Vout));
		d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, max(Vin, Vout));
	}
}

return d->Volt[index];

}

void EqualiseStaticVoltageDPDT(void* ComponentAdderss)
{
DpdtStruct* d = (DpdtStruct*)ComponentAdderss;

if (d->NS1)
{

	d->Volt[out12] = VoltChange(d->PinId[out12], out12, ComponentAdderss, V_OPEN);
	d->Volt[out22] = VoltChange(d->PinId[out22], out22, ComponentAdderss, V_OPEN);


	double Vin = VoltRequest(d->PinId[in1], ComponentAdderss);
	double Vout = VoltRequest(d->PinId[out11], ComponentAdderss);


	if (Vin == GND || Vout == GND)
	{
		d->Volt[out11] = VoltChange(d->PinId[out11], out11, ComponentAdderss, GND);
		d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, GND);
	}

	else if (Vin == V_OPEN)
		d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, Vout);

	else if (Vout == V_OPEN)
		d->Volt[out11] = VoltChange(d->PinId[out11], out11, ComponentAdderss, Vin);

	else
	{
		d->Volt[out11] = VoltChange(d->PinId[out11], out11, ComponentAdderss, max(Vin, Vout));
		d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, max(Vin, Vout));
	}

	Vin = VoltRequest(d->PinId[in2], ComponentAdderss);
	Vout = VoltRequest(d->PinId[out21], ComponentAdderss);

	if (Vin == GND || Vout == GND)
	{
		d->Volt[out21] = VoltChange(d->PinId[out21], out21, ComponentAdderss, GND);
		d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, GND);
	}

	else if (Vin == V_OPEN)
		d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, Vout);

	else if (Vout == V_OPEN)
		d->Volt[out21] = VoltChange(d->PinId[out21], out21, ComponentAdderss, Vin);

	else
	{
		d->Volt[out21] = VoltChange(d->PinId[out21], out21, ComponentAdderss, max(Vin, Vout));
		d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, max(Vin, Vout));
	}
}
else
{

	d->Volt[out11] = VoltChange(d->PinId[out11], out11, ComponentAdderss, V_OPEN);
	d->Volt[out21] = VoltChange(d->PinId[out21], out21, ComponentAdderss, V_OPEN);


	double Vin = VoltRequest(d->PinId[in1], ComponentAdderss);
	double Vout = VoltRequest(d->PinId[out12], ComponentAdderss);


	if (Vin == GND || Vout == GND)
	{
		d->Volt[out12] = VoltChange(d->PinId[out12], out12, ComponentAdderss, GND);
		d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, GND);
	}

	else if (Vin == V_OPEN)
		d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, Vout);

	else if (Vout == V_OPEN)
		d->Volt[out12] = VoltChange(d->PinId[out12], out12, ComponentAdderss, Vin);

	else
	{
		d->Volt[out12] = VoltChange(d->PinId[out12], out12, ComponentAdderss, max(Vin, Vout));
		d->Volt[in1] = VoltChange(d->PinId[in1], in1, ComponentAdderss, max(Vin, Vout));
	}

	Vin = VoltRequest(d->PinId[in2], ComponentAdderss);
	Vout = VoltRequest(d->PinId[out22], ComponentAdderss);

	if (Vin == GND || Vout == GND)
	{
		d->Volt[out22] = VoltChange(d->PinId[out22], out22, ComponentAdderss, GND);
		d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, GND);
	}

	else if (Vin == V_OPEN)
		d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, Vout);

	else if (Vout == V_OPEN)
		d->Volt[out22] = VoltChange(d->PinId[out22], out22, ComponentAdderss, Vin);

	else
	{
		d->Volt[out22] = VoltChange(d->PinId[out22], out22, ComponentAdderss, max(Vin, Vout));
		d->Volt[in2] = VoltChange(d->PinId[in2], in2, ComponentAdderss, max(Vin, Vout));
	}
}

}

void ToggleState(DpdtStruct* d, void* ImageLocation)
{
d->image = (d->image == DPDT_1) ? DPDT_2 : DPDT_1;
d->NS1 = (d->NS1 == TRUE) ? FALSE : TRUE;
SetImage(d->image, ImageLocation);
RefreshImages();
}

void HandleDpdtEvent(void * ComponentAddress, int Event, BOOL SimulationStarted, void * ImageLocation, UINT ImageId, HWND * h)
{

DpdtStruct* d = (DpdtStruct*)ComponentAddress;

if (SimulationStarted)
{
	switch (Event)
	{
	case EVENT_BUTTON_DOWN:
		ToggleState(d, ImageLocation);
		EqualiseStaticVoltageDPDT(ComponentAddress);
		break;
	case EVENT_BUTTON_UP:
		if (!d->latched)
		{
			ToggleState(d, ImageLocation);
			EqualiseStaticVoltageDPDT(ComponentAddress);
		}
		break;
	default:
		break;
	}
}
else
{
	switch (Event)
	{
	case EVENT_CLICK:
		DpdtSettingsDialog(ComponentAddress, ImageLocation);
		break;
	default:
		break;
	}
}

}

double DpdtVoltChanged(void * ComponentAddress, BOOL SimulationStarted, int index, double Volt, int Source, void * ImageLocation)
{

if (SimulationStarted)
	return EqualiseRuntimeVoltageDPDT(ComponentAddress, index, Volt);
	
return Volt;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant