mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
groveelectromagnet: Initial implementation
This module implements support for the Grove Electromagnet. Signed-off-by: Zion Orent <zorent@ics.com> Signed-off-by: Jon Trulson <jtrulson@ics.com> Signed-off-by: John Van Drasek <john.r.van.drasek@intel.com>
This commit is contained in:
parent
ae10518a0f
commit
ce6cd0a19f
@ -89,6 +89,7 @@ add_executable (hmtrp-example hmtrp.cxx)
|
||||
add_executable (nunchuck-example nunchuck.cxx)
|
||||
add_executable (otp538u-example otp538u.cxx)
|
||||
add_executable (grovecollision-example grovecollision.cxx)
|
||||
add_executable (groveelectromagnet-example groveelectromagnet.cxx)
|
||||
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
|
||||
@ -160,6 +161,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/hmtrp)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/nunchuck)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/otp538u)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/grovecollision)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/groveelectromagnet)
|
||||
|
||||
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
|
||||
@ -250,3 +252,4 @@ target_link_libraries (hmtrp-example hmtrp ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (nunchuck-example nunchuck ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (otp538u-example otp538u ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (grovecollision-example grovecollision ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (groveelectromagnet-example groveelectromagnet ${CMAKE_THREAD_LIBS_INIT})
|
||||
|
79
examples/c++/groveelectromagnet.cxx
Normal file
79
examples/c++/groveelectromagnet.cxx
Normal file
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Author: Zion Orent <zorent@ics.com>
|
||||
* 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 <iostream>
|
||||
#include <time.h>
|
||||
#include <signal.h>
|
||||
#include "groveelectromagnet.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int shouldRun = true;
|
||||
|
||||
void sig_handler(int signo)
|
||||
{
|
||||
if (signo == SIGINT)
|
||||
shouldRun = false;
|
||||
}
|
||||
|
||||
float get_time()
|
||||
{
|
||||
return ((float)(clock()))/CLOCKS_PER_SEC;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
//! [Interesting]
|
||||
// The was tested with the Grove Electromagnetic Module
|
||||
// Instantiate a Grove Electromagnet on digital pin D2
|
||||
upm::GroveElectromagnet* magnet = new upm::GroveElectromagnet(2);
|
||||
cout << "Starting up magnet...." << endl;
|
||||
magnet->off();
|
||||
|
||||
bool magnetState = false;
|
||||
float time_passed = get_time();
|
||||
|
||||
// Turn magnet on and off every 5 seconds
|
||||
while (shouldRun)
|
||||
{
|
||||
if ((get_time() - time_passed) > 5.0)
|
||||
{
|
||||
magnetState = !magnetState;
|
||||
if (magnetState)
|
||||
magnet->on();
|
||||
else
|
||||
magnet->off();
|
||||
cout << "Turning magnet " << ((magnetState) ? "on" : "off") << endl;
|
||||
time_passed = get_time();
|
||||
}
|
||||
}
|
||||
|
||||
//! [Interesting]
|
||||
magnet->off();
|
||||
cout << "Exiting" << endl;
|
||||
|
||||
delete magnet;
|
||||
return 0;
|
||||
}
|
57
examples/javascript/groveelectromagnet.js
Normal file
57
examples/javascript/groveelectromagnet.js
Normal file
@ -0,0 +1,57 @@
|
||||
/*jslint node:true, vars:true, bitwise:true, unparam:true */
|
||||
/*jshint unused:true */
|
||||
/*
|
||||
* Author: Zion Orent <zorent@ics.com>
|
||||
* 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 electromagnet_lib = require("jsupm_groveelectromagnet");
|
||||
// The was tested with the Grove Electromagnetic Module
|
||||
// Instantiate a Grove Electromagnet on digital pin D2
|
||||
var electromagnet_obj = new electromagnet_lib.GroveElectromagnet(2);
|
||||
console.log("Starting up magnet....");
|
||||
electromagnet_obj.off();
|
||||
|
||||
var magnetState = false;
|
||||
|
||||
// Turn magnet on and off every 5 seconds
|
||||
var myInterval = setInterval(function()
|
||||
{
|
||||
magnetState = !magnetState;
|
||||
if (magnetState)
|
||||
electromagnet_obj.on();
|
||||
else
|
||||
electromagnet_obj.off();
|
||||
console.log("Turning magnet " + ((magnetState) ? "on" : "off"));
|
||||
}, 5000);
|
||||
|
||||
// When exiting: clear interval, turn off magnet, run memory cleanup, and print message
|
||||
process.on('SIGINT', function()
|
||||
{
|
||||
clearInterval(myInterval);
|
||||
electromagnet_obj.off();
|
||||
electromagnet_obj = null;
|
||||
electromagnet_lib.cleanUp();
|
||||
electromagnet_lib = null;
|
||||
console.log("Exiting...");
|
||||
process.exit(0);
|
||||
});
|
5
src/groveelectromagnet/CMakeLists.txt
Normal file
5
src/groveelectromagnet/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
set (libname "groveelectromagnet")
|
||||
set (libdescription "upm groveelectromagnet sensor module")
|
||||
set (module_src ${libname}.cxx)
|
||||
set (module_h ${libname}.h)
|
||||
upm_module_init("-lrt")
|
54
src/groveelectromagnet/groveelectromagnet.cxx
Normal file
54
src/groveelectromagnet/groveelectromagnet.cxx
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Author: Zion Orent <sorent@ics.com>
|
||||
* 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 "groveelectromagnet.h"
|
||||
|
||||
using namespace upm;
|
||||
|
||||
GroveElectromagnet::GroveElectromagnet(int pin)
|
||||
{
|
||||
m_gpio = mraa_gpio_init(pin);
|
||||
mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT);
|
||||
}
|
||||
|
||||
GroveElectromagnet::~GroveElectromagnet()
|
||||
{
|
||||
mraa_gpio_close(m_gpio);
|
||||
}
|
||||
|
||||
void GroveElectromagnet::on()
|
||||
{
|
||||
mraa_result_t error = MRAA_SUCCESS;
|
||||
error = mraa_gpio_write (m_gpio, HIGH);
|
||||
if (error != MRAA_SUCCESS)
|
||||
mraa_result_print(error);
|
||||
}
|
||||
|
||||
void GroveElectromagnet::off()
|
||||
{
|
||||
mraa_result_t error = MRAA_SUCCESS;
|
||||
error = mraa_gpio_write (m_gpio, LOW);
|
||||
if (error != MRAA_SUCCESS)
|
||||
mraa_result_print(error);
|
||||
}
|
68
src/groveelectromagnet/groveelectromagnet.h
Normal file
68
src/groveelectromagnet/groveelectromagnet.h
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Author: Zion Orent <sorent@ics.com>
|
||||
* 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 <mraa/gpio.h>
|
||||
|
||||
#define HIGH 1
|
||||
#define LOW 0
|
||||
|
||||
namespace upm {
|
||||
/**
|
||||
* @brief UPM module for the Grove Electromagnet
|
||||
* @defgroup groveelectromagnet libupm-groveelectromagnet
|
||||
*/
|
||||
/**
|
||||
* @brief C++ API for the Grove Electromagnet
|
||||
*
|
||||
* The Grove Electromagnet can hold up to 1 KG
|
||||
*
|
||||
* @ingroup gpio groveelectromagnet
|
||||
* @snippet groveelectromagnet.cxx Interesting
|
||||
*/
|
||||
class GroveElectromagnet {
|
||||
public:
|
||||
/**
|
||||
* Grove Electromagnet Constructor
|
||||
*
|
||||
* @param pin digital pin to use
|
||||
*/
|
||||
GroveElectromagnet(int pin);
|
||||
/**
|
||||
* Grove Electromagnet Destructor
|
||||
*/
|
||||
~GroveElectromagnet();
|
||||
/**
|
||||
* Turn magnet on
|
||||
*/
|
||||
void on();
|
||||
/**
|
||||
* Turn magnet off
|
||||
*/
|
||||
void off();
|
||||
|
||||
private:
|
||||
mraa_gpio_context m_gpio;
|
||||
};
|
||||
}
|
8
src/groveelectromagnet/jsupm_groveelectromagnet.i
Normal file
8
src/groveelectromagnet/jsupm_groveelectromagnet.i
Normal file
@ -0,0 +1,8 @@
|
||||
%module jsupm_groveelectromagnet
|
||||
%include "../upm.i"
|
||||
|
||||
%{
|
||||
#include "groveelectromagnet.h"
|
||||
%}
|
||||
|
||||
%include "groveelectromagnet.h"
|
9
src/groveelectromagnet/pyupm_groveelectromagnet.i
Normal file
9
src/groveelectromagnet/pyupm_groveelectromagnet.i
Normal file
@ -0,0 +1,9 @@
|
||||
%module pyupm_groveelectromagnet
|
||||
%include "../upm.i"
|
||||
|
||||
%feature("autodoc", "3");
|
||||
|
||||
%include "groveelectromagnet.h"
|
||||
%{
|
||||
#include "groveelectromagnet.h"
|
||||
%}
|
Loading…
x
Reference in New Issue
Block a user