flex: Added flex sensor C source

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

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck 2016-09-09 16:24:35 -07:00
parent bec533d8bd
commit e2bec11c23
6 changed files with 474 additions and 5 deletions

View File

@ -102,6 +102,7 @@ add_example (gsr)
add_example (light) add_example (light)
add_example (ldt0028) add_example (ldt0028)
add_example (joystick12) add_example (joystick12)
add_example (flex)
# 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)

86
examples/c/flex.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 "flex.h"
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main()
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a flex sensor on analog pin A0
flex_context sensor = flex_init(0);
if (!sensor)
{
printf("flex_init() failed.\n");
return -1;
}
// Set the aref, scale, and offset
flex_set_aref(sensor, 5.0);
flex_set_scale(sensor, 1.0);
flex_set_offset(sensor, -.1);
printf("aRef: %0.03f scale: %0.03f offset: %0.03f\n\n",
flex_get_aref(sensor),
flex_get_scale(sensor),
flex_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;
flex_get_normalized(sensor, &normalized);
flex_get_raw_volts(sensor, &raw_volts);
flex_get_volts(sensor, &volts);
printf("Normalized output: %0.03f, raw flex sensor output: %0.03f v "
"adjusted output: %0.03f v\n", normalized, raw_volts, volts);
usleep(500000);
}
//! [Interesting]
printf("Exiting\n");
flex_close(sensor);
return 0;
}

View File

@ -1,5 +1,8 @@
set (libname "flex") upm_mixed_module_init (NAME flex
set (libdescription "upm flex sensor module") DESCRIPTION "upm flex sensor module"
set (module_src ${libname}.cxx) C_HDR flex.h
set (module_hpp ${libname}.hpp) C_SRC flex.c
upm_module_init() CPP_HDR flex.hpp
CPP_SRC flex.cxx
FTI_SRC flex_fti.c
REQUIRES mraa)

127
src/flex/flex.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 "flex.h"
flex_context flex_init(int16_t pin)
{
flex_context dev = (flex_context) malloc(sizeof(struct _flex_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 flex_close(flex_context dev)
{
mraa_aio_close(dev->aio);
free(dev);
}
upm_result_t flex_set_aref(const flex_context dev, float aref)
{
dev->m_aRef = aref;
return UPM_SUCCESS;
}
upm_result_t flex_set_scale(const flex_context dev, float scale)
{
dev->m_scale = scale;
return UPM_SUCCESS;
}
upm_result_t flex_set_offset(const flex_context dev, float offset)
{
dev->m_offset = offset;
return UPM_SUCCESS;
}
float flex_get_aref(const flex_context dev)
{
return dev->m_aRef;
}
float flex_get_scale(const flex_context dev)
{
return dev->m_scale;
}
float flex_get_offset(const flex_context dev)
{
return dev->m_offset;
}
upm_result_t flex_get_normalized(const flex_context dev, float *value)
{
*value = mraa_aio_read_float(dev->aio);
if (*value < 0)
return UPM_ERROR_OPERATION_FAILED;
}
upm_result_t flex_get_raw_volts(const flex_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 flex_get_volts(const flex_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;
}

133
src/flex/flex.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 _flex_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;
} *flex_context;
/**
* Initialize analog sensor
* @param pin Analog pin
* @return sensor context
*/
flex_context flex_init(int16_t pin);
/**
* Analog sensor destructor
* @param sensor context pointer
*/
void flex_close(flex_context dev);
/**
* Set ADC reference voltage
* @param dev sensor context pointer
* @param aref ADC reference voltage
* @return Function result code
*/
upm_result_t flex_set_aref(const flex_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 flex_set_scale(const flex_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 flex_set_offset(const flex_context dev, float offset);
/**
* Get sensor aref
* @param dev sensor context pointer
* @return Sensor ADC reference voltage
*/
float flex_get_aref(const flex_context dev);
/**
* Get sensor scale
* @param dev sensor context pointer
* @return Sensor scale
*/
float flex_get_scale(const flex_context dev);
/**
* Get sensor offset
* @param dev sensor context pointer
* @return Sensor offset
*/
float flex_get_offset(const flex_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 flex_get_normalized(const flex_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 flex_get_raw_volts(const flex_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 flex_get_volts(const flex_context dev, float *value);
#ifdef __cplusplus
}
#endif

119
src/flex/flex_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 "flex.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_flex_name[] = "flex";
const char upm_flex_description[] = "Analog flex sensor";
const upm_protocol_t upm_flex_protocol[] = {UPM_ANALOG};
const upm_sensor_t upm_flex_category[] = {UPM_RAW};
// forward declarations
const void* upm_flex_get_ft(upm_sensor_t sensor_type);
void* upm_flex_init_str(const char* protocol, const char* params);
void upm_flex_close(void* dev);
const upm_sensor_descriptor_t upm_flex_get_descriptor();
upm_result_t upm_flex_set_offset(const void* dev, float offset);
upm_result_t upm_flex_set_scale(const void* dev, float scale);
upm_result_t upm_flex_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_flex_init_str,
.upm_sensor_close = &upm_flex_close,
.upm_sensor_get_descriptor = &upm_flex_get_descriptor
};
/* 2. RAW function table */
static const upm_raw_ft ft_raw =
{
.upm_raw_set_offset = &upm_flex_set_offset,
.upm_raw_set_scale = &upm_flex_set_scale,
.upm_raw_get_value = &upm_flex_get_value
};
const void* upm_flex_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_flex_init_str(const char* protocol, const char* params)
{
fprintf(stderr, "String initialization - not implemented, using ain0: %s\n", __FILENAME__);
return flex_init(0);
}
void upm_flex_close(void* dev)
{
flex_close((flex_context)dev);
}
const upm_sensor_descriptor_t upm_flex_get_descriptor()
{
/* Fill in the descriptor */
upm_sensor_descriptor_t usd;
usd.name = upm_flex_name;
usd.description = upm_flex_description;
usd.protocol_size = 1;
usd.protocol = upm_flex_protocol;
usd.category_size = 1;
usd.category = upm_flex_category;
return usd;
}
upm_result_t upm_flex_set_offset(const void* dev, float offset)
{
return flex_set_offset((flex_context)dev, offset);
}
upm_result_t upm_flex_set_scale(const void* dev, float scale)
{
return flex_set_scale((flex_context)dev, scale);
}
upm_result_t upm_flex_get_value(const void* dev, float *value)
{
return flex_get_volts((flex_context)dev, value);
}