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 <sisinty.s.patra@intel.com>
This commit is contained in:
Sisinty Sasmita Patra 2016-09-12 10:19:10 -07:00 committed by Noel Eck
parent b266f87450
commit bded0a07d9
21 changed files with 350 additions and 43 deletions

View File

@ -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

View File

@ -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)

View File

@ -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;

View File

@ -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)

View File

@ -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

View File

@ -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();

View File

@ -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:

View File

@ -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)

View File

@ -28,5 +28,5 @@
#include <grovebutton.hpp>
#include <groveled.hpp>
#include <relay.hpp>
#include <groverotary.hpp>
#include <rotary.hpp>
#include <grovetemp.hpp>

View File

@ -29,9 +29,9 @@
#include "relay.hpp"
%}
%include "groverotary.hpp"
%include "rotary.hpp"
%{
#include "groverotary.hpp"
#include "rotary.hpp"
%}
%include "grovetemp.hpp"

View File

@ -26,9 +26,9 @@
#include "relay.hpp"
%}
%include "groverotary.hpp"
%include "rotary.hpp"
%{
#include "groverotary.hpp"
#include "rotary.hpp"
%}
%include "grovetemp.hpp"

View File

@ -30,9 +30,9 @@
#include "relay.hpp"
%}
%include "groverotary.hpp"
%include "rotary.hpp"
%{
#include "groverotary.hpp"
#include "rotary.hpp"
%}
%include "grovetemp.hpp"

View File

@ -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)

View File

@ -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);
}
}
%}

View File

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

11
src/rotary/pyupm_rotary.i Normal file
View File

@ -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"
%}

78
src/rotary/rotary.c Normal file
View File

@ -0,0 +1,78 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Sisinty Sasmita Patra <sisinty.s.patra@intel.com>
*
* 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 <stdio.h>
#include <stdlib.h>
#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;
}

View File

@ -28,52 +28,51 @@
#include <string>
#include <stdexcept>
#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;
}

57
src/rotary/rotary.h Normal file
View File

@ -0,0 +1,57 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Sisinty Sasmita Patra <sisinty.s.patra@intel.com>
*
* 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 <unistd.h>
#include "upm.h"
#include <mraa/aio.h>
/**
* 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_ */

View File

@ -28,13 +28,14 @@
#include <string>
#include <mraa/aio.hpp>
#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;

125
src/rotary/rotary_fti.c Normal file
View File

@ -0,0 +1,125 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Sisinty Sasmita Patra <sisinty.s.patra@intel.com>
*
* 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, &degrees);
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;
}
}