Page 1 of 1

uclock(), usleep(), localtime() and readdir() equivalents

Posted: Thu Sep 22, 2005 7:59 pm
by 0xF
I'd like to know, how to get the following functionality from C:
  1. A clock with 1ms resolution or better.
  2. A sleep function with 1ms resolution or better
    (or should I spin in a loop using the above clock?)
  3. Realtime clock (year, month, day of month, day of week,
    hour, minute, second)
  4. Read memory card's or CD's directory
    (should I use fioD* from PS2SDK?)
Thank you.

Posted: Fri Sep 23, 2005 1:08 am
by _tmator_
Hi,

1 : See SDL timer code (SDL_Delay)
2 : Just create a thread and wait a sema sent by the thread when X ms will pass
3 : libcdvd i think but not sure
4 : See PS2MENU code in svn/ps2ware, it's a good example to know how read MC and CD'S directory

Re: usleep()

Posted: Thu Jul 06, 2006 10:16 am
by DeRieux
You can try the following code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <tamtypes.h>
#include <sifcmd.h>
#include <kernel.h>
#include <sifrpc.h>
#include <string.h>
#include <sys/stat.h>
#include <debug.h>
#include <timer.h>

#define clearscreen&#40;&#41; scr_clear&#40;&#41; /*Defines easily understood macro */
#define DrawSimpleString&#40;char&#41; scr_printf&#40;char&#41; /* ditto */

void PauseBeforeClear&#40;int wait_timer&#41;;
void SetScreenSize&#40;int x, int y&#41;;

int main&#40;&#41;
&#123;   
   
   SifInitRpc&#40;0&#41;; 
   init_scr&#40;&#41;; 

   
   SetScreenSize&#40;80,25&#41;;
   DrawSimpleString&#40;"\nPause Execution for x Cpu_Ticks&#40;&#41;\n"&#41;;
   DrawSimpleString&#40;"\n..........Code By...............\n"&#41;;
   DrawSimpleString&#40;"\n    William DeRieux, 5/5/2006   \n"&#41;;   
   PauseBeforeClear&#40;100&#41;; /* Timer in Cpu_Ticks&#40;&#41;*/
  
	
	return&#40;0&#41;;
	
&#125;

void PauseBeforeClear&#40;int wait_timer&#41;
&#123;
	int ticks = 0;
	
	do
	&#123;
		ticks = cpu_ticks&#40;&#41;;
	&#125;
	while &#40;ticks != wait_timer&#41;;	
	
	if &#40;ticks == wait_timer&#41;
	&#123;
		clearscreen&#40;&#41;;
	&#125;

&#125;

void SetScreenSize&#40;int x, int y&#41;
&#123;
	scr_setXY&#40;x,y&#41;;
&#125;
This will draw some text on the screen, set the screen size, and then wait a set number of cpu_ticks and clear the screen.

William

Hope this may help.

Followup:

Posted: Thu Jul 06, 2006 10:25 am
by DeRieux
0xF wrote:I'd like to know, how to get the following functionality from C:
  1. A clock with 1ms resolution or better.
  2. A sleep function with 1ms resolution or better
    (or should I spin in a loop using the above clock?)
  3. Realtime clock (year, month, day of month, day of week,
    hour, minute, second)
  4. Read memory card's or CD's directory
    (should I use fioD* from PS2SDK?)
Thank you.
Sleep Function - See My Last Post about cpu_ticks (in timer.h)

EDIT: The Sleep Function does wait a specified number of cpu ticks,
But, it is not consistent and is relatively unreliable...I do not
recommend using it as an actual timer, unless you just want to
pause for a little bit (not wait a specified number of seconds,
minutes, etc)

Shawn_t has a much better solution posted here:
http://forums.ps2dev.org/viewtopic.php? ... clock+tick
(I will be making an additional post there, regarding using that solution to implement a fix for the timer)

clock function - possible use cpu_ticks as well

Realtime Clock - have a look at time.h
snippet from time.h
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};