1 /*jslint node:true, vars:true, bitwise:true, unparam:true */ 2 /*jshint unused:true */ 3 /* 4 * Author: Zion Orent <zorent (at) ics.com> 5 * Copyright (c) 2015 Intel Corporation. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining 8 * a copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sublicense, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be 16 * included in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 */ 26 27 var LEDBar = require("jsupm_my9221"); 28 29 // Instantiate a MY9221, we use D2 for the data, and D3 for the 30 // data clock. This was tested with a Grove LED bar. 31 var myLEDBar = new LEDBar.MY9221(2, 3); 32 33 var directionBool = true; 34 35 // A note on timing: 36 // In the C++ example, the system sleeps 10 times for 50 milliseconds 37 // between each LED lighting. After the LED has reached the last light 38 // of the cycle, the system sleeps again for 1 second. 39 // The sleeps are cumulative, so the system has slept for 1.5 seconds 40 // sum total for each cycle. 41 // setInterval and setTimeout make asynchronous function calls; 42 // they aren't cumulative. 43 // In order to approximate the behavior of the C++ example, we need 44 // to call each iteration 1.5 seconds apart instead of 1 second apart. 45 var myInterval = setInterval(function() 46 { 47 // start showing LED strip with just the first one lit 48 show_LED(1, directionBool); 49 }, (1000 + (10*50)) ); 50 51 function show_LED(level, direction) 52 { 53 // If it's less than 10 54 // light up the LED now 55 // call show_LED again in 50 ms 56 if (level <= 10) 57 { 58 myLEDBar.setBarLevel(level, directionBool); 59 setTimeout(show_LED, 50, ++level, directionBool); 60 } 61 // Switch LED lighting directions between lighting cycles 62 else 63 directionBool = !directionBool; 64 } 65 66 // When exiting: clear LED strip lights, clear interval, print exit message 67 process.on('SIGINT', function() 68 { 69 myLEDBar.setBarLevel(0, true); 70 clearInterval(myInterval); 71 console.log("Exiting..."); 72 process.exit(0); 73 }); 74