/*
	BAREZN2.C

	Bare znet slave controller program.
	See barezn1.c for usage notes.
*/

#define ZNMASTER 0		//	I am a slave
#use vdriver.lib
#use aasczn.lib
//#use eziopk23.lib		//	needed only if you are using a PK23xx controller

#define BUFSIZ 0x10
	
main() {
	char InFrom2[BUFSIZ], OutTo2[BUFSIZ];
	CHANNEL Slave1;
	char locBuf[30];
	char messCnt;
	char *ptr;
	int cnt;
	int moved;
	char ch;
	unsigned long int txTime;
	unsigned sequence;

	VdInit();

	Slave1 = aascOpen(DEV_ZNET,0,ZN1200*16|ZNSLAVE*1,NULL);
	znID = 2;

	aascSetReadBuf(Slave1,InFrom2,sizeof(InFrom2));
	aascSetWriteBuf(Slave1,OutTo2,sizeof(OutTo2));
	aascTxSwitch(Slave1,1);
	aascRxSwitch(Slave1,1);
	aascFlush(Slave1);
	ptr = "normal ";
	sequence = 0;

	while (1) {
		znPollSlave();

		costate {
			waitfor(DelayMs(500));
			hitwd();
		}

		costate {
			runwatch();
			cnt = 7;
			while (cnt) {
				moved = aascWriteBlk(Slave1, ptr, cnt, 0);
				cnt -= moved;
				ptr += moved;
			}
			txTime = MS_TIMER;
			do {
				cnt = aascReadChar(Slave1, &ch);
			} while ((!cnt) && (MS_TIMER-txTime < 2000));
			if (cnt) ptr = "normal ";
			else {
				ptr = "timeout";
			}
			++sequence;
		}
	}
}