Geen omschrijving

my9221.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*jslint node:true, vars:true, bitwise:true, unparam:true */
  2. /*jshint unused:true */
  3. /*
  4. * Author: Zion Orent <zorent@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. var LEDBar = require("jsupm_my9221");
  27. // Instantiate a MY9221, we use D2 for the data, and D3 for the
  28. // data clock. This was tested with a Grove LED bar.
  29. var myLEDBar = new LEDBar.MY9221(2, 3);
  30. var directionBool = true;
  31. // A note on timing:
  32. // In the C++ example, the system sleeps 10 times for 50 milliseconds
  33. // between each LED lighting. After the LED has reached the last light
  34. // of the cycle, the system sleeps again for 1 second.
  35. // The sleeps are cumulative, so the system has slept for 1.5 seconds
  36. // sum total for each cycle.
  37. // setInterval and setTimeout make asynchronous function calls;
  38. // they aren't cumulative.
  39. // In order to approximate the behavior of the C++ example, we need
  40. // to call each iteration 1.5 seconds apart instead of 1 second apart.
  41. var myInterval = setInterval(function()
  42. {
  43. // start showing LED strip with just the first one lit
  44. show_LED(1, directionBool);
  45. }, (1000 + (10*50)) );
  46. function show_LED(level, direction)
  47. {
  48. // If it's less than 10
  49. // light up the LED now
  50. // call show_LED again in 50 ms
  51. if (level <= 10)
  52. {
  53. myLEDBar.setBarLevel(level, directionBool);
  54. setTimeout(show_LED, 50, ++level, directionBool);
  55. }
  56. // Switch LED lighting directions between lighting cycles
  57. else
  58. directionBool = !directionBool;
  59. }
  60. // When exiting: clear LED strip lights, clear interval, print exit message
  61. process.on('SIGINT', function()
  62. {
  63. myLEDBar.setBarLevel(0, true);
  64. clearInterval(myInterval);
  65. console.log("Exiting...");
  66. process.exit(0);
  67. });