c++ pspsdk function calls by (m/k)hz frequency?

Dec 17, 2012 at 1:08pm
I've written a generator (using a thread), that generates up to 10 timed functions (each having their own speed (in hz)). It will cause x functions per second (x is the ammount of hz supplied using addTimer().

Basic includes and variables:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "headers/types.h" //Basic types!
#include "headers/timers.h" //Timer support function data!
#include "headers/emu/threads.h" //Thread for timer item!

//Timer step in ms! Originally 100ms
#define TIMER_STEP 10000

float timerfreqs[10] = {0,0,0,0,0,0,0,0,0,0};;
Handler timers[10] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
uint_32 counters[10] = {0,0,0,0,0,0,0,0,0,0};
char timernames[10][256] = {"","","","","","","","","",""}; //Timer name!
int TIMER_RUNNING = 0; //Whether to run timers or pause!
int action_confirmed = 0; //Action confirmed (after stop)? 


Timer thread itself (it'll handle all timers):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//This handles all current used timers!
void timer_thread() //Handler for timer!
{
	int curtimer;
	while (1) //Keep running!
	{

		if (TIMER_RUNNING && !action_confirmed) //Request to run?
		{
			action_confirmed = 1; //Confirmed to run!
		}
		else if (!TIMER_RUNNING && !action_confirmed) //To stop running?
		{
			action_confirmed = 1; //Confirmed!
			return; //Stop timer!
		}


		uint_32 bonus = 0; //Bonus of other running threads!
		bonus = 0; //Init bonus!
		
		for (curtimer=0; curtimer<10; curtimer++) //Process timers!
		{
			if ((timers[curtimer]!=NULL) && (timerfreqs[curtimer]!=0.0)) //Timer set?
			{
				counters[curtimer] += TIMER_STEP+bonus; //Increase counter, including the bonus gotten from the other threads running!
				while ((counters[curtimer]-(1000000/timerfreqs[curtimer]))>=0) //Overflow?
				{
					counters[curtimer] -= (1000000/timerfreqs[curtimer]); //Decrease counter!
					char name[256];
					bzero(name,sizeof(name)); //Init name!
					strcpy(name,"timer_sub_"); //Root!
					strcat(name,timernames[curtimer]); //Set name!
					startThread(timers[curtimer],name); //Fire the timer handler
					delay(10); //Little time for the thread!
					counters[curtimer] += 10; //Add the bonus of the previous thread!
					bonus += 10; //We have a bonus for multiple threads!
				}
				while (counters[curtimer]>10000000) //Way too much (10 seconds)?
				{
					counters[curtimer] -= 10000000; //Remove some (10 seconds)!
				}
			}
		}

		sceKernelDelayThread(TIMER_STEP); //Lousy, take 100ms breaks!
	}
}


Timer management:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
void addtimer(float frequency, Handler timer, char *name)
{
	int i;
	for (i=0; i<10; i++) //Check for existing timer!
	{
		if (strcmp(timernames[i],name)==0) //Found?
		{
			timers[i] = timer; //Edit timer if needed!
//Leave counter alone!
			timerfreqs[i] = frequency; //Edit frequency!
//Timer name is already set!
		}
	}

//Now for new timers!

	for (i=0; i<10; i++)
	{
		if (timerfreqs[i]==0.0) //Not set?
		{
			timers[i] = timer; //Set timer!
			counters[i] = 0; //Reset counter!
			timerfreqs[i] = frequency; //Start timer!
			strcpy(timernames[i],name); //Timer name!
			break;
		}
	}
}

void cleartimers() //Clear all running timers!
{
	int i;
	for (i=0; i<10; i++)
	{
		if (timerfreqs[i]!=0.0) //Set?
		{
			if (strcmp(timernames[i],"")!=0) //Set and has a name?
			{
				removetimer(timernames[i]); //Remove!
			}
		}
	}
}

void removetimer(char *name) //Removes a timer!
{
	int i;
	for (i=0; i<10; i++)
	{
		if (timerfreqs[i]!=0.0) //Enabled?
		{
			if (strcmp(timernames[i],name)==0) //Timer enabled and selected?
			{
				timerfreqs[i] = 0.0; //Turn timer off!
				timers[i] = NULL; //Remove timer!
				counters[i] = 0; //Reset counter!
				break;
			}
		}
	}
}

void startTimers()
{
	if (!TIMER_RUNNING) //Not already running?
	{
		TIMER_RUNNING = 1; //Start timers!
		startThread(&timer_thread,"timer_thread"); //Timer thread start!
	}
}

void stopTimers()
{
	if (TIMER_RUNNING) //Running already (we can terminate it)?
	{
		action_confirmed = 0; //Init!
		TIMER_RUNNING = 0; //Stop timers command for the timer thread!
		while (!action_confirmed) //Wait to stop!
		{
			delay(1); //Wait a bit for the thread to stop!
		}
	}
}

void resetTimers() //Reset all timers to off and turn off all handlers!
{
	stopTimers(); //Stop timer thread!
	int i;
	for (i=0; i<10; i++) //Go all!
	{
		timerfreqs[i] = 0.0; //Turn timer off!
		timers[i] = NULL; //Remove timer!
		counters[i] = 0; //Reset counter!
	}
}


Anybody knows a better way to do this (the timer_thread() is the one doing all the timing, the others are just for starting, stopping, resetting, adding and removing timers from emulation)?
Dec 17, 2012 at 2:06pm
Why do you shout '!' in your comments? lol
Topic archived. No new replies allowed.