Last active
December 15, 2020 11:15
-
-
Save fanoush/e8db2729844972ef1a0ae31719f31537 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// https://www.espruino.com/Reference#l_NRF_setServices | |
// https://github.com/gfwilliams/workshop-thingy52/blob/master/step5.md | |
// http://forum.espruino.com/conversations/322805/ | |
var atf={}; | |
var user=""; | |
atf.USER=function(p,t){ | |
if(p){ | |
user=p; | |
} | |
return user; | |
}; | |
var units=0; | |
atf.UNITS=function(p,t){ | |
if(p){ | |
units=p; | |
} | |
return units; | |
}; | |
var hrmon=0; | |
atf.HRMONITOR=function(p,t){ | |
if(p){ | |
hrmon=p; | |
} | |
return hrmon; | |
}; | |
var findphone=0; | |
atf.FINDPHONE=function(p,t){ | |
if(p){ | |
findphone=p; | |
} | |
return findphone; | |
}; | |
var handsup=0; | |
atf.HANDSUP=function(p,t){ | |
if(p){ | |
handsup=p; | |
} | |
return handsup; | |
}; | |
atf.DT=function(params){ | |
if(params){ | |
var i,pl=params.length>>1; | |
if (pl!=5&&pl!=7) return "ERR"; | |
//set date | |
var p=[];for(i=0;i<pl;i++) p[i]=params.substr(i*2,2); | |
i=0; | |
w.setClock((pl>6?p[i++]:"20")+p[i++]+"-"+p[i++]+"-"+p[i++]+"T"+p[i++]+":"+p[i++]+":"+(pl>6?p[i++]:"00")); | |
return params; | |
} | |
var d=Date(Date.now()-tzoff).toISOString();res=""; | |
// cut numbers from ISO date string | |
[2,5,8,11,14,17].forEach((i)=>{res+=d.substr(i,2);}); | |
return res; | |
}; | |
var TZ=0; | |
var tzoff=0; | |
var tform=0;//12 or 24 | |
var lan=0; | |
var steps=1234; | |
var syn=0; | |
var topace=0; | |
function handleATcommands(cmd,params){ | |
var res=null; | |
var cmdtype=(cmd.substr(1,2).toUpperCase()=="T+")?cmd.charAt(0).toUpperCase():0; | |
log("cmd="+cmd+",type="+cmdtype+",params="+params); | |
var cmdname=((cmdtype)?cmd.substr(3):cmd).toUpperCase(); | |
var f=atf[cmdname];if(typeof(f)=="function") return f(params,cmdtype); | |
switch(cmdname){ | |
case 'BOND': // TODO wait for button press, return ERR if not pressed | |
return "OK"; | |
case 'ACT': | |
return "0"; | |
case 'BATT': | |
return parseInt((w.battVoltage()-3.6)/0.006); | |
case 'STEPSTORE': | |
if (params){ | |
} | |
return "OK"; | |
case 'SYN': // 1= show sync animation (circle arrows) | |
if (params){ | |
syn=Number(params); | |
} | |
return syn; | |
case 'LAN': // | |
if (params){ | |
lan=Number(params); | |
} | |
return lan; | |
case 'TIMEFORMAT': // | |
if (params){ | |
tform=Number(params); | |
} | |
return tform; | |
case 'PACE': // steps | |
if (params){ | |
steps=Number(params); | |
} | |
return steps; | |
case 'DATA': // TODO send stored data | |
if (params){ | |
} | |
return "0,0,0,0"; | |
case 'TOPACE'://if 1 then we should start sending steps via NT+TOPACE=steps on any change | |
if (params){ | |
topace=Number(params); | |
} | |
// if 0 we report last no. of steps | |
return topace===0 ? steps : "OK"; | |
case 'TIMEZONE': | |
if(params){ | |
res=Number(params); | |
if (!isNaN(res) && (Math.abs(res)<=12*3600)){ | |
TZ=res/3600; | |
E.setTimeZone(TZ); | |
tzoff=Date().getTimezoneOffset()*6E4; | |
} | |
return params; | |
} | |
return String(TZ*3600); | |
case "VER": | |
return cmdtype ? (cmdtype=="A" ? "110.200.051" : "100.016.051") : process.env.VERSION; | |
} | |
return res; | |
} | |
function handleATLine(l){ | |
var cmd,params,res=null,pari=l.indexOf('='); //any params? | |
if(pari>0){ | |
cmd=l.slice(0,pari);params=l.slice(pari+1); | |
} else { | |
params=null;cmd=l; | |
} | |
if (cmd.length==2 && cmd.toUpperCase()=="AT") return "OK\r\n"; | |
//type - 0 or first letter - b,a,n if starts with [ABN]T+ | |
try { | |
res=handleATcommands(cmd,params); | |
} catch(e){ | |
//log(e); | |
if (cmdtype) res="ERR"; | |
} | |
return (res===null) ? null : cmd+":"+res+"\r\n"; | |
} | |
var line=""; | |
function handleCommand(s){ | |
line+=s; | |
if (s.endsWith("\n") || s.endsWith("\r")){ | |
var res=handleATLine(line.trim());//trim removes crlf | |
line=""; | |
return res; | |
} | |
} | |
function handleWrite(evt, svc, ch){ | |
//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)==="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<=20) | |
NRF.updateServices(s); | |
else { | |
// if longer than maxLen=20 split into chunks | |
var l=val.length; | |
var i=0; | |
while (l>0){ | |
c[ch].value=new Uint8Array(val,i,(l>20)?20:l); | |
NRF.updateServices(s); | |
i+=20;l-=20; | |
} | |
} | |
} | |
function enableDesayServices(){ | |
NRF.setAdvertising({},{ | |
name:"HX03W", | |
discoverable: false/*, | |
manufacturer:0xAB4E, | |
manufacturerData: NRF.getAddress().split(':').map(x=>parseInt(x,16))*/ | |
}); | |
NRF.setServices({ | |
0x190A : { | |
0x0002 : { | |
maxLen: 20, | |
notify: true | |
}, | |
0x0001 : { | |
writable : true, | |
maxLen: 20, | |
onWrite : function(evt) { | |
handleWrite(evt,'0x190A','0x0002'); | |
} | |
} | |
}, | |
0x190B : { | |
0x0004 : { | |
maxLen: 20, | |
notify: true | |
}, | |
0x0003 : { | |
writable : true, | |
maxLen: 20, | |
onWrite : function(evt) { | |
handleWrite(evt,'0x190B','0x0004'); | |
} | |
} | |
} | |
}, { | |
uart:false/*,advertise: [ 'FEE7' ]*/ | |
}); | |
} | |
function enableNordicUART(){ | |
NRF.setServices(undefined,{uart:true}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment