Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
- <html>
- <head>
- <title>Remote Controlled Camera Tank</title>
- <script language="JavaScript">
- var lines= 0;
- var maxlines= 24;
- var direction=0;
- var cam_direction=0;
- // 0 - stationary
- // 1 - forward
- // 2 - reverse
- // 3 - turning left
- // 4 - turning right
- // I could probably do this with a tenth the memory, but pfha.
- var key_w=0;
- var key_s=0;
- var key_a=0;
- var key_d=0;
- var key_up=0;
- var key_down=0;
- var key_left=0;
- var key_right=0;
- var key_home=0;
- var xmlhttp=false;
- var ds="";
- // To run locally, use
- // <img src="http://192.168.1.99/videostream.cgi" id="video_stream">
- // <img src="/videostream.cgi" id="video_stream">
- // instead of the relative reference, and put the address in tank_url:
- //var tank_url='http://192.168.1.99';
- var tank_url='';
- var deadman = 0;
- function loadDoc(docname) {
- if (xmlhttp) {
- xmlhttp.open("GET", docname,true);
- xmlhttp.onreadystatechange=function() {
- if (xmlhttp.readyState==4) {
- ds=xmlhttp.responseText;
- document.forms[0].textArea1.value=ds+ds.length;
- }
- }
- xmlhttp.send(null)
- }
- }
- function init()
- {
- document.testform.t.value+= '';
- lines= 0;
- if (document.addEventListener)
- {
- document.addEventListener("keydown",keydown,false);
- document.addEventListener("keypress",keypress,false);
- document.addEventListener("keyup",keyup,false);
- document.addEventListener("textInput",textinput,false);
- }
- else if (document.attachEvent)
- {
- document.attachEvent("onkeydown", keydown);
- document.attachEvent("onkeypress", keypress);
- document.attachEvent("onkeyup", keyup);
- document.attachEvent("ontextInput", textinput);
- }
- else
- {
- document.onkeydown= keydown;
- document.onkeypress= keypress;
- document.onkeyup= keyup;
- document.ontextinput= textinput; // probably doesn't work
- }
- /*@cc_on @*/
- /*@if (@_jscript_version >= 5)
- try {
- xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
- } catch (e) {
- try {
- xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
- } catch (E) {
- xmlhttp = false;
- }
- }
- @else
- xmlhttp = false;
- @end @*/
- if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
- try {
- xmlhttp = new XMLHttpRequest();
- } catch (e) {
- xmlhttp = false;
- }
- }
- dodeadman();
- }
- function showmesg(t)
- {
- var old= document.testform.t.value;
- if (lines >= maxlines)
- {
- var i= old.indexOf('\n');
- if (i >= 0)
- old= old.substr(i+1);
- }
- else
- lines++;
- document.testform.t.value= old + t + '\n';
- }
- function keyval(n)
- {
- if (n == null) return 'undefined';
- var s= pad(3,n);
- if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
- while (s.length < 9) s+= ' ';
- return s;
- }
- function keymesg(w,e)
- {
- var row= 0;
- var head= [w, ' '];
- showmesg(head[row] +
- ' keyCode=' + keyval(e.keyCode) +
- ' which=' + keyval(e.which) +
- ' charCode=' + keyval(e.charCode));
- row= 1;
- handleinput(w,keyval(e.keyCode));
- if (row == 0)
- showmesg(head[row]);
- }
- function pad(n,s)
- {
- s+= '';
- while (s.length < n) s+= ' ';
- return s;
- }
- function suppressdefault(e,flag)
- {
- if (flag)
- {
- if (e.preventDefault) e.preventDefault();
- if (e.stopPropagation) e.stopPropagation();
- }
- return !flag;
- }
- function keydown(e)
- {
- if (!e) e= event;
- keymesg('keydown ',e);
- return suppressdefault(e,true);
- }
- function keyup(e)
- {
- if (!e) e= event;
- keymesg('keyup ',e);
- return suppressdefault(e,true);
- }
- function keypress(e)
- {
- if (!e) e= event;
- keymesg('keypress',e);
- return suppressdefault(e,true);
- }
- function textinput(e)
- {
- if (!e) e= event;
- //showmesg('textInput data=' + e.data);
- showmesg('textInput data='+e.data);
- return suppressdefault(e,true);
- }
- function handleinput(w,k)
- {
- // w - 87
- // s - 83
- // a - 65
- // d - 68
- // up - 38
- // down - 40
- // left - 37
- // right - 39
- var new_direction = 0;
- var new_cam_direction = 0;
- k = k.substr(0,2);
- if (w=='keypress')
- {
- return;
- }
- switch (k)
- {
- case '87':
- //alert(':'+w+':');
- if (w=='keydown ')
- key_w=1;
- else
- key_w=0;
- break;
- case '83':
- if (w=='keydown ')
- key_s=1;
- else
- key_s=0;
- break;
- case '65':
- if (w=='keydown ')
- key_a=1;
- else
- key_a=0;
- break;
- case '68':
- if (w=='keydown ')
- key_d=1;
- else
- key_d=0;
- break;
- case '38':
- if (w=='keydown ')
- key_up=1;
- else
- key_up=0;
- break;
- case '40':
- if (w=='keydown ')
- key_down=1;
- else
- key_down=0;
- break;
- case '37':
- if (w=='keydown ')
- key_left=1;
- else
- key_left=0;
- break;
- case '39':
- if (w=='keydown ')
- key_right=1;
- else
- key_right=0;
- break;
- case '36':
- if (w=='keydown ')
- key_home=1;
- else
- key_home=0;
- break;
- }
- if (key_w == 1)
- {
- new_direction = 1;
- } else if(key_s == 1)
- {
- new_direction = 2;
- } else if(key_a == 1)
- {
- new_direction = 3;
- } else if(key_d == 1)
- {
- new_direction = 4;
- } else {
- new_direction = 0;
- }
- if (key_up == 1)
- {
- new_cam_direction = 1;
- } else if(key_down == 1)
- {
- new_cam_direction = 2;
- } else if(key_left == 1)
- {
- new_cam_direction = 3;
- } else if(key_right == 1)
- {
- new_cam_direction = 4;
- } else if(key_home == 1)
- {
- new_cam_direction = 5;
- } else {
- new_cam_direction = 0;
- }
- if (new_direction != direction)
- {
- handlemovement(new_direction);
- }
- if (new_cam_direction != cam_direction)
- {
- handlecammovement(new_cam_direction);
- }
- }
- function handlemovement(dir)
- {
- direction = dir;
- //comm_write.cgi?port=0&baud=4098&bytes=4&data=arf1
- command = 'arf0';
- switch (dir)
- {
- case 0:
- //action_zone.location = '/comm_write.cgi?port=0&baud=4098&bytes=4&data=arf0'
- command = 'arf0';
- break;
- case 1:
- //loadDoc('/comm_write.cgi?port=0&baud=4098&bytes=4&data=arf1');
- command = 'arf1';
- //action_zone.location = '/comm_write.cgi?port=0&baud=4098&bytes=4&data=arf1'
- break;
- case 2:
- //action_zone.location = '/comm_write.cgi?port=0&baud=4098&bytes=4&data=arb1'
- command = 'arb1';
- break;
- case 3:
- //action_zone.location = '/comm_write.cgi?port=0&baud=4098&bytes=4&data=arr1'
- command = 'arr1';
- break;
- case 4:
- //action_zone.location = '/comm_write.cgi?port=0&baud=4098&bytes=4&data=arl1'
- command = 'arl1';
- break;
- }
- if (tank_url != '')
- {
- action_zone.location = tank_url+'/comm_write.cgi?port=0&baud=4098&bytes=4&data='+command;
- } else {
- loadDoc('/comm_write.cgi?port=0&baud=4098&bytes=4&data='+command);
- }
- clearTimeout(deadman);
- dodeadman();
- //post_to_url('/comm_write.cgi', {'port':'0','baud':'4098','bytes':'4','data':'arf1'},'post');
- //alert(dir);
- }
- function handlecammovement(dir)
- {
- cam_direction = dir;
- command = '1';
- switch (dir)
- {
- case 0:
- //action_zone.location = '/decoder_control.cgi?command=1'
- command = '1';
- break;
- case 1:
- //action_zone.location = '/decoder_control.cgi?command=0'
- command = '0';
- break;
- case 2:
- //action_zone.location = '/decoder_control.cgi?command=2'
- command = '2';
- break;
- case 3:
- //action_zone.location = '/decoder_control.cgi?command=4'
- command = '4';
- break;
- case 4:
- //action_zone.location = '/decoder_control.cgi?command=6'
- command = '6';
- break;
- case 5:
- //action_zone.location = '/decoder_control.cgi?command=6'
- command = '31';
- break;
- }
- //loadDoc('/decoder_control.cgi?command='+command);
- //action_zone.location = 'http://192.168.1.99/decoder_control.cgi?command='+command
- if (tank_url != '')
- {
- action_zone.location = tank_url+'/decoder_control.cgi?command='+command;
- } else {
- loadDoc('/decoder_control.cgi?command='+command);
- }
- }
- function handledeadman()
- {
- if (direction != 0)
- handlemovement(direction);
- dodeadman();
- }
- function dodeadman()
- {
- deadman=setTimeout("handledeadman()", 2000);
- }
- </script>
- </head>
- <body>
- <form name="testform">
- <h2>Vroom</h2><p>
- <img src="/videostream.cgi" id="video_stream">
- <textarea name="t" rows="1" cols="90" hidden=true></textarea>
- <br>
- WSAD keys steer tank.<br>
- Arrow keys steer camera.<br>
- Home button points camera dead ahead.<br>
- </form>
- <script language="JavaScript">
- init();
- </script>
- <iframe name="action_zone" id="action_zone" style="display:none"></iframe>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement