From 1ef301c15ec916ab0231a49fdce1b38e73b67702 Mon Sep 17 00:00:00 2001 From: James Lambert Date: Wed, 11 Oct 2023 19:18:28 -0600 Subject: [PATCH] Get rumble pak mostly working --- src/controls/controller.c | 27 ++++++++++++++++++++++----- src/controls/rumble_pak.c | 4 +++- src/levels/static_render.c | 17 ----------------- src/main.c | 5 ++++- src/scene/scene.c | 2 +- src/util/profile.c | 2 +- 6 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/controls/controller.c b/src/controls/controller.c index fff0636..9000bf5 100644 --- a/src/controls/controller.c +++ b/src/controls/controller.c @@ -4,6 +4,7 @@ #include "util/memory.h" #include #include "rumble_pak.h" +#include "../util/profile.h" #include @@ -119,17 +120,17 @@ int controllerHandleMessage() { gRumblePakState = RumblepakStateInitialized; } else { gRumblePakState = RumblepakStateDisconnected; - gRumblePakOn = 0; } } if (gControllerStatus[0].status != CONT_CARD_ON) { gRumblePakState = RumblepakStateDisconnected; - gRumblePakOn = 0; } } return FALSE; } +u8 gRumbleFailCount; + void controllersReadPendingData(void) { OSMesg msg; int shouldCheckStatus; @@ -142,6 +143,8 @@ void controllersReadPendingData(void) { int targetRumbleStatus = rumblePakCalculateState(); + u64 timeStart = profileStart(); + if (gRumblePakState == RumblepakStateInitialized) { if (targetRumbleStatus != gRumblePakOn) { shouldCheckStatus = FALSE; @@ -150,16 +153,30 @@ void controllersReadPendingData(void) { s32 rumbleError = targetRumbleStatus ? osMotorStart(&gRumbleBackFs) : osMotorStop(&gRumbleBackFs); if (rumbleError == PFS_ERR_CONTRFAIL) { - continue; + if (i == 2) { + ++gRumbleFailCount; + } } else if (rumbleError != 0) { gRumblePakState = RumblepakStateDisconnected; - gRumblePakOn = 0; break; } else { gRumblePakOn = targetRumbleStatus; - break; + gRumbleFailCount = 0; } } + } else if (!targetRumbleStatus) { + osMotorStop(&gRumbleBackFs); + } + } + + profileEnd(timeStart, 2); + + if (gRumbleFailCount == 3) { + if (osMotorInit(&gControllerMsgQ, &gRumbleBackFs, 0) == 0) { + gRumblePakState = RumblepakStateInitialized; + } else { + gRumblePakState = RumplepakStateUninitialized; + } } diff --git a/src/controls/rumble_pak.c b/src/controls/rumble_pak.c index 1365e29..c74ff47 100644 --- a/src/controls/rumble_pak.c +++ b/src/controls/rumble_pak.c @@ -25,6 +25,8 @@ void rumblePakClipInit() { curr->wave = NULL; curr->currentSample = 0; curr->rumbleId = 0; + + prev = curr; } prev->next = NULL; @@ -146,7 +148,7 @@ int rumblePakCalculateState() { amplitude = 3; } - int result = amplitude >= gRumbleCurrentBit; + int result = amplitude == 3 || amplitude > gRumbleCurrentBit; gRumbleCurrentBit = (gRumbleCurrentBit + 1) & 0x3; return result; } \ No newline at end of file diff --git a/src/levels/static_render.c b/src/levels/static_render.c index f229260..c98b4dc 100644 --- a/src/levels/static_render.c +++ b/src/levels/static_render.c @@ -6,7 +6,6 @@ #include "../graphics/render_scene.h" #include "../math/mathf.h" #include "../scene/signals.h" -#include "../util/profile.h" #include "../build/assets/materials/static.h" @@ -28,14 +27,10 @@ void staticRenderPopulateRooms(struct FrustrumCullingInformation* cullingInfo, M struct Vector3 boxCenter; - u64 startTime = profileStart(); - if (transformIndex == NO_TRANSFORM_INDEX) { if (isOutsideFrustrum(cullingInfo, box)) { - profileEnd(startTime, 2); continue; } - profileEnd(startTime, 2); boxCenter.x = (float)((box->minX + box->maxX) * (0.5f / SCENE_SCALE)); boxCenter.y = (float)(box->minY + box->maxY) * (0.5f / SCENE_SCALE); @@ -61,19 +56,15 @@ void staticRenderPopulateRooms(struct FrustrumCullingInformation* cullingInfo, M shiftedBox.maxZ = box->maxZ + z; if (isOutsideFrustrum(cullingInfo, &shiftedBox)) { - profileEnd(startTime, 2); continue; } - profileEnd(startTime, 2); boxCenter.x = (float)(shiftedBox.minX + shiftedBox.maxX) * (0.5f / SCENE_SCALE); boxCenter.y = (float)(shiftedBox.minY + shiftedBox.maxY) * (0.5f / SCENE_SCALE); boxCenter.z = (float)(shiftedBox.minZ + shiftedBox.maxZ) * (0.5f / SCENE_SCALE); } - startTime = profileStart(); renderSceneAdd(renderScene, gCurrentLevel->staticContent[i].displayList, matrix, gCurrentLevel->staticContent[i].materialIndex, &boxCenter, NULL); - profileEnd(startTime, 3); } } @@ -128,17 +119,9 @@ void staticRender(struct Transform* cameraTransform, struct FrustrumCullingInfor struct RenderScene* renderScene = renderSceneNew(cameraTransform, renderState, MAX_RENDER_COUNT, visibleRooms); - u64 startTime = profileStart(); staticRenderPopulateRooms(cullingInfo, staticTransforms, renderScene); - profileEnd(startTime, 4); - - startTime = profileStart(); dynamicRenderPopulateRenderScene(dynamicList, stageIndex, renderScene, cameraTransform, cullingInfo, visibleRooms); - profileEnd(startTime, 5); - - startTime = profileStart(); renderSceneGenerate(renderScene, renderState); - profileEnd(startTime, 6); renderSceneFree(renderScene); } diff --git a/src/main.c b/src/main.c index 57e8e01..866aae5 100644 --- a/src/main.c +++ b/src/main.c @@ -22,6 +22,7 @@ #include "sk64/skelatool_animator.h" #include "util/dynamic_asset_loader.h" #include "util/profile.h" +#include "controls/rumble_pak.h" #include "levels/levels.h" #include "savefile/checkpoint.h" @@ -202,10 +203,11 @@ static void gameProc(void* arg) { contactSolverInit(&gContactSolver); portalSurfaceCleanupQueueInit(); savefileLoad(); - levelLoadWithCallbacks(MAIN_MENU); + levelLoadWithCallbacks(7); gCurrentTestSubject = 0; cutsceneRunnerReset(); controllersInit(); + rumblePakClipInit(); initAudio(fps); soundPlayerInit(); skSetSegmentLocation(CHARACTER_ANIMATION_SEGMENT, (unsigned)_animation_segmentSegmentRomStart); @@ -233,6 +235,7 @@ static void gameProc(void* arg) { portalSurfaceCleanupQueueInit(); heapInit(_heapStart, memoryEnd); levelLoadWithCallbacks(levelGetQueued()); + rumblePakClipInit(); cutsceneRunnerReset(); dynamicAssetsReset(); // if a portal fire button is being held diff --git a/src/scene/scene.c b/src/scene/scene.c index 2b65309..b65836a 100644 --- a/src/scene/scene.c +++ b/src/scene/scene.c @@ -329,7 +329,7 @@ u8 gFireGunRumbleWaveData[] = { struct RumblePakWave gFireGunRumbleWave = { .samples = gFireGunRumbleWaveData, .sampleCount = 8, - .samplesPerTick = 1 << 6, + .samplesPerTick = 1 << 5, }; void sceneCheckPortals(struct Scene* scene) { diff --git a/src/util/profile.c b/src/util/profile.c index aee9f85..3c2aedf 100644 --- a/src/util/profile.c +++ b/src/util/profile.c @@ -20,7 +20,7 @@ void profileReport() { OSTime reportStartTime = osGetTime(); gProfileData.lastReportStart = OS_CYCLES_TO_USEC(reportStartTime - gProfileData.lastReportStart); - // gdbSendMessage(GDBDataTypeRawBinary, (char*)&gProfileData, sizeof(struct ProfileData)); + gdbSendMessage(GDBDataTypeRawBinary, (char*)&gProfileData, sizeof(struct ProfileData)); for (int i = 0; i < MAX_PROFILE_BINS; ++i) { gProfileData.timeAccumulation[i] = 0;