Programming:  Timer events

Well, this program animates a message on the screen, its pump being a timer.

We can only count on one Timer, it is called OBJ_500MSEC. How long do you think it elapses before the timer notifies us? 500 milliseconds? The power of labels!!!

If you need finer intervals, well now you need to use PollEvents(). It doesn't give you timers out of nothing, but it's not blocking and you can manage the delay in the event-loop (Use LibWait). It's not an elegant solution but...

#include	<stdrom.h>
#include	"define.h"
#include	"libc.h"

I couldn't find this label in the SDK header files, so i defined it here

#define IW_EXT500M 0x0001

We just define the message we want to animate and a label to remember the screen width.

#define MESSAGE "Pocket-Viewer"
#define SCREEN_W 160

If we want to be notified on the timer event we must put it in the touch table just like we did with the touch events. The timer is called OBJ_500MSEC, its action ACT_500MSEC. No coordinates, we assign them "the" dummy value.

TCHTBL TchList[2] = 
{
	/* Timer      */ 
		0, 0, 0, 0,        
		ACT_500MSEC,
		OBJ_500MSEC,
		0x0000,

	/* End */
		0, 0, 0, 0,
		ACT_NONE,
		OBJ_END,
		0x0000
};

Here we make some noise, everytime the timer calls out!! Try it ;)

void DoMessage()
{
	static int pos;
	int msg_x, msg_w;
	char msg_text[32];

	pos++;

	if (pos > strlen(MESSAGE))
	{
		pos = 0;
		return;
	}

	LibClrDisp();
	strncpy(msg_text, MESSAGE, pos);
	msg_text[pos] = 0;
	msg_w = LibGetProStrSize(IB_PFONT3, msg_text);
	msg_x = (SCREEN_W - msg_w) / 2;

	LibGdsBox(msg_x - 2, 76 - 2, msg_x + msg_w + 2, 84 + 4); 
	LibPutProStr(IB_PFONT3, msg_x, 76, msg_text, msg_w);
	LibPutDisp();
}

Right! Just how you guessed, our usual events manager, BUT...

void main()
{
	TCHSTS tsts;
	bool bExit = FALSE;

	LibTchStackClr();
	LibTchStackPush(NULL);

	LibTchStackPush(TchHardIcon);
	LibTchStackPush(&TchList[0]);

	LibTchInit();

	for (;bExit == FALSE;)
	{
		LibTchWait(&tsts);           

...that's how we listen to the timer event. This time we don't test the "obj" field. That's because the timer event may happen the same moment a touch event happens, in this case we have to test "ext" for the timer and "obj" for the other object. But we don't know if this is the case, so we always test "ext" for the timer, it is set anyway.

		if (tsts.ext == IW_EXT500M)
			DoMessage();

		if (tsts.obj == OBJ_HIC_ESC)        
			bExit = TRUE;
	}

	LibTchStackClr();
	LibJumpMenu();
}