mirror of
				https://github.com/eclipse/upm.git
				synced 2025-10-31 07:04:14 +03:00 
			
		
		
		
	guvas12d: C implementation; FTI; C++ wraps C
Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
		| @@ -33,7 +33,6 @@ bool shouldRun = true; | ||||
|  | ||||
| // analog voltage, usually 3.3 or 5.0 | ||||
| #define GUVAS12D_AREF   5.0 | ||||
| #define SAMPLES_PER_QUERY 1024 | ||||
|  | ||||
| void sig_handler(int signo) | ||||
| { | ||||
| @@ -46,8 +45,8 @@ int main() | ||||
|     signal(SIGINT, sig_handler); | ||||
|  | ||||
| //! [Interesting] | ||||
|   // The was tested with the Grove UV Sensor module. | ||||
|   // It has a sensing range from between 200-400nm.  It's strongest | ||||
|     // This was tested with the Grove UV Sensor module. | ||||
|     // It has a sensing range from between 240-370nm.  It's strongest | ||||
|     // response is around 320-360nm. | ||||
|  | ||||
|     // Instantiate a GUVAS12D on analog pin A0 | ||||
| @@ -57,9 +56,10 @@ int main() | ||||
|  | ||||
|     while (shouldRun) | ||||
|     { | ||||
|       cout << "AREF: " << GUVAS12D_AREF  | ||||
|            << ", Voltage value (higher means more UV): "  | ||||
|            << volts->value(GUVAS12D_AREF, SAMPLES_PER_QUERY) << endl; | ||||
|         cout << "Volts: " << volts->volts() | ||||
|              << ", Intensity: " << volts->intensity() | ||||
|              << " mW/m^2" | ||||
|              << endl; | ||||
|  | ||||
|         sleep(1); | ||||
|     } | ||||
|   | ||||
| @@ -133,6 +133,7 @@ add_example (uln200xa) | ||||
| add_example (mma7660) | ||||
| add_example (buzzer) | ||||
| add_example (ppd42ns) | ||||
| add_example (guvas12d) | ||||
|  | ||||
| # Custom examples | ||||
| add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) | ||||
|   | ||||
							
								
								
									
										89
									
								
								examples/c/guvas12d.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								examples/c/guvas12d.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,89 @@ | ||||
| /* | ||||
|  * 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 <stdio.h> | ||||
| #include <signal.h> | ||||
|  | ||||
| #include <upm_utilities.h> | ||||
| #include <guvas12d.h> | ||||
|  | ||||
| bool shouldRun = true; | ||||
|  | ||||
| // analog voltage, usually 3.3 or 5.0 | ||||
| #define GUVAS12D_AREF   5.0 | ||||
|  | ||||
| void sig_handler(int signo) | ||||
| { | ||||
|     if (signo == SIGINT) | ||||
|         shouldRun = false; | ||||
| } | ||||
|  | ||||
| int main() | ||||
| { | ||||
|     signal(SIGINT, sig_handler); | ||||
|  | ||||
| //! [Interesting] | ||||
|     // This was tested with the Grove UV Sensor module. | ||||
|     // It has a sensing range from between 240-370nm.  It's strongest | ||||
|     // response is around 320-360nm. | ||||
|  | ||||
|     // Instantiate a GUVAS12D on analog pin A0 | ||||
|     guvas12d_context uv = guvas12d_init(0, GUVAS12D_AREF); | ||||
|  | ||||
|     if (!uv) | ||||
|     { | ||||
|         printf("guvas12d_init() failed\n"); | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     // The higher the voltage the more intense the UV radiation. | ||||
|     while (shouldRun) | ||||
|     { | ||||
|         float volts = 0; | ||||
|         float intensity = 0; | ||||
|  | ||||
|         if (guvas12d_get_volts(uv, &volts)) | ||||
|         { | ||||
|             printf("guvas12d_get_volts() failed\n"); | ||||
|             return 1; | ||||
|         } | ||||
|  | ||||
|         if (guvas12d_get_intensity(uv, &intensity)) | ||||
|         { | ||||
|             printf("guvas12d_get_intensity() failed\n"); | ||||
|             return 1; | ||||
|         } | ||||
|  | ||||
|         printf("Volts: %f, Intensity %f mW/m^2\n", volts, intensity); | ||||
|  | ||||
|         upm_delay(1); | ||||
|     } | ||||
|  | ||||
|     printf("Exiting\n"); | ||||
|  | ||||
|     guvas12d_close(uv); | ||||
| //! [Interesting] | ||||
|     return 0; | ||||
| } | ||||
| @@ -1,6 +1,7 @@ | ||||
| /* | ||||
|  * Author: Stefan Andritoiu <stefan.andritoiu@intel.com> | ||||
|  * Copyright (c) 2015 Intel Corporation. | ||||
|  *         Jon Trulson <jtrulson@ics.com> | ||||
|  * Copyright (c) 2015-2016 Intel Corporation. | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining | ||||
|  * a copy of this software and associated documentation files (the | ||||
| @@ -25,18 +26,20 @@ | ||||
| public class GUVAS12DSample { | ||||
|     // analog voltage, usually 3.3 or 5.0 | ||||
|     private static final float GUVAS12D_AREF = 5; | ||||
| 	private static final int SAMPLES_PER_QUERY = 1024; | ||||
|  | ||||
|     public static void main(String[] args) throws InterruptedException { | ||||
|         // ! [Interesting] | ||||
| 		// Instantiate a GUVAS12D on analog pin A3 | ||||
| 		upm_guvas12d.GUVAS12D volts = new upm_guvas12d.GUVAS12D(3); | ||||
|         // Instantiate a GUVAS12D on analog pin A0 | ||||
|         upm_guvas12d.GUVAS12D volts = new upm_guvas12d.GUVAS12D(0, | ||||
|                                                                 GUVAS12D_AREF); | ||||
|  | ||||
| 		while (true) { | ||||
| 			float value = volts.value(GUVAS12D_AREF, SAMPLES_PER_QUERY); | ||||
|  | ||||
| 			System.out.println("AREF: " + GUVAS12D_AREF | ||||
| 					+ ", Voltage value (higher means more UV): " + value); | ||||
|         while (true) | ||||
|         { | ||||
|             System.out.println("Volts: " | ||||
|                                + volts.volts() | ||||
|                                + ", Intensity: " | ||||
|                                + volts.intensity() | ||||
|                                + " mW/m^2"); | ||||
|  | ||||
|             Thread.sleep(1000); | ||||
|         } | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| /* | ||||
| * Author: Zion Orent <zorent@ics.com> | ||||
| * Copyright (c) 2014 Intel Corporation. | ||||
| *         Jon Trulson <jtrulson@ics.com> | ||||
| * Copyright (c) 2014-2016 Intel Corporation. | ||||
| * | ||||
| * Permission is hereby granted, free of charge, to any person obtaining | ||||
| * a copy of this software and associated documentation files (the | ||||
| @@ -24,27 +25,23 @@ | ||||
|  | ||||
| var UVSensor = require('jsupm_guvas12d'); | ||||
|  | ||||
| // Instantiate a UV sensor on analog pin A0 | ||||
| var myUVSensor = new UVSensor.GUVAS12D(0); | ||||
|  | ||||
| // analog voltage, usually 3.3 or 5.0 | ||||
| var g_GUVAS12D_AREF = 5.0; | ||||
| var g_SAMPLES_PER_QUERY = 1024; | ||||
|  | ||||
| // Instantiate a UV sensor on analog pin A0 | ||||
| var myUVSensor = new UVSensor.GUVAS12D(0, g_GUVAS12D_AREF); | ||||
|  | ||||
| setInterval(function() | ||||
| { | ||||
| 	var outputStr = "AREF: " + g_GUVAS12D_AREF  | ||||
| 					+ ", Voltage value (higher means more UV): " | ||||
| 					+ roundNum(myUVSensor.value(g_GUVAS12D_AREF, g_SAMPLES_PER_QUERY), 6); | ||||
|     var outputStr = "Volts: " | ||||
|         + myUVSensor.volts() | ||||
| 	+ ", Intensity: " | ||||
|         + myUVSensor.intensity() | ||||
|         + " mW/m^2"; | ||||
|  | ||||
|     console.log(outputStr); | ||||
| }, 1000); | ||||
|  | ||||
| function roundNum(num, decimalPlaces) | ||||
| { | ||||
| 	var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000)); | ||||
| 	return (Math.round((num + extraNum) * (Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces)); | ||||
| } | ||||
|  | ||||
| // Print message when exiting | ||||
| process.on('SIGINT', function() | ||||
| { | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| #!/usr/bin/python | ||||
| # Author: Zion Orent <zorent@ics.com> | ||||
| # Copyright (c) 2015 Intel Corporation. | ||||
| #         Jon Trulson <jtrulson@ics.com> | ||||
| # Copyright (c) 2015-2016 Intel Corporation. | ||||
| # | ||||
| # Permission is hereby granted, free of charge, to any person obtaining | ||||
| # a copy of this software and associated documentation files (the | ||||
| @@ -26,12 +27,11 @@ import time, sys, signal, atexit | ||||
| from upm import pyupm_guvas12d as upmUV | ||||
|  | ||||
| def main(): | ||||
|     # Instantiate a UV sensor on analog pin A0 | ||||
|     myUVSensor = upmUV.GUVAS12D(0); | ||||
|  | ||||
|     # analog voltage, usually 3.3 or 5.0 | ||||
|     GUVAS12D_AREF = 5.0; | ||||
|     SAMPLES_PER_QUERY = 1024; | ||||
|  | ||||
|     # Instantiate a UV sensor on analog pin A0 | ||||
|     myUVSensor = upmUV.GUVAS12D(0, GUVAS12D_AREF); | ||||
|  | ||||
|     ## Exit handlers ## | ||||
|     # This function stops python from printing a stacktrace when you hit control-C | ||||
| @@ -48,10 +48,8 @@ def main(): | ||||
|     signal.signal(signal.SIGINT, SIGINTHandler) | ||||
|  | ||||
|     while(1): | ||||
|         s = ("AREF:  {0}, " | ||||
|         "Voltage value (higher means more UV): " | ||||
|         "{1}".format(GUVAS12D_AREF, | ||||
|         myUVSensor.value(GUVAS12D_AREF, SAMPLES_PER_QUERY))) | ||||
|         s = ("Volts:  {0}, Intensity: {1} mW/m^2".format(myUVSensor.volts(), | ||||
|                                                          myUVSensor.intensity())) | ||||
|         print(s) | ||||
|         time.sleep(1) | ||||
|  | ||||
|   | ||||
| @@ -1,5 +1,9 @@ | ||||
| set (libname "guvas12d") | ||||
| set (libdescription "Guvas12d UV sensor module") | ||||
| set (module_src ${libname}.cxx) | ||||
| set (module_hpp ${libname}.hpp) | ||||
| upm_module_init() | ||||
| upm_mixed_module_init (NAME guvas12d | ||||
|     DESCRIPTION "Guvas12d analog UV sensor" | ||||
|     C_HDR guvas12d.h | ||||
|     C_SRC guvas12d.c | ||||
|     CPP_HDR guvas12d.hpp | ||||
|     CPP_SRC guvas12d.cxx | ||||
|     FTI_SRC guvas12d_fti.c | ||||
|     CPP_WRAPS_C | ||||
|     REQUIRES mraa) | ||||
|   | ||||
							
								
								
									
										128
									
								
								src/guvas12d/guvas12d.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										128
									
								
								src/guvas12d/guvas12d.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,128 @@ | ||||
| /* | ||||
|  * 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 <assert.h> | ||||
| #include <string.h> | ||||
| #include <upm_utilities.h> | ||||
|  | ||||
| #include "guvas12d.h" | ||||
|  | ||||
| guvas12d_context guvas12d_init(int pin, float aref) | ||||
| { | ||||
|     guvas12d_context dev = | ||||
|         (guvas12d_context)malloc(sizeof(struct _guvas12d_context)); | ||||
|  | ||||
|     if (!dev) | ||||
|         return NULL; | ||||
|  | ||||
|     memset((void *)dev, 0, sizeof(struct _guvas12d_context)); | ||||
|  | ||||
|     // make sure MRAA is initialized | ||||
|     int mraa_rv; | ||||
|     if ((mraa_rv = mraa_init()) != MRAA_SUCCESS) | ||||
|     { | ||||
|         printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv); | ||||
|         guvas12d_close(dev); | ||||
|         return NULL; | ||||
|     } | ||||
|  | ||||
|     // initialize the MRAA context | ||||
|  | ||||
|     if (!(dev->aio = mraa_aio_init(pin))) | ||||
|     { | ||||
|         printf("%s: mraa_aio_init failed.\n", __FUNCTION__); | ||||
|         guvas12d_close(dev); | ||||
|  | ||||
|         return NULL; | ||||
|     } | ||||
|  | ||||
|     dev->ares = (float)((1 << mraa_aio_get_bit(dev->aio)) - 1); | ||||
|     dev->aref = aref; | ||||
|     dev->scale = 1.0; | ||||
|     dev->offset = 0.0; | ||||
|  | ||||
|     return dev; | ||||
| } | ||||
|  | ||||
| void guvas12d_close(guvas12d_context dev) | ||||
| { | ||||
|     assert(dev != NULL); | ||||
|  | ||||
|     if (dev->aio) | ||||
|         mraa_aio_close(dev->aio); | ||||
|  | ||||
|     free(dev); | ||||
| } | ||||
|  | ||||
| upm_result_t guvas12d_get_volts(const guvas12d_context dev, float *volts) | ||||
| { | ||||
|     assert(dev != NULL); | ||||
|  | ||||
|     int val; | ||||
|  | ||||
|     val = mraa_aio_read(dev->aio); | ||||
|     if (val < 0) | ||||
|     { | ||||
|         printf("%s: mraa_aio_read() failed\n", __FUNCTION__); | ||||
|         return UPM_ERROR_OPERATION_FAILED; | ||||
|     } | ||||
|     *volts = (float)val * (dev->aref / dev->ares); | ||||
|  | ||||
|     return UPM_SUCCESS; | ||||
| } | ||||
|  | ||||
| upm_result_t guvas12d_get_intensity(const guvas12d_context dev, | ||||
|                                     float *intensity) | ||||
| { | ||||
|     assert(dev != NULL); | ||||
|  | ||||
|     float volts; | ||||
|     if (guvas12d_get_volts(dev, &volts)) | ||||
|     { | ||||
|         printf("%s: guvas12d_get_volts() failed\n", __FUNCTION__); | ||||
|         return UPM_ERROR_OPERATION_FAILED; | ||||
|     } | ||||
|  | ||||
|     // Seeed magic number 307.0 | ||||
|     *intensity = volts * 307.0; | ||||
|  | ||||
|     *intensity = *intensity * dev->scale + (dev->offset * dev->scale); | ||||
|  | ||||
|     return UPM_SUCCESS; | ||||
| } | ||||
|  | ||||
| void guvas12d_set_offset(const guvas12d_context dev, float offset) | ||||
| { | ||||
|     assert(dev != NULL); | ||||
|  | ||||
|     dev->offset = offset; | ||||
| } | ||||
|  | ||||
| void guvas12d_set_scale(const guvas12d_context dev, float scale) | ||||
| { | ||||
|     assert(dev != NULL); | ||||
|  | ||||
|     dev->scale = scale; | ||||
| } | ||||
| @@ -1,6 +1,6 @@ | ||||
| /* | ||||
|  * Author: Jon Trulson <jtrulson@ics.com> | ||||
|  * Copyright (c) 2014 Intel Corporation. | ||||
|  * Copyright (c) 2014-2016 Intel Corporation. | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining | ||||
|  * a copy of this software and associated documentation files (the | ||||
| @@ -31,36 +31,63 @@ | ||||
| using namespace upm; | ||||
| using namespace std; | ||||
|  | ||||
| GUVAS12D::GUVAS12D(int pin) | ||||
| GUVAS12D::GUVAS12D(int pin, float aref) : | ||||
|     m_guvas12d(guvas12d_init(pin, aref)) | ||||
| { | ||||
|   if ( !(m_aio = mraa_aio_init(pin)) ) | ||||
|     if (!m_guvas12d) | ||||
|     { | ||||
|       throw std::invalid_argument(std::string(__FUNCTION__) + | ||||
|                                   ": mraa_aio_init() failed, invalid pin?"); | ||||
|       throw std::runtime_error(std::string(__FUNCTION__) + | ||||
|                                ": guvas12d_init() failed"); | ||||
|       return; | ||||
|     } | ||||
| } | ||||
|  | ||||
| GUVAS12D::~GUVAS12D() | ||||
| { | ||||
|   mraa_aio_close(m_aio); | ||||
|     guvas12d_close(m_guvas12d); | ||||
| } | ||||
|  | ||||
| float GUVAS12D::value(float aref, unsigned int samples) | ||||
| { | ||||
|   int val; | ||||
|   unsigned long sum = 0; | ||||
|     (void)(samples); // unused, this method is deprecated. | ||||
|  | ||||
|   for (unsigned int i=0; i<samples; i++) | ||||
|     { | ||||
|       val = mraa_aio_read(m_aio); | ||||
|       if (val == -1) return -1; | ||||
|       sum += val; | ||||
|       usleep(2000); | ||||
|     // this is a hack, but this function should go away anyway | ||||
|     if (aref != m_guvas12d->aref) | ||||
|         m_guvas12d->aref = aref; | ||||
|  | ||||
|     return volts(); | ||||
| } | ||||
|  | ||||
|   sum = sum / samples; | ||||
|   float volts = (float)sum * aref / 1024.0; | ||||
| float GUVAS12D::volts() | ||||
| { | ||||
|     float volts; | ||||
|     if (guvas12d_get_volts(m_guvas12d, &volts)) | ||||
|     { | ||||
|       throw std::runtime_error(std::string(__FUNCTION__) + | ||||
|                                ": guvas12d_get_volts() failed"); | ||||
|     } | ||||
|  | ||||
|     return volts; | ||||
| } | ||||
|  | ||||
| float GUVAS12D::intensity() | ||||
| { | ||||
|     float i; | ||||
|     if (guvas12d_get_intensity(m_guvas12d, &i)) | ||||
|     { | ||||
|       throw std::runtime_error(std::string(__FUNCTION__) + | ||||
|                                ": guvas12d_get_intensity() failed"); | ||||
|     } | ||||
|  | ||||
|     return i; | ||||
| } | ||||
|  | ||||
| void GUVAS12D::setScale(float scale) | ||||
| { | ||||
|     guvas12d_set_scale(m_guvas12d, scale); | ||||
| } | ||||
|  | ||||
| void GUVAS12D::setOffset(float offset) | ||||
| { | ||||
|     guvas12d_set_offset(m_guvas12d, offset); | ||||
| } | ||||
|   | ||||
							
								
								
									
										108
									
								
								src/guvas12d/guvas12d.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								src/guvas12d/guvas12d.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,108 @@ | ||||
| /* | ||||
|  * 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. | ||||
|  */ | ||||
| #pragma once | ||||
|  | ||||
| #include <stdlib.h> | ||||
| #include <stdio.h> | ||||
| #include <upm.h> | ||||
| #include <mraa/aio.h> | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
|  | ||||
|     /** | ||||
|      * @file guvas12d.h | ||||
|      * @library guvas12d | ||||
|      * @brief C API for the guvas12d driver | ||||
|      * | ||||
|      * @include guvas12d.c | ||||
|      */ | ||||
|  | ||||
|     /** | ||||
|      * Device context | ||||
|      */ | ||||
|     typedef struct _guvas12d_context { | ||||
|         mraa_aio_context    aio; | ||||
|  | ||||
|         // ADC reference voltage | ||||
|         float               aref; | ||||
|         // ADC resolution | ||||
|         float               ares; | ||||
|  | ||||
|         float               scale; | ||||
|         float               offset; | ||||
|     } *guvas12d_context; | ||||
|  | ||||
|     /** | ||||
|      * GUVA-S12D UV sensor constructor | ||||
|      * | ||||
|      * @param pin Analog pin to use | ||||
|      */ | ||||
|     guvas12d_context guvas12d_init(int pin, float aref); | ||||
|  | ||||
|     /** | ||||
|      * GUVAS12D destructor | ||||
|      */ | ||||
|     void guvas12d_close(guvas12d_context dev); | ||||
|  | ||||
|     /** | ||||
|      * Gets the voltage value from the sensor | ||||
|      * | ||||
|      * @param volts Pointer to average voltage reading | ||||
|      * @return UPM status. | ||||
|      */ | ||||
|     upm_result_t guvas12d_get_volts(const guvas12d_context dev, float *volts); | ||||
|  | ||||
|     /** | ||||
|      * Gets the illumination intensity in mW/m^2 | ||||
|      * | ||||
|      * @param volts Pointer to average voltage reading | ||||
|      * @return UPM status. | ||||
|      * @return illumination intensity in mW/m^2 | ||||
|      */ | ||||
|     upm_result_t guvas12d_get_intensity(const guvas12d_context dev, | ||||
|                                         float *intensity); | ||||
|  | ||||
|     /** | ||||
|      * Set sensor offset.  This offset is applied to the illumination | ||||
|      * intensity value before scaling.  Default is 0.0. | ||||
|      * | ||||
|      * @param dev sensor context pointer | ||||
|      * @param offset Offset to apply. | ||||
|      */ | ||||
|     void guvas12d_set_offset(const guvas12d_context dev, float offset); | ||||
|  | ||||
|     /** | ||||
|      * Set sensor scale.  This offset is applied to the illumination | ||||
|      * intensity value before scaling.  Default is 1.0. | ||||
|      * | ||||
|      * @param dev sensor context pointer | ||||
|      * @param scale Scale to apply. | ||||
|      */ | ||||
|     void guvas12d_set_scale(const guvas12d_context dev, float scale); | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
| @@ -1,6 +1,6 @@ | ||||
| /* | ||||
|  * Author: Jon Trulson <jtrulson@ics.com> | ||||
|  * Copyright (c) 2014 Intel Corporation. | ||||
|  * Copyright (c) 2014-2016 Intel Corporation. | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining | ||||
|  * a copy of this software and associated documentation files (the | ||||
| @@ -23,8 +23,7 @@ | ||||
|  */ | ||||
| #pragma once | ||||
|  | ||||
| #include <string> | ||||
| #include <mraa/aio.h> | ||||
| #include <guvas12d.h> | ||||
|  | ||||
| namespace upm { | ||||
|     /** | ||||
| @@ -52,26 +51,68 @@ namespace upm { | ||||
|      */ | ||||
|     class GUVAS12D { | ||||
|     public: | ||||
|  | ||||
|         /** | ||||
|          * GUVA-S12D UV sensor constructor | ||||
|          * | ||||
|          * @param pin Analog pin to use | ||||
|          * @param aref Analog reference voltage to use | ||||
|          */ | ||||
|     GUVAS12D(int pin); | ||||
|         GUVAS12D(int pin, float aref=5.0); | ||||
|  | ||||
|         /** | ||||
|          * GUVAS12D destructor | ||||
|          */ | ||||
|         ~GUVAS12D(); | ||||
|  | ||||
|         /** | ||||
|          * @deprecated This method is being replaced by the volts() | ||||
|          * and illumination() methods. | ||||
|          * | ||||
|          * Gets the average voltage value from the sensor | ||||
|          * | ||||
|          * @param aref Reference voltage in use (usually 5.0 V or 3.3 V) | ||||
|      * @param samples Number of samples to average over | ||||
|          * @param samples Number of samples to average over (currently | ||||
|          * ignored) | ||||
|          * @return Average voltage reading | ||||
|          * | ||||
|          */ | ||||
|         float value(float aref, unsigned int samples); | ||||
|  | ||||
|         /** | ||||
|          * Gets the voltage value from the sensor | ||||
|          * | ||||
|          * @return Voltage reading | ||||
|          * | ||||
|          */ | ||||
|         float volts(); | ||||
|  | ||||
|         /** | ||||
|          * Gets the computed illumination intensity from the sensor in | ||||
|          * mW/m^2. | ||||
|          * | ||||
|          * @return Intensity over the sensitive wavelengths in mW/m^2 | ||||
|          * | ||||
|          */ | ||||
|         float intensity(); | ||||
|  | ||||
|         /** | ||||
|          * Set sensor scale.  This scale is applied to the intensity value | ||||
|          * before the offset is applied.  Default is 1.0. | ||||
|          * | ||||
|          * @param scale Scale to apply. | ||||
|          */ | ||||
|         void setScale(float scale); | ||||
|  | ||||
|         /** | ||||
|          * Set sensor offset.  This offset is applied to the intensity | ||||
|          * value before scaling.  Default is 0.0. | ||||
|          * | ||||
|          * @param offset Offset to apply. | ||||
|          */ | ||||
|         void setOffset(float offset); | ||||
|  | ||||
|     private: | ||||
|     mraa_aio_context m_aio; | ||||
|         guvas12d_context m_guvas12d; | ||||
|     }; | ||||
| } | ||||
|   | ||||
							
								
								
									
										119
									
								
								src/guvas12d/guvas12d_fti.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										119
									
								
								src/guvas12d/guvas12d_fti.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,119 @@ | ||||
| /* | ||||
|  * 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 <string.h> | ||||
| #include <stdlib.h> | ||||
|  | ||||
| #include "guvas12d.h" | ||||
| #include "fti/upm_sensor.h" | ||||
| #include "fti/upm_voltage.h" | ||||
|  | ||||
| /** | ||||
|  * This file implements the Function Table Interface (FTI) for this sensor | ||||
|  */ | ||||
|  | ||||
| const char upm_guvas12d_name[] = "GUVAS12D"; | ||||
| const char upm_guvas12d_description[] = "Analog UV sensor"; | ||||
| const upm_protocol_t upm_guvas12d_protocol[] = {UPM_ANALOG}; | ||||
| const upm_sensor_t upm_guvas12d_category[] = {UPM_VOLTAGE}; | ||||
|  | ||||
| // forward declarations | ||||
| void* upm_guvas12d_init_str(const char* protocol, const char* params); | ||||
| void upm_guvas12d_close(void* dev); | ||||
| const void* upm_guvas12d_get_ft(upm_sensor_t sensor_type); | ||||
| const upm_sensor_descriptor_t upm_guvas12d_get_descriptor(); | ||||
| upm_result_t upm_guvas12d_set_offset(const void* dev, float offset); | ||||
| upm_result_t upm_guvas12d_set_scale(const void* dev, float scale); | ||||
| upm_result_t upm_guvas12d_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_guvas12d_init_str, | ||||
|     .upm_sensor_close = &upm_guvas12d_close, | ||||
|     .upm_sensor_get_descriptor = &upm_guvas12d_get_descriptor | ||||
| }; | ||||
|  | ||||
| /* 2. VOLTAGE function table */ | ||||
| static const upm_voltage_ft ft_voltage = | ||||
| { | ||||
|     .upm_voltage_set_offset = &upm_guvas12d_set_offset, | ||||
|     .upm_voltage_set_scale = &upm_guvas12d_set_scale, | ||||
|     .upm_voltage_get_value = &upm_guvas12d_get_value | ||||
| }; | ||||
|  | ||||
| const void* upm_guvas12d_get_ft(upm_sensor_t sensor_type) | ||||
| { | ||||
|     switch(sensor_type) | ||||
|     { | ||||
|         case UPM_SENSOR: | ||||
|             return &ft_gen; | ||||
|         case UPM_VOLTAGE: | ||||
|             return &ft_voltage; | ||||
|         default: | ||||
|             return NULL; | ||||
|     } | ||||
| } | ||||
|  | ||||
| void* upm_guvas12d_init_str(const char* protocol, const char* params) | ||||
| { | ||||
|     return NULL; | ||||
| } | ||||
|  | ||||
| void upm_guvas12d_close(void* dev) | ||||
| { | ||||
|     guvas12d_close((guvas12d_context)dev); | ||||
| } | ||||
|  | ||||
| const upm_sensor_descriptor_t upm_guvas12d_get_descriptor() | ||||
| { | ||||
|     /* Fill in the descriptor */ | ||||
|     upm_sensor_descriptor_t usd; | ||||
|     usd.name = upm_guvas12d_name; | ||||
|     usd.description = upm_guvas12d_description; | ||||
|     usd.protocol_size = 1; | ||||
|     usd.protocol = upm_guvas12d_protocol; | ||||
|     usd.category_size = 1; | ||||
|     usd.category = upm_guvas12d_category; | ||||
|  | ||||
|     return usd; | ||||
| } | ||||
|  | ||||
| upm_result_t upm_guvas12d_set_offset(const void* dev, float offset) | ||||
| { | ||||
|     guvas12d_set_offset((guvas12d_context)dev, offset); | ||||
|     return UPM_SUCCESS; | ||||
| } | ||||
|  | ||||
| upm_result_t upm_guvas12d_set_scale(const void* dev, float scale) | ||||
| { | ||||
|     guvas12d_set_scale((guvas12d_context)dev, scale); | ||||
|     return UPM_SUCCESS; | ||||
| } | ||||
|  | ||||
| upm_result_t upm_guvas12d_get_value(const void* dev, float *value) | ||||
| { | ||||
|     return guvas12d_get_volts((guvas12d_context)dev, value); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Jon Trulson
					Jon Trulson