diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index d319d83c..2e98276f 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -81,6 +81,7 @@ add_executable (adxl345-example adxl345.cxx) add_executable (rpr220-example rpr220.cxx) add_executable (rpr220-intr-example rpr220-intr.cxx) add_executable (mma7660-example mma7660.cxx) +add_executable (cjq4435-example cjq4435.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -147,6 +148,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/rotaryencoder) include_directories (${PROJECT_SOURCE_DIR}/src/adxl345) include_directories (${PROJECT_SOURCE_DIR}/src/rpr220) include_directories (${PROJECT_SOURCE_DIR}/src/mma7660) +include_directories (${PROJECT_SOURCE_DIR}/src/cjq4435) target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT}) @@ -231,3 +233,4 @@ target_link_libraries (adxl345-example adxl345 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (rpr220-example rpr220 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (rpr220-intr-example rpr220 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (mma7660-example mma7660 ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (cjq4435-example cjq4435 ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/cjq4435.cxx b/examples/cjq4435.cxx new file mode 100644 index 00000000..f9d84d43 --- /dev/null +++ b/examples/cjq4435.cxx @@ -0,0 +1,77 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2015 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include "cjq4435.h" + +using namespace std; + +int shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + + +int main () +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // Instantiate a CJQ4435 MOSFET on a PWM capable digital pin D3 + upm::CJQ4435* mosfet = new upm::CJQ4435(3); + + mosfet->setPeriodMS(10); + mosfet->enable(true); + + while (shouldRun) + { + // start with a duty cycle of 0.0 (off) and increment to 1.0 (on) + for (float i=0.0; i <= 1.0; i+=0.1) + { + mosfet->setDutyCycle(i); + usleep(100000); + } + sleep(1); + // Now take it back down + // start with a duty cycle of 1.0 (on) and decrement to 0.0 (off) + for (float i=1.0; i >= 0.0; i-=0.1) + { + mosfet->setDutyCycle(i); + usleep(100000); + } + sleep(1); + } + +//! [Interesting] + + cout << "Exiting..." << endl; + + delete mosfet; + return 0; +} diff --git a/examples/javascript/cjq4435.js b/examples/javascript/cjq4435.js new file mode 100644 index 00000000..38706964 --- /dev/null +++ b/examples/javascript/cjq4435.js @@ -0,0 +1,85 @@ +/*jslint node:true, vars:true, bitwise:true, unparam:true */ +/*jshint unused:true */ + +/* +* Author: Zion Orent +* Copyright (c) 2015 Intel Corporation. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +var MOSFETsensor = require("jsupm_cjq4435"); + +var g_addnumBool = true; +var g_cycleNum = 0.0; +var g_cycleCount = 0; + +// Instantiate a CJQ4435 MOSFET on a PWM capable digital pin D3 +var myMOSFETsensor = new MOSFETsensor.CJQ4435(3); + +myMOSFETsensor.setPeriodMS(10); +myMOSFETsensor.enable(true); + +// A note on timing: +// In the C++ example, the system sleeps 11 times for 100 milliseconds +// between each duty cycle. After reaching the last point of the cycle, +// the system sleeps again for 1 second. +// The sleeps are cumulative, so the system has slept for 2.1 seconds +// sum total for each cycle. +// setInterval and setTimeout make asynchronous function calls; +// they aren't cumulative. +// In order to approximate the behavior of the C++ example, we need +// to call each iteration 2.1 seconds apart instead of 1 second apart. + +var myInterval = setInterval(function() +{ + setDutyCycle(); +}, (1000 + (11*100)) ); + + +// We start with a duty cycle of 0.0 (off) and increment to 1.0 (on) +// Then we take it back down, +// starting with a duty cycle of 1.0 (on) and decrement to 0.0 (off) +function setDutyCycle() +{ + myMOSFETsensor.setDutyCycle(g_cycleNum); + if (g_addnumBool) + g_cycleNum += 0.1; + else + g_cycleNum -= 0.1; + g_cycleCount++; + if (g_cycleCount > 10) + { + g_addnumBool = !g_addnumBool; + g_cycleCount = 0; + } + else + setTimeout(setDutyCycle, 100); +} + + +// When exiting: clear interval and print exit message +process.on('SIGINT', function() +{ + clearInterval(myInterval); + myMOSFETsensor.off(); + console.log("Exiting..."); + process.exit(0); +}); diff --git a/src/cjq4435/CMakeLists.txt b/src/cjq4435/CMakeLists.txt new file mode 100644 index 00000000..b9c954b6 --- /dev/null +++ b/src/cjq4435/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "cjq4435") +set (libdescription "upm cjq4435 MOSFET module") +set (module_src ${libname}.cxx) +set (module_h ${libname}.h) +upm_module_init() diff --git a/src/cjq4435/cjq4435.cxx b/src/cjq4435/cjq4435.cxx new file mode 100644 index 00000000..3277eab9 --- /dev/null +++ b/src/cjq4435/cjq4435.cxx @@ -0,0 +1,108 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2015 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include + +#include "cjq4435.h" + +using namespace upm; +using namespace std; + +CJQ4435::CJQ4435(int pin) +{ + if ( !(m_pwm = mraa_pwm_init(pin)) ) + { + cerr << __FUNCTION__ << ": mraa_pwm_init() failed" << endl; + return; + } + + m_enabled = false; +} + +CJQ4435::~CJQ4435() +{ + if (m_enabled) + mraa_pwm_enable(m_pwm, 0); + + mraa_pwm_close(m_pwm); +} + +void CJQ4435::setPeriodUS(int us) +{ + if (mraa_pwm_period_us(m_pwm, us) != MRAA_SUCCESS) + cerr << __FUNCTION__ << ": period specified is not supported" + << endl; +} + +void CJQ4435::setPeriodMS(int ms) +{ + if (mraa_pwm_period_ms(m_pwm, ms) != MRAA_SUCCESS) + cerr << __FUNCTION__ << ": period specified is not supported" + << endl; +} + +void CJQ4435::setPeriodSeconds(float seconds) +{ + if (mraa_pwm_period(m_pwm, seconds) != MRAA_SUCCESS) + cerr << __FUNCTION__ << ": period specified is not supported" + << endl; +} + +void CJQ4435::enable(bool enable) +{ + m_enabled = enable; + mraa_pwm_enable(m_pwm, ((enable) ? 1 : 0)); +} + +void CJQ4435::setDutyCycle(float dutyCycle) +{ + if (dutyCycle < 0.0) + dutyCycle = 0.0; + + if (dutyCycle > 1.0) + dutyCycle = 1.0; + + mraa_pwm_write(m_pwm, dutyCycle); +} + +void CJQ4435::on() +{ + // set a 1 second period, with 100% duty cycle + + enable(false); + setPeriodUS(1000); + setDutyCycle(1.0); + enable(true); +} + +void CJQ4435::off() +{ + // set a 1 second period, with 0% duty cycle + + enable(false); + setPeriodUS(1000); + setDutyCycle(0.0); + enable(true); +} + diff --git a/src/cjq4435/cjq4435.h b/src/cjq4435/cjq4435.h new file mode 100644 index 00000000..f5e5f1aa --- /dev/null +++ b/src/cjq4435/cjq4435.h @@ -0,0 +1,112 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2015 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#pragma once + +#include + +namespace upm { + + /** + * @brief C++ API for the CJQ4435 MOSFET + * + * UPM module for the CJQ4435 MOSFET. It was developed using the + * Grove MOSFET module. A MOSFET is like a switch, but it can + * switch much faster than a mechanical relay. Here, we implement + * support via MRAA's PWM (Pulse Width Modulation) functions. + * + * Please note, that the available periods will vary depending on + * the capabilities of your device. + * + * @ingroup gpio pwm + * @snippet cjq4435.cxx Interesting + */ + class CJQ4435 { + public: + /** + * CJQ4435 constructor + * + * @param pin digital pin to use - this pin must be PWM capable + */ + CJQ4435(int pin); + + /** + * CJQ4435 Destructor + */ + ~CJQ4435(); + + /** + * set the period in microseconds + * + * @param us period in microseconds + */ + void setPeriodUS(int us); + + /** + * set the period in milliseconds + * + * @param ms period in milliseconds + */ + void setPeriodMS(int ms); + + /** + * set the period in seconds + * + * @param seconds period in seconds + */ + void setPeriodSeconds(float seconds); + + /** + * enable output + * + * @param enable enable PWM output if true, disable if false + */ + void enable(bool enable); + + /** + * set the duty cycle. The duty cycle is a floating point number + * between 0.0 (always off) to 1.0 (always on). It represents how + * much time as a percentage, per period, that the output will be + * driven high. + * + * @param dutyCycle the duty cycle to use + */ + void setDutyCycle(float dutyCycle); + + /** + * a shortcut for turning the output to continuous on (high) + */ + void on(); + + /** + * a shortcut for turning the output to continuous off (low) + */ + void off(); + + private: + bool m_enabled; + mraa_pwm_context m_pwm; + }; +} + + diff --git a/src/cjq4435/jsupm_cjq4435.i b/src/cjq4435/jsupm_cjq4435.i new file mode 100644 index 00000000..3fe19eec --- /dev/null +++ b/src/cjq4435/jsupm_cjq4435.i @@ -0,0 +1,8 @@ +%module jsupm_cjq4435 +%include "../upm.i" + +%{ + #include "cjq4435.h" +%} + +%include "cjq4435.h" diff --git a/src/cjq4435/pyupm_cjq4435.i b/src/cjq4435/pyupm_cjq4435.i new file mode 100644 index 00000000..4891afc0 --- /dev/null +++ b/src/cjq4435/pyupm_cjq4435.i @@ -0,0 +1,9 @@ +%module pyupm_cjq4435 +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "cjq4435.h" +%{ + #include "cjq4435.h" +%}