From bded0a07d95bdd011d428de80184b51a63542cc9 Mon Sep 17 00:00:00 2001 From: Sisinty Sasmita Patra Date: Mon, 12 Sep 2016 10:19:10 -0700 Subject: [PATCH] Rotary: Added C source Added C source for rotary angle sensor * Renamed all files from groverrotary to rotary * Replaced all instances of groverrotary with rotary * Added C source for rotary angle sensor * Updated all CMake files Signed-off-by: Sisinty Sasmita Patra --- doxy/samples.mapping.txt | 2 +- examples/c++/CMakeLists.txt | 2 +- examples/c++/{groverotary.cxx => rotary.cxx} | 2 +- examples/java/CMakeLists.txt | 2 +- ...oveRotarySample.java => RotarySample.java} | 4 +- .../javascript/{groverotary.js => rotary.js} | 2 +- examples/python/{groverotary.py => rotary.py} | 2 +- src/grove/CMakeLists.txt | 4 +- src/grove/grove.hpp | 2 +- src/grove/javaupm_grove.i | 4 +- src/grove/jsupm_grove.i | 4 +- src/grove/pyupm_grove.i | 4 +- src/rotary/CMakeLists.txt | 8 ++ src/rotary/javaupm_rotary.i | 19 +++ src/rotary/jsupm_rotary.i | 8 ++ src/rotary/pyupm_rotary.i | 11 ++ src/rotary/rotary.c | 78 +++++++++++ .../groverotary.cxx => rotary/rotary.cxx} | 29 ++-- src/rotary/rotary.h | 57 ++++++++ .../groverotary.hpp => rotary/rotary.hpp} | 24 ++-- src/rotary/rotary_fti.c | 125 ++++++++++++++++++ 21 files changed, 350 insertions(+), 43 deletions(-) rename examples/c++/{groverotary.cxx => rotary.cxx} (97%) rename examples/java/{GroveRotarySample.java => RotarySample.java} (95%) rename examples/javascript/{groverotary.js => rotary.js} (97%) rename examples/python/{groverotary.py => rotary.py} (98%) create mode 100644 src/rotary/CMakeLists.txt create mode 100644 src/rotary/javaupm_rotary.i create mode 100644 src/rotary/jsupm_rotary.i create mode 100644 src/rotary/pyupm_rotary.i create mode 100644 src/rotary/rotary.c rename src/{grove/groverotary.cxx => rotary/rotary.cxx} (74%) create mode 100644 src/rotary/rotary.h rename src/{grove/groverotary.hpp => rotary/rotary.hpp} (87%) create mode 100644 src/rotary/rotary_fti.c diff --git a/doxy/samples.mapping.txt b/doxy/samples.mapping.txt index 2114129d..6d73d4af 100644 --- a/doxy/samples.mapping.txt +++ b/doxy/samples.mapping.txt @@ -17,7 +17,7 @@ grovelinefinder.cxx GroveLineFinderSample.java light.cxx LightSample.java grovemoisture.cxx GroveMoistureSample.java relay.cxx RelaySample.java -groverotary.cxx GroveRotarySample.java +rotary.cxx RotarySample.java grovescam.cxx GROVESCAMSample.java slide.cxx SlideSample.java grovespeaker.cxx GroveSpeakerSample.java diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index 22d02f93..7e4200ed 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -92,7 +92,7 @@ add_example (relay) add_example (light) add_example (temperature) add_example (grovebutton) -add_example (groverotary) +add_example (rotary) add_example (slide) add_example (buzzer-sound) add_example (nrf24l01-transmitter) diff --git a/examples/c++/groverotary.cxx b/examples/c++/rotary.cxx similarity index 97% rename from examples/c++/groverotary.cxx rename to examples/c++/rotary.cxx index aa2ee57f..eecd8b2a 100644 --- a/examples/c++/groverotary.cxx +++ b/examples/c++/rotary.cxx @@ -33,7 +33,7 @@ int main () { //! [Interesting] // Instantiate a rotary sensor on analog pin A0 - upm::GroveRotary* knob = new upm::GroveRotary(0); + upm::Rotary* knob = new upm::Rotary(0); // Print sensor name to confirm it initialized properly cout << knob->name() << endl; diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt index ed532bcb..17e1f28b 100644 --- a/examples/java/CMakeLists.txt +++ b/examples/java/CMakeLists.txt @@ -47,7 +47,7 @@ add_example(GroveMQ9 gas) add_example(O2Example o2) add_example(GroveQTouch at42qt1070) add_example(RelaySample grove) -add_example(GroveRotarySample grove) +add_example(RotarySample grove) add_example(GROVESCAMSample grovescam) add_example(SlideSample slide) add_example(GroveSpeakerSample grovespeaker) diff --git a/examples/java/GroveRotarySample.java b/examples/java/RotarySample.java similarity index 95% rename from examples/java/GroveRotarySample.java rename to examples/java/RotarySample.java index f1b98c74..965cb650 100644 --- a/examples/java/GroveRotarySample.java +++ b/examples/java/RotarySample.java @@ -22,11 +22,11 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -public class GroveRotarySample { +public class RotarySample { public static void main(String[] args) throws InterruptedException { // ! [Interesting] - upm_grove.GroveRotary knob = new upm_grove.GroveRotary(0); + upm_grove.Rotary knob = new upm_grove.Rotary(0); while (true) { float abs_value = knob.abs_value(); // Absolute raw value diff --git a/examples/javascript/groverotary.js b/examples/javascript/rotary.js similarity index 97% rename from examples/javascript/groverotary.js rename to examples/javascript/rotary.js index 040f38c6..c26f20dc 100644 --- a/examples/javascript/groverotary.js +++ b/examples/javascript/rotary.js @@ -26,7 +26,7 @@ var upm_grove = require('jsupm_grove'); //setup access analog input Analog pin #0 (A0) -var groveRotary = new upm_grove.GroveRotary(0); +var groveRotary = new upm_grove.Rotary(0); loop(); diff --git a/examples/python/groverotary.py b/examples/python/rotary.py similarity index 98% rename from examples/python/groverotary.py rename to examples/python/rotary.py index 6d62c3c8..98396d46 100644 --- a/examples/python/groverotary.py +++ b/examples/python/rotary.py @@ -24,7 +24,7 @@ from time import sleep import pyupm_grove as grove # New knob on AIO pin 0 -knob = grove.GroveRotary(0) +knob = grove.Rotary(0) # Loop indefinitely while True: diff --git a/src/grove/CMakeLists.txt b/src/grove/CMakeLists.txt index 25751d1c..5c44a346 100644 --- a/src/grove/CMakeLists.txt +++ b/src/grove/CMakeLists.txt @@ -1,7 +1,7 @@ upm_mixed_module_init (NAME grove DESCRIPTION "upm grove module" - CPP_HDR grovebutton.hpp groveled.hpp groverotary.hpp + CPP_HDR grovebutton.hpp groveled.hpp grovetemp.hpp grovebase.hpp grove.hpp - CPP_SRC grovebutton.cxx groveled.cxx groverotary.cxx + CPP_SRC grovebutton.cxx groveled.cxx grovetemp.cxx REQUIRES mraa) diff --git a/src/grove/grove.hpp b/src/grove/grove.hpp index 77fa796c..82fab7ef 100644 --- a/src/grove/grove.hpp +++ b/src/grove/grove.hpp @@ -28,5 +28,5 @@ #include #include #include -#include +#include #include diff --git a/src/grove/javaupm_grove.i b/src/grove/javaupm_grove.i index eef5df66..735f4503 100644 --- a/src/grove/javaupm_grove.i +++ b/src/grove/javaupm_grove.i @@ -29,9 +29,9 @@ #include "relay.hpp" %} -%include "groverotary.hpp" +%include "rotary.hpp" %{ - #include "groverotary.hpp" + #include "rotary.hpp" %} %include "grovetemp.hpp" diff --git a/src/grove/jsupm_grove.i b/src/grove/jsupm_grove.i index 5bc3f2d2..ad54df78 100644 --- a/src/grove/jsupm_grove.i +++ b/src/grove/jsupm_grove.i @@ -26,9 +26,9 @@ #include "relay.hpp" %} -%include "groverotary.hpp" +%include "rotary.hpp" %{ - #include "groverotary.hpp" + #include "rotary.hpp" %} %include "grovetemp.hpp" diff --git a/src/grove/pyupm_grove.i b/src/grove/pyupm_grove.i index 5686f598..b8751f1a 100644 --- a/src/grove/pyupm_grove.i +++ b/src/grove/pyupm_grove.i @@ -30,9 +30,9 @@ #include "relay.hpp" %} -%include "groverotary.hpp" +%include "rotary.hpp" %{ - #include "groverotary.hpp" + #include "rotary.hpp" %} %include "grovetemp.hpp" diff --git a/src/rotary/CMakeLists.txt b/src/rotary/CMakeLists.txt new file mode 100644 index 00000000..4a93bd9f --- /dev/null +++ b/src/rotary/CMakeLists.txt @@ -0,0 +1,8 @@ +upm_mixed_module_init (NAME rotary + DESCRIPTION "UPM Relay Module: rotary" + C_HDR rotary.h + C_SRC rotary.c + CPP_HDR rotary.hpp + CPP_SRC rotary.cxx + FTI_SRC rotary_fti.c + REQUIRES mraa) diff --git a/src/rotary/javaupm_rotary.i b/src/rotary/javaupm_rotary.i new file mode 100644 index 00000000..c7c774e1 --- /dev/null +++ b/src/rotary/javaupm_rotary.i @@ -0,0 +1,19 @@ +%module javaupm_rotary +%include "../upm.i" + +%{ + #include "rotary.hpp" +%} + +%include "rotary.hpp" + +%pragma(java) jniclasscode=%{ + static { + try { + System.loadLibrary("javaupm_rotary"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. \n" + e); + System.exit(1); + } + } +%} diff --git a/src/rotary/jsupm_rotary.i b/src/rotary/jsupm_rotary.i new file mode 100644 index 00000000..71b9ed91 --- /dev/null +++ b/src/rotary/jsupm_rotary.i @@ -0,0 +1,8 @@ +%module jsupm_rotary +%include "../upm.i" + +%{ + #include "rotary.hpp" +%} + +%include "rotary.hpp" diff --git a/src/rotary/pyupm_rotary.i b/src/rotary/pyupm_rotary.i new file mode 100644 index 00000000..eb65616a --- /dev/null +++ b/src/rotary/pyupm_rotary.i @@ -0,0 +1,11 @@ +// Include doxygen-generated documentation +%include "pyupm_doxy2swig.i" +%module pyupm_rotary +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "rotary.hpp" +%{ + #include "rotary.hpp" +%} diff --git a/src/rotary/rotary.c b/src/rotary/rotary.c new file mode 100644 index 00000000..8e293020 --- /dev/null +++ b/src/rotary/rotary.c @@ -0,0 +1,78 @@ +/* + * Author: Brendan Le Foll + * Sisinty Sasmita Patra + * + * Copyright (c) 2016 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 "rotary.h" + +rotary_context rotary_init(int pin, float aRef) +{ + rotary_context dev = (rotary_context)malloc(sizeof(struct _rotary_context)); + + if(dev == NULL) return NULL; + + dev->aio = mraa_aio_init(pin); + + if(dev->aio == NULL){ + free(dev); + return NULL; + } + + dev->m_aRef = aRef; + + // get adc bit range + dev->m_aRes = (1 << mraa_aio_get_bit(dev->aio)); + + return dev; +} + +void rotary_close(rotary_context dev) +{ + mraa_aio_close(dev->aio); + free(dev); +} + +upm_result_t rotary_get_value_voltage (const rotary_context dev, + float* volts) +{ + float val = 0.0; + val = mraa_aio_read(dev->aio); + *volts = (dev->m_aRef / dev->m_aRes) * (float)val; + + return UPM_SUCCESS; +} + +upm_result_t rotary_get_value_angle (rotary_context dev, float* rotval) +{ + float val = 0.0; + val = mraa_aio_read(dev->aio); + + // return degrees + *rotval = val * (float)ROTARY_MAX_ANGLE / dev->m_aRes; + + return UPM_SUCCESS; +} diff --git a/src/grove/groverotary.cxx b/src/rotary/rotary.cxx similarity index 74% rename from src/grove/groverotary.cxx rename to src/rotary/rotary.cxx index 925c97fd..03fe8140 100644 --- a/src/grove/groverotary.cxx +++ b/src/rotary/rotary.cxx @@ -28,52 +28,51 @@ #include #include -#include "groverotary.hpp" +#include "rotary.hpp" #include "math.h" using namespace upm; -GroveRotary::GroveRotary(unsigned int pin) +Rotary::Rotary(unsigned int pin) { if ( !(m_aio = mraa_aio_init(pin)) ) { throw std::invalid_argument(std::string(__FUNCTION__) + ": mraa_aio_init() failed, invalid pin?"); return; } - m_name = "Rotary Angle Sensor"; } -GroveRotary::~GroveRotary() +Rotary::~Rotary() { mraa_aio_close(m_aio); } -float GroveRotary::abs_value() +float Rotary::abs_value() { return (float) mraa_aio_read(m_aio); } -float GroveRotary::abs_deg() +float Rotary::abs_deg() { - return GroveRotary::abs_value() * (float) m_max_angle / 1023.0; + return Rotary::abs_value() * (float) m_max_angle / 1023.0; } -float GroveRotary::abs_rad() +float Rotary::abs_rad() { - return GroveRotary::abs_deg() * M_PI / 180.0; + return Rotary::abs_deg() * M_PI / 180.0; } -float GroveRotary::rel_value() +float Rotary::rel_value() { - return GroveRotary::abs_value() - 512.0; + return Rotary::abs_value() - 512.0; } -float GroveRotary::rel_deg() +float Rotary::rel_deg() { - return GroveRotary::rel_value() * (float) m_max_angle / 1023.0; + return Rotary::rel_value() * (float) m_max_angle / 1023.0; } -float GroveRotary::rel_rad() +float Rotary::rel_rad() { - return GroveRotary::rel_deg() * M_PI / 180.0; + return Rotary::rel_deg() * M_PI / 180.0; } diff --git a/src/rotary/rotary.h b/src/rotary/rotary.h new file mode 100644 index 00000000..653d2fe7 --- /dev/null +++ b/src/rotary/rotary.h @@ -0,0 +1,57 @@ +/* + * Author: Brendan Le Foll + * Sisinty Sasmita Patra + * + * Copyright (c) 2016 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. + */ + +#ifndef ROTARY_H_ +#define ROTARY_H_ + +#include + +#include "upm.h" +#include + +/** + * driver context + */ +typedef struct _rotary_context { + mraa_aio_context aio; + float m_aRef; + int16_t m_aRes; +} *rotary_context; + +#define ROTARY_MAX_ANGLE 300 + +typedef struct _rotary_context* rotary_context; + +rotary_context rotary_init(int pin, float aRef); + +void rotary_close(rotary_context dev); + +upm_result_t rotary_get_value_voltage(const rotary_context dev, float* volts); + +// degrees only +upm_result_t rotary_get_value_angle(rotary_context dev, float* rotval); + +#endif /* ROTARY_H_ */ diff --git a/src/grove/groverotary.hpp b/src/rotary/rotary.hpp similarity index 87% rename from src/grove/groverotary.hpp rename to src/rotary/rotary.hpp index 86c8ad6c..e18a9cd7 100644 --- a/src/grove/groverotary.hpp +++ b/src/rotary/rotary.hpp @@ -28,13 +28,14 @@ #include #include -#include "grovebase.hpp" + +#include "rotary.hpp" namespace upm { /** - * @library grove - * @sensor groverotary + * @library rotary + * @sensor rotary * @comname Grove Rotary Angle Sensor * @altname Rotary Potentiometer * @type ainput @@ -42,27 +43,27 @@ namespace upm { * @con analog * @kit gsk * - * @brief API for the Grove Rotary Angle Sensor (Knob) + * @brief API for the Rotary Angle Sensor (Knob) * - * Basic UPM module for the Grove rotary angle sensor (knob) on analog. Provides + * Basic UPM module for the rotary angle sensor (knob) on analog. Provides * a set of functions to read the absolute pin value, degrees or radians, and another set * to do the same relative to the center of the knob's range. * - * @image html groverotary.jpeg - * @snippet groverotary.cxx Interesting + * @image html rotary.jpeg + * @snippet rotary.cxx Interesting */ -class GroveRotary: public Grove { +class Rotary{ public: /** * Grove rotary angle sensor constructor * * @param pin Number of the analog pin to use */ - GroveRotary(unsigned int pin); + Rotary(unsigned int pin); /** - * GroveRotary destructor + * Rotary destructor */ - ~GroveRotary(); + ~Rotary(); /** * Gets the absolute raw value from the AIO pin * @@ -99,6 +100,7 @@ class GroveRotary: public Grove { * @return Signed radians from the ADC */ float rel_rad(); + std::string name(){ return "Rotary Angle Sensor";} private: mraa_aio_context m_aio; static const int m_max_angle = 300; diff --git a/src/rotary/rotary_fti.c b/src/rotary/rotary_fti.c new file mode 100644 index 00000000..33428727 --- /dev/null +++ b/src/rotary/rotary_fti.c @@ -0,0 +1,125 @@ +/* + * Author: Brendan Le Foll + * Sisinty Sasmita Patra + * + * Copyright (c) 2016 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 "rotary.h" +#include "upm_fti.h" + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_rotary_name[] = "Grove Rotary"; +const char upm_rotary_description[] = "Analog Grove Rotary Sensor"; +const upm_protocol_t upm_rotary_protocol[] = {UPM_ANALOG}; +const upm_sensor_t upm_rotary_category[] = {UPM_VOLTAGE, UPM_ANGLE}; + +// forward declarations +const upm_sensor_descriptor_t upm_rotary_get_descriptor(); +const void* upm_rotary_get_ft(upm_sensor_t sensor_type); +void* upm_rotary_init_name(); +void upm_rotary_close(void* dev); +upm_result_t upm_rotary_get_value_voltage (const void* dev, float* volts); +upm_result_t upm_rotary_get_value_angle(void* dev, float* rotval, + upm_angle_u unit); + +const upm_sensor_descriptor_t upm_rotary_get_descriptor() { + upm_sensor_descriptor_t usd; + usd.name = upm_rotary_name; + usd.description = upm_rotary_description; + usd.protocol_size = 1; + usd.protocol = upm_rotary_protocol; + usd.category_size = 1; + usd.category = upm_rotary_category; + return usd; +} + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = &upm_rotary_init_name, + .upm_sensor_close = &upm_rotary_close, + .upm_sensor_get_descriptor = &upm_rotary_get_descriptor +}; + +static const upm_voltage_ft vft = +{ + .upm_voltage_get_value = &upm_rotary_get_value_voltage +}; + +static const upm_angle_ft aft = +{ + .upm_angle_get_value = &upm_rotary_get_value_angle +}; + +const void* upm_rotary_get_ft(upm_sensor_t sensor_type) { + if(sensor_type == UPM_SENSOR) { + return &ft; + } + if(sensor_type == UPM_VOLTAGE) { + return &vft; + } + if(sensor_type == UPM_ANGLE) { + return &aft; + } + return NULL; +} + +void* upm_rotary_init_name(){ + return NULL; +} + +void upm_rotary_close(void* dev) +{ + rotary_close((rotary_context)dev); +} + +upm_result_t upm_rotary_get_value_voltage (const void* dev, float* volts) +{ + return rotary_get_value_voltage((rotary_context)dev, volts); +} + +upm_result_t upm_rotary_get_value_angle (void* dev, float* rotval, + upm_angle_u unit) +{ + float degrees; + + // returned in degrees + rotary_get_value_angle((rotary_context)dev, °rees); + + switch(unit) + { + case DEGREES: + *rotval = degrees; + return UPM_SUCCESS; + + case RADIANS: + *rotval = degrees * M_PI / 180.0; + + return UPM_SUCCESS; + + default: + return UPM_ERROR_INVALID_PARAMETER; + } +}