RT1050 GUI demo using emWin library

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fsl_ft5406_rt.c Source File

fsl_ft5406_rt.c

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_lpi2c.h"
00037 #include "fsl_ft5406_rt.h"
00038 
00039 typedef struct _ft5406_rt_touch_point
00040 {
00041     uint8_t XH;
00042     uint8_t XL;
00043     uint8_t YH;
00044     uint8_t YL;
00045     uint8_t RESERVED[2];
00046 } ft5406_rt_touch_point_t;
00047 
00048 typedef struct _ft5406_rt_touch_data
00049 {
00050     uint8_t GEST_ID;
00051     uint8_t TD_STATUS;
00052     ft5406_rt_touch_point_t TOUCH[FT5406_RT_MAX_TOUCHES];
00053 } ft5406_rt_touch_data_t;
00054 
00055 #define TOUCH_POINT_GET_EVENT(T) ((touch_event_t)((T).XH >> 6))
00056 #define TOUCH_POINT_GET_ID(T) ((T).YH >> 4)
00057 #define TOUCH_POINT_GET_X(T) ((((T).XH & 0x0f) << 8) | (T).XL)
00058 #define TOUCH_POINT_GET_Y(T) ((((T).YH & 0x0f) << 8) | (T).YL)
00059 
00060 status_t FT5406_RT_Init(ft5406_rt_handle_t *handle, LPI2C_Type *base)
00061 {
00062     lpi2c_master_transfer_t *xfer = &(handle->xfer);
00063     status_t status;
00064     uint8_t mode;
00065 
00066     assert(handle);
00067     assert(base);
00068 
00069     if (!handle || !base)
00070     {
00071         return kStatus_InvalidArgument;
00072     }
00073 
00074     handle->base = base;
00075 
00076     /* clear transfer structure and buffer */
00077     memset(xfer, 0, sizeof(*xfer));
00078     memset(handle->touch_buf, 0, FT5406_RT_TOUCH_DATA_LEN);
00079 
00080     /* set device mode to normal operation */
00081     mode = 0;
00082     xfer->slaveAddress = FT5406_RT_I2C_ADDRESS;
00083     xfer->direction = kLPI2C_Write;
00084     xfer->subaddress = 0;
00085     xfer->subaddressSize = 1;
00086     xfer->data = &mode;
00087     xfer->dataSize = 1;
00088     xfer->flags = kLPI2C_TransferDefaultFlag;
00089 
00090     status = LPI2C_MasterTransferBlocking(handle->base, &handle->xfer);
00091 
00092     /* prepare transfer structure for reading touch data */
00093     xfer->slaveAddress = FT5406_RT_I2C_ADDRESS;
00094     xfer->direction = kLPI2C_Read;
00095     xfer->subaddress = 1;
00096     xfer->subaddressSize = 1;
00097     xfer->data = handle->touch_buf;
00098     xfer->dataSize = FT5406_RT_TOUCH_DATA_LEN;
00099     xfer->flags = kLPI2C_TransferDefaultFlag;
00100 
00101     return status;
00102 }
00103 
00104 status_t FT5406_RT_Denit(ft5406_rt_handle_t *handle)
00105 {
00106     assert(handle);
00107 
00108     if (!handle)
00109     {
00110         return kStatus_InvalidArgument;
00111     }
00112 
00113     handle->base = NULL;
00114     return kStatus_Success;
00115 }
00116 
00117 status_t FT5406_RT_ReadTouchData(ft5406_rt_handle_t *handle)
00118 {
00119     assert(handle);
00120 
00121     if (!handle)
00122     {
00123         return kStatus_InvalidArgument;
00124     }
00125 
00126     return LPI2C_MasterTransferBlocking(handle->base, &handle->xfer);
00127 }
00128 
00129 status_t FT5406_RT_GetSingleTouch(ft5406_rt_handle_t *handle, touch_event_t *touch_event, int *touch_x, int *touch_y)
00130 {
00131     status_t status;
00132     touch_event_t touch_event_local;
00133 
00134     status = FT5406_RT_ReadTouchData(handle);
00135 
00136     if (status == kStatus_Success)
00137     {
00138         ft5406_rt_touch_data_t *touch_data = (ft5406_rt_touch_data_t *)(void *)(handle->touch_buf);
00139 
00140         if (touch_event == NULL)
00141         {
00142             touch_event = &touch_event_local;
00143         }
00144         *touch_event = TOUCH_POINT_GET_EVENT(touch_data->TOUCH[0]);
00145 
00146         /* Update coordinates only if there is touch detected */
00147         if ((*touch_event == kTouch_Down ) || (*touch_event == kTouch_Contact ))
00148         {
00149             if (touch_x)
00150             {
00151                 *touch_x = TOUCH_POINT_GET_X(touch_data->TOUCH[0]);
00152             }
00153             if (touch_y)
00154             {
00155                 *touch_y = TOUCH_POINT_GET_Y(touch_data->TOUCH[0]);
00156             }
00157         }
00158     }
00159 
00160     return status;
00161 }
00162 
00163 status_t FT5406_RT_GetMultiTouch(ft5406_rt_handle_t *handle,
00164                                  int *touch_count,
00165                                  touch_point_t touch_array[FT5406_RT_MAX_TOUCHES])
00166 {
00167     status_t status;
00168 
00169     status = FT5406_RT_ReadTouchData(handle);
00170 
00171     if (status == kStatus_Success)
00172     {
00173         ft5406_rt_touch_data_t *touch_data = (ft5406_rt_touch_data_t *)(void *)(handle->touch_buf);
00174         int i;
00175 
00176         /* Decode number of touches */
00177         if (touch_count)
00178         {
00179             *touch_count = touch_data->TD_STATUS;
00180         }
00181 
00182         /* Decode valid touch points */
00183         for (i = 0; i < touch_data->TD_STATUS; i++)
00184         {
00185             touch_array[i].TOUCH_ID = TOUCH_POINT_GET_ID(touch_data->TOUCH[i]);
00186             touch_array[i].TOUCH_EVENT = TOUCH_POINT_GET_EVENT(touch_data->TOUCH[i]);
00187             touch_array[i].TOUCH_X = TOUCH_POINT_GET_X(touch_data->TOUCH[i]);
00188             touch_array[i].TOUCH_Y = TOUCH_POINT_GET_Y(touch_data->TOUCH[i]);
00189         }
00190 
00191         /* Clear vacant elements of touch_array */
00192         for (; i < FT5406_RT_MAX_TOUCHES; i++)
00193         {
00194             touch_array[i].TOUCH_ID = 0;
00195             touch_array[i].TOUCH_EVENT = kTouch_Reserved ;
00196             touch_array[i].TOUCH_X = 0;
00197             touch_array[i].TOUCH_Y = 0;
00198         }
00199     }
00200 
00201     return status;
00202 }