index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // (c) 2014 Don Coleman
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /* global mainPage, deviceList, refreshButton */
  15. /* global detailPage, resultDiv, pulseWidthInput, motorButton, buzzerButton, disconnectButton */
  16. /* global ble, cordova */
  17. /* jshint browser: true , devel: true*/
  18. 'use strict';
  19. // this is MetaWear's UART service
  20. var metawear = {
  21. serviceUUID: "326a9000-85cb-9195-d9dd-464cfbbae75a",
  22. txCharacteristic: "326a9001-85cb-9195-d9dd-464cfbbae75a", // transmit is from the phone's perspective
  23. rxCharacteristic: "326a9006-85cb-9195-d9dd-464cfbbae75a" // receive is from the phone's perspective
  24. };
  25. var app = {
  26. deviceId: "",
  27. initialize: function() {
  28. this.bindEvents();
  29. detailPage.hidden = true;
  30. },
  31. bindEvents: function() {
  32. document.addEventListener('deviceready', this.onDeviceReady, false);
  33. refreshButton.addEventListener('touchstart', this.refreshDeviceList, false);
  34. motorButton.addEventListener('click', this.onMotorButton, false);
  35. buzzerButton.addEventListener('click', this.onBuzzerButton, false);
  36. disconnectButton.addEventListener('touchstart', this.disconnect, false);
  37. deviceList.addEventListener('touchstart', this.connect, false); // assume not scrolling
  38. },
  39. onDeviceReady: function() {
  40. app.refreshDeviceList();
  41. },
  42. refreshDeviceList: function() {
  43. deviceList.innerHTML = ''; // empties the list
  44. if (cordova.platformId === 'android') { // Android filtering is broken
  45. ble.scan([], 5, app.onDiscoverDevice, app.onError);
  46. } else {
  47. ble.scan([metawear.serviceUUID], 5, app.onDiscoverDevice, app.onError);
  48. }
  49. },
  50. onDiscoverDevice: function(device) {
  51. var listItem = document.createElement('li'),
  52. html = '<b>' + device.name + '</b><br/>' +
  53. 'RSSI: ' + device.rssi + '&nbsp;|&nbsp;' +
  54. device.id;
  55. listItem.dataset.deviceId = device.id;
  56. listItem.innerHTML = html;
  57. deviceList.appendChild(listItem);
  58. },
  59. connect: function(e) {
  60. app.deviceId = e.target.dataset.deviceId;
  61. var onConnect = function() {
  62. app.enableButtonFeedback(app.subscribeForIncomingData, app.onError);
  63. app.showDetailPage();
  64. };
  65. ble.connect(app.deviceId, onConnect, app.onError);
  66. },
  67. onData: function(buffer) { // data received from MetaWear
  68. var data = new Uint8Array(buffer);
  69. var message = "";
  70. if (data[0] === 1 && data[1] === 1) { // module = 1, opscode = 1
  71. if (data[2] === 1) { // button state
  72. message = "Button pressed";
  73. } else {
  74. message = "Button released";
  75. }
  76. }
  77. resultDiv.innerHTML = resultDiv.innerHTML + message + "<br/>";
  78. resultDiv.scrollTop = resultDiv.scrollHeight;
  79. },
  80. writeData: function(buffer, success, failure) { // to to be sent to MetaWear
  81. if (!success) {
  82. success = function() {
  83. console.log("success");
  84. resultDiv.innerHTML = resultDiv.innerHTML + "Sent: " + JSON.stringify(new Uint8Array(buffer)) + "<br/>";
  85. resultDiv.scrollTop = resultDiv.scrollHeight;
  86. };
  87. }
  88. if (!failure) {
  89. failure = app.onError;
  90. }
  91. ble.writeCommand(app.deviceId, metawear.serviceUUID, metawear.txCharacteristic, buffer, success, failure);
  92. },
  93. subscribeForIncomingData: function() {
  94. ble.startNotification(app.deviceId, metawear.serviceUUID, metawear.rxCharacteristic, app.onData, app.onError);
  95. },
  96. enableButtonFeedback: function(success, failure) {
  97. var data = new Uint8Array(6);
  98. data[0] = 0x01; // mechanical switch
  99. data[1] = 0x01; // switch state ops code
  100. data[2] = 0x01; // enable
  101. app.writeData(data.buffer, success, failure);
  102. },
  103. onMotorButton: function(event) {
  104. var pulseWidth = pulseWidthInput.value;
  105. var data = new Uint8Array(6);
  106. data[0] = 0x07; // module
  107. data[1] = 0x01; // pulse ops code
  108. data[2] = 0x80; // Motor
  109. data[3] = pulseWidth & 0xFF; // Pulse Width
  110. data[4] = pulseWidth >> 8; // Pulse Width
  111. data[5] = 0x00; // Some magic bullshit
  112. app.writeData(data.buffer);
  113. },
  114. onBuzzerButton: function(event) {
  115. var pulseWidth = pulseWidthInput.value;
  116. var data = new Uint8Array(6);
  117. data[0] = 0x07; // module
  118. data[1] = 0x01; // pulse ops code
  119. data[2] = 0xF8; // Buzzer
  120. data[3] = pulseWidth & 0xFF; // Pulse Width
  121. data[4] = pulseWidth >> 8; // Pulse Width
  122. data[5] = 0x01; // Some magic?
  123. app.writeData(data.buffer);
  124. },
  125. disconnect: function(event) {
  126. ble.disconnect(app.deviceId, app.showMainPage, app.onError);
  127. app.deviceId = "";
  128. },
  129. showMainPage: function() {
  130. mainPage.hidden = false;
  131. detailPage.hidden = true;
  132. },
  133. showDetailPage: function() {
  134. mainPage.hidden = true;
  135. detailPage.hidden = false;
  136. resultDiv.innerHTML = "<i>Press the button on the MetaWear</i><br/>";
  137. },
  138. onError: function(reason) {
  139. alert("ERROR: " + reason); // real apps should use notification.alert
  140. }
  141. };