Finalize documenting and clean up of website code
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

Signed-off-by: Severin Kaderli <severin@kaderli.dev>
This commit is contained in:
Severin Kaderli 2022-11-23 18:35:04 +01:00
parent 436e261fa7
commit 79fbcdedc3
Signed by: severinkaderli
GPG key ID: F419F8835B72F0C4
5 changed files with 142 additions and 69 deletions

View file

@ -1,27 +1,61 @@
const hideClass = "hide";
let isSending = false;
/**
* Global flag that indicates if any bits should be transmitted. This flag
* is used to stop the transmission of bits.
*
* @type {Boolean}
*/
let isTransmitting = false;
/**
* Array that contains all currently running web workers.
*
* @type {Worker[]}
*/
let workers = [];
/**
* @type {HTMLInputElement}
*/
const textInput = document.getElementById("textInput");
/**
* @type {HTMLButtonElement}
*/
const startSendingButton = document.getElementById("start-sending-button");
/**
* @type {HTMLButtonElement}
*/
const stopSendingButton = document.getElementById("stop-sending-button");
/**
* @type {HTMLButtonElement}
*/
const startCalibrationButton = document.getElementById("start-calibration-button");
/**
* @type {HTMLButtonElement}
*/
const stopCalibrationButton = document.getElementById("stop-calibration-button");
// Add event listeners to the buttons
startCalibrationButton.addEventListener("click", startCalibration);
stopCalibrationButton.addEventListener("click", stopCalibration);
startSendingButton.addEventListener("click", startSending);
stopSendingButton.addEventListener("click", stopSending);
function getWebWorker() {
return new Worker("./scripts/Worker.js");
}
/**
* Starts the web workers and add them to the global worker array.
*/
function startWorkers() {
for (let i = 0; i < 8; i++) {
workers.push(getWebWorker());
for (let i = 0; i < Constants.NUMBER_OF_WORKERS; i++) {
workers.push(new Worker("./scripts/Worker.js"));
}
}
/**
* Terminates all currently running workers and clear out the worker array.
*/
function stopWorkers() {
for (const worker of workers) {
worker.terminate();
@ -30,32 +64,38 @@ function stopWorkers() {
workers = [];
}
/**
* Starts the calibration process by simply starting all web workers.
*/
function startCalibration() {
console.log("Starting calibration");
startCalibrationButton.classList.add(hideClass);
stopCalibrationButton.classList.remove(hideClass);
Utility.setMessage("Calibration currently ongoing.");
startCalibrationButton.classList.add(Constants.HIDE_CLASS);
stopCalibrationButton.classList.remove(Constants.HIDE_CLASS);
startWorkers();
}
/**
* Stops the calibration process by stopping all currently running web workers.
*/
function stopCalibration() {
console.log("Stopping calibration");
stopCalibrationButton.classList.add(hideClass);
startCalibrationButton.classList.remove(hideClass);
Utility.setMessage("");
stopCalibrationButton.classList.add(Constants.HIDE_CLASS);
startCalibrationButton.classList.remove(Constants.HIDE_CLASS);
stopWorkers();
}
/**
* Starts sending the message in the input after validating that the input is
* valid.
*/
function startSending() {
textInput.setCustomValidity("");
// Validate the input
textInput.setCustomValidity("");
if (!textInput.checkValidity()) {
console.log(textInput.validity);
if (textInput.validity.valueMissing) {
textInput.setCustomValidity("Please enter between 1 and 16 characters.");
}
@ -68,37 +108,41 @@ function startSending() {
return;
}
startSendingButton.classList.add(hideClass);
stopSendingButton.classList.remove(hideClass);
Utility.setMessage(`Sending message: ${textInput.value}`);
console.log(`Start sending message: ${textInput.value}`);
startSendingButton.classList.add(Constants.HIDE_CLASS);
stopSendingButton.classList.remove(Constants.HIDE_CLASS);
const packet = new Packet(textInput.value);
const signal = Utility.manchesterEncode(packet.getData());
isSending = true;
isTransmitting = true;
transmitSignal(signal);
}
/**
* Stop the sending process.
*/
function stopSending() {
console.log("Stop Sending");
Utility.setMessage("");
stopSendingButton.classList.add(hideClass);
startSendingButton.classList.remove(hideClass);
isSending = false;
stopSendingButton.classList.add(Constants.HIDE_CLASS);
startSendingButton.classList.remove(Constants.HIDE_CLASS);
isTransmitting = false;
}
/**
* Transmit the given bit array until the sending is stopped.
* Transmits the given bit array until the sending is stopped. The transmission
* is stopped when the global isTransmitting flag is set to false.
*
* @param {Number[]} signal
*/
async function transmitSignal(signal) {
while (isSending) {
while (isTransmitting) {
// Send the preamble
for (const bit of PREAMBLE) {
if (!isSending) {
for (const bit of Constants.PREAMBLE) {
if (!isTransmitting) {
stopWorkers();
return;
}
@ -108,7 +152,7 @@ async function transmitSignal(signal) {
}
for (let i = 0; i < signal.length; i++) {
if (!isSending) {
if (!isTransmitting) {
stopWorkers();
return;
}
@ -120,17 +164,17 @@ async function transmitSignal(signal) {
}
/**
* This either starts or stops the web workers depending on the bit value
* and then waits for a duration of CLOCK_TIME.
* Starts or stops the web workers depending on the bit value and then waits
* for a duration of Constants.CLOCK_TIME.
*
* @param {Number} bit
*/
function transmitBit(bit) {
if (bit === 1) {
startWorkers();
return new Promise((resolve) => setTimeout(resolve, CLOCK_TIME));
return new Promise((resolve) => setTimeout(resolve, Constants.CLOCK_TIME));
}
stopWorkers();
return new Promise((resolve) => setTimeout(resolve, CLOCK_TIME));
return new Promise((resolve) => setTimeout(resolve, Constants.CLOCK_TIME));
}