slide: Added slide potentiometer sensor C source

Added the C source for the flex sensor with necessary changes to cmake,
examples, and docs.

    * Renamed all files with groveslide to slide
    * Replaced all instances of groveslide with slide
    * Added C source for slide sensor
    * Updated all cmake files
    * Added C example for slide sensor
    * Split out slide sensor from grove library

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck 2016-09-09 17:21:03 -07:00
parent e2bec11c23
commit e58c651b67
24 changed files with 550 additions and 51 deletions

View File

Before

Width:  |  Height:  |  Size: 8.7 KiB

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@ -19,7 +19,7 @@ grovemoisture.cxx GroveMoistureSample.java
groverelay.cxx GroveRelaySample.java
groverotary.cxx GroveRotarySample.java
grovescam.cxx GROVESCAMSample.java
groveslide.cxx GroveSlideSample.java
slide.cxx SlideSample.java
grovespeaker.cxx GroveSpeakerSample.java
vdiv.cxx VDivSample.java
grovewater.cxx GroveWaterSample.java

View File

@ -93,7 +93,7 @@ add_example (light)
add_example (grovetemp)
add_example (grovebutton)
add_example (groverotary)
add_example (groveslide)
add_example (slide)
add_example (buzzer-sound)
add_example (nrf24l01-transmitter)
add_example (nrf24l01-receiver)

View File

@ -26,14 +26,14 @@
#include <unistd.h>
#include <iostream>
#include <iomanip>
#include "grove.hpp"
#include "slide.hpp"
using namespace std;
int main ()
{
//! [Interesting]
upm::GroveSlide* slide = new upm::GroveSlide(0); // Instantiate new grove slide potentiometer on analog pin A0
upm::Slide* slide = new upm::Slide(0); // Instantiate new grove slide potentiometer on analog pin A0
cout << slide->name() << endl;

View File

@ -103,6 +103,7 @@ add_example (light)
add_example (ldt0028)
add_example (joystick12)
add_example (flex)
add_example (slide)
# Custom examples
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)

86
examples/c/slide.c Normal file
View File

@ -0,0 +1,86 @@
/*
* Author: Jon Trulson <jtrulson@ics.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 <unistd.h>
#include <signal.h>
#include "slide.h"
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main()
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a slide sensor on analog pin A0
slide_context sensor = slide_init(0);
if (!sensor)
{
printf("slide_init() failed.\n");
return -1;
}
// Set the aref, scale, and offset
slide_set_aref(sensor, 5.0);
slide_set_scale(sensor, 1.0);
slide_set_offset(sensor, -.1);
printf("aRef: %0.03f scale: %0.03f offset: %0.03f\n\n",
slide_get_aref(sensor),
slide_get_scale(sensor),
slide_get_offset(sensor));
// Every half a second, sample the sensor output
while (shouldRun)
{
float normalized = 0.0;
float raw_volts = 0.0;
float volts = 0.0;
slide_get_normalized(sensor, &normalized);
slide_get_raw_volts(sensor, &raw_volts);
slide_get_volts(sensor, &volts);
printf("Normalized output: %0.03f, raw slide sensor output: %0.03f v "
"adjusted output: %0.03f v\n", normalized, raw_volts, volts);
usleep(500000);
}
//! [Interesting]
printf("Exiting\n");
slide_close(sensor);
return 0;
}

View File

@ -49,7 +49,7 @@ add_example(GroveQTouch at42qt1070)
add_example(GroveRelaySample grove)
add_example(GroveRotarySample grove)
add_example(GROVESCAMSample grovescam)
add_example(GroveSlideSample grove)
add_example(SlideSample grove)
add_example(GroveSpeakerSample grovespeaker)
add_example(GroveTempSample grove)
add_example(VDivSample vdiv)

View File

@ -22,11 +22,11 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
public class GroveSlideSample {
public class SlideSample {
public static void main (String args[]) throws InterruptedException {
//! [Interesting]
// Instantiate new grove slide potentiometer on analog pin A0
upm_grove.GroveSlide slide = new upm_grove.GroveSlide(0);
upm_grove.Slide slide = new upm_grove.Slide(0);
while (true) {
float raw_value = slide.raw_value();

View File

@ -25,14 +25,14 @@
var upm_grove = require('jsupm_grove');
//setup access analog input Analog pin #0 (A0)
var groveSlide = new upm_grove.GroveSlide(0);
var Slide = new upm_grove.Slide(0);
loop();
function loop()
{
var raw = groveSlide.raw_value();
var volts = groveSlide.voltage_value();
var raw = Slide.raw_value();
var volts = Slide.voltage_value();
//write the slider values to the console
console.log("Slider Value: " + raw + " = " + volts.toFixed(2) + " V");

View File

@ -24,7 +24,7 @@ from time import sleep
import pyupm_grove as grove
# New Grove Slider on AIO pin 0
slider = grove.GroveSlide(0)
slider = grove.Slide(0)
# Loop indefinitely
while True:

View File

@ -1,8 +1,7 @@
upm_mixed_module_init (NAME grove
DESCRIPTION "upm grove module"
CPP_HDR grovebutton.hpp groveled.hpp groverelay.hpp groverotary.hpp
groveslide.hpp grovetemp.hpp grovebase.hpp
grove.hpp
grovetemp.hpp grovebase.hpp grove.hpp
CPP_SRC grovebutton.cxx groveled.cxx groverelay.cxx groverotary.cxx
groveslide.cxx grovetemp.cxx
grovetemp.cxx
REQUIRES mraa)

View File

@ -29,5 +29,4 @@
#include <groveled.hpp>
#include <groverelay.hpp>
#include <groverotary.hpp>
#include <groveslide.hpp>
#include <grovetemp.hpp>

View File

@ -34,11 +34,6 @@
#include "groverotary.hpp"
%}
%include "groveslide.hpp"
%{
#include "groveslide.hpp"
%}
%include "grovetemp.hpp"
%{
#include "grovetemp.hpp"

View File

@ -31,11 +31,6 @@
#include "groverotary.hpp"
%}
%include "groveslide.hpp"
%{
#include "groveslide.hpp"
%}
%include "grovetemp.hpp"
%{
#include "grovetemp.hpp"

View File

@ -35,11 +35,6 @@
#include "groverotary.hpp"
%}
%include "groveslide.hpp"
%{
#include "groveslide.hpp"
%}
%include "grovetemp.hpp"
%{
#include "grovetemp.hpp"

8
src/slide/CMakeLists.txt Normal file
View File

@ -0,0 +1,8 @@
upm_mixed_module_init (NAME slide
DESCRIPTION "Slide potentiometer sensor"
C_HDR slide.h
C_SRC slide.c
CPP_HDR slide.hpp
CPP_SRC slide.cxx
FTI_SRC slide_fti.c
REQUIRES mraa)

19
src/slide/javaupm_slide.i Normal file
View File

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

8
src/slide/jsupm_slide.i Normal file
View File

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

11
src/slide/pyupm_slide.i Normal file
View File

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

127
src/slide/slide.c Normal file
View File

@ -0,0 +1,127 @@
/*
* Author:
* 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 <string.h>
#include <stdlib.h>
#include "slide.h"
slide_context slide_init(int16_t pin)
{
slide_context dev = (slide_context) malloc(sizeof(struct _slide_context));
if (dev == NULL)
return NULL;
/* Init aio pin */
dev->aio = mraa_aio_init(pin);
if (dev->aio == NULL) {
free(dev);
return NULL;
}
/* Set the ADC ref, scale, and offset defaults */
dev->m_aRef = 5.0;
dev->m_scale = 1.0;
dev->m_offset = 0.0;
return dev;
}
void slide_close(slide_context dev)
{
mraa_aio_close(dev->aio);
free(dev);
}
upm_result_t slide_set_aref(const slide_context dev, float aref)
{
dev->m_aRef = aref;
return UPM_SUCCESS;
}
upm_result_t slide_set_scale(const slide_context dev, float scale)
{
dev->m_scale = scale;
return UPM_SUCCESS;
}
upm_result_t slide_set_offset(const slide_context dev, float offset)
{
dev->m_offset = offset;
return UPM_SUCCESS;
}
float slide_get_aref(const slide_context dev)
{
return dev->m_aRef;
}
float slide_get_scale(const slide_context dev)
{
return dev->m_scale;
}
float slide_get_offset(const slide_context dev)
{
return dev->m_offset;
}
upm_result_t slide_get_normalized(const slide_context dev, float *value)
{
*value = mraa_aio_read_float(dev->aio);
if (*value < 0)
return UPM_ERROR_OPERATION_FAILED;
}
upm_result_t slide_get_raw_volts(const slide_context dev, float *value)
{
*value = mraa_aio_read_float(dev->aio);
if (*value < 0)
return UPM_ERROR_OPERATION_FAILED;
/* Scale by the ADC reference voltage */
*value *= dev->m_aRef;
return UPM_SUCCESS;
}
upm_result_t slide_get_volts(const slide_context dev, float *value)
{
*value = mraa_aio_read_float(dev->aio);
if (*value < 0)
return UPM_ERROR_OPERATION_FAILED;
/* Apply raw scale */
*value *= dev->m_scale;
/* Scale to aRef */
*value *= dev->m_aRef;
/* Apply the offset in volts */
*value += dev->m_offset;
return UPM_SUCCESS;
}

