From c5d12c17c736a8ddc07daec43b0f1fa826e560e9 Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Wed, 21 Aug 2024 21:18:46 +0200 Subject: [PATCH 1/6] Sending now works --- assets/js/winkey.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/assets/js/winkey.js b/assets/js/winkey.js index 74df8e7d7..c80f79ed1 100644 --- a/assets/js/winkey.js +++ b/assets/js/winkey.js @@ -122,6 +122,7 @@ async function connect() { statusBar.innerText = "Connected"; connectButton.innerText = "Disconnect" + let decoder = new TextDecoderStream(); inputDone = port.readable.pipeTo(decoder.writable); inputStream = decoder.readable; @@ -130,8 +131,10 @@ async function connect() { outputDone = encoder.readable.pipeTo(port.writable); outputStream = encoder.writable; - writeToByte("0x00, 0x02"); - writeToByte("0x02, 0x00"); + // Seems this might not be needed, leaving it for now + // writeToByte("[0x00, 0x02]"); + // writeToByte("[0x02, 0x00]"); + $('#winkey_buttons').show(); @@ -152,7 +155,8 @@ async function writeToStream(line) { var enc = new TextEncoder(); // always utf-8 const writer = outputStream.getWriter(); - writer.write(line); + + writer.write([line.toUpperCase()]); writer.releaseLock(); } @@ -187,11 +191,11 @@ async function disconnect() { //When the send button is pressed function clickSend() { - writeToStream(sendText.value); - writeToStream("\r"); - - //and clear the input field, so it's clear it has been sent - sendText.value = ""; + writeToStream(sendText.value).then(function() { + // writeToStream("\r"); + //and clear the input field, so it's clear it has been sent + $('#sendText').val(''); + }); } From 9fd106356320671ff366a5d124287ff811056e20 Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Sat, 24 Aug 2024 14:53:42 +0200 Subject: [PATCH 2/6] Init works. Can now set CW speed. --- application/views/qso/index.php | 25 +++++---- assets/js/winkey.js | 98 +++++++++++++++++++++++++-------- 2 files changed, 90 insertions(+), 33 deletions(-) diff --git a/application/views/qso/index.php b/application/views/qso/index.php index cd3f8c42f..c82e5fd78 100644 --- a/application/views/qso/index.php +++ b/application/views/qso/index.php @@ -647,22 +647,25 @@ + -
- diff --git a/assets/js/winkey.js b/assets/js/winkey.js index c80f79ed1..c4bc653f5 100644 --- a/assets/js/winkey.js +++ b/assets/js/winkey.js @@ -28,7 +28,19 @@ ModeSelected.addEventListener('change', (event) => { } }); +$('#winkeycwspeed').change(function (event) { + // Get the value from the input + let speed = parseInt($('#winkeycwspeed').val(), 10); + // Convert to hexadecimal and pad if necessary + let hexspeed = speed.toString(16).padStart(2, '0'); + + // Create the command + let command = `02 ${hexspeed}`; + + // Send the command as hex bytes + sendHexToSerial(command); +}); let function1Name, function1Macro, function2Name, function2Macro, function3Name, function3Macro, function4Name, function4Macro, function5Name, function5Macro; @@ -122,19 +134,16 @@ async function connect() { statusBar.innerText = "Connected"; connectButton.innerText = "Disconnect" - let decoder = new TextDecoderStream(); inputDone = port.readable.pipeTo(decoder.writable); inputStream = decoder.readable; - const encoder = new TextEncoderStream(); - outputDone = encoder.readable.pipeTo(port.writable); - outputStream = encoder.writable; - - // Seems this might not be needed, leaving it for now - // writeToByte("[0x00, 0x02]"); - // writeToByte("[0x02, 0x00]"); - + // Keyer init + sendHexToSerial("00 02"); + await delay(300); // Wait for 300ms + sendHexToSerial("02 00"); + await delay(300); // Wait for 300ms + sendHexToSerial("02 14"); // init 20 wpm $('#winkey_buttons').show(); @@ -150,25 +159,70 @@ async function connect() { } } -//Write to the Serial port -async function writeToStream(line) { - var enc = new TextEncoder(); // always utf-8 - - const writer = outputStream.getWriter(); - - writer.write([line.toUpperCase()]); - writer.releaseLock(); +function delay(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); } -async function writeToByte(line) { - const writer = outputStream.getWriter(); - const data = new Uint8Array([line]); - writer.write(data); - writer.releaseLock(); +// Helper function to convert a hex string to a Uint8Array +function hexStringToUint8Array(hexString) { + // Remove any spaces or non-hex characters + hexString = hexString.replace(/[^0-9a-f]/gi, ''); + + // Ensure the string has an even length + if (hexString.length % 2 !== 0) { + console.warn('Hex string has an odd length, padding with a leading zero.'); + hexString = '0' + hexString; + } + + const byteArray = new Uint8Array(hexString.length / 2); + + for (let i = 0; i < hexString.length; i += 2) { + byteArray[i / 2] = parseInt(hexString.substr(i, 2), 16); + } + + return byteArray; +} + +async function sendHexToSerial(hexString) { + if (port && port.writable) { + // Convert the hex string to a Uint8Array + const byteArray = hexStringToUint8Array(hexString); + + // Create a writer from the writable stream + const writer = port.writable.getWriter(); + + try { + // Write the byte array to the serial port + await writer.write(byteArray); + } catch (error) { + console.error('Error writing to serial port:', error); + } finally { + // Release the lock on the writer + writer.releaseLock(); + } + } else { + console.error('Port is not available or writable.'); + } +} + +//Write to the Serial port +async function writeToStream(line) { + const outputStream = port.writable.getWriter(); + + // Convert the text to a Uint8Array + const encoder = new TextEncoder(); + const buffer = encoder.encode(line.toUpperCase()); + + // Write the Uint8Array to the serial port + await outputStream.write(buffer); + + // Release the stream lock + outputStream.releaseLock(); } //Disconnect from the Serial port async function disconnect() { + sendHexToSerial("00 03"); if (reader) { await reader.cancel(); From 5c4e50c231f5bfce11bb6b69401b8f400deecdae Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Sat, 24 Aug 2024 16:01:38 +0200 Subject: [PATCH 3/6] Added button to stop CW sending --- application/views/qso/index.php | 1 + assets/js/winkey.js | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/application/views/qso/index.php b/application/views/qso/index.php index c82e5fd78..6cc47c1ca 100644 --- a/application/views/qso/index.php +++ b/application/views/qso/index.php @@ -653,6 +653,7 @@