NXP
/
rt1050_emwin_gui_demo
RT1050 GUI demo using emWin library
source/fsl_ft5406_rt.c@0:dd702039127a, 2018-09-20 (annotated)
- Committer:
- alejandroRL
- Date:
- Thu Sep 20 19:16:34 2018 +0000
- Revision:
- 0:dd702039127a
emWin GUI Demo for RT1050
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alejandroRL | 0:dd702039127a | 1 | /* |
alejandroRL | 0:dd702039127a | 2 | * The Clear BSD License |
alejandroRL | 0:dd702039127a | 3 | * Copyright (c) 2016, Freescale Semiconductor, Inc. |
alejandroRL | 0:dd702039127a | 4 | * Copyright 2016-2017 NXP |
alejandroRL | 0:dd702039127a | 5 | * All rights reserved. |
alejandroRL | 0:dd702039127a | 6 | * |
alejandroRL | 0:dd702039127a | 7 | * Redistribution and use in source and binary forms, with or without modification, |
alejandroRL | 0:dd702039127a | 8 | * are permitted (subject to the limitations in the disclaimer below) provided |
alejandroRL | 0:dd702039127a | 9 | * that the following conditions are met: |
alejandroRL | 0:dd702039127a | 10 | * |
alejandroRL | 0:dd702039127a | 11 | * o Redistributions of source code must retain the above copyright notice, this list |
alejandroRL | 0:dd702039127a | 12 | * of conditions and the following disclaimer. |
alejandroRL | 0:dd702039127a | 13 | * |
alejandroRL | 0:dd702039127a | 14 | * o Redistributions in binary form must reproduce the above copyright notice, this |
alejandroRL | 0:dd702039127a | 15 | * list of conditions and the following disclaimer in the documentation and/or |
alejandroRL | 0:dd702039127a | 16 | * other materials provided with the distribution. |
alejandroRL | 0:dd702039127a | 17 | * |
alejandroRL | 0:dd702039127a | 18 | * o Neither the name of the copyright holder nor the names of its |
alejandroRL | 0:dd702039127a | 19 | * contributors may be used to endorse or promote products derived from this |
alejandroRL | 0:dd702039127a | 20 | * software without specific prior written permission. |
alejandroRL | 0:dd702039127a | 21 | * |
alejandroRL | 0:dd702039127a | 22 | * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. |
alejandroRL | 0:dd702039127a | 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
alejandroRL | 0:dd702039127a | 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
alejandroRL | 0:dd702039127a | 25 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
alejandroRL | 0:dd702039127a | 26 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
alejandroRL | 0:dd702039127a | 27 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
alejandroRL | 0:dd702039127a | 28 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
alejandroRL | 0:dd702039127a | 29 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
alejandroRL | 0:dd702039127a | 30 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
alejandroRL | 0:dd702039127a | 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
alejandroRL | 0:dd702039127a | 32 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
alejandroRL | 0:dd702039127a | 33 | */ |
alejandroRL | 0:dd702039127a | 34 | |
alejandroRL | 0:dd702039127a | 35 | #include "fsl_common.h" |
alejandroRL | 0:dd702039127a | 36 | #include "fsl_lpi2c.h" |
alejandroRL | 0:dd702039127a | 37 | #include "fsl_ft5406_rt.h" |
alejandroRL | 0:dd702039127a | 38 | |
alejandroRL | 0:dd702039127a | 39 | typedef struct _ft5406_rt_touch_point |
alejandroRL | 0:dd702039127a | 40 | { |
alejandroRL | 0:dd702039127a | 41 | uint8_t XH; |
alejandroRL | 0:dd702039127a | 42 | uint8_t XL; |
alejandroRL | 0:dd702039127a | 43 | uint8_t YH; |
alejandroRL | 0:dd702039127a | 44 | uint8_t YL; |
alejandroRL | 0:dd702039127a | 45 | uint8_t RESERVED[2]; |
alejandroRL | 0:dd702039127a | 46 | } ft5406_rt_touch_point_t; |
alejandroRL | 0:dd702039127a | 47 | |
alejandroRL | 0:dd702039127a | 48 | typedef struct _ft5406_rt_touch_data |
alejandroRL | 0:dd702039127a | 49 | { |
alejandroRL | 0:dd702039127a | 50 | uint8_t GEST_ID; |
alejandroRL | 0:dd702039127a | 51 | uint8_t TD_STATUS; |
alejandroRL | 0:dd702039127a | 52 | ft5406_rt_touch_point_t TOUCH[FT5406_RT_MAX_TOUCHES]; |
alejandroRL | 0:dd702039127a | 53 | } ft5406_rt_touch_data_t; |
alejandroRL | 0:dd702039127a | 54 | |
alejandroRL | 0:dd702039127a | 55 | #define TOUCH_POINT_GET_EVENT(T) ((touch_event_t)((T).XH >> 6)) |
alejandroRL | 0:dd702039127a | 56 | #define TOUCH_POINT_GET_ID(T) ((T).YH >> 4) |
alejandroRL | 0:dd702039127a | 57 | #define TOUCH_POINT_GET_X(T) ((((T).XH & 0x0f) << 8) | (T).XL) |
alejandroRL | 0:dd702039127a | 58 | #define TOUCH_POINT_GET_Y(T) ((((T).YH & 0x0f) << 8) | (T).YL) |
alejandroRL | 0:dd702039127a | 59 | |
alejandroRL | 0:dd702039127a | 60 | status_t FT5406_RT_Init(ft5406_rt_handle_t *handle, LPI2C_Type *base) |
alejandroRL | 0:dd702039127a | 61 | { |
alejandroRL | 0:dd702039127a | 62 | lpi2c_master_transfer_t *xfer = &(handle->xfer); |
alejandroRL | 0:dd702039127a | 63 | status_t status; |
alejandroRL | 0:dd702039127a | 64 | uint8_t mode; |
alejandroRL | 0:dd702039127a | 65 | |
alejandroRL | 0:dd702039127a | 66 | assert(handle); |
alejandroRL | 0:dd702039127a | 67 | assert(base); |
alejandroRL | 0:dd702039127a | 68 | |
alejandroRL | 0:dd702039127a | 69 | if (!handle || !base) |
alejandroRL | 0:dd702039127a | 70 | { |
alejandroRL | 0:dd702039127a | 71 | return kStatus_InvalidArgument; |
alejandroRL | 0:dd702039127a | 72 | } |
alejandroRL | 0:dd702039127a | 73 | |
alejandroRL | 0:dd702039127a | 74 | handle->base = base; |
alejandroRL | 0:dd702039127a | 75 | |
alejandroRL | 0:dd702039127a | 76 | /* clear transfer structure and buffer */ |
alejandroRL | 0:dd702039127a | 77 | memset(xfer, 0, sizeof(*xfer)); |
alejandroRL | 0:dd702039127a | 78 | memset(handle->touch_buf, 0, FT5406_RT_TOUCH_DATA_LEN); |
alejandroRL | 0:dd702039127a | 79 | |
alejandroRL | 0:dd702039127a | 80 | /* set device mode to normal operation */ |
alejandroRL | 0:dd702039127a | 81 | mode = 0; |
alejandroRL | 0:dd702039127a | 82 | xfer->slaveAddress = FT5406_RT_I2C_ADDRESS; |
alejandroRL | 0:dd702039127a | 83 | xfer->direction = kLPI2C_Write; |
alejandroRL | 0:dd702039127a | 84 | xfer->subaddress = 0; |
alejandroRL | 0:dd702039127a | 85 | xfer->subaddressSize = 1; |
alejandroRL | 0:dd702039127a | 86 | xfer->data = &mode; |
alejandroRL | 0:dd702039127a | 87 | xfer->dataSize = 1; |
alejandroRL | 0:dd702039127a | 88 | xfer->flags = kLPI2C_TransferDefaultFlag; |
alejandroRL | 0:dd702039127a | 89 | |
alejandroRL | 0:dd702039127a | 90 | status = LPI2C_MasterTransferBlocking(handle->base, &handle->xfer); |
alejandroRL | 0:dd702039127a | 91 | |
alejandroRL | 0:dd702039127a | 92 | /* prepare transfer structure for reading touch data */ |
alejandroRL | 0:dd702039127a | 93 | xfer->slaveAddress = FT5406_RT_I2C_ADDRESS; |
alejandroRL | 0:dd702039127a | 94 | xfer->direction = kLPI2C_Read; |
alejandroRL | 0:dd702039127a | 95 | xfer->subaddress = 1; |
alejandroRL | 0:dd702039127a | 96 | xfer->subaddressSize = 1; |
alejandroRL | 0:dd702039127a | 97 | xfer->data = handle->touch_buf; |
alejandroRL | 0:dd702039127a | 98 | xfer->dataSize = FT5406_RT_TOUCH_DATA_LEN; |
alejandroRL | 0:dd702039127a | 99 | xfer->flags = kLPI2C_TransferDefaultFlag; |
alejandroRL | 0:dd702039127a | 100 | |
alejandroRL | 0:dd702039127a | 101 | return status; |
alejandroRL | 0:dd702039127a | 102 | } |
alejandroRL | 0:dd702039127a | 103 | |
alejandroRL | 0:dd702039127a | 104 | status_t FT5406_RT_Denit(ft5406_rt_handle_t *handle) |
alejandroRL | 0:dd702039127a | 105 | { |
alejandroRL | 0:dd702039127a | 106 | assert(handle); |
alejandroRL | 0:dd702039127a | 107 | |
alejandroRL | 0:dd702039127a | 108 | if (!handle) |
alejandroRL | 0:dd702039127a | 109 | { |
alejandroRL | 0:dd702039127a | 110 | return kStatus_InvalidArgument; |
alejandroRL | 0:dd702039127a | 111 | } |
alejandroRL | 0:dd702039127a | 112 | |
alejandroRL | 0:dd702039127a | 113 | handle->base = NULL; |
alejandroRL | 0:dd702039127a | 114 | return kStatus_Success; |
alejandroRL | 0:dd702039127a | 115 | } |
alejandroRL | 0:dd702039127a | 116 | |
alejandroRL | 0:dd702039127a | 117 | status_t FT5406_RT_ReadTouchData(ft5406_rt_handle_t *handle) |
alejandroRL | 0:dd702039127a | 118 | { |
alejandroRL | 0:dd702039127a | 119 | assert(handle); |
alejandroRL | 0:dd702039127a | 120 | |
alejandroRL | 0:dd702039127a | 121 | if (!handle) |
alejandroRL | 0:dd702039127a | 122 | { |
alejandroRL | 0:dd702039127a | 123 | return kStatus_InvalidArgument; |
alejandroRL | 0:dd702039127a | 124 | } |
alejandroRL | 0:dd702039127a | 125 | |
alejandroRL | 0:dd702039127a | 126 | return LPI2C_MasterTransferBlocking(handle->base, &handle->xfer); |
alejandroRL | 0:dd702039127a | 127 | } |
alejandroRL | 0:dd702039127a | 128 | |
alejandroRL | 0:dd702039127a | 129 | status_t FT5406_RT_GetSingleTouch(ft5406_rt_handle_t *handle, touch_event_t *touch_event, int *touch_x, int *touch_y) |
alejandroRL | 0:dd702039127a | 130 | { |
alejandroRL | 0:dd702039127a | 131 | status_t status; |
alejandroRL | 0:dd702039127a | 132 | touch_event_t touch_event_local; |
alejandroRL | 0:dd702039127a | 133 | |
alejandroRL | 0:dd702039127a | 134 | status = FT5406_RT_ReadTouchData(handle); |
alejandroRL | 0:dd702039127a | 135 | |
alejandroRL | 0:dd702039127a | 136 | if (status == kStatus_Success) |
alejandroRL | 0:dd702039127a | 137 | { |
alejandroRL | 0:dd702039127a | 138 | ft5406_rt_touch_data_t *touch_data = (ft5406_rt_touch_data_t *)(void *)(handle->touch_buf); |
alejandroRL | 0:dd702039127a | 139 | |
alejandroRL | 0:dd702039127a | 140 | if (touch_event == NULL) |
alejandroRL | 0:dd702039127a | 141 | { |
alejandroRL | 0:dd702039127a | 142 | touch_event = &touch_event_local; |
alejandroRL | 0:dd702039127a | 143 | } |
alejandroRL | 0:dd702039127a | 144 | *touch_event = TOUCH_POINT_GET_EVENT(touch_data->TOUCH[0]); |
alejandroRL | 0:dd702039127a | 145 | |
alejandroRL | 0:dd702039127a | 146 | /* Update coordinates only if there is touch detected */ |
alejandroRL | 0:dd702039127a | 147 | if ((*touch_event == kTouch_Down) || (*touch_event == kTouch_Contact)) |
alejandroRL | 0:dd702039127a | 148 | { |
alejandroRL | 0:dd702039127a | 149 | if (touch_x) |
alejandroRL | 0:dd702039127a | 150 | { |
alejandroRL | 0:dd702039127a | 151 | *touch_x = TOUCH_POINT_GET_X(touch_data->TOUCH[0]); |
alejandroRL | 0:dd702039127a | 152 | } |
alejandroRL | 0:dd702039127a | 153 | if (touch_y) |
alejandroRL | 0:dd702039127a | 154 | { |
alejandroRL | 0:dd702039127a | 155 | *touch_y = TOUCH_POINT_GET_Y(touch_data->TOUCH[0]); |
alejandroRL | 0:dd702039127a | 156 | } |
alejandroRL | 0:dd702039127a | 157 | } |
alejandroRL | 0:dd702039127a | 158 | } |
alejandroRL | 0:dd702039127a | 159 | |
alejandroRL | 0:dd702039127a | 160 | return status; |
alejandroRL | 0:dd702039127a | 161 | } |
alejandroRL | 0:dd702039127a | 162 | |
alejandroRL | 0:dd702039127a | 163 | status_t FT5406_RT_GetMultiTouch(ft5406_rt_handle_t *handle, |
alejandroRL | 0:dd702039127a | 164 | int *touch_count, |
alejandroRL | 0:dd702039127a | 165 | touch_point_t touch_array[FT5406_RT_MAX_TOUCHES]) |
alejandroRL | 0:dd702039127a | 166 | { |
alejandroRL | 0:dd702039127a | 167 | status_t status; |
alejandroRL | 0:dd702039127a | 168 | |
alejandroRL | 0:dd702039127a | 169 | status = FT5406_RT_ReadTouchData(handle); |
alejandroRL | 0:dd702039127a | 170 | |
alejandroRL | 0:dd702039127a | 171 | if (status == kStatus_Success) |
alejandroRL | 0:dd702039127a | 172 | { |
alejandroRL | 0:dd702039127a | 173 | ft5406_rt_touch_data_t *touch_data = (ft5406_rt_touch_data_t *)(void *)(handle->touch_buf); |
alejandroRL | 0:dd702039127a | 174 | int i; |
alejandroRL | 0:dd702039127a | 175 | |
alejandroRL | 0:dd702039127a | 176 | /* Decode number of touches */ |
alejandroRL | 0:dd702039127a | 177 | if (touch_count) |
alejandroRL | 0:dd702039127a | 178 | { |
alejandroRL | 0:dd702039127a | 179 | *touch_count = touch_data->TD_STATUS; |
alejandroRL | 0:dd702039127a | 180 | } |
alejandroRL | 0:dd702039127a | 181 | |
alejandroRL | 0:dd702039127a | 182 | /* Decode valid touch points */ |
alejandroRL | 0:dd702039127a | 183 | for (i = 0; i < touch_data->TD_STATUS; i++) |
alejandroRL | 0:dd702039127a | 184 | { |
alejandroRL | 0:dd702039127a | 185 | touch_array[i].TOUCH_ID = TOUCH_POINT_GET_ID(touch_data->TOUCH[i]); |
alejandroRL | 0:dd702039127a | 186 | touch_array[i].TOUCH_EVENT = TOUCH_POINT_GET_EVENT(touch_data->TOUCH[i]); |
alejandroRL | 0:dd702039127a | 187 | touch_array[i].TOUCH_X = TOUCH_POINT_GET_X(touch_data->TOUCH[i]); |
alejandroRL | 0:dd702039127a | 188 | touch_array[i].TOUCH_Y = TOUCH_POINT_GET_Y(touch_data->TOUCH[i]); |
alejandroRL | 0:dd702039127a | 189 | } |
alejandroRL | 0:dd702039127a | 190 | |
alejandroRL | 0:dd702039127a | 191 | /* Clear vacant elements of touch_array */ |
alejandroRL | 0:dd702039127a | 192 | for (; i < FT5406_RT_MAX_TOUCHES; i++) |
alejandroRL | 0:dd702039127a | 193 | { |
alejandroRL | 0:dd702039127a | 194 | touch_array[i].TOUCH_ID = 0; |
alejandroRL | 0:dd702039127a | 195 | touch_array[i].TOUCH_EVENT = kTouch_Reserved; |
alejandroRL | 0:dd702039127a | 196 | touch_array[i].TOUCH_X = 0; |
alejandroRL | 0:dd702039127a | 197 | touch_array[i].TOUCH_Y = 0; |
alejandroRL | 0:dd702039127a | 198 | } |
alejandroRL | 0:dd702039127a | 199 | } |
alejandroRL | 0:dd702039127a | 200 | |
alejandroRL | 0:dd702039127a | 201 | return status; |
alejandroRL | 0:dd702039127a | 202 | } |