Browse Source

rewrite car.js as oo javascript

virmaior 3 years ago
parent
commit
74a07c5e4d
1 changed files with 45 additions and 123 deletions
  1. 45 123
      SD_ROOT/wz_mini/www/car/car.js

+ 45 - 123
SD_ROOT/wz_mini/www/car/car.js

@@ -1,127 +1,49 @@
-$(document).ready(function() {
-
-$('#forward').click(function() {
-$.post("../cgi-bin/car.sh", "forward" );
-});
-
-addEventListener("keydown", function (e) {
-if (e.key === "w") {  
-$.post("../cgi-bin/car.sh", "forward" );
-}
-});
-
-$('#reverse').click(function() {
-$.post("../cgi-bin/car.sh", "reverse" );
-});
-
-addEventListener("keydown", function (e) {
-if (e.key === "s") {  
-$.post("../cgi-bin/car.sh", "reverse" );
-}
-});
-
-$('#left').click(function() {
-$.post("../cgi-bin/car.sh", "left" );
-});
-
-addEventListener("keydown", function (e) {
-if (e.key === "a") {  
-$.post("../cgi-bin/car.sh", "left" );
-}
-});
-
-$('#right').click(function() {
-$.post("../cgi-bin/car.sh", "right" );
-});
-
-addEventListener("keydown", function (e) {
-if (e.key === "d") {  
-$.post("../cgi-bin/car.sh", "right" );
-}
-});
-
-
-
-$('#forward_left').click(function() {
-$.post("../cgi-bin/car.sh", "forward_left" );
-});
-
-
-addEventListener("keydown", function (e) {
-if (e.key === "q") {  
-$.post("../cgi-bin/car.sh", "forward_left" );
-}
-});
-
-$('#forward_right').click(function() {
-$.post("../cgi-bin/car.sh", "forward_right" );
-});
-
-addEventListener("keydown", function (e) {
-if (e.key === "e") {  
-$.post("../cgi-bin/car.sh", "forward_right" );
+var wz_mini_car = {
+  post: function(action)
+  {
+          $.post("../cgi-bin/car.sh", action);
+  }  ,
+  init: function() {
+    this.logarray = [];
+
+    $('.wz_car_BUTTON').on('click',function(e){
+      var action = $(this).attr('id');
+      wz_mini_car.post(action);
+    });  
+
+    /* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
+      switch is strict
+    */
+    
+    addEventListener("keydown", function (e) {
+      var action = false;
+      switch(e.key) {
+        case "w": action = "forward"; break;
+        case "s": action = "reverse"; break;
+        case "a": action = "left"; break;
+        case "d": action = "right"; break;
+        case "q": action =  "forward_left" ; break;
+        case "e": action = "forward_right"; break;
+        case "z": action = "reverse_left"; break;
+        case "c": action = "reverse_right" ; break;
+        case "x": action = "all_stop" ; break;  
+        /* everything was "x" below here ... assigned other letters */
+        case "h": action = "headlight" ; break;
+        case "i": action = "irled" ; break;
+        case "k": action = "honk" ; break;
+      } 
+      if (action) {
+        wz_mini_car.post(action);
+      }  
+    });  
+  },
+  log: function(text)
+  {
+    this.logarray.push(text);
+  }  
 }
-});
-
-$('#reverse_left').click(function() {
-$.post("../cgi-bin/car.sh", "reverse_left" );
-});
-
-addEventListener("keydown", function (e) {
-if (e.key === "z") {  
-$.post("../cgi-bin/car.sh", "reverse_left" );
-}
-});
-
-$('#reverse_right').click(function() {
-$.post("../cgi-bin/car.sh", "reverse_right" );
-});
-
-addEventListener("keydown", function (e) {
-if (e.key === "c") {  
-$.post("../cgi-bin/car.sh", "reverse_right" );
-}
-});
-
-$('#all_stop').click(function() {
-$.post("../cgi-bin/car.sh", "all_stop" );
-});
-
-addEventListener("keydown", function (e) {
-if (e.key === "x") {  
-$.post("../cgi-bin/car.sh", "all_stop" );
-}
-});
-
-$('#headlight').click(function() {
-$.post("../cgi-bin/car.sh", "headlight" );
-});
-
-addEventListener("keydown", function (e) {
-if (e.key === "x") {  
-$.post("../cgi-bin/car.sh", "headlight" );
-}
-});
-
-$('#irled').click(function() {
-$.post("../cgi-bin/car.sh", "irled" );
-});
-
-addEventListener("keydown", function (e) {
-if (e.key === "x") {  
-$.post("../cgi-bin/car.sh", "irled" );
-}
-});
-
-$('#honk').click(function() {
-$.post("../cgi-bin/car.sh", "honk" );
-});
-
-addEventListener("keydown", function (e) {
-if (e.key === "x") {  
-$.post("../cgi-bin/car.sh", "honk" );
-}
-});
 
 
+$(document).ready(function() {
+  wz_mini_car.init();
 });