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