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