Last active
December 8, 2018 18:00
-
-
Save csprance/dea885add7e20ad825eba57256540658 to your computer and use it in GitHub Desktop.
Morse Code Arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import five, { Button } from 'johnny-five'; | |
| const alpha: { [k: string]: string } = { | |
| '.-': 'A', | |
| '-...': 'B', | |
| '-.-.': 'C', | |
| '-..': 'D', | |
| '.': 'E', | |
| '..-.': 'F', | |
| '--.': 'G', | |
| '....': 'H', | |
| '..': 'I', | |
| '.---': 'J', | |
| '-.-': 'K', | |
| '.-..': 'L', | |
| '--': 'M', | |
| '-.': 'N', | |
| '---': 'O', | |
| '.--.': 'P', | |
| '--.-': 'Q', | |
| '.-.': 'R', | |
| '...': 'S', | |
| '-': 'T', | |
| '..-': 'U', | |
| '...-': 'V', | |
| '.--': 'W', | |
| '-..-': 'X', | |
| '-.--': 'Y', | |
| '--..': 'Z' | |
| }; | |
| const board = new five.Board({ port: 'COM3', debug: true }); | |
| board.on('ready', () => { | |
| // Create button and listen to events | |
| const button: Button = new five.Button(13); | |
| let count: number; | |
| let stringValue: string = ''; | |
| const waitForInput = debounce(() => { | |
| // Output the character | |
| console.log(alpha[stringValue]); | |
| // Reset the value | |
| stringValue = ''; | |
| }, 800); | |
| button.on('down', () => { | |
| // Start counting how long the button is held down | |
| count = new Date().getTime(); | |
| }); | |
| button.on('up', () => { | |
| // Run our debounced function | |
| waitForInput(); | |
| // Finish counting how long the button was held down for | |
| const interval = new Date().getTime() - count; | |
| stringValue = interval > 200 ? stringValue + '-' : stringValue + '.'; | |
| count = 0; | |
| }); | |
| board.repl.inject({ | |
| button | |
| }); | |
| }); | |
| /** | |
| * Returns a function, that, as long as it continues to be invoked, will not | |
| * be triggered. The function will be called after it stops being called for | |
| * N milliseconds. If `immediate` is passed, trigger the function on the | |
| * leading edge, instead of the trailing. The function also has a property 'clear' | |
| * that is a function which will clear the timer to prevent previously scheduled executions. | |
| * | |
| * @source underscore.js | |
| * @see http://unscriptable.com/2009/03/20/debouncing-javascript-methods/ | |
| * @param {Function} func - Function to wrap | |
| * @param {Number} wait - timeout in ms (`100`) | |
| * @param {Boolean} immediate - Whether to execute at the beginning (`false`) | |
| * @api public | |
| */ | |
| export default ( | |
| func: () => any, | |
| wait: number = 100, | |
| immediate: boolean = false | |
| ) => { | |
| let timeout: NodeJS.Timeout | null; | |
| let context: any; | |
| let timestamp: number; | |
| let result: any; | |
| let args: any; | |
| function later() { | |
| const last = Date.now() - timestamp; | |
| if (last < wait && last >= 0) { | |
| timeout = setTimeout(later, wait - last); | |
| } else { | |
| timeout = null; | |
| if (!immediate) { | |
| result = func.apply(context, args); | |
| context = args = null; | |
| } | |
| } | |
| } | |
| const debounced = function() { | |
| // @ts-ignore | |
| context = this; | |
| args = arguments; | |
| timestamp = Date.now(); | |
| const callNow = immediate && !timeout; | |
| if (!timeout) { | |
| timeout = setTimeout(later, wait); | |
| } | |
| if (callNow) { | |
| result = func.apply(context, args); | |
| context = args = null; | |
| } | |
| return result; | |
| }; | |
| debounced.clear = () => { | |
| if (timeout) { | |
| clearTimeout(timeout); | |
| timeout = null; | |
| } | |
| }; | |
| debounced.flush = () => { | |
| if (timeout) { | |
| result = func.apply(context, args); | |
| context = args = null; | |
| clearTimeout(timeout); | |
| timeout = null; | |
| } | |
| }; | |
| return debounced; | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment