From 1caf805d2b93fadc1eac33c2c38736f48ea8d76f Mon Sep 17 00:00:00 2001 From: Abhishek Malik Date: Mon, 12 Sep 2016 15:25:45 -0700 Subject: [PATCH] Moisture: Add C Src and Example Changed from GroveMoisture. Signed-off-by: Abhishek Malik --- examples/c++/CMakeLists.txt | 2 +- .../c++/{grovemoisture.cxx => moisture.cxx} | 6 +-- examples/c/CMakeLists.txt | 1 + examples/c/moisture.c | 25 ++++++++++ examples/java/CMakeLists.txt | 2 +- ...oistureSample.java => MoistureSample.java} | 4 +- .../{grovemoisture.js => moisture.js} | 4 +- .../python/{grovemoisture.py => moisture.py} | 4 +- src/grovemoisture/CMakeLists.txt | 9 ---- src/grovemoisture/jsupm_grovemoisture.i | 8 ---- src/moisture/CMakeLists.txt | 9 ++++ .../javaupm_moisture.i} | 8 ++-- src/moisture/jsupm_moisture.i | 8 ++++ src/{grovemoisture => moisture}/moisture.c | 10 ++-- .../moisture.cxx} | 8 ++-- src/{grovemoisture => moisture}/moisture.h | 10 ++-- .../moisture.hpp} | 25 +++++----- .../moisture_fti.c} | 46 +++++++++---------- .../pyupm_moisture.i} | 8 ++-- 19 files changed, 112 insertions(+), 85 deletions(-) rename examples/c++/{grovemoisture.cxx => moisture.cxx} (93%) create mode 100644 examples/c/moisture.c rename examples/java/{GroveMoistureSample.java => MoistureSample.java} (93%) rename examples/javascript/{grovemoisture.js => moisture.js} (94%) rename examples/python/{grovemoisture.py => moisture.py} (96%) delete mode 100644 src/grovemoisture/CMakeLists.txt delete mode 100644 src/grovemoisture/jsupm_grovemoisture.i create mode 100644 src/moisture/CMakeLists.txt rename src/{grovemoisture/javaupm_grovemoisture.i => moisture/javaupm_moisture.i} (62%) create mode 100644 src/moisture/jsupm_moisture.i rename src/{grovemoisture => moisture}/moisture.c (86%) rename src/{grovemoisture/grovemoisture.cxx => moisture/moisture.cxx} (92%) rename src/{grovemoisture => moisture}/moisture.h (88%) rename src/{grovemoisture/grovemoisture.hpp => moisture/moisture.hpp} (82%) rename src/{grovemoisture/grovemoisture_fti.c => moisture/moisture_fti.c} (56%) rename src/{grovemoisture/pyupm_grovemoisture.i => moisture/pyupm_moisture.i} (56%) diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index 179755bb..386e5258 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -142,7 +142,7 @@ add_example (adc121c021) add_example (ds1307) add_example (a110x) add_example (gp2y0a) -add_example (grovemoisture) +add_example (moisture) add_example (groveehr) add_example (ta12200) add_example (grovelinefinder) diff --git a/examples/c++/grovemoisture.cxx b/examples/c++/moisture.cxx similarity index 93% rename from examples/c++/grovemoisture.cxx rename to examples/c++/moisture.cxx index f63a088d..03dff8dd 100644 --- a/examples/c++/grovemoisture.cxx +++ b/examples/c++/moisture.cxx @@ -25,7 +25,7 @@ #include #include #include -#include "grovemoisture.hpp" +#include "moisture.hpp" using namespace std; @@ -43,8 +43,8 @@ int main () signal(SIGINT, sig_handler); //! [Interesting] - // Instantiate a Grove Moisture sensor on analog pin A0 - upm::GroveMoisture* moisture = new upm::GroveMoisture(0); + // Instantiate a Moisture sensor on analog pin A0 + upm::Moisture* moisture = new upm::Moisture(0); // Values (approximate): // 0-300, sensor in air or dry soil diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 5771211b..ffabafc5 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -114,6 +114,7 @@ add_example (ttp223) add_example (loudness) add_example (tsl2561) add_example (collision) +add_example (moisture) # Custom examples add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) diff --git a/examples/c/moisture.c b/examples/c/moisture.c new file mode 100644 index 00000000..04a4e096 --- /dev/null +++ b/examples/c/moisture.c @@ -0,0 +1,25 @@ +//Modified: Abhishek Malik + +#include +#include +#include +#include +#include +#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; +} + diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt index 8632b4f3..e5bfeb5d 100644 --- a/examples/java/CMakeLists.txt +++ b/examples/java/CMakeLists.txt @@ -41,7 +41,7 @@ add_example(GroveLEDSample grove) add_example(LightSample light) add_example(GroveLineFinderSample grovelinefinder) add_example(GroveMDSample grovemd) -add_example(GroveMoistureSample grovemoisture) +add_example(MoistureSample moisture) add_example(GroveMQ3 gas) add_example(GroveMQ9 gas) add_example(O2Example o2) diff --git a/examples/java/GroveMoistureSample.java b/examples/java/MoistureSample.java similarity index 93% rename from examples/java/GroveMoistureSample.java rename to examples/java/MoistureSample.java index 8fb32d6e..5695ed70 100644 --- a/examples/java/GroveMoistureSample.java +++ b/examples/java/MoistureSample.java @@ -22,10 +22,10 @@ * 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 { // ! [Interesting] - upm_grovemoisture.GroveMoisture gm = new upm_grovemoisture.GroveMoisture(1); + upm_moisture.Moisture gm = new upm_moisture.Moisture(1); while (true) { int moisture_val = gm.value(); diff --git a/examples/javascript/grovemoisture.js b/examples/javascript/moisture.js similarity index 94% rename from examples/javascript/grovemoisture.js rename to examples/javascript/moisture.js index 76fa9ae9..28c9c221 100644 --- a/examples/javascript/grovemoisture.js +++ b/examples/javascript/moisture.js @@ -23,10 +23,10 @@ */ //Load Grove Moisture module -var grove_moisture = require('jsupm_grovemoisture'); +var moisture = require('jsupm_moisture'); // Instantiate a Grove Moisture sensor on analog pin A0 -var myMoistureObj = new grove_moisture.GroveMoisture(0); +var myMoistureObj = new moisture.Moisture(0); // Values (approximate): // 0-300, sensor in air or dry soil diff --git a/examples/python/grovemoisture.py b/examples/python/moisture.py similarity index 96% rename from examples/python/grovemoisture.py rename to examples/python/moisture.py index 78d3fe47..1723d3e8 100644 --- a/examples/python/grovemoisture.py +++ b/examples/python/moisture.py @@ -22,10 +22,10 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import time, sys, signal, atexit -import pyupm_grovemoisture as upmMoisture +import pyupm_moisture as upmMoisture # Instantiate a Grove Moisture sensor on analog pin A0 -myMoisture = upmMoisture.GroveMoisture(0) +myMoisture = upmMoisture.Moisture(0) ## Exit handlers ## diff --git a/src/grovemoisture/CMakeLists.txt b/src/grovemoisture/CMakeLists.txt deleted file mode 100644 index 89faa382..00000000 --- a/src/grovemoisture/CMakeLists.txt +++ /dev/null @@ -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) diff --git a/src/grovemoisture/jsupm_grovemoisture.i b/src/grovemoisture/jsupm_grovemoisture.i deleted file mode 100644 index 40a877ee..00000000 --- a/src/grovemoisture/jsupm_grovemoisture.i +++ /dev/null @@ -1,8 +0,0 @@ -%module jsupm_grovemoisture -%include "../upm.i" - -%{ - #include "grovemoisture.hpp" -%} - -%include "grovemoisture.hpp" diff --git a/src/moisture/CMakeLists.txt b/src/moisture/CMakeLists.txt new file mode 100644 index 00000000..196f069c --- /dev/null +++ b/src/moisture/CMakeLists.txt @@ -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) diff --git a/src/grovemoisture/javaupm_grovemoisture.i b/src/moisture/javaupm_moisture.i similarity index 62% rename from src/grovemoisture/javaupm_grovemoisture.i rename to src/moisture/javaupm_moisture.i index 1cf217c7..c5a4f1f2 100644 --- a/src/grovemoisture/javaupm_grovemoisture.i +++ b/src/moisture/javaupm_moisture.i @@ -1,16 +1,16 @@ -%module javaupm_grovemoisture +%module javaupm_moisture %include "../upm.i" %{ - #include "grovemoisture.hpp" + #include "moisture.hpp" %} -%include "grovemoisture.hpp" +%include "moisture.hpp" %pragma(java) jniclasscode=%{ static { try { - System.loadLibrary("javaupm_grovemoisture"); + System.loadLibrary("javaupm_moisture"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load. \n" + e); System.exit(1); diff --git a/src/moisture/jsupm_moisture.i b/src/moisture/jsupm_moisture.i new file mode 100644 index 00000000..49605004 --- /dev/null +++ b/src/moisture/jsupm_moisture.i @@ -0,0 +1,8 @@ +%module jsupm_moisture +%include "../upm.i" + +%{ + #include "moisture.hpp" +%} + +%include "moisture.hpp" \ No newline at end of file diff --git a/src/grovemoisture/moisture.c b/src/moisture/moisture.c similarity index 86% rename from src/grovemoisture/moisture.c rename to src/moisture/moisture.c index f3387940..b56c734b 100644 --- a/src/grovemoisture/moisture.c +++ b/src/moisture/moisture.c @@ -25,9 +25,9 @@ #include "moisture.h" -grovemoisture_context grovemoisture_init(int pin) { - grovemoisture_context dev = - (grovemoisture_context) malloc(sizeof(struct _grovemoisture_context)); +moisture_context moisture_init(int pin) { + moisture_context dev = + (moisture_context) malloc(sizeof(struct _moisture_context)); if (dev == NULL) { printf("Unable to allocate memory for device context\n"); @@ -47,12 +47,12 @@ grovemoisture_context grovemoisture_init(int pin) { return dev; } -void grovemoisture_close(grovemoisture_context dev) { +void moisture_close(moisture_context dev) { mraa_aio_close(dev->aio); free(dev); } -upm_result_t grovemoisture_get_moisture(grovemoisture_context dev, +upm_result_t moisture_get_moisture(moisture_context dev, int* moisture) { *moisture = mraa_aio_read(dev->aio); diff --git a/src/grovemoisture/grovemoisture.cxx b/src/moisture/moisture.cxx similarity index 92% rename from src/grovemoisture/grovemoisture.cxx rename to src/moisture/moisture.cxx index aa44e783..9b1964c1 100644 --- a/src/grovemoisture/grovemoisture.cxx +++ b/src/moisture/moisture.cxx @@ -26,23 +26,23 @@ #include #include -#include "grovemoisture.hpp" +#include "moisture.hpp" using namespace upm; -GroveMoisture::GroveMoisture(int pin) +Moisture::Moisture(int pin) { if ( !(m_aio = mraa_aio_init(pin)) ) throw std::invalid_argument(std::string(__FUNCTION__) + ": mraa_aio_init() failed, invalid pin?"); } -GroveMoisture::~GroveMoisture() +Moisture::~Moisture() { mraa_aio_close(m_aio); } -int GroveMoisture::value() +int Moisture::value() { return mraa_aio_read(m_aio); } diff --git a/src/grovemoisture/moisture.h b/src/moisture/moisture.h similarity index 88% rename from src/grovemoisture/moisture.h rename to src/moisture/moisture.h index d7785269..ab8869e0 100644 --- a/src/grovemoisture/moisture.h +++ b/src/moisture/moisture.h @@ -39,10 +39,10 @@ extern "C" { /** * device context */ -typedef struct _grovemoisture_context { +typedef struct _moisture_context { mraa_aio_context aio; uint16_t analog_pin; -} *grovemoisture_context; +} *moisture_context; /** * Init function @@ -50,14 +50,14 @@ typedef struct _grovemoisture_context { * @param pin analog pin number * @return void* pointer to the sensor struct */ -grovemoisture_context grovemoisture_init(int pin); +moisture_context moisture_init(int pin); /** * Close function * * @param dev pointer to the sensor structure */ -void grovemoisture_close(grovemoisture_context dev); +void moisture_close(moisture_context dev); /** * Get Moisture function. @@ -66,7 +66,7 @@ void grovemoisture_close(grovemoisture_context dev); * @param moisture pointer that will be used to store the * 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); #ifdef __cplusplus diff --git a/src/grovemoisture/grovemoisture.hpp b/src/moisture/moisture.hpp similarity index 82% rename from src/grovemoisture/grovemoisture.hpp rename to src/moisture/moisture.hpp index 2b08170e..8728db2c 100644 --- a/src/grovemoisture/grovemoisture.hpp +++ b/src/moisture/moisture.hpp @@ -28,23 +28,24 @@ namespace upm { /** - * @brief Grove Moisture Sensor library + * @brief Moisture Sensor library * @defgroup grovemoisture libupm-grovemoisture * @ingroup seeed analog liquid eak hak */ /** - * @library grovemoisture - * @sensor grovemoisture - * @comname Grove Moisture Sensor + * @library moisture + * @sensor moisture + * @comname Moisture Sensor + * @altname Grove Moisture Sensor * @type liquid * @man seeed * @con analog * @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 * of soil or whether there is water around the sensor. * As the moisture content increases, so does the value that is read. @@ -52,20 +53,20 @@ namespace upm { * nor to be used outdoors. * * @image html grovemoisture.jpg - * @snippet grovemoisture.cxx Interesting + * @snippet moisture.cxx Interesting */ - class GroveMoisture { + class Moisture { public: /** - * Grove analog moisture sensor constructor + * Analog moisture sensor constructor * * @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 * diff --git a/src/grovemoisture/grovemoisture_fti.c b/src/moisture/moisture_fti.c similarity index 56% rename from src/grovemoisture/grovemoisture_fti.c rename to src/moisture/moisture_fti.c index 401e414c..99655595 100644 --- a/src/grovemoisture/grovemoisture_fti.c +++ b/src/moisture/moisture_fti.c @@ -30,42 +30,42 @@ * This file implements the Function Table Interface (FTI) for this sensor */ -const char upm_grovemoisture_name[] = "Grove Moisture"; -const char upm_grovemoisture_description[] = "Analog Grove Moisture Sensor"; -const upm_protocol_t upm_grovemoisture_protocol[] = {UPM_ANALOG}; -const upm_sensor_t upm_grovemoisture_category[] = {UPM_MOISTURE}; +const char upm_moisture_name[] = "Moisture"; +const char upm_moisture_description[] = "Analog Moisture Sensor"; +const upm_protocol_t upm_moisture_protocol[] = {UPM_ANALOG}; +const upm_sensor_t upm_moisture_category[] = {UPM_MOISTURE}; // forward declarations -const void* upm_grovemoisture_get_ft(upm_sensor_t sensor_type); -void* upm_grovemoisture_init_name(); -void upm_grovemoisture_close(void* dev); -upm_result_t upm_grovemoisture_get_moisture(void* dev, int* moisture); +const void* upm_moisture_get_ft(upm_sensor_t sensor_type); +void* upm_moisture_init_name(); +void upm_moisture_close(void* dev); +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; - usd.name = upm_grovemoisture_name; - usd.description = upm_grovemoisture_description; + usd.name = upm_moisture_name; + usd.description = upm_moisture_description; usd.protocol_size = 1; - usd.protocol = upm_grovemoisture_protocol; + usd.protocol = upm_moisture_protocol; usd.category_size = 1; - usd.category = upm_grovemoisture_category; + usd.category = upm_moisture_category; return usd; } static const upm_sensor_ft ft = { - .upm_sensor_init_name = &upm_grovemoisture_init_name, - .upm_sensor_close = &upm_grovemoisture_close, - .upm_sensor_get_descriptor = &upm_grovemoisture_get_descriptor + .upm_sensor_init_name = &upm_moisture_init_name, + .upm_sensor_close = &upm_moisture_close, + .upm_sensor_get_descriptor = &upm_moisture_get_descriptor }; 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){ return &mft; } @@ -75,14 +75,14 @@ const void* upm_grovemoisture_get_ft(upm_sensor_t sensor_type){ return NULL; } -void* upm_grovemoisture_init_name(){ +void* upm_moisture_init_name(){ return NULL; } -void upm_grovemoisture_close(void* dev){ - grovemoisture_close((grovemoisture_context)dev); +void upm_moisture_close(void* dev){ + moisture_close((moisture_context)dev); } -upm_result_t upm_grovemoisture_get_moisture(void* dev, int* moisture){ - return grovemoisture_get_moisture((grovemoisture_context)dev, moisture); +upm_result_t upm_moisture_get_moisture(void* dev, int* moisture){ + return moisture_get_moisture((moisture_context)dev, moisture); } diff --git a/src/grovemoisture/pyupm_grovemoisture.i b/src/moisture/pyupm_moisture.i similarity index 56% rename from src/grovemoisture/pyupm_grovemoisture.i rename to src/moisture/pyupm_moisture.i index e661aadf..e67f6353 100644 --- a/src/grovemoisture/pyupm_grovemoisture.i +++ b/src/moisture/pyupm_moisture.i @@ -1,11 +1,11 @@ // Include doxygen-generated documentation %include "pyupm_doxy2swig.i" -%module pyupm_grovemoisture +%module pyupm_moisture %include "../upm.i" %feature("autodoc", "3"); -%include "grovemoisture.hpp" +%include "moisture.hpp" %{ - #include "grovemoisture.hpp" -%} + #include "moisture.hpp" +%} \ No newline at end of file