rower-website
Version:
A web server which access the PM5 rowing machine over usb and displays the wattage value.
89 lines (74 loc) • 3.32 kB
JavaScript
var socket = io();
let wattage = 0;
let startWattage = wattage;
let lastWattage = wattage;
let maxWattage = wattage;
let average = 0;
let sum = 0; // Variable to store the sum of values
let count = 0; // Variable to store the number of values
function computeCumulativeAverage(newValue) {
sum += newValue;
count++;
return sum / count;
}
function computeMaxWattage(newValue) {
if (newValue > maxWattage) {
maxWattage = newValue; // Update lastMaxWattage if newValue is greater
}
return maxWattage; // Return the computed maximum wattage
}
document.addEventListener("DOMContentLoaded", function() {
var countdownElement = document.getElementById("countdown");
var tenthsCountdownElement = document.getElementById("tenthsCountdown");
var countdownDuration = 5;
var initialMessage = "Ready?";
countdownElement.textContent = initialMessage;
function startCountdown() {
var countdown = countdownDuration;
var countdownInterval = setInterval(function() {
countdown--;
if (countdown > 1) {
countdownElement.textContent = (countdown-1).toString();
} else if (countdown === 1){
countdownElement.textContent = "Go!";
// Start the tenths of a second countdown
startTenthsCountdown();
} else {
countdownElement.textContent = "";
clearInterval(countdownInterval);
}
}, 1000);
}
function startTenthsCountdown() {
var tenthsCountdown = 100;
var tenthsCountdownInterval = setInterval(function() {
tenthsCountdown--;
var seconds = Math.floor(tenthsCountdown / 10);
var tenths = tenthsCountdown % 10;
tenthsCountdownElement.textContent = seconds + "." + tenths;
if (tenthsCountdown > 0) {
socket.on('usbData', function(number) {
wattage = number;
maxWattage = Math.round(computeMaxWattage(wattage));
average = Math.round(computeCumulativeAverage(wattage));
document.getElementById('wattage').textContent = 'Wattage: ' + wattage +"W";
document.getElementById('max').textContent = 'Peak: ' + maxWattage +"W";
document.getElementById('average').textContent = 'Average: ' + average +"W";
// Function to update the bar chart
function updateChart() {
document.getElementById('current-bar').style.bottom = `${wattage/7}%`;
document.getElementById('average-bar').style.bottom = `${average/7}%`;
document.getElementById('peak-bar').style.bottom = `${maxWattage/7}%`;
}
// Call the updateChart function to initialize the chart
updateChart();
});
}
if (tenthsCountdown <= 0) {
clearInterval(tenthsCountdownInterval);
tenthsCountdownElement.textContent = "STOP!";
}
}, 100);
}
startCountdown();
});