1 /*jslint node:true, vars:true, bitwise:true, unparam:true */ 2 /*jshint unused:true */ 3 4 /* 5 * Author: Zion Orent <zorent (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 MOSFETsensor = require("jsupm_cjq4435"); 29 30 var g_addnumBool = true; 31 var g_cycleNum = 0.0; 32 var g_cycleCount = 0; 33 34 // Instantiate a CJQ4435 MOSFET on a PWM capable digital pin D3 35 var myMOSFETsensor = new MOSFETsensor.CJQ4435(3); 36 37 myMOSFETsensor.setPeriodMS(10); 38 myMOSFETsensor.enable(true); 39 40 // A note on timing: 41 // In the C++ example, the system sleeps 11 times for 100 milliseconds 42 // between each duty cycle. After reaching the last point of the cycle, 43 // the system sleeps again for 1 second. 44 // The sleeps are cumulative, so the system has slept for 2.1 seconds 45 // sum total for each cycle. 46 // setInterval and setTimeout make asynchronous function calls; 47 // they aren't cumulative. 48 // In order to approximate the behavior of the C++ example, we need 49 // to call each iteration 2.1 seconds apart instead of 1 second apart. 50 51 var myInterval = setInterval(function() 52 { 53 setDutyCycle(); 54 }, (1000 + (11*100)) ); 55 56 57 // We start with a duty cycle of 0.0 (off) and increment to 1.0 (on) 58 // Then we take it back down, 59 // starting with a duty cycle of 1.0 (on) and decrement to 0.0 (off) 60 function setDutyCycle() 61 { 62 myMOSFETsensor.setDutyCycle(g_cycleNum); 63 if (g_addnumBool) 64 g_cycleNum += 0.1; 65 else 66 g_cycleNum -= 0.1; 67 g_cycleCount++; 68 if (g_cycleCount > 10) 69 { 70 g_addnumBool = !g_addnumBool; 71 g_cycleCount = 0; 72 } 73 else 74 setTimeout(setDutyCycle, 100); 75 } 76 77 78 // When exiting: clear interval and print exit message 79 process.on('SIGINT', function() 80 { 81 clearInterval(myInterval); 82 myMOSFETsensor = null; 83 MOSFETsensor.cleanUp(); 84 MOSFETsensor = null; 85 console.log("Exiting..."); 86 process.exit(0); 87 }); 88