{"action":"created","comment":{"url":"https://api.github.com/repos/rwaldron/johnny-five/pulls/comments/30008804","id":30008804,"diff_hunk":"@@ -11,149 +121,109 @@ var Board = require(\"../lib/board.js\"),\n  * five.Joystick({\n  *   pins: [ x, y[, z] ]\n  *   freq: ms\n- *  });\n+ * });\n  *\n  *\n  * @param {Object} opts [description]\n  *\n  */\n-\n function Joystick(opts) {\n-\n   if (!(this instanceof Joystick)) {\n     return new Joystick(opts);\n   }\n \n-  var err = null;\n+  var controller = null;\n+\n+  var state = {\n+    x: {\n+      value: 0,\n+      previous: 0,\n+      zeroV: null,\n+    },\n+    y: {\n+      value: 0,\n+      previous: 0,\n+      zeroV: null,\n+    }\n+  };\n \n   Board.Component.call(\n     this, opts = Board.Options(opts)\n   );\n \n-  this.mode = this.io.MODES.ANALOG;\n-\n-  // Joystick instance properties\n-  this.freq = opts.freq || 500;\n+  if (opts.controller && typeof opts.controller === \"string\") {\n+    controller = Controllers[opts.controller.toUpperCase()];\n+  } else {\n+    controller = opts.controller;\n+  }\n \n-  this.magnitude = 0;\n+  if (controller == null) {\n+    controller = Controllers[\"ANALOG\"];\n+  }\n \n-  this.axis = {\n-    x: 0,\n-    y: 0\n-  };\n+  Object.defineProperties(this, controller);\n \n-  this.fixed = {\n-    x: 0,\n-    y: 0\n-  };\n+  if (!this.toAxis) {\n+    this.toAxis = opts.toAxis || function(raw) { return raw; };\n+  }\n \n-  this.normalized = {\n-    x: 0,\n-    y: 0\n-  };\n+  priv.set(this, state);\n \n-  this.raw = {\n-    x: 0,\n-    y: 0\n-  };\n+  if (typeof this.initialize === \"function\") {\n+    this.initialize(opts, function(data) {\n+      var isChange = false;\n \n+      Object.keys(data).forEach(function(axis) {\n+        var value = data[axis];\n+        var sensor = state[axis];\n+        var zeroV = sensor.zeroV < 0 ? sensor.zeroV * -1 : sensor.zeroV;\n \n-  // TODO: calculate joystick direction based on X/Y values\n-  this.direction = \"\";\n+        sensor.value = value + zeroV;\n \n-  this.pins.forEach(function(pin) {\n-    // Set the pin to input mode\n-    this.io.pinMode(pin, this.mode);\n+        if (this.x !== sensor.x) {\n+          sensor.x = this.x;\n+          isChange = true;\n+        }\n \n-    // Arduino 0-1023  512\n-    this.io.analogRead(pin, function(data) {\n+        if (this.y !== sensor.y) {\n+          sensor.y = this.y;\n+          isChange = true;\n+        }\n+      }, this);\n \n-      var val = data / 1023;\n+      this.emit(\"data\", {\n+        x: state.x.value,\n+        y: state.y.value\n+      });\n \n-      // register X axis, L/R (A0)\n-      if (pin === 1) {\n-        this.axis.x = val;\n-        this.raw.x = data;\n-        // register Y axis, U/D (A1)\n-      } else {\n-        this.axis.y = val;\n-        this.raw.y = data;\n+      if (isChange) {","path":"lib/joystick.js","position":240,"original_position":240,"commit_id":"f7eb23416633fb11fa8e9a0aa90674c50043a094","original_commit_id":"f7eb23416633fb11fa8e9a0aa90674c50043a094","user":{"login":"scottgonzalez","id":141167,"avatar_url":"https://avatars.githubusercontent.com/u/141167?v=3","gravatar_id":"","url":"https://api.github.com/users/scottgonzalez","html_url":"https://github.com/scottgonzalez","followers_url":"https://api.github.com/users/scottgonzalez/followers","following_url":"https://api.github.com/users/scottgonzalez/following{/other_user}","gists_url":"https://api.github.com/users/scottgonzalez/gists{/gist_id}","starred_url":"https://api.github.com/users/scottgonzalez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scottgonzalez/subscriptions","organizations_url":"https://api.github.com/users/scottgonzalez/orgs","repos_url":"https://api.github.com/users/scottgonzalez/repos","events_url":"https://api.github.com/users/scottgonzalez/events{/privacy}","received_events_url":"https://api.github.com/users/scottgonzalez/received_events","type":"User","site_admin":false},"body":"Should joystick support a threshold similar to the sensor component?","created_at":"2015-05-11T01:01:54Z","updated_at":"2015-05-11T01:01:54Z","html_url":"https://github.com/rwaldron/johnny-five/pull/785#discussion_r30008804","pull_request_url":"https://api.github.com/repos/rwaldron/johnny-five/pulls/785","_links":{"self":{"href":"https://api.github.com/repos/rwaldron/johnny-five/pulls/comments/30008804"},"html":{"href":"https://github.com/rwaldron/johnny-five/pull/785#discussion_r30008804"},"pull_request":{"href":"https://api.github.com/repos/rwaldron/johnny-five/pulls/785"}}},"pull_request":{"url":"https://api.github.com/repos/rwaldron/johnny-five/pulls/785","id":35022928,"html_url":"https://github.com/rwaldron/johnny-five/pull/785","diff_url":"https://github.com/rwaldron/johnny-five/pull/785.diff","patch_url":"https://github.com/rwaldron/johnny-five/pull/785.patch","issue_url":"https://api.github.com/repos/rwaldron/johnny-five/issues/785","number":785,"state":"open","locked":false,"title":"Joystick: Controller refactor, Esplora support.","user":{"login":"rwaldron","id":27985,"avatar_url":"https://avatars.githubusercontent.com/u/27985?v=3","gravatar_id":"","url":"https://api.github.com/users/rwaldron","html_url":"https://github.com/rwaldron","followers_url":"https://api.github.com/users/rwaldron/followers","following_url":"https://api.github.com/users/rwaldron/following{/other_user}","gists_url":"https://api.github.com/users/rwaldron/gists{/gist_id}","starred_url":"https://api.github.com/users/rwaldron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rwaldron/subscriptions","organizations_url":"https://api.github.com/users/rwaldron/orgs","repos_url":"https://api.github.com/users/rwaldron/repos","events_url":"https://api.github.com/users/rwaldron/events{/privacy}","received_events_url":"https://api.github.com/users/rwaldron/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-05-08T16:10:14Z","updated_at":"2015-05-11T01:01:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"35abe52a4b9336361c9c5a29afb0fa4c8b23c759","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/rwaldron/johnny-five/pulls/785/commits","review_comments_url":"https://api.github.com/repos/rwaldron/johnny-five/pulls/785/comments","review_comment_url":"https://api.github.com/repos/rwaldron/johnny-five/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rwaldron/johnny-five/issues/785/comments","statuses_url":"https://api.github.com/repos/rwaldron/johnny-five/statuses/f7eb23416633fb11fa8e9a0aa90674c50043a094","head":{"label":"rwaldron:joystick","ref":"joystick","sha":"f7eb23416633fb11fa8e9a0aa90674c50043a094","user":{"login":"rwaldron","id":27985,"avatar_url":"https://avatars.githubusercontent.com/u/27985?v=3","gravatar_id":"","url":"https://api.github.com/users/rwaldron","html_url":"https://github.com/rwaldron","followers_url":"https://api.github.com/users/rwaldron/followers","following_url":"https://api.github.com/users/rwaldron/following{/other_user}","gists_url":"https://api.github.com/users/rwaldron/gists{/gist_id}","starred_url":"https://api.github.com/users/rwaldron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rwaldron/subscriptions","organizations_url":"https://api.github.com/users/rwaldron/orgs","repos_url":"https://api.github.com/users/rwaldron/repos","events_url":"https://api.github.com/users/rwaldron/events{/privacy}","received_events_url":"https://api.github.com/users/rwaldron/received_events","type":"User","site_admin":false},"repo":{"id":3880513,"name":"johnny-five","full_name":"rwaldron/johnny-five","owner":{"login":"rwaldron","id":27985,"avatar_url":"https://avatars.githubusercontent.com/u/27985?v=3","gravatar_id":"","url":"https://api.github.com/users/rwaldron","html_url":"https://github.com/rwaldron","followers_url":"https://api.github.com/users/rwaldron/followers","following_url":"https://api.github.com/users/rwaldron/following{/other_user}","gists_url":"https://api.github.com/users/rwaldron/gists{/gist_id}","starred_url":"https://api.github.com/users/rwaldron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rwaldron/subscriptions","organizations_url":"https://api.github.com/users/rwaldron/orgs","repos_url":"https://api.github.com/users/rwaldron/repos","events_url":"https://api.github.com/users/rwaldron/events{/privacy}","received_events_url":"https://api.github.com/users/rwaldron/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/rwaldron/johnny-five","description":"JavaScript Robotics and IoT programming framework, developed at Bocoup. Based on Arduino Firmata Protocol","fork":false,"url":"https://api.github.com/repos/rwaldron/johnny-five","forks_url":"https://api.github.com/repos/rwaldron/johnny-five/forks","keys_url":"https://api.github.com/repos/rwaldron/johnny-five/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rwaldron/johnny-five/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rwaldron/johnny-five/teams","hooks_url":"https://api.github.com/repos/rwaldron/johnny-five/hooks","issue_events_url":"https://api.github.com/repos/rwaldron/johnny-five/issues/events{/number}","events_url":"https://api.github.com/repos/rwaldron/johnny-five/events","assignees_url":"https://api.github.com/repos/rwaldron/johnny-five/assignees{/user}","branches_url":"https://api.github.com/repos/rwaldron/johnny-five/branches{/branch}","tags_url":"https://api.github.com/repos/rwaldron/johnny-five/tags","blobs_url":"https://api.github.com/repos/rwaldron/johnny-five/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rwaldron/johnny-five/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rwaldron/johnny-five/git/refs{/sha}","trees_url":"https://api.github.com/repos/rwaldron/johnny-five/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rwaldron/johnny-five/statuses/{sha}","languages_url":"https://api.github.com/repos/rwaldron/johnny-five/languages","stargazers_url":"https://api.github.com/repos/rwaldron/johnny-five/stargazers","contributors_url":"https://api.github.com/repos/rwaldron/johnny-five/contributors","subscribers_url":"https://api.github.com/repos/rwaldron/johnny-five/subscribers","subscription_url":"https://api.github.com/repos/rwaldron/johnny-five/subscription","commits_url":"https://api.github.com/repos/rwaldron/johnny-five/commits{/sha}","git_commits_url":"https://api.github.com/repos/rwaldron/johnny-five/git/commits{/sha}","comments_url":"https://api.github.com/repos/rwaldron/johnny-five/comments{/number}","issue_comment_url":"https://api.github.com/repos/rwaldron/johnny-five/issues/comments{/number}","contents_url":"https://api.github.com/repos/rwaldron/johnny-five/contents/{+path}","compare_url":"https://api.github.com/repos/rwaldron/johnny-five/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rwaldron/johnny-five/merges","archive_url":"https://api.github.com/repos/rwaldron/johnny-five/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rwaldron/johnny-five/downloads","issues_url":"https://api.github.com/repos/rwaldron/johnny-five/issues{/number}","pulls_url":"https://api.github.com/repos/rwaldron/johnny-five/pulls{/number}","milestones_url":"https://api.github.com/repos/rwaldron/johnny-five/milestones{/number}","notifications_url":"https://api.github.com/repos/rwaldron/johnny-five/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rwaldron/johnny-five/labels{/name}","releases_url":"https://api.github.com/repos/rwaldron/johnny-five/releases{/id}","created_at":"2012-03-30T20:09:52Z","updated_at":"2015-05-10T21:29:45Z","pushed_at":"2015-05-10T02:21:56Z","git_url":"git://github.com/rwaldron/johnny-five.git","ssh_url":"git@github.com:rwaldron/johnny-five.git","clone_url":"https://github.com/rwaldron/johnny-five.git","svn_url":"https://github.com/rwaldron/johnny-five","homepage":"","size":59881,"stargazers_count":3671,"watchers_count":3671,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":463,"mirror_url":null,"open_issues_count":108,"forks":463,"open_issues":108,"watchers":3671,"default_branch":"master"}},"base":{"label":"rwaldron:master","ref":"master","sha":"6a31cdf6f59927afc7e051cbd19f8e71e4544796","user":{"login":"rwaldron","id":27985,"avatar_url":"https://avatars.githubusercontent.com/u/27985?v=3","gravatar_id":"","url":"https://api.github.com/users/rwaldron","html_url":"https://github.com/rwaldron","followers_url":"https://api.github.com/users/rwaldron/followers","following_url":"https://api.github.com/users/rwaldron/following{/other_user}","gists_url":"https://api.github.com/users/rwaldron/gists{/gist_id}","starred_url":"https://api.github.com/users/rwaldron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rwaldron/subscriptions","organizations_url":"https://api.github.com/users/rwaldron/orgs","repos_url":"https://api.github.com/users/rwaldron/repos","events_url":"https://api.github.com/users/rwaldron/events{/privacy}","received_events_url":"https://api.github.com/users/rwaldron/received_events","type":"User","site_admin":false},"repo":{"id":3880513,"name":"johnny-five","full_name":"rwaldron/johnny-five","owner":{"login":"rwaldron","id":27985,"avatar_url":"https://avatars.githubusercontent.com/u/27985?v=3","gravatar_id":"","url":"https://api.github.com/users/rwaldron","html_url":"https://github.com/rwaldron","followers_url":"https://api.github.com/users/rwaldron/followers","following_url":"https://api.github.com/users/rwaldron/following{/other_user}","gists_url":"https://api.github.com/users/rwaldron/gists{/gist_id}","starred_url":"https://api.github.com/users/rwaldron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rwaldron/subscriptions","organizations_url":"https://api.github.com/users/rwaldron/orgs","repos_url":"https://api.github.com/users/rwaldron/repos","events_url":"https://api.github.com/users/rwaldron/events{/privacy}","received_events_url":"https://api.github.com/users/rwaldron/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/rwaldron/johnny-five","description":"JavaScript Robotics and IoT programming framework, developed at Bocoup. Based on Arduino Firmata Protocol","fork":false,"url":"https://api.github.com/repos/rwaldron/johnny-five","forks_url":"https://api.github.com/repos/rwaldron/johnny-five/forks","keys_url":"https://api.github.com/repos/rwaldron/johnny-five/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rwaldron/johnny-five/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rwaldron/johnny-five/teams","hooks_url":"https://api.github.com/repos/rwaldron/johnny-five/hooks","issue_events_url":"https://api.github.com/repos/rwaldron/johnny-five/issues/events{/number}","events_url":"https://api.github.com/repos/rwaldron/johnny-five/events","assignees_url":"https://api.github.com/repos/rwaldron/johnny-five/assignees{/user}","branches_url":"https://api.github.com/repos/rwaldron/johnny-five/branches{/branch}","tags_url":"https://api.github.com/repos/rwaldron/johnny-five/tags","blobs_url":"https://api.github.com/repos/rwaldron/johnny-five/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rwaldron/johnny-five/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rwaldron/johnny-five/git/refs{/sha}","trees_url":"https://api.github.com/repos/rwaldron/johnny-five/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rwaldron/johnny-five/statuses/{sha}","languages_url":"https://api.github.com/repos/rwaldron/johnny-five/languages","stargazers_url":"https://api.github.com/repos/rwaldron/johnny-five/stargazers","contributors_url":"https://api.github.com/repos/rwaldron/johnny-five/contributors","subscribers_url":"https://api.github.com/repos/rwaldron/johnny-five/subscribers","subscription_url":"https://api.github.com/repos/rwaldron/johnny-five/subscription","commits_url":"https://api.github.com/repos/rwaldron/johnny-five/commits{/sha}","git_commits_url":"https://api.github.com/repos/rwaldron/johnny-five/git/commits{/sha}","comments_url":"https://api.github.com/repos/rwaldron/johnny-five/comments{/number}","issue_comment_url":"https://api.github.com/repos/rwaldron/johnny-five/issues/comments{/number}","contents_url":"https://api.github.com/repos/rwaldron/johnny-five/contents/{+path}","compare_url":"https://api.github.com/repos/rwaldron/johnny-five/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rwaldron/johnny-five/merges","archive_url":"https://api.github.com/repos/rwaldron/johnny-five/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rwaldron/johnny-five/downloads","issues_url":"https://api.github.com/repos/rwaldron/johnny-five/issues{/number}","pulls_url":"https://api.github.com/repos/rwaldron/johnny-five/pulls{/number}","milestones_url":"https://api.github.com/repos/rwaldron/johnny-five/milestones{/number}","notifications_url":"https://api.github.com/repos/rwaldron/johnny-five/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rwaldron/johnny-five/labels{/name}","releases_url":"https://api.github.com/repos/rwaldron/johnny-five/releases{/id}","created_at":"2012-03-30T20:09:52Z","updated_at":"2015-05-10T21:29:45Z","pushed_at":"2015-05-10T02:21:56Z","git_url":"git://github.com/rwaldron/johnny-five.git","ssh_url":"git@github.com:rwaldron/johnny-five.git","clone_url":"https://github.com/rwaldron/johnny-five.git","svn_url":"https://github.com/rwaldron/johnny-five","homepage":"","size":59881,"stargazers_count":3671,"watchers_count":3671,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":463,"mirror_url":null,"open_issues_count":108,"forks":463,"open_issues":108,"watchers":3671,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rwaldron/johnny-five/pulls/785"},"html":{"href":"https://github.com/rwaldron/johnny-five/pull/785"},"issue":{"href":"https://api.github.com/repos/rwaldron/johnny-five/issues/785"},"comments":{"href":"https://api.github.com/repos/rwaldron/johnny-five/issues/785/comments"},"review_comments":{"href":"https://api.github.com/repos/rwaldron/johnny-five/pulls/785/comments"},"review_comment":{"href":"https://api.github.com/repos/rwaldron/johnny-five/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rwaldron/johnny-five/pulls/785/commits"},"statuses":{"href":"https://api.github.com/repos/rwaldron/johnny-five/statuses/f7eb23416633fb11fa8e9a0aa90674c50043a094"}}}}