From e2bec11c2370560b8cd781fe61beadf138eb683f Mon Sep 17 00:00:00 2001 From: Noel Eck Date: Fri, 9 Sep 2016 16:24:35 -0700 Subject: [PATCH] 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 --- examples/c/CMakeLists.txt | 1 + examples/c/flex.c | 86 ++++++++++++++++++++++++ src/flex/CMakeLists.txt | 13 ++-- src/flex/flex.c | 127 ++++++++++++++++++++++++++++++++++++ src/flex/flex.h | 133 ++++++++++++++++++++++++++++++++++++++ src/flex/flex_fti.c | 119 ++++++++++++++++++++++++++++++++++ 6 files changed, 474 insertions(+), 5 deletions(-) create mode 100644 examples/c/flex.c create mode 100644 src/flex/flex.c create mode 100644 src/flex/flex.h create mode 100644 src/flex/flex_fti.c diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 5e08cc8d..0ed3c077 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -102,6 +102,7 @@ add_example (gsr) add_example (light) add_example (ldt0028) add_example (joystick12) +add_example (flex) # Custom examples add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) diff --git a/examples/c/flex.c b/examples/c/flex.c new file mode 100644 index 00000000..32b22a3b --- /dev/null +++ b/examples/c/flex.c @@ -0,0 +1,86 @@ +/* + * Author: Jon Trulson + * 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 "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; +} diff --git a/src/flex/CMakeLists.txt b/src/flex/CMakeLists.txt index e1410530..20415259 100644 --- a/src/flex/CMakeLists.txt +++ b/src/flex/CMakeLists.txt @@ -1,5 +1,8 @@ -set (libname "flex") -set (libdescription "upm flex sensor module") -set (module_src ${libname}.cxx) -set (module_hpp ${libname}.hpp) -upm_module_init() +upm_mixed_module_init (NAME flex + DESCRIPTION "upm flex sensor module" + C_HDR flex.h + C_SRC flex.c + CPP_HDR flex.hpp + CPP_SRC flex.cxx + FTI_SRC flex_fti.c + REQUIRES mraa) diff --git a/src/flex/flex.c b/src/flex/flex.c new file mode 100644 index 00000000..ca44a8e1 --- /dev/null +++ b/src/flex/flex.c @@ -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 +#include + +#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; +} diff --git a/src/flex/flex.h b/src/flex/flex.h new file mode 100644 index 00000000..d143dbd4 --- /dev/null +++ b/src/flex/flex.h @@ -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 diff --git a/src/flex/flex_fti.c b/src/flex/flex_fti.c new file mode 100644 index 00000000..848ed67f --- /dev/null +++ b/src/flex/flex_fti.c @@ -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 +#include + +#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); +}