Home | History | Annotate | Download | only in javascript
      1 /*jslint node:true, vars:true, bitwise:true, unparam:true */
      2 /*jshint unused:true */
      3 
      4 /*
      5  * Author: Jon Trulson <jtrulson (at) ics.com>
      6  * Copyright (c) 2015 Intel Corporation.
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining
      9  * a copy of this software and associated documentation files (the
     10  * "Software"), to deal in the Software without restriction, including
     11  * without limitation the rights to use, copy, modify, merge, publish,
     12  * distribute, sublicense, and/or sell copies of the Software, and to
     13  * permit persons to whom the Software is furnished to do so, subject to
     14  * the following conditions:
     15  *
     16  * The above copyright notice and this permission notice shall be
     17  * included in all copies or substantial portions of the Software.
     18  *
     19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     26  */
     27 
     28 var grovemdObj = require('jsupm_grovemd');
     29 
     30 // This example demonstrates using the GroveMD to drive a stepper motor
     31 
     32 function start()
     33 {
     34     if (motor)
     35     {
     36         // configure it, for this example, we'll assume 200 steps per rev
     37         motor.configStepper(200);
     38         motor.setStepperSteps(100);
     39         // start it going at 10 RPM
     40         motor.enableStepper(grovemdObj.GroveMD.STEP_DIR_CW, 10);
     41     }
     42 }
     43 
     44 function reverse()
     45 {
     46     if (motor)
     47     {
     48 	// now reverse
     49         motor.setStepperSteps(100);
     50         // start it going at 10 RPM
     51         motor.enableStepper(grovemdObj.GroveMD.STEP_DIR_CCW, 10);
     52 	}
     53 }
     54 
     55 function end()
     56 {
     57     if (motor)
     58     {
     59 	motor.disableStepper();
     60     }
     61     exit();
     62 }
     63 
     64 // When exiting: clear memory and print exit message
     65 function exit()
     66 {
     67     if (motor)
     68     {
     69 	motor = null;
     70 	grovemdObj.cleanUp();
     71     }
     72     grovemdObj = null;
     73     console.log("Exiting");
     74     process.exit(0);
     75 }
     76 
     77 
     78 // Instantiate an I2C Grove Motor Driver on I2C bus 0
     79 var motor = new grovemdObj.GroveMD(
     80     grovemdObj.GROVEMD_I2C_BUS,
     81     grovemdObj.GROVEMD_DEFAULT_I2C_ADDR);
     82 
     83 start();
     84 
     85 setTimeout(function()
     86 {
     87     reverse();
     88     setTimeout(end, 3000);
     89 }, 3000);
     90 
     91 
     92 process.on('SIGINT', function()
     93 {
     94     exit();
     95 });
     96