// robot2.js
var lon = false;
var pag='
<html><head><style>
td {text-align:center; vertical-align:middle; }
</style>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>var ifrm;
function eb(b) { ifrm.src = "/ifrm.html?e=" + b.id; }
</script>
</head><body onload="ifrm = document.getElementById(\'ifrm\');">
<h3>Hello from <i><b>robot!</b></i></h3>
<table width="100%"><tr>
<td><button id="le" onclick="eb(this)"><br>LEFT EYE<br><br></button></td>
<td><button id="be" onclick="eb(this)"><br>BOTH EYES<br><br></button></td>
<td><button id="re" onclick="eb(this)"><br>RIGHT EYE<br><br></button></td>
</tr><tr>
<td><button id="lb" onclick="eb(this)"><br>LEFT BLINK<br><br></button></td>
<td><button id="bl" onclick="eb(this)"><br>BLINK<br><br></button></td>
<td><button id="rb" onclick="eb(this)"><br>RIGHT BLINK<br><br></button></td>
</tr></table><table width="100%"><tr>
<td><button id="s-3" onclick="eb(this)"><br>-f<br><br></button></td>
<td><button id="s-9" onclick="eb(this)"><br>-m<br><br></button></td>
<td><button id="s-27"onclick="eb(this)"><br>-s<br><br></button></td>
<td><button id="s+0" onclick="eb(this)">Antenna<br>Spin<br>STOP<br></button></td>
<td><button id="s+27"onclick="eb(this)"><br>+s<br><br></button></td>
<td><button id="s+9" onclick="eb(this)"><br>+m<br><br></button></td>
<td><button id="s+3" onclick="eb(this)"><br>+f<br><br></button></td>
</tr></table>
<iframe id="ifrm" width="100%" height="30"></iframe>
</body></html>
';
pinMode(LED1,"output");
pinMode(LED2,"output");
pinMode(A0,"output");
pinMode(B0,"output");
var WebServer = require('WebServer')
, wifi = require('EspruinoWiFi')
;
function onInit() {
if (wifi)
wifi.startAP('robot', {
authMode:'open', password:'password'
}, startServer);
else
startServer();
}
function startServer() {
var ws = new WebServer(
{ port: 80
, file_system: '/var/www/'
, memory:
{'index.html':{'type':'text/html','content': pag }
,'ifrm.html':{'type':'text/html','content':
'<html><body>e: XX</body></html>' }
}});
ws.on('start', function (WebServer) {
if (lon) console.log('WebServer listening on port ' + WebServer.port); });
ws.on('request', function (request, response, parsedUrl, WebServer) {
var e = (parsedUrl.query) ? parsedUrl.query.e : null, v;
if (lon) console.log('WebServer requested', parsedUrl, response, "e="+e);
if (e=="le"||e=="be") { v = !digitalRead(LED2);
digitalWrite(LED2,v); digitalWrite(B0,v); }
if (e=="re"||e=="be") { v = !digitalRead(LED1);
digitalWrite(LED1,v); digitalWrite(A0,v); }
if (e=="lb") { setTimeout(blnkl,50,13); }
if (e=="bl") { setTimeout(blnkb,50,9); }
if (e=="rb") { setTimeout(blnkr,50,13); }
if ((e+" ").substr(0,1)=="s") { var s = Math.round(1 * e.substr(1));
s=(s<0&&s>-3)?-3:(s>0&&s<3)?3:s; e="s"+s; r(s); }
response.dSnd = response.dSnd.replace("XX",e);
});
ws.on('error', function (err, WebServer) {
if (lon) console.log('WebServer error', err); });
ws.createServer();
}
function blnkl(c) { var v;
v = !digitalRead(LED2); digitalWrite(LED2,v); digitalWrite(B0,v);
if (c) setTimeout(blnkl, 50, c - 1);
}
function blnkb(c) { var v;
v = !digitalRead(LED1); digitalWrite(LED1,v); digitalWrite(A0,v);
v = !digitalRead(LED2); digitalWrite(LED2,v); digitalWrite(B0,v);
if (c) setTimeout(blnkb, 50, c - 1);
}
function blnkr(c) { var v;
v = !digitalRead(LED2); digitalWrite(LED2,v); digitalWrite(A0,v);
if (c) setTimeout(blnkr, 50, c - 1);
}
// mirror stepper code from http://forum.espruino.com/comments/12187872/
// stepper.js
// stPs stepper pins
// st step 0..8
// stT step Time in milliseconds [ms]
// stI step Interval (from setInterval() and for clearInterval()
// sts steps 0001,0011,0010,... pin signals
// stBW sts - steps Backwards
// stFW sts - steps Forward
// dmy ...because of (cond) ? exprT : exprF needs something to assign to
var run = false;
var st = 0;
var stT = 0;
var stI = null;
var sts = null;
var stSt = 0b0000;
var stFW = [0b1000,0b1100,0b0100,0b0110,0b0010,0b0011,0b0001,0b1001];
var stBW = [0b1001,0b0001,0b0011,0b0010,0b0110,0b0100,0b1100,0b1000];
var stPs = [A4,A5,A6,A7];
// setI setInterval(i,stsC) i in [ms] with (optionl) step Change (if not null),
// and direction info (string)
var setI = function(t,stsN,d) {
if (lon) console.log("t = ",t, d);
if (stI) clearInterval(stI);
if (stsN) sts = stsN;
run = true;
stI = setInterval(stp,t);
};
// stp step
var stp = function() { digitalWrite(stPs, sts[st = ++st % 8]); };
// _sFW step ForWard
var _sFW = function(t) {
if (lon) console.log("FW w/ " + t);
if (stT > 0) { setI((stT = t),null," ~F");
} else { if (stT) { st = Math.abs(st - 7); } if (!stI) { st--; } setI((stT = t),stFW," FW"); }
};
// _sBW step BackWards
var _sBW = function(t) {
if (lon) console.log("BW w/ " + t);
if (stT < 0) { setI(-(stT = t),null," ~B");
} else { if (stT) { st = Math.abs(st - 7); } if (!stI) { st--; } setI(-(stT = t),stBW," BW"); }
};
// stop
var stop = function() {
if (lon) console.log("stop");
if (stI) { stI = clearInterval(stI); stI = null; }
if (lon) console.log(stI);
run = false;
digitalWrite(stPs, stSt);
};
// run function - t is stepping interval in [ms]
var r = function(t) {
if (typeof t === "undefined" ) {
if (stT) {
if (lon) console.log((stT > 0) ? "F>B" : "B>F");
r(-stT);
} else {
if (lon) console.log("What ?");
}
} else {
dmy = (t) ? (t>0) ? _sFW(t) : _sBW(t) : stop();
}
};
The page has an iFrame which was used as the communication vehicle (sending a get with parms in line 9 by setting the iFrame's source (url) and url.parse in lines 61..71) so that not the whole page has to be loaded again, and also use a simpler approach than XHTMLRequest / POST / body work... It was all put together in a few hours on Sun afternoon...
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.
Here the complete code:
The page has an iFrame which was used as the communication vehicle (sending a get with parms in line 9 by setting the iFrame's source (url) and url.parse in lines 61..71) so that not the whole page has to be loaded again, and also use a simpler approach than XHTMLRequest / POST / body work... It was all put together in a few hours on Sun afternoon...