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:
- A clock with 1ms resolution or better.
- A sleep function with 1ms resolution or better
(or should I spin in a loop using the above clock?)
- Realtime clock (year, month, day of month, day of week,
hour, minute, second)
- 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() scr_clear() /*Defines easily understood macro */
#define DrawSimpleString(char) scr_printf(char) /* ditto */
void PauseBeforeClear(int wait_timer);
void SetScreenSize(int x, int y);
int main()
{
SifInitRpc(0);
init_scr();
SetScreenSize(80,25);
DrawSimpleString("\nPause Execution for x Cpu_Ticks()\n");
DrawSimpleString("\n..........Code By...............\n");
DrawSimpleString("\n William DeRieux, 5/5/2006 \n");
PauseBeforeClear(100); /* Timer in Cpu_Ticks()*/
return(0);
}
void PauseBeforeClear(int wait_timer)
{
int ticks = 0;
do
{
ticks = cpu_ticks();
}
while (ticks != wait_timer);
if (ticks == wait_timer)
{
clearscreen();
}
}
void SetScreenSize(int x, int y)
{
scr_setXY(x,y);
}
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:
- A clock with 1ms resolution or better.
- A sleep function with 1ms resolution or better
(or should I spin in a loop using the above clock?)
- Realtime clock (year, month, day of month, day of week,
hour, minute, second)
- 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;
};