diff --git a/src/ttp223/CMakeLists.txt b/src/ttp223/CMakeLists.txt index 3ae45946..2485afcc 100644 --- a/src/ttp223/CMakeLists.txt +++ b/src/ttp223/CMakeLists.txt @@ -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) diff --git a/src/ttp223/ttp223.c b/src/ttp223/ttp223.c new file mode 100644 index 00000000..b724fa08 --- /dev/null +++ b/src/ttp223/ttp223.c @@ -0,0 +1,80 @@ +/* + * Author: Sarah Knepper + * Abhishek Malik + * 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; +} diff --git a/src/ttp223/ttp223.h b/src/ttp223/ttp223.h new file mode 100644 index 00000000..9220bd2f --- /dev/null +++ b/src/ttp223/ttp223.h @@ -0,0 +1,81 @@ +/* + * Author: Sarah Knepper + * Abhishek Malik + * 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 +#include +#include + +#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_ */ diff --git a/src/ttp223/ttp223_fti.c b/src/ttp223/ttp223_fti.c new file mode 100644 index 00000000..fb7e7db0 --- /dev/null +++ b/src/ttp223/ttp223_fti.c @@ -0,0 +1,93 @@ +/* + * Author: Sarah Knepper + * Abhishek Malik + * 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); +} \ No newline at end of file