Support sending CRC16 bytes with serial messages (#276)
Optionally add a crc16 checksum. Optionally support sending names in name/value pairs as single characters
This commit is contained in:
parent
ad5950697e
commit
caea7bb39d
1 changed files with 60 additions and 18 deletions
|
|
@ -6,7 +6,7 @@ This program is free software: you can redistribute it and/or modify it under th
|
||||||
|
|
||||||
import { Pattern, isPattern } from '@strudel.cycles/core';
|
import { Pattern, isPattern } from '@strudel.cycles/core';
|
||||||
|
|
||||||
var serialWriter;
|
var writeMessage;
|
||||||
var choosing = false;
|
var choosing = false;
|
||||||
|
|
||||||
export async function getWriter(br = 38400) {
|
export async function getWriter(br = 38400) {
|
||||||
|
|
@ -14,18 +14,30 @@ export async function getWriter(br = 38400) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
choosing = true;
|
choosing = true;
|
||||||
if (serialWriter) {
|
if (writeMessage) {
|
||||||
return serialWriter;
|
return writeMessage;
|
||||||
}
|
}
|
||||||
if ('serial' in navigator) {
|
if ('serial' in navigator) {
|
||||||
const port = await navigator.serial.requestPort();
|
const port = await navigator.serial.requestPort();
|
||||||
await port.open({ baudRate: br });
|
await port.open({ baudRate: br });
|
||||||
// eslint-disable-next-line no-undef
|
const encoder = new TextEncoder();
|
||||||
const textEncoder = new TextEncoderStream();
|
const writer = port.writable.getWriter();
|
||||||
const writableStreamClosed = textEncoder.readable.pipeTo(port.writable);
|
writeMessage = function (message, chk) {
|
||||||
const writer = textEncoder.writable.getWriter();
|
const encoded = encoder.encode(message)
|
||||||
serialWriter = function (message) {
|
if (!chk) {
|
||||||
writer.write(message);
|
writer.write(encoded);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const bytes = new Uint8Array(4);
|
||||||
|
bytes[0] = 124; // | symbol
|
||||||
|
bytes[1] = (chk >> 8) & 0xFF;
|
||||||
|
bytes[2] = chk & 0xFF;
|
||||||
|
bytes[3] = 59; // semicolon
|
||||||
|
const withchk = new Uint8Array(encoded.length+4)
|
||||||
|
withchk.set(encoded);
|
||||||
|
withchk.set(bytes, encoded.length);
|
||||||
|
writer.write(withchk);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
throw 'Webserial is not available in this browser.';
|
throw 'Webserial is not available in this browser.';
|
||||||
|
|
@ -34,18 +46,41 @@ export async function getWriter(br = 38400) {
|
||||||
|
|
||||||
const latency = 0.1;
|
const latency = 0.1;
|
||||||
|
|
||||||
Pattern.prototype.serial = function (...args) {
|
// crc16 (CCITT-FALSE) https://gist.github.com/tijnkooijmans/10981093
|
||||||
return this._withHap((hap) => {
|
function crc16(data) {
|
||||||
if (!serialWriter) {
|
const length = data.length
|
||||||
getWriter(...args);
|
if (length == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
var crc = 0xFFFF;
|
||||||
|
for (var i = 0; i < length; ++i) {
|
||||||
|
crc ^= data.charCodeAt(i) << 8;
|
||||||
|
for (var j = 0; j < 8; ++j) {
|
||||||
|
crc = (crc & 0x8000) > 0 ? (crc << 1) ^ 0x1021 : crc << 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return crc & 0xffff;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pattern.prototype.serial = function (br=38400,sendcrc=false,singlecharids=false) {
|
||||||
|
return this.withHap((hap) => {
|
||||||
|
if (!writeMessage) {
|
||||||
|
getWriter(br);
|
||||||
}
|
}
|
||||||
const onTrigger = (time, hap, currentTime) => {
|
const onTrigger = (time, hap, currentTime) => {
|
||||||
var message = '';
|
var message = '';
|
||||||
|
var chk = 0;
|
||||||
if (typeof hap.value === 'object') {
|
if (typeof hap.value === 'object') {
|
||||||
if ('action' in hap.value) {
|
if ('action' in hap.value) {
|
||||||
message += hap.value['action'] + '(';
|
var action = hap.value['action'];
|
||||||
|
if (singlecharids) {
|
||||||
|
action = action.charAt(0);
|
||||||
|
}
|
||||||
|
message += action + '(';
|
||||||
var first = true;
|
var first = true;
|
||||||
for (const [key, val] of Object.entries(hap.value)) {
|
for (var [key, val] of Object.entries(hap.value)) {
|
||||||
if (key === 'action') {
|
if (key === 'action') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -54,19 +89,26 @@ Pattern.prototype.serial = function (...args) {
|
||||||
} else {
|
} else {
|
||||||
message += ',';
|
message += ',';
|
||||||
}
|
}
|
||||||
message += `${key}:${val}`;
|
if (singlecharids) {
|
||||||
|
key = key.charAt(0);
|
||||||
|
}
|
||||||
|
message += key + ':' + val;
|
||||||
}
|
}
|
||||||
message += ')';
|
message += ')';
|
||||||
|
if (sendcrc) {
|
||||||
|
chk = crc16(message)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
for (const [key, val] of Object.entries(hap.value)) {
|
for (const [key, val] of Object.entries(hap.value)) {
|
||||||
message += `${key}:${val};`;
|
message += `${key}:${val}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
message = hap.value;
|
message = hap.value;
|
||||||
}
|
}
|
||||||
const offset = (time - currentTime + latency) * 1000;
|
const offset = (time - currentTime + latency) * 1000;
|
||||||
window.setTimeout(serialWriter, offset, message);
|
|
||||||
|
window.setTimeout(function () {writeMessage(message, chk)}, offset);
|
||||||
};
|
};
|
||||||
return hap.setContext({ ...hap.context, onTrigger, dominantTrigger: true });
|
return hap.setContext({ ...hap.context, onTrigger, dominantTrigger: true });
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue