Skip to content

Commit

Permalink
Merge pull request #3 from jgphilpott/bug/1/response-data-comes-in-sm…
Browse files Browse the repository at this point in the history
…all-chunks-making-it-hard-to-read

#1: response data comes in small chunks making it hard to read
  • Loading branch information
jgphilpott authored Apr 30, 2024
2 parents 57a36f2 + 12ec237 commit 5fb4315
Showing 1 changed file with 159 additions and 111 deletions.
270 changes: 159 additions & 111 deletions serial/browser/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function connect() {

})

connected()
await connected()

read()

Expand All @@ -72,9 +72,15 @@ async function disconnect() {

try {

await device.forget()
if (device.connected) {

disconnected()
device.connected = false

await device.forget()

await disconnected()

}

} catch (error) {

Expand All @@ -86,7 +92,7 @@ async function disconnect() {

}

function connected() {
async function connected() {

device.connected = true

Expand All @@ -110,7 +116,7 @@ function connected() {

}

function disconnected() {
async function disconnected() {

device.connected = false

Expand All @@ -134,6 +140,8 @@ async function read() {

while (device.readable) {

let response = ""

const decoder = new TextDecoder()
const reader = device.readable.getReader()

Expand All @@ -145,45 +153,35 @@ async function read() {

const {value, done} = await reader.read()

let text = decoder.decode(value).replace("echo:", "").replace("\n", " ")
let chunk = decoder.decode(value).replace(/\n|\r|\n\r|\r\n/g, "§")

text = text.replace("cold extrusion prevented", "<span class='info'>Cold Extrusion Prevented</span><span class='emoji'> 🧊</span>")
text = text.replace("Unknown command:", "<span class='error'>Unknown command:</span>")
text = text.replace("Settings Stored", "<span class='info'>Settings Stored</span>")
text = text.replace("Error:", "<span class='error'>Error:</span>")
text = text.replace("busy:", "<span class='info'>Busy:</span>")
text = text.replace("ok", "<span class='success'>OK</span>")
if (chunk.includes("§")) {

text = text.replace("X:", "<b class='x'>X:</b> ")
text = text.replace("Y:", "<b class='y'>Y:</b> ")
text = text.replace("Z:", "<b class='z'>Z:</b> ")
text = text.replace("E:", "<b class='e'>E:</b> ")
let lines = chunk.split("§")

text = text.replace("T:", "<b class='t'>T:</b> ")
text = text.replace("B:", "<b class='b'>B:</b> ")
text = text.replace("W:", "<b class='w'>W:</b> ")
lines.forEach((line, index) => {

if (text.includes("X:") && text.includes("Y:") && text.includes("Z:") && text.includes("E:")) {
text += "<span class='emoji'> 📌</span>"
}
response += line

if (text.includes("T:") && text.includes("B:")) {
text += "<span class='emoji'> 🌡️</span>"
}
if (lines[index + 1] != undefined) {

if (text.includes("W:")) {
text += "<span class='emoji'> ⏰</span>"
}
if (response.length) {

if (text.includes("Settings Stored")) {
text += "<span class='emoji'> 💾</span>"
}
processOutput(response)

if (text.includes("kill")) {
text += "<span class='emoji'> 💀</span>"
}
}

response = ""

}

log("output", text)
})

} else {

response += chunk

}

if (done) break

Expand All @@ -193,10 +191,14 @@ async function read() {

console.log("Stopped reading serial port.")

console.log(error)

} finally {

reader.releaseLock()

await disconnect()

break

}
Expand All @@ -216,6 +218,109 @@ async function write(text) {

}

function processInput(text) {

text += " "

// Autohome
if (text.includes("G28 ")) text += "<span class='emoji'>🏠</span>" // Go home.

// Move
if (text.includes("G0 ")) text += "<span class='emoji'>👣 ➜</span>" // Non-extrusion movement.
if (text.includes("G1 ")) text += "<span class='emoji'>👣 ➜</span>" // Extrusion movement.
if (text.includes("G2 ")) text += "<span class='emoji'>👣 ⤵</span>" // Clockwise arc movement.
if (text.includes("G3 ")) text += "<span class='emoji'>👣 ⤴</span>" // Counter clockwise arc movement.
if (text.includes("G5 ")) text += "<span class='emoji'>👣 ∿</span>" // Bézier curve movement.

// Temperature
if (text.includes("M104 ")) text += "<span class='emoji'>🔥</span>" // Set nozzle temperature.
if (text.includes("M109 ")) text += "<span class='emoji'>🔥⏰</span>" // Wait for nozzle temperature.
if (text.includes("M140 ")) text += "<span class='emoji'>🔥</span>" // Set bed temperature.
if (text.includes("M190 ")) text += "<span class='emoji'>🔥⏰</span>" // Wait for bed temperature.

// Measurement
if (text.includes("G20 ")) text += "<span class='emoji'>📏</span>" // Set length units to inches.
if (text.includes("G21 ")) text += "<span class='emoji'>📏</span>" // Set length units to millimeters.
if (text.includes("M149 ")) text += "<span class='emoji'>📏🌡️</span>" // Set temperature units to celsius [C], fahrenheit [F] or kelvin [K].

// Pause/Wait
if (text.includes("G4 ")) text += "<span class='emoji'>⏲️</span>" // Uninterruptible pause command.
if (text.includes("M0 ")) text += "<span class='emoji'>⏰</span>" // Interruptible pause command.
if (text.includes("M1 ")) text += "<span class='emoji'>⏰</span>" // Interruptible pause command.
if (text.includes("M400 ")) text += "<span class='emoji'>💤</span>" // Wait for queue to finish.

// Fan
if (text.includes("M106 ")) text += "<span class='emoji'>🪭</span>" // Set fan speed.
if (text.includes("M107 ")) text += "<span class='emoji'>🪭🚫</span>" // Turn fan off.

// Sound
if (text.includes("M300 ")) text += "<span class='emoji'>🔊</span>" // Play Sound.

// Reports
if (text.includes("M114 ")) text += "<span class='emoji'>📌</span>" // Report Current Position.
if (text.includes("M105 ")) text += "<span class='emoji'>🌡️</span>" // Report Current Temperatures.

// Messaging
if (text.includes("M117 ")) text += "<span class='emoji'>💬</span>" // Set an LCD Message.
if (text.includes("M118 ")) text += "<span class='emoji'>📝</span>" // Send a message to the connected host.

// Settings
if (text.includes("M500 ")) text += "<span class='emoji'>💾</span>" // Save Settings.
if (text.includes("M501 ")) text += "<span class='emoji'>📂</span>" // Load Settings.
if (text.includes("M502 ")) text += "<span class='emoji'>🔄</span>" // Reset Settings.

// Interrupt/Shutdown
if (text.includes("M108 ")) text += "<span class='emoji'>⛔</span>" // Interrupt command.
if (text.includes("M112 ")) text += "<span class='emoji'>🛑</span>" // Full Shutdown.

log("input", text)

}

function processOutput(text) {

text = text.replace("echo:", "")

text = text.replace("cold extrusion prevented", "<span class='info'>Cold Extrusion Prevented</span><span class='emoji'> 🧊</span>")
text = text.replace("Unknown command:", "<span class='error'>Unknown command:</span>")
text = text.replace("Settings Stored", "<span class='info'>Settings Stored</span>")
text = text.replace("Error:", "<span class='error'>Error:</span>")
text = text.replace("busy:", "<span class='info'>Busy:</span>")
text = text.replace("ok", "<span class='success'>OK</span>")

text = text.replace(/X:/g, "<b class='x'>X:</b> ")
text = text.replace(/Y:/g, "<b class='y'>Y:</b> ")
text = text.replace(/Z:/g, "<b class='z'>Z:</b> ")
text = text.replace(/E:/g, "<b class='e'>E:</b> ")

text = text.replace(/T:/g, "<b class='t'>T:</b> ")
text = text.replace(/B:/g, "<b class='b'>B:</b> ")
text = text.replace(/W:/g, "<b class='w'>W:</b> ")

if (text.includes("X:") && text.includes("Y:") && text.includes("Z:") && text.includes("E:")) {
text += "<span class='emoji'> 📌</span>"
}

if (text.includes("T:") && text.includes("B:")) {
text += "<span class='emoji'> 🌡️</span>"
}

if (text.includes("W:")) {
text += "<span class='emoji'> ⏰</span>"
}

if (text.includes("Settings Stored")) {
text += "<span class='emoji'> 💾</span>"
}

if (text.includes("kill")) {
text += "<span class='emoji'> 💀</span>"
}

log("output", text)

}

async function reset() {

if (confirm("Are you sure you want to reset? This will clear your logs and restore all settings to their default values.")) {
Expand All @@ -226,14 +331,14 @@ async function reset() {
usbVendorId = null
usbProductId = null

await disconnect()

localWrite("inputs", [])
localWrite("outputs", [])
localWrite("history", [])

$("img#reset").rotate(360)

if (device.connected) await disconnect()

$("textarea#prompt").val("").change()
$("textarea#prompt").attr("rows", 1)

Expand Down Expand Up @@ -268,6 +373,22 @@ async function reset() {

}

function log(zone, text) {

time = "<span class='time'>" + new Date().toLocaleTimeString([], {hour12: false}) + "</span>"
text = "<p>" + time + "<span class='pointer'> >>> </span>" + text + "</p>"

let logs = localRead(zone + "s"); logs.push(text)

$("#" + zone + "").append(text)

let div = document.getElementById(zone)
div.scrollTop = div.scrollHeight

localWrite(zone + "s", logs)

}

function toggleStyle(type, value) {

let noTimestamps =
Expand Down Expand Up @@ -321,22 +442,6 @@ function toggleStyle(type, value) {

}

function log(zone, text) {

time = "<span class='time'>" + new Date().toLocaleTimeString([], {hour12: false}) + "</span>"
text = "<p>" + time + "<span class='pointer'> >>> </span>" + text + "</p>"

let logs = localRead(zone + "s"); logs.push(text)

$("#" + zone + "").append(text)

let div = document.getElementById(zone)
div.scrollTop = div.scrollHeight

localWrite(zone + "s", logs)

}

document.addEventListener("DOMContentLoaded", (event) => {

if (navigator.serial) {
Expand Down Expand Up @@ -540,64 +645,7 @@ document.addEventListener("DOMContentLoaded", (event) => {

text.split("\n").forEach((command) => {

if (command.length) {

command += " "

// Autohome
if (command.includes("G28 ")) command += "<span class='emoji'>🏠</span>" // Go home.

// Move
if (command.includes("G0 ")) command += "<span class='emoji'>👣 ➜</span>" // Non-extrusion movement.
if (command.includes("G1 ")) command += "<span class='emoji'>👣 ➜</span>" // Extrusion movement.
if (command.includes("G2 ")) command += "<span class='emoji'>👣 ⤵</span>" // Clockwise arc movement.
if (command.includes("G3 ")) command += "<span class='emoji'>👣 ⤴</span>" // Counter clockwise arc movement.
if (command.includes("G5 ")) command += "<span class='emoji'>👣 ∿</span>" // Bézier curve movement.

// Temperature
if (command.includes("M104 ")) command += "<span class='emoji'>🔥</span>" // Set nozzle temperature.
if (command.includes("M109 ")) command += "<span class='emoji'>🔥⏰</span>" // Wait for nozzle temperature.
if (command.includes("M140 ")) command += "<span class='emoji'>🔥</span>" // Set bed temperature.
if (command.includes("M190 ")) command += "<span class='emoji'>🔥⏰</span>" // Wait for bed temperature.

// Measurement
if (command.includes("G20 ")) command += "<span class='emoji'>📏</span>" // Set length units to inches.
if (command.includes("G21 ")) command += "<span class='emoji'>📏</span>" // Set length units to millimeters.
if (command.includes("M149 ")) command += "<span class='emoji'>📏🌡️</span>" // Set temperature units to celsius [C], fahrenheit [F] or kelvin [K].

// Pause/Wait
if (command.includes("G4 ")) command += "<span class='emoji'>⏲️</span>" // Uninterruptible pause command.
if (command.includes("M0 ")) command += "<span class='emoji'>⏰</span>" // Interruptible pause command.
if (command.includes("M1 ")) command += "<span class='emoji'>⏰</span>" // Interruptible pause command.
if (command.includes("M400 ")) command += "<span class='emoji'>💤</span>" // Wait for queue to finish.

// Fan
if (command.includes("M106 ")) command += "<span class='emoji'>🪭</span>" // Set fan speed.
if (command.includes("M107 ")) command += "<span class='emoji'>🪭🚫</span>" // Turn fan off.

// Sound
if (command.includes("M300 ")) command += "<span class='emoji'>🔊</span>" // Play Sound.

// Reports
if (command.includes("M114 ")) command += "<span class='emoji'>📌</span>" // Report Current Position.
if (command.includes("M105 ")) command += "<span class='emoji'>🌡️</span>" // Report Current Temperatures.

// Messaging
if (command.includes("M117 ")) command += "<span class='emoji'>💬</span>" // Set an LCD Message.
if (command.includes("M118 ")) command += "<span class='emoji'>📝</span>" // Send a message to the connected host.

// Settings
if (command.includes("M500 ")) command += "<span class='emoji'>💾</span>" // Save Settings.
if (command.includes("M501 ")) command += "<span class='emoji'>📂</span>" // Load Settings.
if (command.includes("M502 ")) command += "<span class='emoji'>🔄</span>" // Reset Settings.

// Interrupt/Shutdown
if (command.includes("M108 ")) command += "<span class='emoji'>⛔</span>" // Interrupt command.
if (command.includes("M112 ")) command += "<span class='emoji'>🛑</span>" // Full Shutdown.

log("input", command)

}
if (command.length) processInput(command)

})

Expand Down

0 comments on commit 5fb4315

Please sign in to comment.