-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.c
147 lines (122 loc) · 3.42 KB
/
system.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <stdlib.h>
#include <loadfile.h>
#include <libpad.h>
#include <timer.h>
#include <math.h>
#define STB_SPRINTF_IMPLEMENTATION
#include "system.h"
static bool g_debugEnabled = false;
static char g_padBuf[256] __attribute__((aligned(64)));
void assDebugInit()
{
g_debugEnabled = true;
}
void assDebug(const char * format, const char *func, int line, ...)
{
if (g_debugEnabled) {
va_list args;
va_start(args, line);
char buffer[2048];
int siz1 = stbsp_vsnprintf(buffer, sizeof(buffer), format, args) + 1;
int siz2 = stbsp_snprintf(&buffer[siz1], sizeof(buffer) - siz1, "[%.9f] [%s:%d] %s\n", assGetTime(), func, line, buffer);
fwrite(&buffer[siz1], siz2, 1, stdout);
va_end(args);
}
}
bool assLoadModule(const char *name)
{
LOG_DEBUG("Loading '%s'", name);
int ret = SifLoadModule(name, 0, NULL);
if (ret < 0) {
LOG_DEBUG("Error: '%s' failed with code %d\n", name, ret);
return false;
}
return true;
}
bool assWaitPad(int port, int slot)
{
while(1) {
int state = padGetState(port, slot);
switch(state) {
case PAD_STATE_FINDPAD:
case PAD_STATE_FINDCTP1:
case PAD_STATE_EXECCMD:
// wait for operation to be done
break;
case PAD_STATE_STABLE:
// pad is ready
return true;
case PAD_STATE_DISCONN:
case PAD_STATE_ERROR:
// pad is not ready
return false;
}
}
}
void assInputInit()
{
// init PAD
LOG_DEBUG("Input init");
assLoadModule("rom0:SIO2MAN");
assLoadModule("rom0:PADMAN");
padInit(0);
padPortOpen(0, 0, g_padBuf);
}
void assUpdateIO(io_t *io)
{
// update timer counter
float time = assGetTime();
float deltaTime = time - io->time;
// FPS counter, moving average per 10 frames
float curFps = deltaTime == 0.0f ? 0.0f : 1.0f / deltaTime;
float avgFps = io->fps + (curFps - io->fps) / 10.0f;
// store time info
io->deltaTime = deltaTime;
io->time = time;
io->fps = avgFps;
// handle PAD reconnection
static int lastPadState = 0;
int padState = padGetState(0, 0);
if (padState != lastPadState) {
if (lastPadState == PAD_STATE_DISCONN && padState == PAD_STATE_EXECCMD) {
if (assWaitPad(0, 0)) {
// PAD_MMODE_LOCK does not allow to change PAD mode by user
padSetMainMode(0, 0, PAD_MMODE_DUALSHOCK, PAD_MMODE_LOCK);
assWaitPad(0, 0);
padEnterPressMode(0, 0);
assWaitPad(0, 0);
LOG_DEBUG("PAD RESET!");
}
}
lastPadState = padState;
}
// handle pad action
struct padButtonStatus buttons;
int ret = padRead(0, 0, &buttons);
if (ret != 0) {
static unsigned short lastBtns = 0;
unsigned short btns = ~buttons.btns;
unsigned short btnsx = lastBtns ^ btns;
unsigned short btnsp = btns & btnsx;
lastBtns = btns;
io->throttle = (float)buttons.cross_p / 255;
const float deadZone = 0.2f;
io->dirx = (((float)buttons.ljoy_h - 127) / 127);
io->dirx = fabsf(io->dirx) < deadZone ? 0.0f : io->dirx;
io->diry = (((float)buttons.ljoy_v - 127) / 127);
io->diry = fabsf(io->diry) < deadZone ? 0.0f : io->diry;
io->fire = btns & PAD_R2;
io->firePressed = btnsp & PAD_R2;
io->left = btns & PAD_LEFT;
io->right = btns & PAD_RIGHT;
io->test = btns & PAD_R3;
io->testPressed = btnsp & PAD_R3;
io->exit = (btns & PAD_START) && (btns & PAD_SELECT);
io->startPressed = btnsp & PAD_START;
}
}
float assGetTime()
{
return (float)GetTimerSystemTime() / kBUSCLK;
}