implemented joypad_get function and made joypad_test.py

This commit is contained in:
gfp34 2022-10-12 12:34:49 -04:00
parent 974601ae71
commit 85dd2d15b7
2 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,6 @@
import emu
import joypad
while True:
print(f"PY: {joypad.read(1)}")
emu.frameadvance()

View File

@ -10,6 +10,7 @@ namespace py = pybind11;
#include "fceupython.h"
#include "movie.h"
#include "state.h"
// Are we running any code right now?
static char* pythonScriptName = NULL;
@ -25,6 +26,12 @@ std::mutex mtx;
std::condition_variable cv;
// Look in fceu.h for macros named like JOY_UP to determine the order.
static const char *button_mappings[] = {
"A", "B", "select", "start", "up", "down", "left", "right"
};
static void emu_frameadvance()
{
// Can't call if a frameAdvance is already waiting
@ -47,12 +54,46 @@ static int emu_framecount()
return FCEUMOV_GetFrame();
}
// dict joypad.read(int which = 1)
//
// Reads the joypads as inputted by the user.
static py::dict joy_get_internal(int player, bool reportUp, bool reportDown) {
// Use the OS-specific code to do the reading.
extern SFORMAT FCEUCTRL_STATEINFO[];
uint8 buttons = ((uint8*) FCEUCTRL_STATEINFO[1].v)[player - 1];
py::dict input;
for (int i = 0; i < 8; i++) {
bool pressed = (buttons & (1<<i)) != 0;
if ((pressed && reportDown) || (!pressed && reportUp)) {
input[button_mappings[i]] = py::bool_(pressed);
}
}
return input;
}
static py::dict joypad_get(int player) {
return joy_get_internal(player, true, true);
}
// static void joypad_set(int player, py::dict input) {
// }
PYBIND11_EMBEDDED_MODULE(emu, m)
{
m.def("frameadvance", emu_frameadvance);
m.def("framecount", emu_framecount);
}
PYBIND11_EMBEDDED_MODULE(joypad, m)
{
m.def("get", joypad_get);
m.def("read", joypad_get);
}
void FCEU_PythonFrameBoundary()
{
std::cout << "in FCEU_PythonFrameBoundary" << std::endl;