Change duration to always calculate rather then counting seconds, Remove power display
This commit is contained in:
@@ -29,10 +29,6 @@
|
||||
<p class="text-lg font-medium">RPM</p>
|
||||
<p id="rpm-display" class="text-3xl font-bold">0</p>
|
||||
</div>
|
||||
<div>
|
||||
<p class="text-lg font-medium">Power</p>
|
||||
<p id="power-display" class="text-3xl font-bold">0</p>
|
||||
</div>
|
||||
<div>
|
||||
<p class="text-lg font-medium">Duration</p>
|
||||
<p id="duration-display" class="text-3xl font-bold">00:00</p>
|
||||
@@ -53,13 +49,11 @@
|
||||
<script>
|
||||
// Get DOM elements
|
||||
const rpmDisplay = document.getElementById('rpm-display');
|
||||
const powerDisplay = document.getElementById('power-display');
|
||||
const durationDisplay = document.getElementById('duration-display');
|
||||
const toggleButton = document.getElementById('toggle-button');
|
||||
// Initialize variables
|
||||
let rpm = 0;
|
||||
let power = 0;
|
||||
let duration = 0;
|
||||
let isRunning = false;
|
||||
let startTime = 0;
|
||||
let intervalId = null;
|
||||
@@ -69,35 +63,44 @@
|
||||
const decimalNumber = (num) => parseFloat(num.toFixed(1));
|
||||
|
||||
// Update RPM and Power
|
||||
function updateRpmPower(rpm, power) {
|
||||
function updateRpm(rpm) {
|
||||
rpmDisplay.textContent = integerNumber(rpm);
|
||||
powerDisplay.textContent = integerNumber(power);
|
||||
}
|
||||
|
||||
// Update Duration
|
||||
function updateDuration() {
|
||||
duration++;
|
||||
const now = new Date();
|
||||
const diff = Math.abs(now - startTime) / 1000; // get difference in seconds
|
||||
let hours = Math.floor(diff / (60 * 60));
|
||||
let minutes = Math.floor(diff / 60) % 60;
|
||||
let seconds = Math.floor(diff);
|
||||
|
||||
// Format duration as mm:ss
|
||||
const minutes = Math.floor(duration / 60).toString().padStart(2, '0');
|
||||
const seconds = (duration % 60).toString().padStart(2, '0');
|
||||
durationDisplay.textContent = `${minutes}:${seconds}`;
|
||||
let timeSegments = [];
|
||||
if (hours > 0) {
|
||||
timeSegments.push(hours);
|
||||
}
|
||||
timeSegments.push(minutes.toString().padStart(2, '0'));
|
||||
timeSegments.push(seconds.toString().padStart(2, '0'));
|
||||
|
||||
durationDisplay.textContent = timeSegments.join(':'); // format as "1:32"
|
||||
}
|
||||
|
||||
// Start workout
|
||||
function startWorkout() {
|
||||
isRunning = true;
|
||||
startTime = Date.now();
|
||||
intervalId = setInterval(() => {
|
||||
updateDuration();
|
||||
}, 1000);
|
||||
// Connect to cadence sensor
|
||||
connect({
|
||||
onChange: parseCSC,
|
||||
}).catch((err) => {
|
||||
}).then(() => {
|
||||
isRunning = true;
|
||||
startTime = Date.now();
|
||||
toggleButton.textContent = 'Stop';
|
||||
intervalId = setInterval(() => {
|
||||
updateDuration();
|
||||
}, 1000);
|
||||
})
|
||||
.catch((err) => {
|
||||
swal("Oops", err.message, "error");
|
||||
});
|
||||
toggleButton.textContent = 'Stop';
|
||||
}
|
||||
|
||||
// Stop workout
|
||||
@@ -188,9 +191,9 @@
|
||||
|
||||
// Call the updateGraph function to start the live updates
|
||||
updateGraph();
|
||||
</script>
|
||||
|
||||
<script>
|
||||
|
||||
// BLE code
|
||||
let characteristic = null;
|
||||
let prevRes = null;
|
||||
|
||||
@@ -214,11 +217,6 @@
|
||||
console.log("> Notifications started");
|
||||
characteristic.addEventListener("characteristicvaluechanged", props.onChange);
|
||||
console.log("> Characteristic value changed event listener added");
|
||||
|
||||
/*
|
||||
btn.classList.remove("bg-blue-600");
|
||||
btn.classList.add("bg-green-600");
|
||||
*/
|
||||
}
|
||||
|
||||
async function disconnect() {
|
||||
@@ -230,10 +228,6 @@
|
||||
"characteristicvaluechanged",
|
||||
handleNotifications
|
||||
);
|
||||
/*
|
||||
btn.classList.remove("bg-green-600");
|
||||
btn.classList.add("bg-blue-600");
|
||||
*/
|
||||
} catch (error) {
|
||||
console.log("Argh! " + error);
|
||||
swal("Oops", error, "error");
|
||||
@@ -269,7 +263,6 @@
|
||||
if (prevRes) {
|
||||
let rpm = revsToRPM(prevRes, res);
|
||||
if (rpm > 0) {
|
||||
|
||||
const newData = {
|
||||
x: data.length,
|
||||
y: decimalNumber(rpm),
|
||||
@@ -278,21 +271,10 @@
|
||||
// Add the new data point to the data array
|
||||
data.push(newData);
|
||||
|
||||
updateRpmPower(rpm, 0);
|
||||
updateRpm(rpm);
|
||||
updateGraph();
|
||||
|
||||
workout.push({ rpm, timestamp: new Date() })
|
||||
/*
|
||||
fetch("/cadence", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ rpm: parseFloat(rpm.toFixed(2)), id: 1 }),
|
||||
});
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
prevRes = res;
|
||||
|
||||
Reference in New Issue
Block a user