From b781405a9583a34eb96e289b7f63f27ddbd28cf4 Mon Sep 17 00:00:00 2001 From: Noel Eck Date: Fri, 9 Sep 2016 14:57:33 -0700 Subject: [PATCH] ldt0028: Added piezo vibration sensor C source Added the C source for the light sensor with necessary changes to cmake, examples, docs. Signed-off-by: Noel Eck --- examples/c/CMakeLists.txt | 1 + examples/c/ldt0028.c | 95 ++++++++++++++++++++++++++ src/ldt0028/CMakeLists.txt | 13 ++-- src/ldt0028/ldt0028.c | 127 +++++++++++++++++++++++++++++++++++ src/ldt0028/ldt0028.h | 133 +++++++++++++++++++++++++++++++++++++ src/ldt0028/ldt0028_fti.c | 92 +++++++++++++++++++++++++ 6 files changed, 456 insertions(+), 5 deletions(-) create mode 100644 examples/c/ldt0028.c create mode 100644 src/ldt0028/ldt0028.c create mode 100644 src/ldt0028/ldt0028.h create mode 100644 src/ldt0028/ldt0028_fti.c diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index e59bf40a..534ba217 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -100,6 +100,7 @@ add_example (o2) add_example (emg) add_example (gsr) add_example (light) +add_example (ldt0028) # Custom examples add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) diff --git a/examples/c/ldt0028.c b/examples/c/ldt0028.c new file mode 100644 index 00000000..6acec84e --- /dev/null +++ b/examples/c/ldt0028.c @@ -0,0 +1,95 @@ +/* + * 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 "ldt0028.h" + +bool shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +int main() +{ + signal(SIGINT, sig_handler); + + //! [Interesting] + + // Instantiate a ldt0028 sensor on analog pin A0 + ldt0028_context sensor = ldt0028_init(0); + + if (!sensor) + { + printf("ldt0028_init() failed.\n"); + return -1; + } + + // Set the aref, scale, and offset + ldt0028_set_aref(sensor, 5.0); + ldt0028_set_scale(sensor, 1.0); + ldt0028_set_offset(sensor, -.1); + printf("aRef: %0.03f scale: %0.03f offset: %0.03f\n\n", + ldt0028_get_aref(sensor), + ldt0028_get_scale(sensor), + ldt0028_get_offset(sensor)); + + float normalized = 0.0; + float raw_volts = 0.0; + float volts = 0.0; + + float norm_base = 0.1; + + printf("Using normalized sensor output average == %f\n", norm_base); + + // Every half a second, sample the sensor output + while (shouldRun) + { + + ldt0028_get_normalized(sensor, &normalized); + ldt0028_get_raw_volts(sensor, &raw_volts); + ldt0028_get_volts(sensor, &volts); + + if (normalized > norm_base) + { + printf("Detected vibration!\n"); + printf("Normalized output: %0.03f, raw ldt0028 sensor output: %0.03f v " + "adjusted output: %0.03f v\n\n", normalized, raw_volts, volts); + } + + usleep(50000); + } + + //! [Interesting] + + printf("Exiting\n"); + + ldt0028_close(sensor); + + return 0; +} diff --git a/src/ldt0028/CMakeLists.txt b/src/ldt0028/CMakeLists.txt index bae23833..cf05e64e 100644 --- a/src/ldt0028/CMakeLists.txt +++ b/src/ldt0028/CMakeLists.txt @@ -1,5 +1,8 @@ -set (libname "ldt0028") -set (libdescription "upm ldt0028") -set (module_src ${libname}.cxx) -set (module_hpp ${libname}.hpp) -upm_module_init() +upm_mixed_module_init (NAME ldt0028 + DESCRIPTION "ldt0028 Piezo Vibration Sensor" + C_HDR ldt0028.h + C_SRC ldt0028.c + CPP_HDR ldt0028.hpp + CPP_SRC ldt0028.cxx + FTI_SRC ldt0028_fti.c + REQUIRES mraa) diff --git a/src/ldt0028/ldt0028.c b/src/ldt0028/ldt0028.c new file mode 100644 index 00000000..9b46746b --- /dev/null +++ b/src/ldt0028/ldt0028.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 "ldt0028.h" + +ldt0028_context ldt0028_init(int16_t pin) +{ + ldt0028_context dev = (ldt0028_context) malloc(sizeof(struct _ldt0028_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 ldt0028_close(ldt0028_context dev) +{ + mraa_aio_close(dev->aio); + free(dev); +} + +upm_result_t ldt0028_set_aref(const ldt0028_context dev, float aref) +{ + dev->m_aRef = aref; + return UPM_SUCCESS; +} + +upm_result_t ldt0028_set_scale(const ldt0028_context dev, float scale) +{ + dev->m_scale = scale; + return UPM_SUCCESS; +} + +upm_result_t ldt0028_set_offset(const ldt0028_context dev, float offset) +{ + dev->m_offset = offset; + return UPM_SUCCESS; +} + +float ldt0028_get_aref(const ldt0028_context dev) +{ + return dev->m_aRef; +} + +float ldt0028_get_scale(const ldt0028_context dev) +{ + return dev->m_scale; +} + +float ldt0028_get_offset(const ldt0028_context dev) +{ + return dev->m_offset; +} + +upm_result_t ldt0028_get_normalized(const ldt0028_context dev, float *value) +{ + *value = mraa_aio_read_float(dev->aio); + if (*value < 0) + return UPM_ERROR_OPERATION_FAILED; +} + +upm_result_t ldt0028_get_raw_volts(const ldt0028_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 ldt0028_get_volts(const ldt0028_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/ldt0028/ldt0028.h b/src/ldt0028/ldt0028.h new file mode 100644 index 00000000..cbd1c260 --- /dev/null +++ b/src/ldt0028/ldt0028.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 _ldt0028_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; +} *ldt0028_context; + +/** + * Initialize analog sensor + * @param pin Analog pin + * @return sensor context + */ +ldt0028_context ldt0028_init(int16_t pin); + +/** + * Analog sensor destructor + * @param sensor context pointer + */ +void ldt0028_close(ldt0028_context dev); + +/** + * Set ADC reference voltage + * @param dev sensor context pointer + * @param aref ADC reference voltage + * @return Function result code + */ +upm_result_t ldt0028_set_aref(const ldt0028_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 ldt0028_set_scale(const ldt0028_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 ldt0028_set_offset(const ldt0028_context dev, float offset); + +/** + * Get sensor aref + * @param dev sensor context pointer + * @return Sensor ADC reference voltage + */ +float ldt0028_get_aref(const ldt0028_context dev); + +/** + * Get sensor scale + * @param dev sensor context pointer + * @return Sensor scale + */ +float ldt0028_get_scale(const ldt0028_context dev); + +/** + * Get sensor offset + * @param dev sensor context pointer + * @return Sensor offset + */ +float ldt0028_get_offset(const ldt0028_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 ldt0028_get_normalized(const ldt0028_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 ldt0028_get_raw_volts(const ldt0028_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 ldt0028_get_volts(const ldt0028_context dev, float *value); +#ifdef __cplusplus +} +#endif diff --git a/src/ldt0028/ldt0028_fti.c b/src/ldt0028/ldt0028_fti.c new file mode 100644 index 00000000..635b3c78 --- /dev/null +++ b/src/ldt0028/ldt0028_fti.c @@ -0,0 +1,92 @@ +/* + * Author: 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 "ldt0028.h" +#include "upm_fti.h" + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_ldt0028_name[] = "LDT0028"; +const char upm_ldt0028_description[] = "Grove Piezo Vibration Sensor"; +const upm_protocol_t upm_ldt0028_protocol[] = {UPM_ANALOG}; +const upm_sensor_t upm_ldt0028_category[] = {UPM_VIBRATION}; + +// forward declarations +const void* upm_ldt0028_get_ft(upm_sensor_t sensor_type); +const void* upm_ldt0028_get_ft(upm_sensor_t sensor_type); +const upm_sensor_descriptor_t upm_ldt0028_get_descriptor(); +void* upm_ldt0028_init_name(); +void upm_ldt0028_close(void* dev); +upm_result_t upm_ldt0028_get_value(void* dev, float* vibval); + +const upm_sensor_descriptor_t upm_ldt0028_get_descriptor() { + upm_sensor_descriptor_t usd; + usd.name = upm_ldt0028_name; + usd.description = upm_ldt0028_description; + usd.protocol_size = 1; + usd.protocol = upm_ldt0028_protocol; + usd.category_size = 1; + usd.category = upm_ldt0028_category; + return usd; +} + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = &upm_ldt0028_init_name, + .upm_sensor_close = &upm_ldt0028_close, + .upm_sensor_get_descriptor = &upm_ldt0028_get_descriptor +}; + +static const upm_vibration_ft vft = +{ + .upm_vibration_get_value = upm_ldt0028_get_value +}; + +const void* upm_ldt0028_get_ft(upm_sensor_t sensor_type) { + if(sensor_type == UPM_SENSOR) { + return &ft; + } + + if(sensor_type == UPM_VIBRATION) { + return &vft; + } + return NULL; +} + +void* upm_ldt0028_init_name(){ + return NULL; +} + +void upm_ldt0028_close(void* dev) +{ + ldt0028_close((ldt0028_context)dev); +} + +upm_result_t upm_ldt0028_get_value(void* dev, float* vibval) +{ + return ldt0028_get_volts((ldt0028_context)dev, vibval); +}