NXP Touch Cursor example for LPCXpresso54608, modified for use with Mbed OS.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fsl_ft5406.cpp Source File

fsl_ft5406.cpp

00001 /*
00002  * The Clear BSD License
00003  * Copyright (c) 2016, Freescale Semiconductor, Inc.
00004  * Copyright 2016-2017 NXP
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without modification,
00008  * are permitted (subject to the limitations in the disclaimer below) provided
00009  * that the following conditions are met:
00010  *
00011  * o Redistributions of source code must retain the above copyright notice, this list
00012  *   of conditions and the following disclaimer.
00013  *
00014  * o Redistributions in binary form must reproduce the above copyright notice, this
00015  *   list of conditions and the following disclaimer in the documentation and/or
00016  *   other materials provided with the distribution.
00017  *
00018  * o Neither the name of the copyright holder nor the names of its
00019  *   contributors may be used to endorse or promote products derived from this
00020  *   software without specific prior written permission.
00021  *
00022  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
00023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00026  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00027  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00028  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00030  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 #include "fsl_common.h"
00036 //#include "fsl_debug_console.h"
00037 #include "fsl_i2c.h"
00038 #include "fsl_ft5406.h"
00039 
00040 typedef struct _ft5406_touch_point
00041 {
00042     uint8_t XH;
00043     uint8_t XL;
00044     uint8_t YH;
00045     uint8_t YL;
00046     uint8_t RESERVED[2];
00047 } ft5406_touch_point_t;
00048 
00049 typedef struct _ft5406_touch_data
00050 {
00051     uint8_t GEST_ID;
00052     uint8_t TD_STATUS;
00053     ft5406_touch_point_t TOUCH[FT5406_MAX_TOUCHES];
00054 } ft5406_touch_data_t;
00055 
00056 #define TOUCH_POINT_GET_EVENT(T) ((touch_event_t)((T).XH >> 6))
00057 #define TOUCH_POINT_GET_ID(T) ((T).YH >> 4)
00058 #define TOUCH_POINT_GET_X(T) ((((T).XH & 0x0f) << 8) | (T).XL)
00059 #define TOUCH_POINT_GET_Y(T) ((((T).YH & 0x0f) << 8) | (T).YL)
00060 
00061 status_t FT5406_Init(ft5406_handle_t *handle, I2C_Type *base)
00062 {
00063     i2c_master_transfer_t *xfer = &(handle->xfer);
00064     status_t status;
00065     uint8_t mode;
00066 
00067     assert(handle);
00068     assert(base);
00069 
00070     if (!handle || !base)
00071     {
00072         return kStatus_InvalidArgument;
00073     }
00074 
00075     handle->base = base;
00076 
00077     /* clear transfer structure and buffer */
00078     memset(xfer, 0, sizeof(*xfer));
00079     memset(handle->touch_buf, 0, FT5406_TOUCH_DATA_LEN);
00080 
00081     /* set device mode to normal operation */
00082     mode = 0;
00083     xfer->slaveAddress = 0x38;
00084     xfer->direction = kI2C_Write;
00085     xfer->subaddress = 0;
00086     xfer->subaddressSize = 1;
00087     xfer->data = &mode;
00088     xfer->dataSize = 1;
00089     xfer->flags = kI2C_TransferDefaultFlag;
00090 
00091     status = I2C_MasterTransferBlocking(handle->base, &handle->xfer);
00092 
00093     /* prepare transfer structure for reading touch data */
00094     xfer->slaveAddress = 0x38;
00095     xfer->direction = kI2C_Read;
00096     xfer->subaddress = 1;
00097     xfer->subaddressSize = 1;
00098     xfer->data = handle->touch_buf;
00099     xfer->dataSize = FT5406_TOUCH_DATA_LEN;
00100     xfer->flags = kI2C_TransferDefaultFlag;
00101 
00102     return status;
00103 }
00104 
00105 status_t FT5406_Denit(ft5406_handle_t *handle)
00106 {
00107     assert(handle);
00108 
00109     if (!handle)
00110     {
00111         return kStatus_InvalidArgument;
00112     }
00113 
00114     handle->base = NULL;
00115     return kStatus_Success;
00116 }
00117 
00118 status_t FT5406_ReadTouchData(ft5406_handle_t *handle)
00119 {
00120     assert(handle);
00121 
00122     if (!handle)
00123     {
00124         return kStatus_InvalidArgument;
00125     }
00126 
00127     return I2C_MasterTransferBlocking(handle->base, &handle->xfer);
00128 }
00129 
00130 status_t FT5406_GetSingleTouch(ft5406_handle_t *handle, touch_event_t *touch_event, int *touch_x, int *touch_y)
00131 {
00132     status_t status;
00133     touch_event_t touch_event_local;
00134 
00135     status = FT5406_ReadTouchData(handle);
00136 
00137     if (status == kStatus_Success)
00138     {
00139         ft5406_touch_data_t *touch_data = (ft5406_touch_data_t *)(void *)(handle->touch_buf);
00140 
00141         if (touch_event == NULL)
00142         {
00143             touch_event = &touch_event_local;
00144         }
00145         *touch_event = TOUCH_POINT_GET_EVENT(touch_data->TOUCH[0]);
00146 
00147         /* Update coordinates only if there is touch detected */
00148         if ((*touch_event == kTouch_Down ) || (*touch_event == kTouch_Contact ))
00149         {
00150             if (touch_x)
00151             {
00152                 *touch_x = TOUCH_POINT_GET_X(touch_data->TOUCH[0]);
00153             }
00154             if (touch_y)
00155             {
00156                 *touch_y = TOUCH_POINT_GET_Y(touch_data->TOUCH[0]);
00157             }
00158         }
00159     }
00160 
00161     return status;
00162 }
00163 
00164 status_t FT5406_GetMultiTouch(ft5406_handle_t *handle, int *touch_count, touch_point_t touch_array[FT5406_MAX_TOUCHES])
00165 {
00166     status_t status;
00167 
00168     status = FT5406_ReadTouchData(handle);
00169 
00170     if (status == kStatus_Success)
00171     {
00172         ft5406_touch_data_t *touch_data = (ft5406_touch_data_t *)(void *)(handle->touch_buf);
00173         int i;
00174 
00175         /* Decode number of touches */
00176         if (touch_count)
00177         {
00178             *touch_count = touch_data->TD_STATUS;
00179         }
00180 
00181         /* Decode valid touch points */
00182         for (i = 0; i < touch_data->TD_STATUS; i++)
00183         {
00184             touch_array[i].TOUCH_ID = TOUCH_POINT_GET_ID(touch_data->TOUCH[i]);
00185             touch_array[i].TOUCH_EVENT = TOUCH_POINT_GET_EVENT(touch_data->TOUCH[i]);
00186             touch_array[i].TOUCH_X = TOUCH_POINT_GET_X(touch_data->TOUCH[i]);
00187             touch_array[i].TOUCH_Y = TOUCH_POINT_GET_Y(touch_data->TOUCH[i]);
00188         }
00189 
00190         /* Clear vacant elements of touch_array */
00191         for (; i < FT5406_MAX_TOUCHES; i++)
00192         {
00193             touch_array[i].TOUCH_ID = 0;
00194             touch_array[i].TOUCH_EVENT = kTouch_Reserved ;
00195             touch_array[i].TOUCH_X = 0;
00196             touch_array[i].TOUCH_Y = 0;
00197         }
00198     }
00199 
00200     return status;
00201 }