Skip to content

Commit

Permalink
Merge pull request #7 from diamant3/add-sample-controller
Browse files Browse the repository at this point in the history
add basic controller sample
  • Loading branch information
sharkwouter authored Sep 14, 2022
2 parents 8e808cc + af676ad commit 7f6c219
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
149 changes: 149 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,155 @@ More libgu examples can be found <a href="https://github.com/pspdev/pspsdk/tree/

</details></p>

### Controller

![](images/controls.png?raw=true)

This is a simple program to use the PSP controller. Click on details below to see the code and how to build it.

<p><details>

<b>main.c</b>:

<pre>
#include &lt;pspkernel.h&gt;
#include &lt;pspdebug.h&gt;
#include &lt;pspctrl.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

PSP_MODULE_INFO("Controller", 0, 1, 1);

PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);

#define printf pspDebugScreenPrintf

int done = 0;

int exit_callback(int arg1, int arg2, void *common)
{
done = 1;
return 0;
}

int callback_thread(SceSize args, void *argp)
{
int cbid = sceKernelCreateCallback("Exit Callback",
exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}

int setup_callbacks(void)
{
int thid = sceKernelCreateThread("update_thread",
callback_thread, 0x11, 0xFA0, 0, 0);

if(thid &gt;= 0)
sceKernelStartThread(thid, 0, 0);
return thid;
}

int main(void)
{
SceCtrlData pad;

pspDebugScreenInit();
setup_callbacks();

sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);

while (!done)
{
pspDebugScreenSetXY(0, 2);
sceCtrlReadBufferPositive(&pad, 1);

printf("Analog X = %d, ", pad.Lx);
printf("Analog Y = %d \n", pad.Ly);

if (pad.Buttons != 0)
{
if (pad.Buttons & PSP_CTRL_SQUARE)
{
printf("Square pressed! \n");
}
if (pad.Buttons & PSP_CTRL_TRIANGLE)
{
printf("Triangle pressed! \n");
}
if (pad.Buttons & PSP_CTRL_CIRCLE)
{
printf("Circle pressed! \n");
}
if (pad.Buttons & PSP_CTRL_CROSS)
{
printf("Cross pressed! \n");
}

if (pad.Buttons & PSP_CTRL_UP)
{
printf("Square pressed! \n");
}
if (pad.Buttons & PSP_CTRL_DOWN)
{
printf("Triangle pressed! \n");
}
if (pad.Buttons & PSP_CTRL_LEFT)
{
printf("Square pressed! \n");
}
if (pad.Buttons & PSP_CTRL_RIGHT)
{
printf("Triangle pressed! \n");
}
}
}

sceKernelExitGame();
return 0;
}
</pre>

<b>CMakeLists.txt</b>:

<pre>
cmake_minimum_required(VERSION 3.0)

project(controls)

add_executable(${PROJECT_NAME} main.c)

target_link_libraries(${PROJECT_NAME} PRIVATE
pspdebug
pspdisplay
pspge
pspctrl
)

# Create an EBOOT.PBP file
create_pbp_file(
TARGET ${PROJECT_NAME}
ICON_PATH NULL
BACKGROUND_PATH NULL
PREVIEW_PATH NULL
TITLE ${PROJECT_NAME}
)
</pre>

Building can be done with:

<pre>
mkdir build && cd build
psp-cmake ..
make
</pre>

<p>This will result in an EBOOT.PBP file in the build directory. Put it in a directory in ms0:/PSP/GAME/ and the PSP can run it.</p>

</details></p>

### Using SDL2

![](images/sdl2.png?raw=true)
Expand Down
Binary file added images/controls.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7f6c219

Please sign in to comment.