Monday, July 20, 2020

Basics on the code PART 9

Today we are going to read the sample about the touch screens (frontal and rear pannel) of the vita.

To help us to understand the functions we have to use the vitasdk documentation, for this specific sample we will use the touch section.  

Note: there seems to be some functions that are based on previous versions of the SDK, but they are with the newer ones, so we'll try to join the explanations unless other requirements arise.

SAMPLE "touch"

touch: A minimal touch sample.


#include <psp2/kernel/processmgr.h>
#include <psp2/touch.h>

#include <stdio.h>
#include <string.h>

#include "debugScreen.h"

#define printf psvDebugScreenPrintf

/* TODO: why touch[port].report[i].force is always at 128 ? */

// main program

int main(int argc, char *argv[]) {
    psvDebugScreenInit();  // needed for the prints to show on the vita screen.


// as in other samples, here is written the stop condition for the sample

    printf("swipe to the bottom with 1 finger to stop\n");
    /* should use SCE_TOUCH_SAMPLING_STATE_START instead of 1 but old SDK have an invalid values */
    sceTouchSetSamplingState(SCE_TOUCH_PORT_FRONT, 1);
    sceTouchSetSamplingState(SCE_TOUCH_PORT_BACK, 1);
    sceTouchEnableTouchForce(SCE_TOUCH_PORT_FRONT);
    sceTouchEnableTouchForce(SCE_TOUCH_PORT_BACK);
   
    SceTouchData touch_old[SCE_TOUCH_PORT_MAX_NUM];
    SceTouchData touch[SCE_TOUCH_PORT_MAX_NUM];



// and here on, it is the loop for the testing and parameters

    while (1) {

        memcpy(touch_old, touch, sizeof(touch_old));
        printf("\e[0;5H");

        int port,i;
        /* sample both back and front surfaces */
        for(port = 0; port < SCE_TOUCH_PORT_MAX_NUM; port++){

//  shows the screen coordinates where you are pressing,
// and saves the result on screen until new coordinates are created

            sceTouchPeek(port, &touch[port], 1);
            printf("%s",((const char*[]){"FRONT","BACK "})[port]);
            /* print every touch coordinates on that surface */
            for(i = 0; i < SCE_TOUCH_MAX_REPORT; i++)
                printf("\e[9%im%4i:%-4i ", (i < touch[port].reportNum)? 7:0,
                       touch[port].report[i].x,touch[port].report[i].y);
            printf("\n");
        }

// stop condition for the loop to exit
/* in real test, this coordinates are too specific and is difficult to sweep in the right position*/

        if ( (touch[SCE_TOUCH_PORT_FRONT].reportNum == 1)
          && (touch_old[SCE_TOUCH_PORT_FRONT].reportNum == 1)
          && (touch[SCE_TOUCH_PORT_FRONT].report[0].y >= 1000)
          && (touch_old[SCE_TOUCH_PORT_FRONT].report[0].y < 1000))
        break;
    }


    sceKernelExitProcess(0);
    return 0;
}


-----------

Some screenshots from the sample running on the vita:

Starting coordinates


One finger pressing the rear pannel

Two fingers pressing the front pannel


Two fingers pressing the rear pannel

Six fingers presing the front channel

And that's all for this sample.

No comments:

Post a Comment