v4
This commit is contained in:
parent
688e42d5dc
commit
f99b5fdd9e
145
index.php
145
index.php
@ -205,14 +205,6 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
|
|||||||
<h2>Active Session Controller</h2>
|
<h2>Active Session Controller</h2>
|
||||||
<button id="startButton" disabled>Start Massage</button>
|
<button id="startButton" disabled>Start Massage</button>
|
||||||
<button id="stopButton" disabled>Stop Massage</button>
|
<button id="stopButton" disabled>Stop Massage</button>
|
||||||
<p>Time Remaining: <span id="timerDisplay">00:00</span></p>
|
|
||||||
<label for="durationSelect">Duration:</label>
|
|
||||||
<select id="durationSelect">
|
|
||||||
<option value="30">30 seconds</option>
|
|
||||||
<option value="60">1 minute</option>
|
|
||||||
<option value="120">2 minutes</option>
|
|
||||||
<option value="300">5 minutes</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -231,13 +223,11 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
|
|||||||
const speedValue = document.getElementById('speedValue');
|
const speedValue = document.getElementById('speedValue');
|
||||||
const startButton = document.getElementById('startButton');
|
const startButton = document.getElementById('startButton');
|
||||||
const stopButton = document.getElementById('stopButton');
|
const stopButton = document.getElementById('stopButton');
|
||||||
const timerDisplay = document.getElementById('timerDisplay');
|
|
||||||
const durationSelect = document.getElementById('durationSelect');
|
|
||||||
|
|
||||||
let gamepad = null;
|
let gamepad = null;
|
||||||
let vibrationInterval = null;
|
let vibrationInterval = null;
|
||||||
let sessionTimer = null;
|
|
||||||
let timeRemaining = 0;
|
|
||||||
let currentPattern = '';
|
let currentPattern = '';
|
||||||
|
|
||||||
// Gamepad API
|
// Gamepad API
|
||||||
@ -252,11 +242,33 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
|
|||||||
window.addEventListener('gamepaddisconnected', (e) => {
|
window.addEventListener('gamepaddisconnected', (e) => {
|
||||||
console.log('Gamepad disconnected from index %d: %s',
|
console.log('Gamepad disconnected from index %d: %s',
|
||||||
e.gamepad.index, e.gamepad.id);
|
e.gamepad.index, e.gamepad.id);
|
||||||
gamepad = null;
|
if (gamepad && gamepad.index === e.gamepad.index) {
|
||||||
|
gamepad = null;
|
||||||
|
}
|
||||||
updateGamepadStatus();
|
updateGamepadStatus();
|
||||||
stopMassage();
|
stopMassage();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Polling for gamepads (required for some browsers/situations)
|
||||||
|
setInterval(() => {
|
||||||
|
const gamepads = navigator.getGamepads();
|
||||||
|
let found = false;
|
||||||
|
for (let i = 0; i < gamepads.length; i++) {
|
||||||
|
if (gamepads[i]) {
|
||||||
|
if (!gamepad) {
|
||||||
|
gamepad = gamepads[i];
|
||||||
|
updateGamepadStatus();
|
||||||
|
}
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found && gamepad) {
|
||||||
|
gamepad = null;
|
||||||
|
updateGamepadStatus();
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
function updateGamepadStatus() {
|
function updateGamepadStatus() {
|
||||||
if (gamepad) {
|
if (gamepad) {
|
||||||
gamepadStatus.textContent = 'Connected';
|
gamepadStatus.textContent = 'Connected';
|
||||||
@ -264,51 +276,73 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
|
|||||||
gamepadIdDisplay.textContent = `ID: ${gamepad.id}`;
|
gamepadIdDisplay.textContent = `ID: ${gamepad.id}`;
|
||||||
startButton.disabled = false;
|
startButton.disabled = false;
|
||||||
} else {
|
} else {
|
||||||
gamepadStatus.textContent = 'Disconnected';
|
gamepadStatus.textContent = 'Disconnected (Press a button!)';
|
||||||
gamepadStatus.className = 'status-disconnected';
|
gamepadStatus.className = 'status-disconnected';
|
||||||
gamepadIdDisplay.textContent = '';
|
gamepadIdDisplay.textContent = 'Ensure your controller is plugged in and press any button to activate.';
|
||||||
startButton.disabled = true;
|
startButton.disabled = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Haptic Patterns
|
// Haptic Patterns
|
||||||
const patterns = {
|
const patterns = {
|
||||||
knead: (intensity, speed) => [{ startDelay: 0, duration: 100 * speed, weakMagnitude: intensity * 0.7, strongMagnitude: intensity },
|
knead: (intensity, speed) => [
|
||||||
{ startDelay: 100 * speed, duration: 100 * speed, weakMagnitude: intensity, strongMagnitude: intensity * 0.7 }],
|
{ duration: 200 * speed, weakMagnitude: intensity * 0.5, strongMagnitude: intensity },
|
||||||
tap: (intensity, speed) => [{ startDelay: 0, duration: 50 * speed, weakMagnitude: intensity, strongMagnitude: intensity }],
|
{ duration: 200 * speed, weakMagnitude: intensity, strongMagnitude: intensity * 0.5 }
|
||||||
wave: (intensity, speed) => [{ startDelay: 0, duration: 200 * speed, weakMagnitude: intensity * 0.2, strongMagnitude: intensity },
|
],
|
||||||
{ startDelay: 200 * speed, duration: 200 * speed, weakMagnitude: intensity, strongMagnitude: intensity * 0.2 }],
|
tap: (intensity, speed) => [
|
||||||
sports_recovery: (intensity, speed) => [ // Strong, pulsating vibration
|
{ duration: 100 * speed, weakMagnitude: intensity, strongMagnitude: intensity },
|
||||||
{ startDelay: 0, duration: 150 * speed, weakMagnitude: intensity, strongMagnitude: intensity },
|
{ duration: 100 * speed, weakMagnitude: 0, strongMagnitude: 0 }
|
||||||
{ startDelay: 150 * speed, duration: 50 * speed, weakMagnitude: intensity * 0.2, strongMagnitude: intensity * 0.2 },
|
],
|
||||||
{ startDelay: 200 * speed, duration: 150 * speed, weakMagnitude: intensity, strongMagnitude: intensity },
|
wave: (intensity, speed) => [
|
||||||
{ startDelay: 350 * speed, duration: 50 * speed, weakMagnitude: intensity * 0.2, strongMagnitude: intensity * 0.2 },
|
{ duration: 400 * speed, weakMagnitude: intensity * 0.2, strongMagnitude: intensity },
|
||||||
|
{ duration: 400 * speed, weakMagnitude: intensity, strongMagnitude: intensity * 0.2 }
|
||||||
|
],
|
||||||
|
sports_recovery: (intensity, speed) => [
|
||||||
|
{ duration: 150 * speed, weakMagnitude: intensity, strongMagnitude: intensity },
|
||||||
|
{ duration: 50 * speed, weakMagnitude: 0, strongMagnitude: 0 },
|
||||||
|
{ duration: 150 * speed, weakMagnitude: intensity, strongMagnitude: intensity },
|
||||||
|
{ duration: 150 * speed, weakMagnitude: 0.2, strongMagnitude: 0.2 }
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let patternStep = 0;
|
||||||
|
|
||||||
function playHapticPattern() {
|
function playHapticPattern() {
|
||||||
if (gamepad && gamepad.vibrationActuator) {
|
// Re-fetch gamepad to get the most up-to-date object
|
||||||
|
const gamepads = navigator.getGamepads();
|
||||||
|
const gp = gamepads[gamepad?.index];
|
||||||
|
|
||||||
|
if (gp && gp.vibrationActuator) {
|
||||||
const selectedPattern = patternSelect.value;
|
const selectedPattern = patternSelect.value;
|
||||||
const intensity = parseFloat(intensityRange.value);
|
const intensity = parseFloat(intensityRange.value);
|
||||||
const speed = parseFloat(speedRange.value);
|
const speed = parseFloat(speedRange.value);
|
||||||
|
|
||||||
currentPattern = selectedPattern; // Store the current pattern
|
const steps = patterns[selectedPattern](intensity, speed);
|
||||||
|
const step = steps[patternStep % steps.length];
|
||||||
|
|
||||||
const effect = patterns[selectedPattern](intensity, speed);
|
gp.vibrationActuator.playEffect('dual-rumble', {
|
||||||
gamepad.vibrationActuator.playEffect('dual-rumble', {
|
|
||||||
startDelay: 0,
|
startDelay: 0,
|
||||||
duration: 0, // Duration will be managed by interval
|
duration: step.duration,
|
||||||
weakMagnitude: 0,
|
weakMagnitude: step.weakMagnitude,
|
||||||
strongMagnitude: 0,
|
strongMagnitude: step.strongMagnitude
|
||||||
haptics: effect
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
patternStep++;
|
||||||
|
|
||||||
|
// Schedule next step
|
||||||
|
vibrationInterval = setTimeout(playHapticPattern, step.duration);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function startMassage() {
|
function startMassage() {
|
||||||
if (!gamepad) {
|
if (!gamepad) {
|
||||||
alert('Please connect a gamepad first!');
|
// Try to get gamepad from navigator if not detected by event
|
||||||
return;
|
const gamepads = navigator.getGamepads();
|
||||||
|
gamepad = Array.from(gamepads).find(g => g !== null);
|
||||||
|
if (!gamepad) {
|
||||||
|
alert('Please connect a gamepad first and press any button!');
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
startButton.disabled = true;
|
startButton.disabled = true;
|
||||||
@ -316,39 +350,25 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
|
|||||||
patternSelect.disabled = true;
|
patternSelect.disabled = true;
|
||||||
intensityRange.disabled = true;
|
intensityRange.disabled = true;
|
||||||
speedRange.disabled = true;
|
speedRange.disabled = true;
|
||||||
durationSelect.disabled = true;
|
|
||||||
|
|
||||||
timeRemaining = parseInt(durationSelect.value);
|
patternStep = 0;
|
||||||
updateTimerDisplay();
|
|
||||||
|
|
||||||
// Start the haptic pattern
|
// Start the haptic pattern loop
|
||||||
playHapticPattern();
|
playHapticPattern();
|
||||||
// Restart pattern every cycle if it has a specific duration (e.g., tap)
|
|
||||||
vibrationInterval = setInterval(playHapticPattern, 400 * parseFloat(speedRange.value)); // Adjust interval based on pattern complexity
|
|
||||||
|
|
||||||
// Start the session timer
|
logSession(patternSelect.value, parseFloat(intensityRange.value), 0); // Log with 0 duration for manual sessions
|
||||||
sessionTimer = setInterval(() => {
|
|
||||||
timeRemaining--;
|
|
||||||
updateTimerDisplay();
|
|
||||||
if (timeRemaining <= 0) {
|
|
||||||
stopMassage();
|
|
||||||
}
|
|
||||||
}, 1000);
|
|
||||||
|
|
||||||
logSession(currentPattern, parseFloat(intensityRange.value), parseInt(durationSelect.value));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function stopMassage() {
|
function stopMassage() {
|
||||||
if (vibrationInterval) {
|
if (vibrationInterval) {
|
||||||
clearInterval(vibrationInterval);
|
clearTimeout(vibrationInterval);
|
||||||
vibrationInterval = null;
|
vibrationInterval = null;
|
||||||
}
|
}
|
||||||
if (sessionTimer) {
|
// Stop vibration
|
||||||
clearInterval(sessionTimer);
|
const gamepads = navigator.getGamepads();
|
||||||
sessionTimer = null;
|
const gp = gamepads[gamepad?.index];
|
||||||
}
|
if (gp && gp.vibrationActuator) {
|
||||||
if (gamepad && gamepad.vibrationActuator) {
|
gp.vibrationActuator.reset();
|
||||||
gamepad.vibrationActuator.reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
startButton.disabled = false;
|
startButton.disabled = false;
|
||||||
@ -356,16 +376,9 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
|
|||||||
patternSelect.disabled = false;
|
patternSelect.disabled = false;
|
||||||
intensityRange.disabled = false;
|
intensityRange.disabled = false;
|
||||||
speedRange.disabled = false;
|
speedRange.disabled = false;
|
||||||
durationSelect.disabled = false;
|
|
||||||
|
|
||||||
timerDisplay.textContent = '00:00';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateTimerDisplay() {
|
|
||||||
const minutes = Math.floor(timeRemaining / 60);
|
|
||||||
const seconds = timeRemaining % 60;
|
|
||||||
timerDisplay.textContent = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Event Listeners
|
// Event Listeners
|
||||||
startButton.addEventListener('click', startMassage);
|
startButton.addEventListener('click', startMassage);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user