Created
April 8, 2021 06:52
-
-
Save fanoush/04d0777f99bb4d153aa1a40aa218db55 to your computer and use it in GitHub Desktop.
simple javascript evaluator service (to test larger MTU)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
maxlen=56; | |
NRF.setServices({ | |
0x190A : { // Desay AT commands service ID (DS-D6, HX03W) | |
0x0002 : { | |
maxLen: maxlen, | |
notify: true | |
}, | |
0x0001 : { | |
writable : true, | |
maxLen: maxlen, | |
onWrite : function(evt) { | |
handleWrite(evt,'0x190A','0x0002',maxlen); | |
} | |
} | |
} | |
},{uart:true}); // still enable console | |
function handleWrite(evt, svc, ch, len){ | |
//log(svc+" got "+evt.data); | |
var val=handleCommand(E.toString(evt.data)); | |
if (val===undefined || val===null) return; | |
if (typeof(val)==="number") val=val.toString(); | |
if (typeof(val)==="object") val=JSON.stringify(val); | |
if (typeof(val)==="string") { | |
//log("reply="+val); | |
val=E.toArrayBuffer(val); | |
} | |
if (!val.length) return; | |
// now update service with the value | |
var c={}; | |
var s={};s[svc]=c; | |
c[ch]={value : val, notify: true}; | |
if (val.length<=len) | |
NRF.updateServices(s); | |
else { | |
// if longer than maxlen split into chunks | |
var l=val.length; | |
var i=0; | |
while (l>0){ | |
c[ch].value=new Uint8Array(val,i,(l>len)?len:l); | |
NRF.updateServices(s); | |
i+=len;l-=len; | |
} | |
} | |
} | |
var line=""; | |
function handleCommand(s){ | |
line+=s; | |
if (s.endsWith("\n") || s.endsWith("\r")){ | |
var res=handleLine(line.trim());//trim removes crlf | |
line=""; | |
return res; | |
} | |
} | |
function handleLine(l){ | |
try { | |
res=eval(l); | |
} catch(e){ | |
res="Uncaught "+e.type+": "+e.message; | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment