Advertisement
Guest User

Untitled

a guest
Nov 11th, 2011
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <title>Remote Controlled Camera Tank</title>
  5.  
  6. <script language="JavaScript">
  7.  
  8. var lines= 0;
  9. var maxlines= 24;
  10.  
  11. var direction=0;
  12. var cam_direction=0;
  13. // 0 - stationary
  14. // 1 - forward
  15. // 2 - reverse
  16. // 3 - turning left
  17. // 4 - turning right
  18.  
  19. // I could probably do this with a tenth the memory, but pfha.
  20. var key_w=0;
  21. var key_s=0;
  22. var key_a=0;
  23. var key_d=0;
  24. var key_up=0;
  25. var key_down=0;
  26. var key_left=0;
  27. var key_right=0;
  28. var key_home=0;
  29.  
  30. var xmlhttp=false;
  31. var ds="";
  32. // To run locally, use
  33. // <img src="http://192.168.1.99/videostream.cgi" id="video_stream">
  34. // <img src="/videostream.cgi" id="video_stream">
  35. // instead of the relative reference, and put the address in tank_url:
  36. //var tank_url='http://192.168.1.99';
  37. var tank_url='';
  38.  
  39. var deadman = 0;
  40.  
  41. function loadDoc(docname) {
  42.  
  43.     if (xmlhttp) {
  44.    
  45.     xmlhttp.open("GET", docname,true);
  46.      xmlhttp.onreadystatechange=function() {
  47.       if (xmlhttp.readyState==4) {
  48.            ds=xmlhttp.responseText;
  49.        document.forms[0].textArea1.value=ds+ds.length;
  50.       }
  51.      }
  52.      xmlhttp.send(null)
  53.      }
  54.  
  55. }
  56.  
  57. function init()
  58. {
  59.     document.testform.t.value+= '';
  60.     lines= 0;
  61.  
  62.     if (document.addEventListener)
  63.     {
  64.        document.addEventListener("keydown",keydown,false);
  65.        document.addEventListener("keypress",keypress,false);
  66.        document.addEventListener("keyup",keyup,false);
  67.        document.addEventListener("textInput",textinput,false);
  68.     }
  69.     else if (document.attachEvent)
  70.     {
  71.        document.attachEvent("onkeydown", keydown);
  72.        document.attachEvent("onkeypress", keypress);
  73.        document.attachEvent("onkeyup", keyup);
  74.        document.attachEvent("ontextInput", textinput);
  75.     }
  76.     else
  77.     {
  78.        document.onkeydown= keydown;
  79.        document.onkeypress= keypress;
  80.        document.onkeyup= keyup;
  81.        document.ontextinput= textinput;   // probably doesn't work
  82.     }
  83.    
  84.     /*@cc_on @*/
  85.  
  86. /*@if (@_jscript_version >= 5)
  87.  
  88.     try {
  89.         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  90.     } catch (e) {
  91.         try {
  92.             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  93.         } catch (E) {
  94.             xmlhttp = false;
  95.         }
  96.     }
  97.  
  98. @else
  99.  
  100.     xmlhttp = false;
  101.  
  102. @end @*/
  103.  
  104.     if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  105.  
  106.         try {
  107.           xmlhttp = new XMLHttpRequest();
  108.         } catch (e) {
  109.           xmlhttp = false;
  110.         }
  111.     }
  112.     dodeadman();
  113. }
  114.  
  115. function showmesg(t)
  116. {
  117.    var old= document.testform.t.value;
  118.    if (lines >= maxlines)
  119.    {
  120.     var i= old.indexOf('\n');
  121.     if (i >= 0)
  122.         old= old.substr(i+1);
  123.    }
  124.    else
  125.     lines++;
  126.  
  127.    document.testform.t.value= old + t + '\n';
  128. }
  129.  
  130. function keyval(n)
  131. {
  132.     if (n == null) return 'undefined';
  133.     var s= pad(3,n);
  134.     if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
  135.     while (s.length < 9) s+= ' ';
  136.     return s;
  137. }
  138.  
  139. function keymesg(w,e)
  140. {
  141.     var row= 0;
  142.     var head= [w, '        '];
  143.    
  144.     showmesg(head[row] +
  145.             ' keyCode=' + keyval(e.keyCode) +
  146.         ' which=' + keyval(e.which) +
  147.             ' charCode=' + keyval(e.charCode));
  148.     row= 1;
  149.     handleinput(w,keyval(e.keyCode));
  150.    
  151.     if (row == 0)
  152.     showmesg(head[row]);
  153. }
  154.  
  155. function pad(n,s)
  156. {
  157.    s+= '';
  158.    while (s.length < n) s+= ' ';
  159.    return s;
  160. }
  161.  
  162. function suppressdefault(e,flag)
  163. {
  164.    if (flag)
  165.    {
  166.        if (e.preventDefault) e.preventDefault();
  167.        if (e.stopPropagation) e.stopPropagation();
  168.    }
  169.    return !flag;
  170. }
  171.  
  172. function keydown(e)
  173. {
  174.    if (!e) e= event;
  175.    keymesg('keydown ',e);
  176.    return suppressdefault(e,true);
  177. }
  178.  
  179. function keyup(e)
  180. {
  181.    if (!e) e= event;
  182.    keymesg('keyup   ',e);
  183.    return suppressdefault(e,true);
  184. }
  185.  
  186. function keypress(e)
  187. {
  188.    if (!e) e= event;
  189.    keymesg('keypress',e);
  190.    return suppressdefault(e,true);
  191. }
  192.  
  193. function textinput(e)
  194. {
  195.    if (!e) e= event;
  196.    //showmesg('textInput  data=' + e.data);
  197.    showmesg('textInput data='+e.data);
  198.    return suppressdefault(e,true);
  199. }
  200.  
  201. function handleinput(w,k)
  202. {
  203.     // w - 87
  204.     // s - 83
  205.     // a - 65
  206.     // d - 68
  207.     // up - 38
  208.     // down - 40
  209.     // left - 37
  210.     // right - 39
  211.  
  212.     var new_direction = 0;
  213.     var new_cam_direction = 0;
  214.    
  215.     k = k.substr(0,2);
  216.  
  217.     if (w=='keypress')
  218.     {
  219.         return;
  220.     }
  221.    
  222.     switch (k)
  223.     {
  224.         case '87':
  225.             //alert(':'+w+':');
  226.             if (w=='keydown ')
  227.                 key_w=1;
  228.             else
  229.                 key_w=0;
  230.             break;
  231.         case '83':
  232.             if (w=='keydown ')
  233.                 key_s=1;
  234.             else
  235.                 key_s=0;
  236.             break;
  237.         case '65':
  238.             if (w=='keydown ')
  239.                 key_a=1;
  240.             else
  241.                 key_a=0;
  242.             break;
  243.         case '68':
  244.             if (w=='keydown ')
  245.                 key_d=1;
  246.             else
  247.                 key_d=0;
  248.             break;
  249.         case '38':
  250.             if (w=='keydown ')
  251.                 key_up=1;
  252.             else
  253.                 key_up=0;
  254.             break;
  255.         case '40':
  256.             if (w=='keydown ')
  257.                 key_down=1;
  258.             else
  259.                 key_down=0;
  260.             break;
  261.         case '37':
  262.             if (w=='keydown ')
  263.                 key_left=1;
  264.             else
  265.                 key_left=0;
  266.             break;
  267.         case '39':
  268.             if (w=='keydown ')
  269.                 key_right=1;
  270.             else
  271.                 key_right=0;
  272.             break;
  273.         case '36':
  274.             if (w=='keydown ')
  275.                 key_home=1;
  276.             else
  277.                 key_home=0;
  278.             break;
  279.     }
  280.    
  281.     if (key_w == 1)
  282.     {
  283.         new_direction = 1;
  284.     } else if(key_s == 1)
  285.     {
  286.         new_direction = 2;
  287.     } else if(key_a == 1)
  288.     {
  289.         new_direction = 3;
  290.     } else if(key_d == 1)
  291.     {
  292.         new_direction = 4;
  293.     } else {
  294.         new_direction = 0;
  295.     }
  296.    
  297.     if (key_up == 1)
  298.     {
  299.         new_cam_direction = 1;
  300.     } else if(key_down == 1)
  301.     {
  302.         new_cam_direction = 2;
  303.     } else if(key_left == 1)
  304.     {
  305.         new_cam_direction = 3;
  306.     } else if(key_right == 1)
  307.     {
  308.         new_cam_direction = 4;
  309.     } else if(key_home == 1)
  310.     {
  311.         new_cam_direction = 5;
  312.     } else {
  313.         new_cam_direction = 0;
  314.     }
  315.  
  316.     if (new_direction != direction)
  317.     {
  318.         handlemovement(new_direction);
  319.     }  
  320.    
  321.     if (new_cam_direction != cam_direction)
  322.     {
  323.         handlecammovement(new_cam_direction);      
  324.     }
  325. }
  326.  
  327. function handlemovement(dir)
  328. {
  329.     direction = dir;
  330.     //comm_write.cgi?port=0&baud=4098&bytes=4&data=arf1
  331.     command = 'arf0';
  332.     switch (dir)
  333.     {
  334.         case 0:
  335.             //action_zone.location = '/comm_write.cgi?port=0&baud=4098&bytes=4&data=arf0'
  336.             command = 'arf0';
  337.             break;
  338.         case 1:
  339.             //loadDoc('/comm_write.cgi?port=0&baud=4098&bytes=4&data=arf1');
  340.             command = 'arf1';
  341.             //action_zone.location = '/comm_write.cgi?port=0&baud=4098&bytes=4&data=arf1'
  342.             break;
  343.         case 2:
  344.             //action_zone.location = '/comm_write.cgi?port=0&baud=4098&bytes=4&data=arb1'
  345.             command = 'arb1';
  346.             break;
  347.         case 3:
  348.             //action_zone.location = '/comm_write.cgi?port=0&baud=4098&bytes=4&data=arr1'
  349.             command = 'arr1';
  350.             break;
  351.         case 4:
  352.             //action_zone.location = '/comm_write.cgi?port=0&baud=4098&bytes=4&data=arl1'
  353.             command = 'arl1';
  354.             break;
  355.     }
  356.     if (tank_url != '')
  357.     {
  358.         action_zone.location = tank_url+'/comm_write.cgi?port=0&baud=4098&bytes=4&data='+command;
  359.     } else {
  360.         loadDoc('/comm_write.cgi?port=0&baud=4098&bytes=4&data='+command);
  361.     }
  362.     clearTimeout(deadman);
  363.     dodeadman();
  364.    
  365.     //post_to_url('/comm_write.cgi', {'port':'0','baud':'4098','bytes':'4','data':'arf1'},'post');
  366.     //alert(dir);
  367. }
  368.  
  369. function handlecammovement(dir)
  370. {
  371.     cam_direction = dir;
  372.     command = '1';
  373.     switch (dir)
  374.     {
  375.         case 0:
  376.             //action_zone.location = '/decoder_control.cgi?command=1'
  377.             command = '1';
  378.             break;
  379.         case 1:
  380.             //action_zone.location = '/decoder_control.cgi?command=0'
  381.             command = '0';
  382.             break;
  383.         case 2:
  384.             //action_zone.location = '/decoder_control.cgi?command=2'
  385.             command = '2';
  386.             break;
  387.         case 3:
  388.             //action_zone.location = '/decoder_control.cgi?command=4'
  389.             command = '4';
  390.             break;
  391.         case 4:
  392.             //action_zone.location = '/decoder_control.cgi?command=6'
  393.             command = '6';
  394.             break;
  395.         case 5:
  396.             //action_zone.location = '/decoder_control.cgi?command=6'
  397.             command = '31';
  398.             break;
  399.     }
  400.     //loadDoc('/decoder_control.cgi?command='+command);
  401.     //action_zone.location = 'http://192.168.1.99/decoder_control.cgi?command='+command
  402.     if (tank_url != '')
  403.     {
  404.         action_zone.location = tank_url+'/decoder_control.cgi?command='+command;
  405.     } else {
  406.         loadDoc('/decoder_control.cgi?command='+command);
  407.     }
  408. }
  409.  
  410. function handledeadman()
  411. {
  412.     if (direction != 0)
  413.         handlemovement(direction);
  414.     dodeadman();
  415. }
  416.  
  417. function dodeadman()
  418. {
  419.     deadman=setTimeout("handledeadman()", 2000);
  420. }
  421.  
  422. </script>
  423. </head>
  424. <body>
  425. <form name="testform">
  426. <h2>Vroom</h2><p>
  427. <img src="/videostream.cgi" id="video_stream">
  428.  
  429. <textarea name="t" rows="1" cols="90" hidden=true></textarea>
  430. <br>
  431. WSAD keys steer tank.<br>
  432. Arrow keys steer camera.<br>
  433. Home button points camera dead ahead.<br>
  434. </form>
  435. <script language="JavaScript">
  436. init();
  437. </script>
  438. <iframe name="action_zone" id="action_zone" style="display:none"></iframe>
  439. </body>
  440. </html>
  441.  
  442.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement