Skip to content

Instantly share code, notes, and snippets.

@MaBecker
Created December 11, 2018 05:42
Show Gist options
  • Save MaBecker/c6b406e632824cad2167364d3199a4da to your computer and use it in GitHub Desktop.
Save MaBecker/c6b406e632824cad2167364d3199a4da to your computer and use it in GitHub Desktop.
Special char in modules cause error when using require to load from storage
s = require('Storage');
s.write('SBUS',`exports.connect = function(uart, rxPin) {
var sbus = {
channels : new Uint16Array(18),
frameLoss : false,
failSafe : false
};
uart.setup(100000, {rx:rxPin, parity:"e",stopbits:2});
uart.removeAllListeners();
var data = "";
uart.on('data', function(d) {
data += d;
var idx = data.indexOf("\x00\x0f");
while (idx >= 0) {
if (idx==23) {
var l = E.toUint8Array(data.substr(0,23));
var status = l[22];
E.mapInPlace(l,sbus.channels,undefined,-11);
sbus.channels.set([status&128?2047:0,status&64?2047:0],16);
sbus.frameLoss = !!(status&32);
sbus.failSafe = !!(status&16);
sbus.emit('frame', sbus);
}
data = data.substr(idx+2);
idx = data.indexOf("\x00\x0f");
}
});
return sbus;
};`);
/* Minify with Espruino
export BOARD=
espruino -m SBus.js -o SBus.emin.js --board $BOARD
*/
s.write('SBUSemin',`exports.connect=function(c,d){var a={channels:new Uint16Array(18),frameLoss:!1,failSafe:!1};c.setup(100000,{rx:d,parity:'e',stopbits:2}),c.removeAllListeners();var b='';return c.on('data',function(f){b+=f;var c=b.indexOf('\0\x0F');while(c>=0){if(c==23){var e=E.toUint8Array(b.substr(0,23));var d=e[22];E.mapInPlace(e,a.channels,undefined,-11),a.channels.set([d&128?2047:0,d&64?2047:0],16),a.frameLoss=!!(d&32),a.failSafe=!!(d&16),a.emit('frame',a);}b=b.substr(c+2),c=b.indexOf('\0\x0F');}}),a;};`);
/* Minify with Google
export PATHtoED=/Espruino/repos/EspruinoDocs
alias minjs='_minjs(){ export CD=$(pwd); cd $PATHtoED; node bin/minify.js $CD/$1.js $CD/$1.gmin.js;cd $CD;};_minjs'
minjs SBus
*/
s.write('SBUSgmin',`exports.connect=function(b,f){var a={channels:new Uint16Array(18),frameLoss:!1,failSafe:!1};b.setup(1E5,{rx:f,parity:"e",stopbits:2});b.removeAllListeners();var c="";b.on("data",function(d){c+=d;for(d=c.indexOf("\x00\u000f");0<=d;){if(23==d){var b=E.toUint8Array(c.substr(0,23)),e=b[22];E.mapInPlace(b,a.channels,void 0,-11);a.channels.set([e&128?2047:0,e&64?2047:0],16);a.frameLoss=!!(e&32);a.failSafe=!!(e&16);a.emit("frame",a)}c=c.substr(d+2);d=c.indexOf("\x00\u000f")}});return a}`);
SBUS = require('SBUS');
SBUSemin = require('SBUSemin');
SBUSgmin = require('SBUSgmin');
/* Output for global
>global
={
s: function () { [native code] },
SBUS: {
connect: function (uart,rxPin) { ... }
},
SyntaxError: function () { [native code] },
SBUSemin: { },
SBUSgmin: { }
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment