Things that are already time critical become even more so when handled interpretatively, as Espruino - to be precise: the JavaScript part of Espruino - is doing, even though events are queued to not slow down the catching and registering of them. In addition to that, for replay the pauses between the pulses matter as well and have to be considered (I do not know what the example does).
My stab at this would be to just record the times in the loop and do all math afterwards... To prep the values for replay, you may even adjust them for the execution time used by Espruino.
I have no feeling for the actual figures / numbers, I'm just thinking through what happens... and all needs time, and since you expect actually an over-all accuracy of 4 digits, 1 millisecond matters...
pinMode(D1,"input_pullup");
var rTs = []; // raw times
var times = []; // calculated from raw times
var recordingWatch;
function startRecording() {
rTs = [];
recordingWatch = setWatch(function(e) {
rTs.push(e);
}, D1, { repeat: true, edge: "both", debounce: 0 });
}
function stopRecording() {
clearWatch(recordingWatch);
currentWatch = null;
}
// times[] array will include calculates the pulse on and off times
// even-indexed values are on-times, odd-indexed ones are off-times
function calculateTimes() {
var tCurrent, tLast; // t-current and t-Last
times = [];
rTs.forEach(function(e, idx){
if (idx === 1) {
tLast = e.time;
} else {
times.push((tCurrent = e.time) - tLast);
tLast = tCurrent;
}
});
}
Sequence of events are:
upload code
connect wires to remote
console command startRecording()
perform command on remote
console command stopRecording()
console command calculateTimes()
console.command console.log(times) to show the calculated times
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Things that are already time critical become even more so when handled interpretatively, as Espruino - to be precise: the JavaScript part of Espruino - is doing, even though events are queued to not slow down the catching and registering of them. In addition to that, for replay the pauses between the pulses matter as well and have to be considered (I do not know what the example does).
My stab at this would be to just record the times in the loop and do all math afterwards... To prep the values for replay, you may even adjust them for the execution time used by Espruino.
I have no feeling for the actual figures / numbers, I'm just thinking through what happens... and all needs time, and since you expect actually an over-all accuracy of 4 digits, 1 millisecond matters...
Sequence of events are:
startRecording()
stopRecording()
calculateTimes()
console.log(times)
to show the calculated timesDo you see any difference?