Skip to content

Commit

Permalink
add stackmat debug dialog
Browse files Browse the repository at this point in the history
cs0x7f committed Oct 2, 2024
1 parent 5e0ed07 commit 5057db7
Showing 2 changed files with 108 additions and 34 deletions.
70 changes: 37 additions & 33 deletions src/js/hardware/stackmat.js
Original file line number Diff line number Diff line change
@@ -159,6 +159,15 @@ var stackmat = execMain(function() {
} else if (last_bit_length > 100) {
distortionStat = 1;
}
if (sampleRemain > 0) {
sampleRemain--;
sampleData['raw'].push(signal);
sampleData['bin'].push(lastSgn);
if (sampleRemain == 0) {
sampleCallback && sampleCallback(sampleData);
sampleCallback = null;
}
}
}


@@ -171,6 +180,7 @@ var stackmat = execMain(function() {
var no_state_length = 0;

function appendBit(bit) {
var newByte = null;
bitBuffer.push(bit);
if (bit != last_bit) {
last_bit = bit;
@@ -209,11 +219,18 @@ var stackmat = execMain(function() {
for (var i = 8; i > 0; i--) {
val = val << 1 | (bitBuffer[i] == idle_val ? 1 : 0);
}
byteBuffer.push(String.fromCharCode(val));
newByte = String.fromCharCode(val);
byteBuffer.push(newByte);
decode(byteBuffer);
bitBuffer = [];
}
}
if (sampleRemain > 0) {
sampleData['bits'].push(bit);
if (newByte != null) {
sampleData['bytes'].push(newByte);
}
}
}

function decode(byteBuffer) {
@@ -314,6 +331,24 @@ var stackmat = execMain(function() {
callback(stackmat_state);
}
}
if (sampleRemain > 0) {
sampleData['bits'].push(bit);
}
}

var sampleCallback = null;
var sampleRemain = 0;
var sampleData = {};

function getSample(duration, callback) {
sampleCallback = callback;
sampleRemain = Math.ceil(duration * (audio_context["sampleRate"] || 44100));
sampleData = {
'raw': [], // signal after agc
'bin': [], // binarized signal
'bits': [],
'bytes': []
};
}

var stackmat_state = {
@@ -336,40 +371,9 @@ var stackmat = execMain(function() {
init: init,
stop: stop,
updateInputDevices: updateInputDevices,
getSample: getSample,
setCallBack: function(func) {
callback = func;
}
}
});

execMain(function() {
if (!window.nativeStackmat) {
return;
}
stackmat = (function() {
DEBUG && console.log('Use Native Stackmat');
var callbackName = 'stackmat_callback_' + ~~(Math.random() * 10000000);
var callback;
nativeStackmat.setCallback(callbackName);
window[callbackName] = function(obj) {
DEBUG && console.log(JSON.stringify(obj));
callback && callback(obj);
}
return {
init: function() {
nativeStackmat.init();
return Promise.resolve();
},
stop: function() {
nativeStackmat.stop();
return Promise.resolve();
},
updateInputDevices: function() {
return Promise.resolve([[undefined, 'native']]);
},
setCallBack: function(func) {
callback = func;
}
}
})();
});
72 changes: 71 additions & 1 deletion src/js/tools/stackmatutil.js
Original file line number Diff line number Diff line change
@@ -4,8 +4,77 @@ var stackmatutil = execMain(function(CubieCube) {

var statusSpan = $('<span>').html('status: unknown');
var deviceSelect = $('<select style="font-size: 1rem;">');
var debugClick = $('<span class="click" style="font-family:iconfont;padding-left:0.5em;">\ue69d</span>');
var isShown = false;

var debugDiv;
var debugCanvas;
var debugText;
var debugSampleClick;
var debugIsShow = false;

function doDebugSample() {
if (!debugDiv) {
return;
}
stackmat.getSample(0.2, function(data) {
if (!debugIsShow) {
return;
}
debugText.html(
'RxBits: [' + data['bits'].join('') + ']<br>' +
'RxBytes: [' + escape(data['bytes'].join('')) + ']');
var width = Math.max(debugCanvas.width(), 1024);
var height = width * 0.3;
debugCanvas.attr('width', width);
debugCanvas.attr('height', height);
var ctx = debugCanvas[0].getContext('2d');
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, width, height);
var raw = data['raw'];
var bin = data['bin'];

ctx.strokeStyle = '#ccc';
ctx.beginPath();
ctx.moveTo(0, height * 0.5);
ctx.lineTo(width, height * 0.5);
ctx.stroke();

ctx.strokeStyle = '#444';
ctx.beginPath();
ctx.moveTo(0, height * 0.5 - height * 0.3 * raw[0]);
for (var i = 1; i < raw.length; i++) {
ctx.lineTo(i * width / (raw.length - 1), height * 0.5 - height * 0.3 * raw[i]);
}
ctx.stroke();

ctx.strokeStyle = '#00f';
ctx.beginPath();
ctx.moveTo(0, height * 0.8 - height * 0.6 * bin[0]);
for (var i = 1; i < bin.length; i++) {
ctx.lineTo(i * width / (bin.length - 1), height * 0.8 - height * 0.6 * bin[i]);
}
ctx.stroke();
});
}

function clearDebug() {
debugIsShow = false;
}

function showDebugDialog() {
if (!debugDiv) {
debugDiv = $('<div>');
debugCanvas = $('<canvas style="display:block; width:95%; margin:auto;">');
debugText = $('<span style="word-break:break-all;">');
debugSampleClick = $('<span class="click">Sample!</span>');
debugDiv.append(debugSampleClick, debugCanvas, debugText);
}
debugIsShow = true;
debugSampleClick.reclk(showDebugDialog);
kernel.showDialog([debugDiv, clearDebug, clearDebug, clearDebug], 'share', 'Stackmat Debug', doDebugSample);
}

function updateStatus(value) {
if (!isShown) {
return;
@@ -26,7 +95,8 @@ var stackmatutil = execMain(function(CubieCube) {
return;
}
isShown = true;
fdiv.empty().append(statusSpan, '<br>', 'Device:&nbsp;&nbsp;', deviceSelect);
fdiv.empty().append(statusSpan, '<br>', 'Device:&nbsp;&nbsp;', deviceSelect, debugClick);
debugClick.reclk(showDebugDialog);
}

function updateDevices() {

0 comments on commit 5057db7

Please sign in to comment.