An Example Amulet Application

Part 3 of the Amulet Manual by Laird and Jones
#include "amulet.h"	/* all Amulet applications must include this file */
#include 

Message(init_msg)
	int value;	/* value and delay are user-defined fields */
	int delay;
EndMessage;

Message(start_msg)	/* start_msg is a basic message with no user-defined
EndMessage;		   fields */


Chare(looping_chr)
	int times;	/* times and delay are user-defined fields */
	int delay;

Entries {init, loop};

Code(looping_chr)

	Entry(init, init_msg)
		timetyp t1, t2;
		chr->times = msg->value;
		chr->delay = msg->delay;
		
		printf("Starting loop in five seconds.\n");
		
		/* set the begin time to 5 seconds in the future*/
		get_current_time(t1);
		addtime_seconds(t1, 5);
		
		/* the latest this message can be sent is 5.1 
		   seconds from now */
		settime(t2, t1);
		addtime_millisecs(t2, 100);
		
		SendLater(start_msg, chr, loop, t1, t2);
		EndSendLater;
	EndEntry;
	
	Entry(loop, start_msg)
		chr->times -= 1;
		if (chr->times > 0) {
			printf("Looping.....%d more iteration(s) to perform\n", chr->times);
			{
				timetyp t1, t2;
				
				/* set the begin time to chr->delay millisecs
				   in the future */
				get_current_time(t1);
				addtime_millisecs(t1, chr->delay);
				
				/* the latest the message can be delivered
				   is (chr->delay + 100) ms from now */
				settime(t2, t1);
				addtime_millisecs(t2, 100);
				
				SendLater(start_msg, chr, loop, t1, t2);
				EndSendLater;
			}
		} else
			printf("Looping.....done\n");
			
	EndEntry;
	
EndChare;


AmuletInit
	timetyp t;
	CreateChare(looper, looping_chr);
	CreateMsg(loopmsg, init_msg);
	
	printf("\n\n***Real-Time Demonstration***\n\n");
	printf("Enter the number of iterations to perform: ");
	scanf("%d", &loopmsg->value);
	printf("Enter the delay (in milliseconds) between iterations: ");
	scanf("%d", &loopmsg->delay);
	
	get_current_time(t);
	addtime_millisecs(t, 500);
	
	SendMsgNow(loopmsg, looper, init, t);
EndAmuletInit;