Moisture: Add C Src and Example

Changed from GroveMoisture.

Signed-off-by: Abhishek Malik <abhishek.malik@intel.com>
This commit is contained in:
Abhishek Malik 2016-09-12 15:25:45 -07:00 committed by Noel Eck
parent 0f7bb5573c
commit 1caf805d2b
19 changed files with 112 additions and 85 deletions

View File

@ -142,7 +142,7 @@ add_example (adc121c021)
add_example (ds1307) add_example (ds1307)
add_example (a110x) add_example (a110x)
add_example (gp2y0a) add_example (gp2y0a)
add_example (grovemoisture) add_example (moisture)
add_example (groveehr) add_example (groveehr)
add_example (ta12200) add_example (ta12200)
add_example (grovelinefinder) add_example (grovelinefinder)

View File

@ -25,7 +25,7 @@
#include <unistd.h> #include <unistd.h>
#include <iostream> #include <iostream>
#include <signal.h> #include <signal.h>
#include "grovemoisture.hpp" #include "moisture.hpp"
using namespace std; using namespace std;
@ -43,8 +43,8 @@ int main ()
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
// Instantiate a Grove Moisture sensor on analog pin A0 // Instantiate a Moisture sensor on analog pin A0
upm::GroveMoisture* moisture = new upm::GroveMoisture(0); upm::Moisture* moisture = new upm::Moisture(0);
// Values (approximate): // Values (approximate):
// 0-300, sensor in air or dry soil // 0-300, sensor in air or dry soil

View File

@ -114,6 +114,7 @@ add_example (ttp223)
add_example (loudness) add_example (loudness)
add_example (tsl2561) add_example (tsl2561)
add_example (collision) add_example (collision)
add_example (moisture)
# Custom examples # Custom examples
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)

25
examples/c/moisture.c Normal file
View File

@ -0,0 +1,25 @@
//Modified: Abhishek Malik <abhishek.malik@intel.com>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "moisture.h"
int main()
{
moisture_context dev = moisture_init(14);
int val;
while(1){
if(moisture_get_moisture(dev, &val) != UPM_SUCCESS){
printf("Failed to get any values from the sensor\n");
}
printf("Moisture Value: %d\n", val);
upm_delay(1);
}
moisture_close(dev);
return 0;
}

View File

@ -41,7 +41,7 @@ add_example(GroveLEDSample grove)
add_example(LightSample light) add_example(LightSample light)
add_example(GroveLineFinderSample grovelinefinder) add_example(GroveLineFinderSample grovelinefinder)
add_example(GroveMDSample grovemd) add_example(GroveMDSample grovemd)
add_example(GroveMoistureSample grovemoisture) add_example(MoistureSample moisture)
add_example(GroveMQ3 gas) add_example(GroveMQ3 gas)
add_example(GroveMQ9 gas) add_example(GroveMQ9 gas)
add_example(O2Example o2) add_example(O2Example o2)

View File

