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 myMotorShield_obj = null; 32 if (MotorShield_lib) 33 { 34 MotorShield_lib.cleanUp(); 35 MotorShield_lib = null; 36 } 37 process.exit(0); 38 } 39 40 var MotorShield_lib = require('jsupm_adafruitms1438'); 41 42 43 /* Import header values */ 44 var I2CBus = MotorShield_lib.ADAFRUITMS1438_I2C_BUS; 45 var I2CAddr = MotorShield_lib.ADAFRUITMS1438_DEFAULT_I2C_ADDR; 46 47 var M3motor = MotorShield_lib.AdafruitMS1438.MOTOR_M3; 48 var MotorDirCW = MotorShield_lib.AdafruitMS1438.DIR_CW; 49 var MotorDirCCW = MotorShield_lib.AdafruitMS1438.DIR_CCW; 50 51 52 // Instantiate an Adafruit MS 1438 on I2C bus 0 53 var myMotorShield_obj = new MotorShield_lib.AdafruitMS1438(I2CBus, I2CAddr); 54 55 56 // Setup for use with a DC motor connected to the M3 port 57 58 // set a PWM period of 50Hz 59 myMotorShield_obj.setPWMPeriod(50); 60 61 // disable first, to be safe 62 myMotorShield_obj.disableMotor(M3motor); 63 64 // set speed at 50% 65 myMotorShield_obj.setMotorSpeed(M3motor, 50); 66 myMotorShield_obj.setMotorDirection(M3motor, MotorDirCW); 67 68 process.stdout.write("Spin M3 at half speed for 3 seconds, "); 69 console.log("then reverse for 3 seconds."); 70 myMotorShield_obj.enableMotor(M3motor); 71 72 setTimeout(function() 73 { 74 console.log("Reversing M3"); 75 myMotorShield_obj.setMotorDirection(M3motor, MotorDirCCW); 76 }, 3000); 77 78 79 setTimeout(function() 80 { 81 console.log("Stopping M3"); 82 myMotorShield_obj.disableMotor(M3motor); 83 exit(); 84 }, 6000); 85 86 process.on('SIGINT', function() 87 { 88 exit(); 89 }); 90