forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

threads

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Alberto



Joined: 12 Feb 2007
Posts: 57
Location: Sofia

PostPosted: Tue Feb 09, 2010 5:52 am    Post subject: threads Reply with quote

Hi all,

sorry for the topic, but couldn't find anything better.

Here's what I need: a procedure that is being constantly called every certain amount of time (I don't care about how often, provided I can count on it being every frame-start, or 60 times a second... I think it should be enough).

I guess this may be done with a separate thread in my application (all I need to do is read the keys and/or analog pad, and store the last-read data in some variables to access them later)

I am very new to using threads (or whatever is necessary to achieve this task); can any of you please shed some light?

Thank you all in advance,
A.
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Tue Feb 09, 2010 6:59 am    Post subject: Reply with quote

That's pretty much what sceCtrlReadBuffer() does. It reads the keys and then reschedules your thread to run at the next vblank.
so just
Code:

do{
sceCtrlReadBuffer(...);
//i get here once per vblank
...
} while (!exit_program);

does what you want.

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
Alberto



Joined: 12 Feb 2007
Posts: 57
Location: Sofia

PostPosted: Tue Feb 09, 2010 7:36 am    Post subject: Reply with quote

Jim wrote:
That's pretty much what sceCtrlReadBuffer() does. It reads the keys and then reschedules your thread to run at the next vblank.
so just
Code:

do{
sceCtrlReadBuffer(...);
//i get here once per vblank
...
} while (!exit_program);

does what you want.

Jim


nope... ehm, what I catually want is something like a function that gets called "automatically", without me to include "check-the-keys();" in my main loop... sorry, to that i can get ;-)
Like a callback on button-presses or on analog-nub movement, or a sort of callback called every framestart.

Actually, I was reading through the toolchain source, and found sceRegisterButtonCallback (and if there was something for the analog nub too would be great...) but cannot get to compile, as it doesn't find a reference to sce_Kernel_{hex numbers here}...

Cheers, A.
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Wed Feb 10, 2010 6:21 am    Post subject: Reply with quote

You can probably do something like that, but I don't know how.
Can I ask why you want that? It doesn't seem all that useful. The callback for the analog joystick sounds like a bad requirement - how far should the nub move before you get a callback, for instance.

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
Alberto



Joined: 12 Feb 2007
Posts: 57
Location: Sofia

PostPosted: Wed Feb 10, 2010 6:15 pm    Post subject: Reply with quote

Jim wrote:
Can I ask why you want that? It doesn't seem all that useful.


of course you can ask; just because I would like to implement something like a keys (and not only) buffer

Jim wrote:
The callback for the analog joystick sounds like a bad requirement - how far should the nub move before you get a callback, for instance.


yeah, that was just a thought.

What about an interrupt handler? I've been looking at "pspintrman.h" but I cannot succeed in hooking anything: either returns negative - error - codes in USER mode, or it freezes at startup in KERNEL mode.

Can you be of any help about?

Thanks,
A.
Back to top
View user's profile Send private message
unsigned int



Joined: 13 Aug 2009
Posts: 22

PostPosted: Wed Feb 10, 2010 9:49 pm    Post subject: Reply with quote

You could write your own callback mechanism using a thread that checks the keys and on changes calls another function (your callback function).

Something like this (I guess this also answers how to create a thread):
Code:


void inputThread(SceSize args, void *argp)
{
  unsigned int lastButtons = 0;
  sceCtrlData currentCtrlData;
 
  while (1)
  {
    sceCtrlPeekButterPositive(&currentCtrlData, 1);

    if ((currentCtrlData.buttons && PSP_CTRL_CROSS) && !(lastButtons && PSP_CTRL_CROSS))
    {
       myButtonCallback(PSP_CTRL_CROSS, KEY_DOWN);
    }
    else if .... etc.

    lastButtons = currentCtrlData.buttons;

    sceKernelDelayThread(10000); // 10 ms
  }
}


int main()
{
  SceUID inputThreadId = sceKernelCreateThread("inputThread", (SceKernelThreadEntry)inputThread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, 0);   
  sceKernelStartThread(inputThreadId, 0, 0);

  while (1)
  {
    // whatever
  }
}
Back to top
View user's profile Send private message
Alberto



Joined: 12 Feb 2007
Posts: 57
Location: Sofia

PostPosted: Wed Feb 10, 2010 11:54 pm    Post subject: Reply with quote

unsigned int wrote:
You could write your own callback mechanism using a thread that checks the keys and on changes calls another function (your callback function).


yes, indeed that is what I was finally worging on, today... seems the only possibility.

Anyway, as I see I cannot have two threads working at the same time, that I need to sceKernelDelayThread() both of them.
But in the while(1) loop of the main in your example, I don't see any delay for the main application thread; have you just forgotten it or is there actually a way to have things run in parallel, and I don't find it?

unsigned int wrote:

Code:

int main()
{
  SceUID inputThreadId = sceKernelCreateThread("inputThread", (SceKernelThreadEntry)inputThread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, 0);   
  sceKernelStartThread(inputThreadId, 0, 0);

  while (1)
  {
    // whatever
  }
}
Back to top
View user's profile Send private message
unsigned int



Joined: 13 Aug 2009
Posts: 22

PostPosted: Thu Feb 11, 2010 12:59 am    Post subject: Reply with quote

You are correct. The PSP incorporates cooperative multithreading, so threads have to actively give up control by sleeping. If the example was compiled like this the thread would never execute after the while loop got entered.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group