{"action":"created","comment":{"url":"https://api.github.com/repos/tessel/t2-firmware/pulls/comments/57379042","id":57379042,"diff_hunk":"@@ -85,6 +94,73 @@ Tessel.prototype.close = function() {\n   }, this);\n };\n \n+Tessel.prototype.pwmFrequency = function(frequency, cb) {\n+  if (frequency < Tessel.pwmMinFrequency || frequency > Tessel.pwmMaxFrequency) {\n+    throw new Error(`${frequency} is outside the bounds of ${Tessel.pwmMinFrequency}-${Tessel.pwmMaxFrequency} Hz`);\n+  }\n+\n+  determineDutyCycleAndPrescalar(frequency, (err, results) => {\n+    if (err) {\n+      throw err;\n+    }\n+    else {\n+      Tessel.pwmPeriod = results.period;\n+      Tessel.pwmPrescalar = results.prescalar;\n+\n+      // We are currently only using TCC Bank 0\n+      // This may be expanded in the future to enable PWM on more pins\n+      const TCC_ID = 0;\n+\n+      var packet = new Buffer(4);\n+      // Write the command id first\n+      packet.writeUInt8(CMD.PWM_PERIOD, 0);\n+      // Write our prescalar to the top 4 bits and TCC id to the bottom 4 bits\n+      packet.writeUInt8((Tessel.pwmPrescalar << 4) | TCC_ID, 1);\n+      // Write our period (16 bits)\n+      packet.writeUInt16BE(Tessel.pwmPeriod, 2);\n+\n+      // Send the packet off to the samd21\n+      // on the first port object (regardless of name)\n+      this.port[Object.keys(this.port)[0]].sock.write(packet, cb);\n+    }\n+  });\n+};\n+\n+/*\n+ Takes in a desired frequency setting and outputs the\n+ necessary prescalar and duty cycle settings based on set period.\n+ Outputs an object in the form of:\n+ {\n+  prescalar: number (0-7),\n+  period: number (0-0xFFFF)\n+ }\n+*/\n+function determineDutyCycleAndPrescalar(frequency, cb) {\n+  // Maximum number of ticks before period completes\n+  const MAX_PERIOD = 0xFFFF;\n+  // Maximum number of unscaled ticks in a second (48 MHz)\n+  const TICKS_PER_SECOND = 48000000;\n+  // Current setting for the prescalar\n+  var prescalarIndex = 0;\n+  // Current period setting\n+  var period = 0;\n+\n+  // If the current frequency would require a period greater than the max\n+  while ((period = Math.floor((TICKS_PER_SECOND / Tessel.pwmPrescalars[prescalarIndex])/frequency)) > MAX_PERIOD) {\n+    // Increase our clock prescalar\n+    prescalarIndex++;\n+\n+    // If we have no higher prescalars\n+    if (prescalarIndex === Tessel.pwmPrescalars.length) {\n+      // Throw an error because this frequency is too low for our possible parameters\n+      return cb(new Error('Unable to find prescalar/duty cycle parameter match for frequency'));\n+    }\n+  }\n+\n+  // We have found a period inside a suitable prescalar, return results\n+  return cb(null, {period: period, prescalar: prescalarIndex});","path":"node/tessel-export.js","position":84,"original_position":84,"commit_id":"53a91293f993bad437ce74449a88b87800054f86","original_commit_id":"53a91293f993bad437ce74449a88b87800054f86","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":"There is nothing async about this function, it should not accept a callback and should only return a value or throw an exception. ","created_at":"2016-03-24T20:03:44Z","updated_at":"2016-03-24T20:03:44Z","html_url":"https://github.com/tessel/t2-firmware/pull/155#discussion_r57379042","pull_request_url":"https://api.github.com/repos/tessel/t2-firmware/pulls/155","_links":{"self":{"href":"https://api.github.com/repos/tessel/t2-firmware/pulls/comments/57379042"},"html":{"href":"https://github.com/tessel/t2-firmware/pull/155#discussion_r57379042"},"pull_request":{"href":"https://api.github.com/repos/tessel/t2-firmware/pulls/155"}}},"pull_request":{"url":"https://api.github.com/repos/tessel/t2-firmware/pulls/155","id":64027308,"html_url":"https://github.com/tessel/t2-firmware/pull/155","diff_url":"https://github.com/tessel/t2-firmware/pull/155.diff","patch_url":"https://github.com/tessel/t2-firmware/pull/155.patch","issue_url":"https://api.github.com/repos/tessel/t2-firmware/issues/155","number":155,"state":"open","locked":false,"title":"[WIP] Adds PWM capabilities and tests","user":{"login":"johnnyman727","id":432909,"avatar_url":"https://avatars.githubusercontent.com/u/432909?v=3","gravatar_id":"","url":"https://api.github.com/users/johnnyman727","html_url":"https://github.com/johnnyman727","followers_url":"https://api.github.com/users/johnnyman727/followers","following_url":"https://api.github.com/users/johnnyman727/following{/other_user}","gists_url":"https://api.github.com/users/johnnyman727/gists{/gist_id}","starred_url":"https://api.github.com/users/johnnyman727/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johnnyman727/subscriptions","organizations_url":"https://api.github.com/users/johnnyman727/orgs","repos_url":"https://api.github.com/users/johnnyman727/repos","events_url":"https://api.github.com/users/johnnyman727/events{/privacy}","received_events_url":"https://api.github.com/users/johnnyman727/received_events","type":"User","site_admin":false},"body":"Pending API comments on https://github.com/tessel/t2-firmware/issues/21.\r\n\r\nFixes #21.\r\n\r\nDocs at https://github.com/tessel/t2-docs/pull/46.","created_at":"2016-03-24T12:32:25Z","updated_at":"2016-03-24T20:03:44Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3c624402b781b49afb05d3c68d2685b3a6f566fd","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/tessel/t2-firmware/pulls/155/commits","review_comments_url":"https://api.github.com/repos/tessel/t2-firmware/pulls/155/comments","review_comment_url":"https://api.github.com/repos/tessel/t2-firmware/pulls/comments{/number}","comments_url":"https://api.github.com/repos/tessel/t2-firmware/issues/155/comments","statuses_url":"https://api.github.com/repos/tessel/t2-firmware/statuses/53a91293f993bad437ce74449a88b87800054f86","head":{"label":"tessel:jon-pwm","ref":"jon-pwm","sha":"53a91293f993bad437ce74449a88b87800054f86","user":{"login":"tessel","id":5573563,"avatar_url":"https://avatars.githubusercontent.com/u/5573563?v=3","gravatar_id":"","url":"https://api.github.com/users/tessel","html_url":"https://github.com/tessel","followers_url":"https://api.github.com/users/tessel/followers","following_url":"https://api.github.com/users/tessel/following{/other_user}","gists_url":"https://api.github.com/users/tessel/gists{/gist_id}","starred_url":"https://api.github.com/users/tessel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tessel/subscriptions","organizations_url":"https://api.github.com/users/tessel/orgs","repos_url":"https://api.github.com/users/tessel/repos","events_url":"https://api.github.com/users/tessel/events{/privacy}","received_events_url":"https://api.github.com/users/tessel/received_events","type":"Organization","site_admin":false},"repo":{"id":29262099,"name":"t2-firmware","full_name":"tessel/t2-firmware","owner":{"login":"tessel","id":5573563,"avatar_url":"https://avatars.githubusercontent.com/u/5573563?v=3","gravatar_id":"","url":"https://api.github.com/users/tessel","html_url":"https://github.com/tessel","followers_url":"https://api.github.com/users/tessel/followers","following_url":"https://api.github.com/users/tessel/following{/other_user}","gists_url":"https://api.github.com/users/tessel/gists{/gist_id}","starred_url":"https://api.github.com/users/tessel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tessel/subscriptions","organizations_url":"https://api.github.com/users/tessel/orgs","repos_url":"https://api.github.com/users/tessel/repos","events_url":"https://api.github.com/users/tessel/events{/privacy}","received_events_url":"https://api.github.com/users/tessel/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/tessel/t2-firmware","description":"Firmware for Tessel 2's SAMD21 coprocessor and related SoC code","fork":false,"url":"https://api.github.com/repos/tessel/t2-firmware","forks_url":"https://api.github.com/repos/tessel/t2-firmware/forks","keys_url":"https://api.github.com/repos/tessel/t2-firmware/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tessel/t2-firmware/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tessel/t2-firmware/teams","hooks_url":"https://api.github.com/repos/tessel/t2-firmware/hooks","issue_events_url":"https://api.github.com/repos/tessel/t2-firmware/issues/events{/number}","events_url":"https://api.github.com/repos/tessel/t2-firmware/events","assignees_url":"https://api.github.com/repos/tessel/t2-firmware/assignees{/user}","branches_url":"https://api.github.com/repos/tessel/t2-firmware/branches{/branch}","tags_url":"https://api.github.com/repos/tessel/t2-firmware/tags","blobs_url":"https://api.github.com/repos/tessel/t2-firmware/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tessel/t2-firmware/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tessel/t2-firmware/git/refs{/sha}","trees_url":"https://api.github.com/repos/tessel/t2-firmware/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tessel/t2-firmware/statuses/{sha}","languages_url":"https://api.github.com/repos/tessel/t2-firmware/languages","stargazers_url":"https://api.github.com/repos/tessel/t2-firmware/stargazers","contributors_url":"https://api.github.com/repos/tessel/t2-firmware/contributors","subscribers_url":"https://api.github.com/repos/tessel/t2-firmware/subscribers","subscription_url":"https://api.github.com/repos/tessel/t2-firmware/subscription","commits_url":"https://api.github.com/repos/tessel/t2-firmware/commits{/sha}","git_commits_url":"https://api.github.com/repos/tessel/t2-firmware/git/commits{/sha}","comments_url":"https://api.github.com/repos/tessel/t2-firmware/comments{/number}","issue_comment_url":"https://api.github.com/repos/tessel/t2-firmware/issues/comments{/number}","contents_url":"https://api.github.com/repos/tessel/t2-firmware/contents/{+path}","compare_url":"https://api.github.com/repos/tessel/t2-firmware/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tessel/t2-firmware/merges","archive_url":"https://api.github.com/repos/tessel/t2-firmware/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tessel/t2-firmware/downloads","issues_url":"https://api.github.com/repos/tessel/t2-firmware/issues{/number}","pulls_url":"https://api.github.com/repos/tessel/t2-firmware/pulls{/number}","milestones_url":"https://api.github.com/repos/tessel/t2-firmware/milestones{/number}","notifications_url":"https://api.github.com/repos/tessel/t2-firmware/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tessel/t2-firmware/labels{/name}","releases_url":"https://api.github.com/repos/tessel/t2-firmware/releases{/id}","deployments_url":"https://api.github.com/repos/tessel/t2-firmware/deployments","created_at":"2015-01-14T19:38:00Z","updated_at":"2016-01-23T14:45:29Z","pushed_at":"2016-03-24T19:53:52Z","git_url":"git://github.com/tessel/t2-firmware.git","ssh_url":"git@github.com:tessel/t2-firmware.git","clone_url":"https://github.com/tessel/t2-firmware.git","svn_url":"https://github.com/tessel/t2-firmware","homepage":"","size":654,"stargazers_count":27,"watchers_count":27,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":11,"mirror_url":null,"open_issues_count":30,"forks":11,"open_issues":30,"watchers":27,"default_branch":"master"}},"base":{"label":"tessel:master","ref":"master","sha":"723bdf970ca22d342ccf6236a19685a72c4bb12d","user":{"login":"tessel","id":5573563,"avatar_url":"https://avatars.githubusercontent.com/u/5573563?v=3","gravatar_id":"","url":"https://api.github.com/users/tessel","html_url":"https://github.com/tessel","followers_url":"https://api.github.com/users/tessel/followers","following_url":"https://api.github.com/users/tessel/following{/other_user}","gists_url":"https://api.github.com/users/tessel/gists{/gist_id}","starred_url":"https://api.github.com/users/tessel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tessel/subscriptions","organizations_url":"https://api.github.com/users/tessel/orgs","repos_url":"https://api.github.com/users/tessel/repos","events_url":"https://api.github.com/users/tessel/events{/privacy}","received_events_url":"https://api.github.com/users/tessel/received_events","type":"Organization","site_admin":false},"repo":{"id":29262099,"name":"t2-firmware","full_name":"tessel/t2-firmware","owner":{"login":"tessel","id":5573563,"avatar_url":"https://avatars.githubusercontent.com/u/5573563?v=3","gravatar_id":"","url":"https://api.github.com/users/tessel","html_url":"https://github.com/tessel","followers_url":"https://api.github.com/users/tessel/followers","following_url":"https://api.github.com/users/tessel/following{/other_user}","gists_url":"https://api.github.com/users/tessel/gists{/gist_id}","starred_url":"https://api.github.com/users/tessel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tessel/subscriptions","organizations_url":"https://api.github.com/users/tessel/orgs","repos_url":"https://api.github.com/users/tessel/repos","events_url":"https://api.github.com/users/tessel/events{/privacy}","received_events_url":"https://api.github.com/users/tessel/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/tessel/t2-firmware","description":"Firmware for Tessel 2's SAMD21 coprocessor and related SoC code","fork":false,"url":"https://api.github.com/repos/tessel/t2-firmware","forks_url":"https://api.github.com/repos/tessel/t2-firmware/forks","keys_url":"https://api.github.com/repos/tessel/t2-firmware/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tessel/t2-firmware/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tessel/t2-firmware/teams","hooks_url":"https://api.github.com/repos/tessel/t2-firmware/hooks","issue_events_url":"https://api.github.com/repos/tessel/t2-firmware/issues/events{/number}","events_url":"https://api.github.com/repos/tessel/t2-firmware/events","assignees_url":"https://api.github.com/repos/tessel/t2-firmware/assignees{/user}","branches_url":"https://api.github.com/repos/tessel/t2-firmware/branches{/branch}","tags_url":"https://api.github.com/repos/tessel/t2-firmware/tags","blobs_url":"https://api.github.com/repos/tessel/t2-firmware/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tessel/t2-firmware/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tessel/t2-firmware/git/refs{/sha}","trees_url":"https://api.github.com/repos/tessel/t2-firmware/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tessel/t2-firmware/statuses/{sha}","languages_url":"https://api.github.com/repos/tessel/t2-firmware/languages","stargazers_url":"https://api.github.com/repos/tessel/t2-firmware/stargazers","contributors_url":"https://api.github.com/repos/tessel/t2-firmware/contributors","subscribers_url":"https://api.github.com/repos/tessel/t2-firmware/subscribers","subscription_url":"https://api.github.com/repos/tessel/t2-firmware/subscription","commits_url":"https://api.github.com/repos/tessel/t2-firmware/commits{/sha}","git_commits_url":"https://api.github.com/repos/tessel/t2-firmware/git/commits{/sha}","comments_url":"https://api.github.com/repos/tessel/t2-firmware/comments{/number}","issue_comment_url":"https://api.github.com/repos/tessel/t2-firmware/issues/comments{/number}","contents_url":"https://api.github.com/repos/tessel/t2-firmware/contents/{+path}","compare_url":"https://api.github.com/repos/tessel/t2-firmware/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tessel/t2-firmware/merges","archive_url":"https://api.github.com/repos/tessel/t2-firmware/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tessel/t2-firmware/downloads","issues_url":"https://api.github.com/repos/tessel/t2-firmware/issues{/number}","pulls_url":"https://api.github.com/repos/tessel/t2-firmware/pulls{/number}","milestones_url":"https://api.github.com/repos/tessel/t2-firmware/milestones{/number}","notifications_url":"https://api.github.com/repos/tessel/t2-firmware/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tessel/t2-firmware/labels{/name}","releases_url":"https://api.github.com/repos/tessel/t2-firmware/releases{/id}","deployments_url":"https://api.github.com/repos/tessel/t2-firmware/deployments","created_at":"2015-01-14T19:38:00Z","updated_at":"2016-01-23T14:45:29Z","pushed_at":"2016-03-24T19:53:52Z","git_url":"git://github.com/tessel/t2-firmware.git","ssh_url":"git@github.com:tessel/t2-firmware.git","clone_url":"https://github.com/tessel/t2-firmware.git","svn_url":"https://github.com/tessel/t2-firmware","homepage":"","size":654,"stargazers_count":27,"watchers_count":27,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":11,"mirror_url":null,"open_issues_count":30,"forks":11,"open_issues":30,"watchers":27,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/tessel/t2-firmware/pulls/155"},"html":{"href":"https://github.com/tessel/t2-firmware/pull/155"},"issue":{"href":"https://api.github.com/repos/tessel/t2-firmware/issues/155"},"comments":{"href":"https://api.github.com/repos/tessel/t2-firmware/issues/155/comments"},"review_comments":{"href":"https://api.github.com/repos/tessel/t2-firmware/pulls/155/comments"},"review_comment":{"href":"https://api.github.com/repos/tessel/t2-firmware/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/tessel/t2-firmware/pulls/155/commits"},"statuses":{"href":"https://api.github.com/repos/tessel/t2-firmware/statuses/53a91293f993bad437ce74449a88b87800054f86"}}}}