View File

@ -28,12 +28,12 @@
#include <string>
#include <stdexcept>
#include "groveslide.hpp"
#include "slide.hpp"
#include "math.h"
using namespace upm;
GroveSlide::GroveSlide(unsigned int pin, float ref_voltage)
Slide::Slide(unsigned int pin, float ref_voltage)
{
if ( !(m_aio = mraa_aio_init(pin)) ) {
throw std::invalid_argument(std::string(__FUNCTION__) +
@ -41,28 +41,27 @@ GroveSlide::GroveSlide(unsigned int pin, float ref_voltage)
return;
}
m_ref_voltage = ref_voltage;
m_name = "Slide Potentiometer";
}
GroveSlide::~GroveSlide()
Slide::~Slide()
{
mraa_aio_close(m_aio);
}
float GroveSlide::raw_value()
float Slide::raw_value()
{
return (float) mraa_aio_read(m_aio);
}
float GroveSlide::voltage_value()
float Slide::voltage_value()
{
// conversion to Volts
float a = GroveSlide::raw_value();
float a = Slide::raw_value();
a = m_ref_voltage * a / 1023.0 ;
return a;
}
float GroveSlide::ref_voltage()
float Slide::ref_voltage()
{
return m_ref_voltage;
}

133
src/slide/slide.h Normal file
View File

@ -0,0 +1,133 @@
/*
* Author:
* 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 "upm.h"
#include "mraa/aio.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* driver context
*/
typedef struct _slide_context {
/* mraa aio pin context */
mraa_aio_context aio;
/* Analog voltage reference */
float m_aRef;
/* Scale */
float m_scale;
/* Offset in sensor units */
float m_offset;
} *slide_context;
/**
* Initialize analog sensor
* @param pin Analog pin
* @return sensor context
*/
slide_context slide_init(int16_t pin);
/**
* Analog sensor destructor
* @param sensor context pointer
*/
void slide_close(slide_context dev);
/**
* Set ADC reference voltage
* @param dev sensor context pointer
* @param aref ADC reference voltage
* @return Function result code
*/
upm_result_t slide_set_aref(const slide_context dev, float aref);
/**
* Set sensor scale. This scale is applied to the return value:
* counts = counts * scale
* @param dev sensor context pointer
* @param scale count scale value used
* @return Function result code
*/
upm_result_t slide_set_scale(const slide_context dev, float scale);
/**
* Set sensor offset. This offset is applied to the return value:
* value = value + offset
* @param dev sensor context pointer
* @param offset count offset value used
* @return Function result code
*/
upm_result_t slide_set_offset(const slide_context dev, float offset);
/**
* Get sensor aref
* @param dev sensor context pointer
* @return Sensor ADC reference voltage
*/
float slide_get_aref(const slide_context dev);
/**
* Get sensor scale
* @param dev sensor context pointer
* @return Sensor scale
*/
float slide_get_scale(const slide_context dev);
/**
* Get sensor offset
* @param dev sensor context pointer
* @return Sensor offset
*/
float slide_get_offset(const slide_context dev);
/**
* Read normalized value for sensor
* @param dev sensor context pointer
* @param *value Normalized value (0.0 -> 1.0)
* @return Function result code
*/
upm_result_t slide_get_normalized(const slide_context dev, float *value);
/**
* Read raw voltage from the sensor
* @param dev sensor context pointer
* @param *value Raw sensor voltage
* @return Function result code
*/
upm_result_t slide_get_raw_volts(const slide_context dev, float *value);
/**
* Read scaled/offset voltage from the sensor
* @param dev sensor context pointer
* @param *value Adjusted sensor voltage
* @return Function result code
*/
upm_result_t slide_get_volts(const slide_context dev, float *value);
#ifdef __cplusplus
}
#endif

View File

@ -28,40 +28,39 @@
#include <string>
#include <mraa/aio.hpp>
#include "grovebase.hpp"
namespace upm {
/**
* @library grove
* @sensor groveslide
* @comname Grove Slide Potentiometer
* @library slide
* @sensor slide
* @comname Slide Potentiometer
* @type ainput
* @man seeed
* @con analog
*
* @brief API for the Grove Slide Potentiometer
* @brief API for the Slide Potentiometer
*
* Basic UPM module for the Grove slide potentiometer on analog that
* Basic UPM module for the slide potentiometer on analog that
* returns either a raw value or a scaled voltage value.
*
* @image html groveslide.jpeg
* @snippet groveslide.cxx Interesting
* @image html slide.jpeg
* @snippet slide.cxx Interesting
*/
class GroveSlide: public Grove {
class Slide {
public:
/**
* Grove analog slide potentiometer constructor
* Analog slide potentiometer constructor
*
* @param pin Number of the analog pin to use
*
* @param ref_voltage Reference voltage the board is set to, as a floating-point value; default is 5.0V
*/
GroveSlide(unsigned int pin, float ref_voltage = 5.0);
Slide(unsigned int pin, float ref_voltage = 5.0);
/**
* GroveSlide destructor
* Slide destructor
*/
~GroveSlide();
~Slide();
/**
* Gets the raw value from the AIO pin
*
@ -80,6 +79,12 @@ class GroveSlide: public Grove {
* @return Reference voltage the class was set for
*/
float ref_voltage();
/* Gets the sensor name
*
* @return sensor name
*/
std::string name() {return "Slide Potentiometer";}
private:
mraa_aio_context m_aio;
float m_ref_voltage;

119
src/slide/slide_fti.c Normal file
View File

@ -0,0 +1,119 @@
/*
* Author:
* 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 <string.h>
#include <stdlib.h>
#include "slide.h"
#include "upm_fti.h"
#include "fti/upm_sensor.h"
#include "fti/upm_raw.h"
/**
* This file implements the Function Table Interface (FTI) for this sensor
*/
const char upm_slide_name[] = "SLIDE";
const char upm_slide_description[] = "Analog slide sensor";
const upm_protocol_t upm_slide_protocol[] = {UPM_ANALOG};
const upm_sensor_t upm_slide_category[] = {UPM_RAW};
// forward declarations
const void* upm_slide_get_ft(upm_sensor_t sensor_type);
void* upm_slide_init_str(const char* protocol, const char* params);
void upm_slide_close(void* dev);
const upm_sensor_descriptor_t upm_slide_get_descriptor();
upm_result_t upm_slide_set_offset(const void* dev, float offset);
upm_result_t upm_slide_set_scale(const void* dev, float scale);
upm_result_t upm_slide_get_value(const void* dev, float *value);
/* This sensor implementes 2 function tables */
/* 1. Generic base function table */
static const upm_sensor_ft ft_gen =
{
.upm_sensor_init_name = &upm_slide_init_str,
.upm_sensor_close = &upm_slide_close,
.upm_sensor_get_descriptor = &upm_slide_get_descriptor
};
/* 2. RAW function table */
static const upm_raw_ft ft_raw =
{
.upm_raw_set_offset = &upm_slide_set_offset,
.upm_raw_set_scale = &upm_slide_set_scale,
.upm_raw_get_value = &upm_slide_get_value
};
const void* upm_slide_get_ft(upm_sensor_t sensor_type)
{
switch(sensor_type)
{
case UPM_SENSOR:
return &ft_gen;
case UPM_RAW:
return &ft_raw;
default:
return NULL;
}
}
void* upm_slide_init_str(const char* protocol, const char* params)
{
fprintf(stderr, "String initialization - not implemented, using ain0: %s\n", __FILENAME__);
return slide_init(0);
}
void upm_slide_close(void* dev)
{
slide_close((slide_context)dev);
}
const upm_sensor_descriptor_t upm_slide_get_descriptor()
{
/* Fill in the descriptor */
upm_sensor_descriptor_t usd;
usd.name = upm_slide_name;
usd.description = upm_slide_description;
usd.protocol_size = 1;
usd.protocol = upm_slide_protocol;
usd.category_size = 1;
usd.category = upm_slide_category;
return usd;
}
upm_result_t upm_slide_set_offset(const void* dev, float offset)
{
return slide_set_offset((slide_context)dev, offset);
}
upm_result_t upm_slide_set_scale(const void* dev, float scale)
{
return slide_set_scale((slide_context)dev, scale);
}
upm_result_t upm_slide_get_value(const void* dev, float *value)
{
return slide_get_volts((slide_context)dev, value);
}