Ahh, thanks. So I guess that's what I thought - your original code was probably fine, but was running a bit too slowly to put the pulses end to end.
And I was trying to be too clever in my code. Due to the awesomeness of JavaScript, it looks like the ternary operator doesn't treat the string "0" as false. Also I think the pausing between isn't working right because setInterval is always calling back every 10ms, rather than 10ms + the length of the signal.
Try:
// 1 added to front, and 10 converted to 0 already
var encode = [
"1111010",
"1110110",
"1110101",
"1110011",
"1101110",
"1101101",
"1101011",
"1011110",
"1011101",
"1011011",
"1010111",
"0111110",
"0111101",
"0111011",
"0110111",
"0101111"];
function sendNibbles(nibbles) {
var repeat = 12;
var cmdStream = "var p=digitalPulse;";
var sigLength = 0.5;
cmdStream+="p(TX,1,0.25);p(TX,0,0.25);"; //msg start bit
nibbles.forEach(function(nibble) {
encode[nibbble].split("").forEach(function(bit) {
var length = (bit=="1")?0.25:1.25;
cmdStream+="p(TX,1,0.25);p(TX,0,"+length+");";
sigLength += 0.25+length;
});
});
cmdStream+="p(TX,1,0.25);"; //msg end bit
sigLength += 0.25;
var interval = setInterval(function() {
eval(cmdStream);
if (repeat-- == 1) clearInterval(interval);
}, sigLength+10.25); // delay 10250us between repeats
}
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.
Ahh, thanks. So I guess that's what I thought - your original code was probably fine, but was running a bit too slowly to put the pulses end to end.
And I was trying to be too clever in my code. Due to the awesomeness of JavaScript, it looks like the ternary operator doesn't treat the string
"0"
as false. Also I think the pausing between isn't working right because setInterval is always calling back every 10ms, rather than 10ms + the length of the signal.Try: