暫無描述

adafruitms1438.js 2.6KB

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