TTP223: Adding C source

Signed-off-by: Abhishek Malik <abhishek.malik@intel.com>
This commit is contained in:
Abhishek Malik 2016-09-02 16:26:09 -07:00 committed by Noel Eck
parent 8c4b5333d5
commit 09b1bb9a60
4 changed files with 263 additions and 5 deletions

View File

@ -1,5 +1,9 @@
set (libname "ttp223")
set (libdescription "upm ttp223")
set (module_src ${libname}.cxx)
set (module_hpp ${libname}.hpp)
upm_module_init()
upm_mixed_module_init (NAME ttp223
DESCRIPTION "upm ttp223 touch sensor module"
C_HDR ttp223.h
C_SRC ttp223.c
CPP_HDR ttp223.hpp
CPP_SRC ttp223.cxx
FTI_SRC ttp223_fti.c
CPP_WRAPS_C
REQUIRES libmraa)

80
src/ttp223/ttp223.c Normal file
View File

@ -0,0 +1,80 @@
/*
* Author: Sarah Knepper <sarah.knepper@intel.com>
* Abhishek Malik <abhishek.malik@intel.com>
* 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 "ttp223.h"
ttp223_context ttp223_init(int pin){
ttp223_context dev =
(ttp223_context) malloc(sizeof(struct _ttp223_context));
if (dev == NULL) {
printf("Unable to allocate space for the sensor struct\n");
return NULL;
}
dev->gpio_pin = pin;
dev->gpio = mraa_gpio_init(dev->gpio_pin);
mraa_gpio_dir(dev->gpio, MRAA_GPIO_IN);
dev->isr_installed = false;
return dev;
}
void ttp223_close(ttp223_context dev){
mraa_gpio_close(dev->gpio);
free(dev);
}
upm_result_t ttp223_is_pressed(ttp223_context dev, bool* value) {
int ret = mraa_gpio_read(dev->gpio);
if (ret > 0)
*value = true;
else
*value = false;
return UPM_SUCCESS;
}
upm_result_t upm_ttp223_install_isr(ttp223_context dev,
mraa_gpio_edge_t edge_level,
void (*isr)(void *), void *arg){
ttp223_uninstall_isr(dev);
mraa_gpio_isr(dev->gpio, edge_level, isr, arg);
dev->isr_installed = true;
return UPM_SUCCESS;
}
upm_result_t ttp223_uninstall_isr(ttp223_context dev){
if (dev->isr_installed)
mraa_gpio_isr_exit(dev->gpio);
dev->isr_installed = false;
return UPM_SUCCESS;
}

81
src/ttp223/ttp223.h Normal file
View File

@ -0,0 +1,81 @@
/*
* Author: Sarah Knepper <sarah.knepper@intel.com>
* Abhishek Malik <abhishek.malik@intel.com>
* 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.
*/
#ifndef TTP223_H_
#define TTP223_H_
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "upm.h"
#include "mraa/gpio.h"
/**
* device context
*/
typedef struct _ttp223_context {
mraa_gpio_context gpio;
uint8_t gpio_pin;
bool isr_installed;
} *ttp223_context;
/**
* Sensor Init function
*
* @param pin The pin number the sensor is attached to
* @return void* pointer to the sensor struct
*/
ttp223_context ttp223_init(int pin);
/**
* Sensor Module close function
*
* @param dev pointer to the sensor struct
*/
void ttp223_close(ttp223_context dev);
/**
* Function to tell if the sensor is pressed
*
* @param dev pointer to the sensor struct
* @param value pointer to store whether the sensor is pressed or not
* @result upm_result_t UPM success/error code
*/
upm_result_t ttp223_is_pressed(ttp223_context dev, bool* value);
/**
*
*/
upm_result_t ttp223_install_isr(ttp223_context dev,
mraa_gpio_edge_t edge_level,
void (*isr)(void *), void *arg);
/**
*
*/
upm_result_t ttp223_uninstall_isr(ttp223_context);
#endif /* TTP223_H_ */

93
src/ttp223/ttp223_fti.c Normal file
View File

@ -0,0 +1,93 @@
/*
* Author: Sarah Knepper <sarah.knepper@intel.com>
* Abhishek Malik <abhishek.malik@intel.com>
* 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 "ttp223.h"
#include "upm_fti.h"
/**
* This file implements the Function Table Interface (FTI) for this sensor
*/
const char upm_ttp223_name[] = "TTP223";
const char upm_ttp223_description[] = "TTP223 Touch Detector Sensor";
const upm_protocol_t upm_ttp223_protocol[] = {UPM_GPIO};
const upm_sensor_t upm_ttp223_category[] = {UPM_SWITCH};
// forward declarations
const upm_sensor_descriptor_t upm_ttp223_get_descriptor();
const void* upm_ttp223_get_ft(upm_sensor_t sensor_type);
void* upm_ttp223_init_name();
void upm_ttp223_close(void* dev);
upm_result_t upm_ttp223_is_pressed(void* dev, bool* value, int num);
const upm_sensor_descriptor_t upm_ttp223_get_descriptor(){
upm_sensor_descriptor_t usd;
usd.name = upm_ttp223_name;
usd.description = upm_ttp223_description;
usd.protocol_size = 1;
usd.protocol = upm_ttp223_protocol;
usd.category_size = 1;
usd.category = upm_ttp223_category;
return usd;
}
static const upm_sensor_ft ft =
{
.upm_sensor_init_name = &upm_ttp223_init_name,
.upm_sensor_close = &upm_ttp223_close,
.upm_sensor_get_descriptor = &upm_ttp223_get_descriptor
};
static const upm_switch_ft sft =
{
.upm_switch_get_value = &upm_ttp223_is_pressed
};
const void* upm_ttp223_get_ft(upm_sensor_t sensor_type){
if(sensor_type == UPM_SWITCH){
return &sft;
}
else if(sensor_type == UPM_SENSOR){
return &ft;
}
return NULL;
}
void* upm_ttp223_init_name(){
return NULL;
}
void upm_ttp223_close(void* dev){
ttp223_close((ttp223_context)dev);
}
upm_result_t upm_ttp223_is_pressed(void* dev, bool* value, int num) {
if(num != 1){
printf("Sorry this sensor driver supports only one touch pad\n");
return UPM_ERROR_OUT_OF_RANGE;
}
return ttp223_is_pressed((ttp223_context)dev, value);
}