No Description

pca9685.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. function exit()
  27. {
  28. console.log("Exiting");
  29. if (myLEDController_obj)
  30. {
  31. // clear the bits we set earlier
  32. myLEDController_obj.ledFullOff(3, false);
  33. myLEDController_obj.ledFullOn(4, false);
  34. }
  35. myLEDController_obj = null;
  36. if (LEDController_lib)
  37. {
  38. LEDController_lib.cleanUp();
  39. LEDController_lib = null;
  40. }
  41. process.exit(0);
  42. }
  43. // The pca9685 is an led controller.
  44. // It's being used in this case to drive motors.
  45. var LEDController_lib = require('jsupm_pca9685');
  46. var I2CBus = LEDController_lib.PCA9685_I2C_BUS;
  47. var I2CAddr = LEDController_lib.PCA9685_DEFAULT_I2C_ADDR;
  48. // Instantiate an PCA9685 on I2C bus 0
  49. var myLEDController_obj = new LEDController_lib.PCA9685(I2CBus, I2CAddr);
  50. // put device to sleep
  51. myLEDController_obj.setModeSleep(true);
  52. // setup a period of 50Hz
  53. myLEDController_obj.setPrescaleFromHz(50);
  54. // wake device up
  55. myLEDController_obj.setModeSleep(false);
  56. // Setup a 50% duty cycle -- on time at 0, off time at 2048 (4096 / 2)
  57. // Set for all channels
  58. var LEDNum = LEDController_lib.PCA9685_ALL_LED;
  59. myLEDController_obj.ledOnTime(LEDNum, 0);
  60. myLEDController_obj.ledOffTime(LEDNum, 2048);
  61. // but, turn channel 3 full off and channel 4 full on
  62. console.log("Turning channel 3 off, and channel 4 on.");
  63. console.log("All other channels will be PWM'd at a 50% duty cycle.");
  64. myLEDController_obj.ledFullOff(3, true);
  65. myLEDController_obj.ledFullOn(4, true);
  66. // now, just sleep for 5 seconds, reset channels 3 and 4, and exit.
  67. console.log("Sleeping for 5 seconds...");
  68. setTimeout(exit, 5000);
  69. process.on('SIGINT', function()
  70. {
  71. exit();
  72. });