Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. var UI = require('ui');
  2. var Vector2 = require('vector2');
  3.  
  4. //Constants
  5. var defaultCurrentSpeed = 5;
  6. var defaultBufferSpeed = 1;
  7.  
  8. var main = new UI.Card({
  9. title: 'Speed Check!',
  10. body: 'Say Good-Bye to speeding tickets. Press Select',
  11. });
  12.  
  13. main.show();
  14. //console.log("Before Select");
  15. main.on('click', 'select', function(e) {
  16. //console.log("After Select");
  17. var selectWindow = new UI.Window({
  18. fullscreen: true,
  19. });
  20.  
  21. var instructions = new UI.Text({
  22. position: new Vector2(0, 20),
  23. size: new Vector2(144, 30),
  24. font: 'gothic-24-bold',
  25. text: 'Scroll up or down to set speed limit',
  26. textAlign: 'center'
  27. });
  28.  
  29.  
  30. var speedValue = new UI.Text({
  31. position: new Vector2(50, 100),
  32. size: new Vector2(100, 100),
  33. font: 'gothic-24-bold',
  34. text: defaultCurrentSpeed,
  35. textAlign: 'center'
  36. });
  37.  
  38. selectWindow.add(instructions);
  39. selectWindow.add(speedValue);
  40. selectWindow.show();
  41.  
  42. selectWindow.on('click', 'up', function(e) {
  43. speedValue.text(++defaultCurrentSpeed);
  44. selectWindow.show();
  45. });
  46.  
  47. selectWindow.on('click', 'down', function(e) {
  48. //add functionality to prevent negative speed limits
  49. speedValue.text(--defaultCurrentSpeed);
  50. selectWindow.show();
  51. });
  52.  
  53. selectWindow.on('click','select',function(e){
  54. //console.log("Select Test Syntax");
  55. var bufferWindow = new UI.Window({
  56. fullscreen : true,
  57. });
  58. var instructions_2 = new UI.Text({
  59. position: new Vector2(30, 15),
  60. size: new Vector2(100, 100),
  61. font: 'gothic-24-bold',
  62. text: 'select your buffer speed',
  63. textAlign: 'center'
  64. });
  65.  
  66. var bufferSpeed = new UI.Text({
  67. position: new Vector2(50, 100),
  68. size: new Vector2(100, 100),
  69. font: 'gothic-24-bold',
  70. text: defaultBufferSpeed,
  71. textAlign: 'center'
  72. });
  73.  
  74. bufferWindow.add(bufferSpeed);
  75. bufferWindow.add(instructions_2);
  76. bufferWindow.show();
  77.  
  78. bufferWindow.on('click', 'up', function(e) {
  79. bufferSpeed.text(++defaultBufferSpeed);
  80. bufferWindow.show();
  81. });
  82.  
  83. bufferWindow.on('click', 'down', function(e) {
  84. if (defaultBufferSpeed > 0) {
  85. bufferSpeed.text(--defaultBufferSpeed);
  86. bufferWindow.show();
  87. }
  88. });
  89. //console.log("result pretest");
  90. bufferWindow.on('click', 'select', function(e) {
  91. var resultWindow = new UI.Window({
  92. fullscreen: true,
  93. });
  94. var resultText = new UI.Text({
  95. position: new Vector2(0, 15),
  96. size: new Vector2(150, 150),
  97. font: 'gothic-24-bold',
  98. text: 'Speedr will warn you when you exceed ' + (defaultBufferSpeed + defaultCurrentSpeed) + ' mph',
  99. textAlign: 'center'
  100. });
  101.  
  102. resultWindow.add(resultText);
  103. resultWindow.show();
  104. var currentLat;
  105. var currentLong;
  106. var prevLat;
  107. var prevLong;
  108. var locationOptions = {
  109. enableHighAccuracy: true,
  110. maximumAge: 10000,
  111. timeout: 1000
  112. };
  113. function updateLocation() {
  114. navigator.geolocation.getCurrentPosition(locationSuccess, locationError, locationOptions);
  115. }
  116.  
  117. function locationSuccess(pos) {
  118. currentLat = pos.coords.latitude;
  119. //console.log(currentLat);
  120. currentLong = pos.coords.longitude;
  121. //console.log(currentLong);
  122. }
  123.  
  124. function locationError(err) {
  125. console.log('location error (' + err.code + '): ' + err.message);
  126. }
  127.  
  128. function distance(lat1, lon1, lat2, lon2) {
  129. var radlat1 = Math.PI * lat1/180;
  130. var radlat2 = Math.PI * lat2/180;
  131. var theta = lon1-lon2;
  132. var radtheta = Math.PI * theta/180;
  133. var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
  134. dist = Math.acos(dist);
  135. dist = dist * 180/Math.PI;
  136. dist = dist * 60 * 1.1515;
  137. //if (unit=="K") { dist = dist * 1.609344; }
  138. //if (unit=="N") { dist = dist * 0.8684; }
  139. //console.log(dist);
  140. return dist;
  141. }
  142.  
  143. /*updateLocation();
  144. //var currentTime = get the current time
  145. prevLat = currentLat;
  146. prevLong = currentLong;
  147. //resultWindow.on('click', 'down', function(e){
  148. //break;
  149. //});
  150. for(var i = 0; i < 3; i++){
  151. speedCheck();
  152. }
  153. function speedCheck() {
  154. updateLocation();
  155. var dist = distance(currentLat, currentLong, prevLat, prevLong);
  156. var mph = dist * 3600; // divide by time difference;
  157. if(mph > (defaultCurrentSpeed + defaultBufferSpeed)) {
  158. var vibe = require('ul/vibe');
  159. vibe.vibrate('short');
  160. }
  161. prevLat = currentLat;
  162. prevLong = currentLong;
  163. //console.log("WhileLat" + currentLat);
  164. //console.log("WhileLong" + currentLong);
  165.  
  166. }*/
  167. });
  168. });
  169. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement