Thursday, July 16, 2020

Basics on the code PART 6

This entry is about managing the data on the system power.

As always, to understand the functions, we should read the vitasdk documentation, particularly the power section.

To understand this sample, we have to know that the power info of the console is given through reading a thread,
that is going to be an endless callback for info from the viewer perspective. This works like any game or program that seem "halt" until you make an action. You only need to see 2 things:
- the thread with the info inside as variables, in a constant refresh on screen.
- the loop that keeps your system waiting for an input (interrupt programming), and lets you (in this case) end the loop, and exit the program.


SAMPLE "power"

power: A minimal power sample.


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

#include <psp2/ctrl.h>
#include <psp2/kernel/processmgr.h>
#include <psp2/power.h>
#include <psp2/rtc.h>

#include "debugScreen.h"

/* Define printf, just to make typing easier */
#define printf psvDebugScreenPrintf


/* Power Callback - Runs when anything related do with power happens;
     e.g. pressed power button, sleep, etc. */
int powerCallback(int notifyId, int notifyCount, int powerInfo, void *common) {

    // doesn't work without a file handle open...?
    FILE *fd = fopen("ux0:data/test_power_cb.txt", "w");

// get the time from the console
    SceDateTime time;
    sceRtcGetCurrentClockLocalTime(&time);

    printf("%04d/%02d/%02d %02d:%02d:%02d:%05d notifyId %i, notifyCount %i, powerInfo 0x%08X\n",
        sceRtcGetYear(&time), sceRtcGetMonth(&time), sceRtcGetDay(&time),
        sceRtcGetHour(&time), sceRtcGetMinute(&time), sceRtcGetSecond(&time), sceRtcGetMicrosecond(&time),
        notifyId, notifyCount, powerInfo);

    fclose(fd);
}

/* Our thread to handle any callbacks. */
int CallbackThread(SceSize args, void *argp) {
    // create and register a callback in this thread.
    int cbid;
    cbid = sceKernelCreateCallback("Power Callback", 0, powerCallback, NULL);
    int registered = scePowerRegisterCallback(cbid);

    printf("\e[16;0H Callback registered id: %i  registered: %i.\n\n", cbid, registered);

    // this thread only handles callbacks.
    // so delay it forever, but handle callbacks.
    while (1) {
        sceKernelDelayThreadCB(10 * 1000 * 1000);
    }

    return registered;
}

/* Sets up the callback thread and returns its thread id */
int setupCallbacks(void) {
    int thid = 0;

    // The main thread does not handle callbacks, so we need to make one to handle them.
    thid = sceKernelCreateThread("callbackThread", CallbackThread, 0x10000100, 0x10000, 0, 0, NULL);
    if (thid >= 0)
        sceKernelStartThread(thid, 0, 0);

    printf("\e[15;0H Thread created %i\n.", thid);
    return thid;
}

/* main routine */
int main(int argc, char *argv[])
{   
    SceCtrlData pad;
    int i = 0;
    int batteryLifeTime = 0;

    psvDebugScreenInit();
   

// show the info on screen with values obtained from functions inside the printf

    printf("PS Vita Power Sample v0.1\n\n");

    printf("External power: %s\n", scePowerIsPowerOnline()? "yes" : "no ");
    printf("Low charge: %s\n", scePowerIsLowBattery()? "yes" : "no ");
    printf("Charging: %s\n", scePowerIsBatteryCharging()? "yes" : "no ");
    printf("Battery life percent: %d%%\n", scePowerGetBatteryLifePercent());
    batteryLifeTime = scePowerGetBatteryLifeTime();
    printf("Battery life time: (%02dh%02dm)\n", batteryLifeTime/60, batteryLifeTime-(batteryLifeTime/60*60));
    printf("Clock frequency of the ARM: %d mHz\n", scePowerGetArmClockFrequency());
    printf("Clock frequency of the BUS: %d mHz\n", scePowerGetBusClockFrequency());
    printf("Clock frequency of the GPU: %d mHz\n", scePowerGetGpuClockFrequency());

    printf("\n\nExperiment by sleeping vita and turning back on. Press start to exit. \n");
    setupCallbacks();


// looping for reading inputs, as any other program do, to know the user actions

    while (1)
    {
        memset(&pad, 0, sizeof(pad));
        sceCtrlPeekBufferPositive(0, &pad, 1);
        
        if (pad.buttons & SCE_CTRL_START) // stops the loop if the user press the start button
            break;
    }

    sceKernelExitProcess(0); 
// close the vita proccess runing, usually close the app

    return 0;
}


-----------

Screenshot from the sample running on the vita:




And that's all for this sample

No comments:

Post a Comment