@ -22,10 +22,10 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
public class GroveMoistureSample { public class MoistureSample {
public static void main(String args[]) throws InterruptedException { public static void main(String args[]) throws InterruptedException {
// ! [Interesting] // ! [Interesting]
upm_grovemoisture.GroveMoisture gm = new upm_grovemoisture.GroveMoisture(1); upm_moisture.Moisture gm = new upm_moisture.Moisture(1);
while (true) { while (true) {
int moisture_val = gm.value(); int moisture_val = gm.value();

View File

@ -23,10 +23,10 @@
*/ */
//Load Grove Moisture module //Load Grove Moisture module
var grove_moisture = require('jsupm_grovemoisture'); var moisture = require('jsupm_moisture');
// Instantiate a Grove Moisture sensor on analog pin A0 // Instantiate a Grove Moisture sensor on analog pin A0
var myMoistureObj = new grove_moisture.GroveMoisture(0); var myMoistureObj = new moisture.Moisture(0);
// Values (approximate): // Values (approximate):
// 0-300, sensor in air or dry soil // 0-300, sensor in air or dry soil

View File

@ -22,10 +22,10 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import time, sys, signal, atexit import time, sys, signal, atexit
import pyupm_grovemoisture as upmMoisture import pyupm_moisture as upmMoisture
# Instantiate a Grove Moisture sensor on analog pin A0 # Instantiate a Grove Moisture sensor on analog pin A0
myMoisture = upmMoisture.GroveMoisture(0) myMoisture = upmMoisture.Moisture(0)
## Exit handlers ## ## Exit handlers ##

View File

@ -1,9 +0,0 @@
upm_mixed_module_init (NAME grovemoisture
DESCRIPTION "upm grove moisture module"
C_HDR moisture.h
C_SRC moisture.c
CPP_HDR grovemoisture.hpp
CPP_SRC grovemoisture.cxx
FTI_SRC grovemoisture_fti.c
CPP_WRAPS_C
REQUIRES libmraa)

View File

@ -1,8 +0,0 @@
%module jsupm_grovemoisture
%include "../upm.i"
%{
#include "grovemoisture.hpp"
%}
%include "grovemoisture.hpp"

View File

@ -0,0 +1,9 @@
upm_mixed_module_init (NAME moisture
DESCRIPTION "upm analog moisture module"
C_HDR moisture.h
C_SRC moisture.c
CPP_HDR moisture.hpp
CPP_SRC moisture.cxx
FTI_SRC moisture_fti.c
CPP_WRAPS_C
REQUIRES libmraa)

View File

@ -1,16 +1,16 @@
%module javaupm_grovemoisture %module javaupm_moisture
%include "../upm.i" %include "../upm.i"
%{ %{
#include "grovemoisture.hpp" #include "moisture.hpp"
%} %}
%include "grovemoisture.hpp" %include "moisture.hpp"
%pragma(java) jniclasscode=%{ %pragma(java) jniclasscode=%{
static { static {
try { try {
System.loadLibrary("javaupm_grovemoisture"); System.loadLibrary("javaupm_moisture");
} catch (UnsatisfiedLinkError e) { } catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e); System.err.println("Native code library failed to load. \n" + e);
System.exit(1); System.exit(1);

View File

@ -0,0 +1,8 @@
%module jsupm_moisture
%include "../upm.i"
%{
#include "moisture.hpp"
%}
%include "moisture.hpp"

View File

@ -25,9 +25,9 @@
#include "moisture.h" #include "moisture.h"
grovemoisture_context grovemoisture_init(int pin) { moisture_context moisture_init(int pin) {
grovemoisture_context dev = moisture_context dev =
(grovemoisture_context) malloc(sizeof(struct _grovemoisture_context)); (moisture_context) malloc(sizeof(struct _moisture_context));
if (dev == NULL) { if (dev == NULL) {
printf("Unable to allocate memory for device context\n"); printf("Unable to allocate memory for device context\n");
@ -47,12 +47,12 @@ grovemoisture_context grovemoisture_init(int pin) {
return dev; return dev;
} }
void grovemoisture_close(grovemoisture_context dev) { void moisture_close(moisture_context dev) {
mraa_aio_close(dev->aio); mraa_aio_close(dev->aio);
free(dev); free(dev);
} }
upm_result_t grovemoisture_get_moisture(grovemoisture_context dev, upm_result_t moisture_get_moisture(moisture_context dev,
int* moisture) { int* moisture) {
*moisture = mraa_aio_read(dev->aio); *moisture = mraa_aio_read(dev->aio);

View File

@ -26,23 +26,23 @@
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include "grovemoisture.hpp" #include "moisture.hpp"
using namespace upm; using namespace upm;
GroveMoisture::GroveMoisture(int pin) Moisture::Moisture(int pin)
{ {
if ( !(m_aio = mraa_aio_init(pin)) ) if ( !(m_aio = mraa_aio_init(pin)) )
throw std::invalid_argument(std::string(__FUNCTION__) + throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_aio_init() failed, invalid pin?"); ": mraa_aio_init() failed, invalid pin?");
} }
GroveMoisture::~GroveMoisture() Moisture::~Moisture()
{ {
mraa_aio_close(m_aio); mraa_aio_close(m_aio);
} }
int GroveMoisture::value() int Moisture::value()
{ {
return mraa_aio_read(m_aio); return mraa_aio_read(m_aio);
} }

View File

@ -39,10 +39,10 @@ extern "C" {
/** /**
* device context * device context
*/ */
typedef struct _grovemoisture_context { typedef struct _moisture_context {
mraa_aio_context aio; mraa_aio_context aio;
uint16_t analog_pin; uint16_t analog_pin;
} *grovemoisture_context; } *moisture_context;
/** /**
* Init function * Init function
@ -50,14 +50,14 @@ typedef struct _grovemoisture_context {
* @param pin analog pin number * @param pin analog pin number
* @return void* pointer to the sensor struct * @return void* pointer to the sensor struct
*/ */
grovemoisture_context grovemoisture_init(int pin); moisture_context moisture_init(int pin);
/** /**
* Close function * Close function
* *
* @param dev pointer to the sensor structure * @param dev pointer to the sensor structure
*/ */
void grovemoisture_close(grovemoisture_context dev); void moisture_close(moisture_context dev);
/** /**
* Get Moisture function. * Get Moisture function.
@ -66,7 +66,7 @@ void grovemoisture_close(grovemoisture_context dev);
* @param moisture pointer that will be used to store the * @param moisture pointer that will be used to store the
* output value from the sensor * output value from the sensor
*/ */
upm_result_t grovemoisture_get_moisture(grovemoisture_context dev, upm_result_t moisture_get_moisture(moisture_context dev,
int* moisture); int* moisture);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -28,23 +28,24 @@
namespace upm { namespace upm {
/** /**
* @brief Grove Moisture Sensor library * @brief Moisture Sensor library
* @defgroup grovemoisture libupm-grovemoisture * @defgroup grovemoisture libupm-grovemoisture
* @ingroup seeed analog liquid eak hak * @ingroup seeed analog liquid eak hak
*/ */
/** /**
* @library grovemoisture * @library moisture
* @sensor grovemoisture * @sensor moisture
* @comname Grove Moisture Sensor * @comname Moisture Sensor
* @altname Grove Moisture Sensor
* @type liquid * @type liquid
* @man seeed * @man seeed
* @con analog * @con analog
* @kit eak hak * @kit eak hak
* *
* @brief API for the Grove Moisture Sensor * @brief API for the Moisture Sensor
* *
* UPM module for the Grove Moisture Sensor. * UPM module for the Moisture Sensor.
* This sensor can be used to detect the moisture content * This sensor can be used to detect the moisture content
* of soil or whether there is water around the sensor. * of soil or whether there is water around the sensor.
* As the moisture content increases, so does the value that is read. * As the moisture content increases, so does the value that is read.
@ -52,20 +53,20 @@ namespace upm {
* nor to be used outdoors. * nor to be used outdoors.
* *
* @image html grovemoisture.jpg * @image html grovemoisture.jpg
* @snippet grovemoisture.cxx Interesting * @snippet moisture.cxx Interesting
*/ */
class GroveMoisture { class Moisture {
public: public:
/** /**
* Grove analog moisture sensor constructor * Analog moisture sensor constructor
* *
* @param pin Analog pin to use * @param pin Analog pin to use
*/ */
GroveMoisture(int pin); Moisture(int pin);
/** /**
* GroveMoisture destructor * Moisture destructor
*/ */
~GroveMoisture(); ~Moisture();
/** /**
* Gets the moisture value from the sensor * Gets the moisture value from the sensor
* *

View File

@ -30,42 +30,42 @@
* This file implements the Function Table Interface (FTI) for this sensor * This file implements the Function Table Interface (FTI) for this sensor
*/ */
const char upm_grovemoisture_name[] = "Grove Moisture"; const char upm_moisture_name[] = "Moisture";
const char upm_grovemoisture_description[] = "Analog Grove Moisture Sensor"; const char upm_moisture_description[] = "Analog Moisture Sensor";
const upm_protocol_t upm_grovemoisture_protocol[] = {UPM_ANALOG}; const upm_protocol_t upm_moisture_protocol[] = {UPM_ANALOG};
const upm_sensor_t upm_grovemoisture_category[] = {UPM_MOISTURE}; const upm_sensor_t upm_moisture_category[] = {UPM_MOISTURE};
// forward declarations // forward declarations
const void* upm_grovemoisture_get_ft(upm_sensor_t sensor_type); const void* upm_moisture_get_ft(upm_sensor_t sensor_type);
void* upm_grovemoisture_init_name(); void* upm_moisture_init_name();
void upm_grovemoisture_close(void* dev); void upm_moisture_close(void* dev);
upm_result_t upm_grovemoisture_get_moisture(void* dev, int* moisture); upm_result_t upm_moisture_get_moisture(void* dev, int* moisture);
const upm_sensor_descriptor_t upm_grovemoisture_get_descriptor (){ const upm_sensor_descriptor_t upm_moisture_get_descriptor (){
upm_sensor_descriptor_t usd; upm_sensor_descriptor_t usd;
usd.name = upm_grovemoisture_name; usd.name = upm_moisture_name;
usd.description = upm_grovemoisture_description; usd.description = upm_moisture_description;
usd.protocol_size = 1; usd.protocol_size = 1;
usd.protocol = upm_grovemoisture_protocol; usd.protocol = upm_moisture_protocol;
usd.category_size = 1; usd.category_size = 1;
usd.category = upm_grovemoisture_category; usd.category = upm_moisture_category;
return usd; return usd;
} }
static const upm_sensor_ft ft = static const upm_sensor_ft ft =
{ {
.upm_sensor_init_name = &upm_grovemoisture_init_name, .upm_sensor_init_name = &upm_moisture_init_name,
.upm_sensor_close = &upm_grovemoisture_close, .upm_sensor_close = &upm_moisture_close,
.upm_sensor_get_descriptor = &upm_grovemoisture_get_descriptor .upm_sensor_get_descriptor = &upm_moisture_get_descriptor
}; };
static const upm_moisture_ft mft = static const upm_moisture_ft mft =
{ {
.upm_moisture_sensor_get_moisture = &upm_grovemoisture_get_moisture .upm_moisture_sensor_get_moisture = &upm_moisture_get_moisture
}; };
const void* upm_grovemoisture_get_ft(upm_sensor_t sensor_type){ const void* upm_moisture_get_ft(upm_sensor_t sensor_type){
if(sensor_type == UPM_MOISTURE){ if(sensor_type == UPM_MOISTURE){
return &mft; return &mft;
} }
@ -75,14 +75,14 @@ const void* upm_grovemoisture_get_ft(upm_sensor_t sensor_type){
return NULL; return NULL;
} }
void* upm_grovemoisture_init_name(){ void* upm_moisture_init_name(){
return NULL; return NULL;
} }
void upm_grovemoisture_close(void* dev){ void upm_moisture_close(void* dev){
grovemoisture_close((grovemoisture_context)dev); moisture_close((moisture_context)dev);
} }
upm_result_t upm_grovemoisture_get_moisture(void* dev, int* moisture){ upm_result_t upm_moisture_get_moisture(void* dev, int* moisture){
return grovemoisture_get_moisture((grovemoisture_context)dev, moisture); return moisture_get_moisture((moisture_context)dev, moisture);
} }

View File

@ -1,11 +1,11 @@
// Include doxygen-generated documentation // Include doxygen-generated documentation
%include "pyupm_doxy2swig.i" %include "pyupm_doxy2swig.i"
%module pyupm_grovemoisture %module pyupm_moisture
%include "../upm.i" %include "../upm.i"
%feature("autodoc", "3"); %feature("autodoc", "3");
%include "grovemoisture.hpp" %include "moisture.hpp"
%{ %{
#include "grovemoisture.hpp" #include "moisture.hpp"
%} %}