Guest User

Untitled

a guest
Jan 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2.  
  3. @Component({
  4. selector: 'app-morse-light',
  5. templateUrl: './morse-light.component.html',
  6. styleUrls: ['./morse-light.component.css']
  7. })
  8. export class MorseLightComponent implements OnInit {
  9.  
  10. System: any;
  11. message: string;
  12. morseTranslation: any = {
  13. 'A': '.-',
  14. 'B': '-...',
  15. 'C': '-.-.',
  16. 'D': '-..',
  17. 'E': '.',
  18. 'F': '..-.',
  19. 'G': '--.',
  20. 'H': '....',
  21. 'I': '..',
  22. 'J': '.---',
  23. 'K': '-.-',
  24. 'L': '.-..',
  25. 'M': '--',
  26. 'N': '-.',
  27. 'O': '---',
  28. 'P': '.--.',
  29. 'Q': '--.-',
  30. 'R': '.-.',
  31. 'S': '...',
  32. 'T': '-',
  33. 'U': '..-',
  34. 'V': '...-',
  35. 'W': '.--',
  36. 'X': '-..-',
  37. 'Y': '-.--',
  38. 'Z': '--..',
  39. '1': '.----',
  40. '2': '..---',
  41. '3': '...--',
  42. '4': '....-',
  43. '5': '.....',
  44. '6': '-....',
  45. '7': '--...',
  46. '8': '---..',
  47. '9': '----.',
  48. '0': '-----',
  49. ' ': '.'
  50. };
  51. output = '';
  52. outputLetter = '';
  53. outputSignal = '';
  54. showMorse = '';
  55.  
  56. constructor() { }
  57.  
  58. ngOnInit() {
  59. const c: any = document.getElementById('flashlight');
  60. const ctx = c.getContext('2d');
  61. ctx.beginPath();
  62. ctx.arc(c.width / 2, c.height - 50, 50, 0, 2 * Math.PI);
  63. ctx.fillStyle = 'white';
  64. ctx.fill();
  65. ctx.stroke();
  66. }
  67.  
  68. async transmit() {
  69. // time = 1200 / words per minute
  70. // 20 words per minute
  71. // follows a 3 to 1 ratio
  72. // 60 milliseconds for one dot
  73. // 180 milliseconds for a dash
  74. // multiplied by factor of 4 to slow it down here
  75. const dot = 60 * 4;
  76. const dash = 180 * 4;
  77.  
  78. this.showMorse = '';
  79. const messageUpper = this.message.toUpperCase();
  80. for (const char of messageUpper) {
  81. this.showMorse = this.showMorse + '(' + char + ') ';
  82. const morseValue = this.morseTranslation[char];
  83. for (const morse of morseValue) {
  84. this.showMorse = this.showMorse + ' ' + morse;
  85. if (morse === '.') {
  86. // dot
  87. await this.flashlight('yellow', dot);
  88. // show white light to show when flash is finished
  89. await this.flashlight('white', 60);
  90. } else {
  91. // dash at 3 X 60 or 180
  92. await this.flashlight('yellow', dash);
  93. // show white light to show when flash is finished
  94. await this.flashlight('white', 60);
  95. }
  96. }
  97. }
  98. }
  99.  
  100. flashlight(color: String, time: any): Promise<any> {
  101. return new Promise(resolve => {
  102. setTimeout(function() {
  103. // this.drawLight(color);
  104. const c: any = document.getElementById('flashlight');
  105. const ctx = c.getContext('2d');
  106. ctx.beginPath();
  107. ctx.arc(c.width / 2, c.height - 50, 50, 0, 2 * Math.PI);
  108. ctx.fillStyle = color;
  109. ctx.fill();
  110. ctx.stroke();
  111. resolve(true);
  112. }, time);
  113. });
  114. }
  115.  
  116. }
Add Comment
Please, Sign In to add comment