mirror of
				https://github.com/eclipse/upm.git
				synced 2025-11-04 00:54:21 +03:00 
			
		
		
		
	GP2Y0A: Adding C source
Signed-off-by: Abhishek Malik <abhishek.malik@intel.com>
This commit is contained in:
		@@ -1,5 +1,9 @@
 | 
			
		||||
set (libname "gp2y0a")
 | 
			
		||||
set (libdescription "upm gp2y0a family of IR distance detectors")
 | 
			
		||||
set (module_src ${libname}.cxx)
 | 
			
		||||
set (module_hpp ${libname}.hpp)
 | 
			
		||||
upm_module_init()
 | 
			
		||||
upm_mixed_module_init (NAME gp2y0a
 | 
			
		||||
    DESCRIPTION "upm gp2y0a family of IR distance detectors"
 | 
			
		||||
    C_HDR gp2y0a.h
 | 
			
		||||
    C_SRC gp2y0a.c
 | 
			
		||||
    CPP_HDR gp2y0a.hpp
 | 
			
		||||
    CPP_SRC gp2y0a.cxx
 | 
			
		||||
    FTI_SRC gp2y0a_fti.c
 | 
			
		||||
    CPP_WRAPS_C
 | 
			
		||||
    REQUIRES mraa)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										62
									
								
								src/gp2y0a/gp2y0a.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								src/gp2y0a/gp2y0a.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,62 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Author: Jon Trulson <jtrulson@ics.com>
 | 
			
		||||
 *         Abhishek Malik <abhishek.malik@intel.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 "gp2y0a.h"
 | 
			
		||||
 | 
			
		||||
gp2y0a_context gp2y0a_init(uint8_t pin, float a_ref){
 | 
			
		||||
    gp2y0a_context dev = (gp2y0a_context) malloc(sizeof(struct _gp2y0a_context));
 | 
			
		||||
 | 
			
		||||
    dev->pin = pin;
 | 
			
		||||
    dev->aio = mraa_aio_init(dev->pin);
 | 
			
		||||
    if(dev->aio == NULL){
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    dev->a_res = (1 << mraa_aio_get_bit(dev->aio));
 | 
			
		||||
 | 
			
		||||
    return dev;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void gp2y0a_close(gp2y0a_context dev){
 | 
			
		||||
    free(dev);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
upm_result_t gp2y0a_get_value(gp2y0a_context dev, float a_ref,
 | 
			
		||||
                              uint8_t samples, float* value){
 | 
			
		||||
    int val;
 | 
			
		||||
    int sum = 0;
 | 
			
		||||
    int i = 0;
 | 
			
		||||
 | 
			
		||||
    if (samples <= 0)
 | 
			
		||||
      samples = 1;
 | 
			
		||||
 | 
			
		||||
    for(i=0; i<samples; i++) {
 | 
			
		||||
        val = mraa_aio_read(dev->aio);
 | 
			
		||||
        sum += val;
 | 
			
		||||
    }
 | 
			
		||||
    val = sum/samples;
 | 
			
		||||
    float volts = (float)(val * a_ref) / (float)dev->a_res;
 | 
			
		||||
    *value = volts;
 | 
			
		||||
 | 
			
		||||
    return UPM_SUCCESS;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										98
									
								
								src/gp2y0a/gp2y0a.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								src/gp2y0a/gp2y0a.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,98 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Author: Jon Trulson <jtrulson@ics.com>
 | 
			
		||||
 *         Abhishek Malik <abhishek.malik@intel.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.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef GP2Y0A_GP2Y0A_H_
 | 
			
		||||
#define GP2Y0A_GP2Y0A_H_
 | 
			
		||||
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <malloc.h>
 | 
			
		||||
 | 
			
		||||
#include "upm.h"
 | 
			
		||||
#include "mraa/aio.h"
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * device context
 | 
			
		||||
 */
 | 
			
		||||
typedef struct _gp2y0a_context {
 | 
			
		||||
    mraa_aio_context            aio;
 | 
			
		||||
    uint8_t                     pin;
 | 
			
		||||
    int                         a_res;
 | 
			
		||||
} *gp2y0a_context;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief GP2Y0A-based IR Proximity Sensor library
 | 
			
		||||
 * @defgroup gp2y0a libupm-gp2y0a
 | 
			
		||||
 * @ingroup seeed analog light
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @library gp2y0a
 | 
			
		||||
 * @sensor gp2y0a
 | 
			
		||||
 * @comname GP2Y0A IR Proximity Sensor
 | 
			
		||||
 * @altname Grove 80cm IR Proximity Sensor
 | 
			
		||||
 * @type light
 | 
			
		||||
 * @man seeed
 | 
			
		||||
 * @con analog
 | 
			
		||||
 *
 | 
			
		||||
 * @brief API for the GP2Y0A family of IR Proximity Sensors
 | 
			
		||||
 *
 | 
			
		||||
 * Sensors of this family return an analog voltage corresponding to
 | 
			
		||||
 * the distance of an object from the sensor. The voltage is lower
 | 
			
		||||
 * when objects are far away; the voltage increases as objects get
 | 
			
		||||
 * closer to the sensor.
 | 
			
		||||
 *
 | 
			
		||||
 * @image html gp2y0a.jpg
 | 
			
		||||
 * @snippet gp2y0a.cxx Interesting
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * GP2Y0A sensor init function
 | 
			
		||||
 *
 | 
			
		||||
 * @param pin Analog pin to use
 | 
			
		||||
 * @param a_ref reference voltage
 | 
			
		||||
 */
 | 
			
		||||
gp2y0a_context gp2y0a_init(uint8_t pin, float a_ref);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * GP2Y0A close function
 | 
			
		||||
 *
 | 
			
		||||
 * @param dev sensor struct
 | 
			
		||||
 */
 | 
			
		||||
void gp2y0a_close(gp2y0a_context dev);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Gets an average voltage value from the sensor
 | 
			
		||||
 *
 | 
			
		||||
 * @param dev sensor struct
 | 
			
		||||
 * @param aref Reference voltage in use (usually 5.0V or 3.3V)
 | 
			
		||||
 * @param samples Number of samples to average over
 | 
			
		||||
 * @return Average voltage reading
 | 
			
		||||
 */
 | 
			
		||||
upm_result_t gp2y0a_get_value(gp2y0a_context dev, float a_ref, uint8_t samples,
 | 
			
		||||
                              float* val);
 | 
			
		||||
 | 
			
		||||
#endif /* GP2Y0A_GP2Y0A_H_ */
 | 
			
		||||
							
								
								
									
										80
									
								
								src/gp2y0a/gp2y0a_fti.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								src/gp2y0a/gp2y0a_fti.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,80 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Author: Jon Trulson <jtrulson@ics.com>
 | 
			
		||||
 *         Abhishek Malik <abhishek.malik@intel.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 "gp2y0a.h"
 | 
			
		||||
 | 
			
		||||
#include "upm_fti.h"
 | 
			
		||||
#include "types/upm_sensor.h"
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This file implements the Function Table Interface (FTI) for this sensor
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
const char upm_gp2y0a_name[] = "GP2Y0A";
 | 
			
		||||
const char upm_gp2y0a_description[] = "Grove 80cm IR Proximity Sensor";
 | 
			
		||||
const upm_protocol_t upm_gp2y0a_protocol[] = {UPM_ANALOG};
 | 
			
		||||
const upm_sensor_t upm_gp2y0a_category[] = {UPM_DISTANCE};
 | 
			
		||||
 | 
			
		||||
// forward declarations
 | 
			
		||||
const void* upm_gp2y0a_get_ft(upm_sensor_t sensor_type);
 | 
			
		||||
const upm_sensor_descriptor_t upm_gp2y0a_get_descriptor();
 | 
			
		||||
void* upm_gp2y0a_init_name();
 | 
			
		||||
void upm_gp2y0a_close(void* dev);
 | 
			
		||||
 | 
			
		||||
const upm_sensor_descriptor_t upm_gp2y0a_get_descriptor(){
 | 
			
		||||
    upm_sensor_descriptor_t usd;
 | 
			
		||||
    usd.name = upm_gp2y0a_name;
 | 
			
		||||
    usd.description = upm_gp2y0a_description;
 | 
			
		||||
    usd.protocol_size = 1;
 | 
			
		||||
    usd.protocol = upm_gp2y0a_protocol;
 | 
			
		||||
    usd.category_size = 1;
 | 
			
		||||
    usd.category = upm_gp2y0a_category;
 | 
			
		||||
    return usd;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const upm_sensor_ft ft =
 | 
			
		||||
{
 | 
			
		||||
    .upm_sensor_init_name = &upm_gp2y0a_init_name,
 | 
			
		||||
    .upm_sensor_close = &upm_gp2y0a_close,
 | 
			
		||||
    .upm_sensor_get_descriptor = &upm_gp2y0a_get_descriptor
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const void* upm_gp2y0a_get_ft(upm_sensor_t sensor_type)
 | 
			
		||||
{
 | 
			
		||||
    switch(sensor_type)
 | 
			
		||||
    {
 | 
			
		||||
        case UPM_SENSOR:
 | 
			
		||||
            return &ft;
 | 
			
		||||
        default:
 | 
			
		||||
            return NULL;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void* upm_gp2y0a_init_name(){
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void upm_gp2y0a_close(void* dev){
 | 
			
		||||
    gp2y0a_close((gp2y0a_context)dev);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user