Daiki Kato / MothionEventConverter
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MotionEventConverter.cpp Source File

MotionEventConverter.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (C) 2016 Renesas Electronics Corporation. All rights reserved.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "MotionEventConverter.h"
00018 
00019 #define MULTI_TOUCH_NUM         (2)
00020 
00021 MotionEventConverter::MotionEventConverter() : sem_touch_int(0) {
00022 }
00023 
00024 void MotionEventConverter::touch_process(TouchKey * p_touch) {
00025     memset(touch_pos, 0, sizeof(touch_pos));
00026     memset(touch_pos_last, 0, sizeof(touch_pos_last));
00027     time_cnt = 0;
00028     touch_num = 0;
00029     touch_num_last = 0;
00030 
00031     p_touch->SetCallback(this, &MotionEventConverter::touch_int_callback);
00032     p_touch->Reset();
00033     t.reset();
00034     t.start();
00035 
00036     while (1) {
00037         sem_touch_int.wait();
00038         // Get Coordinates
00039         touch_num = p_touch->GetCoordinates(MotionEvent::TOUCH_NUM_MAX, &touch_pos[0]);
00040         time_cnt = t.read_ms();
00041         event.clearPointerCount();
00042         if ((touch_num_last == 0) && (touch_num >= MULTI_TOUCH_NUM)) {
00043             // Two points are pressed at the same time
00044             event.setPosData(0, touch_pos[0].x, touch_pos[0].y);
00045             event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_DOWN, time_cnt);
00046             _callback.call(event);
00047 
00048             event.setPosData(1, touch_pos[1].x, touch_pos[1].y);
00049             event.setActionInfo((1 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_DOWN, time_cnt);
00050             _callback.call(event);
00051         } else if ((touch_num_last == 0) && (touch_num == 1)) {
00052             // It was pressed only one point
00053             if (touch_pos[0].valid != false) {
00054                 pidx = 0;
00055             } else {
00056                 pidx = 1;
00057             }
00058             event.setPosData(pidx, touch_pos[pidx].x, touch_pos[pidx].y);
00059             event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_DOWN, time_cnt);
00060             _callback.call(event);
00061         } else if ((touch_num_last == 1) && (touch_num == 2)) {
00062             // The second point is pressed
00063             event.setPosData(0, touch_pos[0].x, touch_pos[0].y);
00064             event.setPosData(1, touch_pos[1].x, touch_pos[1].y);
00065             if (touch_pos[0].valid != touch_pos_last[0].valid) {
00066                 pidx = 0;
00067             } else {
00068                 pidx = 1;
00069             }
00070             event.setActionInfo((pidx << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_DOWN, time_cnt);
00071             _callback.call(event);
00072         } else if ((touch_num_last >= MULTI_TOUCH_NUM) && (touch_num == 0)) {
00073             // Two points are released at the same time
00074             event.setPosData(0, touch_pos_last[0].x, touch_pos_last[0].y);
00075             event.setPosData(1, touch_pos_last[1].x, touch_pos_last[1].y);
00076             event.setActionInfo((1 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_UP, time_cnt);
00077             _callback.call(event);
00078 
00079             event.clearPointerCount();
00080             event.setPosData(0, touch_pos_last[0].x, touch_pos_last[0].y);
00081             event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_UP, time_cnt);
00082             _callback.call(event);
00083         } else if ((touch_num_last == 1) && (touch_num == 0)) {
00084             // The last of one point is released
00085             if (touch_pos[0].valid != touch_pos_last[0].valid) {
00086                 pidx = 0;
00087             } else {
00088                 pidx = 1;
00089             }
00090             event.setPosData(pidx, touch_pos_last[pidx].x, touch_pos_last[pidx].y);
00091             event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_UP, time_cnt);
00092             _callback.call(event);
00093         } else if ((touch_num_last >= MULTI_TOUCH_NUM) && (touch_num == 1)) {
00094             // The second point is released
00095             pidx = 0;
00096             if (touch_pos[0].valid != false) {
00097                 event.setPosData(0, touch_pos[0].x, touch_pos[0].y);
00098             } else {
00099                 event.setPosData(0, touch_pos_last[0].x, touch_pos_last[0].y);
00100             }
00101             if (touch_pos[1].valid != false) {
00102                 event.setPosData(1, touch_pos[1].x, touch_pos[1].y);
00103             } else {
00104                 event.setPosData(1, touch_pos_last[1].x, touch_pos_last[1].y);
00105                 pidx = 1;
00106             }
00107             event.setActionInfo((pidx << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_UP, time_cnt);
00108             _callback.call(event);
00109         } else if (touch_num != 0) {
00110             // Check whether the position has moved
00111             bool moved = false;
00112             for (int i = 0; i < MotionEvent::TOUCH_NUM_MAX; i++) {
00113                 if (touch_pos[i].valid != false) {
00114                     event.setPosData(i, touch_pos[i].x, touch_pos[i].y);
00115                     if ((touch_pos[i].x != touch_pos_last[i].x) || (touch_pos[i].y != touch_pos_last[i].y)) {
00116                         moved = true;
00117                         pidx  = i;
00118                     }
00119                 }
00120             }
00121             if (moved != false) {
00122                 if (touch_num >= MULTI_TOUCH_NUM) {
00123                     pidx = 0;
00124                 }
00125                 event.setActionInfo((pidx << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_MOVE, time_cnt);
00126                 _callback.call(event);
00127             }
00128         } else {
00129             // do nothing
00130         }
00131         touch_pos_last[0] = touch_pos[0];
00132         touch_pos_last[1] = touch_pos[1];
00133         touch_num_last = touch_num;
00134     }
00135 }
00136 
00137 void MotionEventConverter::touch_int_callback(void) {
00138     sem_touch_int.release();
00139 }
00140 
00141 
00142