/*
	LCHRELAY.C

	This program should be run on a PK2400 with latching relays. The
	program closes and opens the two latching relays, pausing for
	the user's keypress (on the PC keyboard) between operations. Note
	that the relays are specified to require 2ms to actuate. Each tick
	is 0.78125ms, so three ticks are sufficient for the relay to
	actuate.

	The truth table of the latching relay is as follows:

	OUT4|OUT6|NO2|NC2
			  or
	OUT5|OUT7|NO1|NC1		comments:
	----+----+---+---
	0   |0   |X  |X		no state change, requires least amount of energy
	1   |0   |C  |O		closes NO1 and opens NC1, draws 4mA
	0   |1   |O  |C		opens NO1 and closes NC1, draws 4mA
	1   |1   |X  |X		unpredictable, requires 8mA, useless state!!

	The best way to use a latching relay is to enable OUTx only when a
	state change is needed. After the state change (takes at most 2ms),
	disable OUTx again to conserve power. For instance, to close NO1,
	do the following:

	disable OUT7
	enable OUT5
	wait for at least 2ms
	disable OUT5

	A final reminder: a latching relay LATCHES! You cannot assume the
	state of the relay at power-up. NOx ("normally open") may be closed
	during power-up. You should make sure the embedded application is safe
	regardless of the state of the latching relay during power-up.
*/

#use eziopk24.lib
#use vdriver.lib

#define ACTUATE_TICK 3

void waitTicks(unsigned waitTick) {
	auto unsigned tck;
	
	tck=TICK_TIMER;
	while (TICK_TIMER-tck < waitTick);
}

void toggleRelay(int x) {
	auto int onSw, offSw;
	auto unsigned tck;

	onSw = 5-(x != 0);
	offSw = 7-(x != 0);
	eioBrdDO(onSw,1);
	waitTicks(ACTUATE_TICK);
	eioBrdDO(onSw,0);
	printf("relay %d should be closed now\npress a key to continue...\n",x);
	while (!kbhit());
	getchar();
	eioBrdDO(offSw,1);
	waitTicks(ACTUATE_TICK);
	eioBrdDO(offSw,0);
	printf("relay %d should be open now\npress a key to continue...\n",x);
	while (!kbhit());
	getchar();
}

main() {
	VdInit();

	eioBrdInit(0);
	while (1) {
		toggleRelay(0);	//	relay 1
		toggleRelay(1);	//	relay 2
	}
}