car.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const queryString = window.location.search;
  2. console.log(queryString);
  3. const urlParams = new URLSearchParams(queryString);
  4. const speed = urlParams.get('speed')
  5. console.log("speed is", speed);
  6. const sleep_timer = urlParams.get('sleep_time')
  7. console.log("sleep_timer is", sleep_timer);
  8. function set_sleep() {
  9. sleep_timer2 = document.getElementById("sleep_timer").value;
  10. window.location = "car.html?sleep_time=" + sleep_timer2;
  11. }
  12. function set_vars() {
  13. sleep_timer2 = document.getElementById("sleep_timer").value;
  14. var speed = $('input[class="speed"]:checked').val();
  15. window.location = "car.html?sleep_time=" + sleep_timer2 + "&speed=" + speed;
  16. }
  17. var wz_mini_car = {
  18. post: function(action)
  19. {
  20. $.post( "../cgi-bin/car.sh", { speed: speed, action: action, sleep_time: sleep_timer } );
  21. } ,
  22. init: function() {
  23. this.logarray = [];
  24. $("[class*='BUTTON']").on('click',function(e) {
  25. var action = $(this).attr('id');
  26. wz_mini_car.post(action);
  27. });
  28. /* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
  29. switch is strict
  30. */
  31. addEventListener("keydown", function (e) {
  32. var action = false;
  33. switch(e.key) {
  34. case "w": action = "forward"; break;
  35. case "s": action = "reverse"; break;
  36. case "a": action = "left"; break;
  37. case "d": action = "right"; break;
  38. case "q": action = "forward_left" ; break;
  39. case "e": action = "forward_right"; break;
  40. case "z": action = "reverse_left"; break;
  41. case "c": action = "reverse_right" ; break;
  42. case "x": action = "all_stop" ; break;
  43. case "h": action = "headlight_on" ; break;
  44. case "g": action = "headlight_off" ; break;
  45. case "j": action = "irled_on" ; break;
  46. case "k": action = "irled_off" ; break;
  47. case "b": action = "honk" ; break;
  48. }
  49. if (action) {
  50. wz_mini_car.post(action);
  51. }
  52. });
  53. },
  54. log: function(text)
  55. {
  56. this.logarray.push(text);
  57. }
  58. }
  59. $(document).ready(function() {
  60. wz_mini_car.init();
  61. });
  62. setInterval(function() {
  63. var myImageElement = document.getElementById('car_feed');
  64. myImageElement.src = '/cgi-bin/jpeg.cgi?channel=1&rand=' + Math.random();
  65. }, 1000);