Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MotionEventConverter.cpp@1:5bfc165e8f08, 2016-06-29 (annotated)
- Committer:
- dkato
- Date:
- Wed Jun 29 05:33:35 2016 +0000
- Revision:
- 1:5bfc165e8f08
- Parent:
- 0:c41d29f1e0fb
update
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| dkato | 0:c41d29f1e0fb | 1 | /* mbed Microcontroller Library |
| dkato | 0:c41d29f1e0fb | 2 | * Copyright (C) 2016 Renesas Electronics Corporation. All rights reserved. |
| dkato | 0:c41d29f1e0fb | 3 | * |
| dkato | 0:c41d29f1e0fb | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| dkato | 0:c41d29f1e0fb | 5 | * you may not use this file except in compliance with the License. |
| dkato | 0:c41d29f1e0fb | 6 | * You may obtain a copy of the License at |
| dkato | 0:c41d29f1e0fb | 7 | * |
| dkato | 0:c41d29f1e0fb | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| dkato | 0:c41d29f1e0fb | 9 | * |
| dkato | 0:c41d29f1e0fb | 10 | * Unless required by applicable law or agreed to in writing, software |
| dkato | 0:c41d29f1e0fb | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| dkato | 0:c41d29f1e0fb | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| dkato | 0:c41d29f1e0fb | 13 | * See the License for the specific language governing permissions and |
| dkato | 0:c41d29f1e0fb | 14 | * limitations under the License. |
| dkato | 0:c41d29f1e0fb | 15 | */ |
| dkato | 0:c41d29f1e0fb | 16 | |
| dkato | 0:c41d29f1e0fb | 17 | #include "MotionEventConverter.h" |
| dkato | 0:c41d29f1e0fb | 18 | |
| dkato | 0:c41d29f1e0fb | 19 | #define MULTI_TOUCH_NUM (2) |
| dkato | 0:c41d29f1e0fb | 20 | |
| dkato | 1:5bfc165e8f08 | 21 | MotionEventConverter::MotionEventConverter() : sem_touch_int(0) { |
| dkato | 1:5bfc165e8f08 | 22 | } |
| dkato | 1:5bfc165e8f08 | 23 | |
| dkato | 1:5bfc165e8f08 | 24 | void MotionEventConverter::touch_process(TouchKey * p_touch) { |
| dkato | 0:c41d29f1e0fb | 25 | memset(touch_pos, 0, sizeof(touch_pos)); |
| dkato | 0:c41d29f1e0fb | 26 | memset(touch_pos_last, 0, sizeof(touch_pos_last)); |
| dkato | 0:c41d29f1e0fb | 27 | time_cnt = 0; |
| dkato | 0:c41d29f1e0fb | 28 | touch_num = 0; |
| dkato | 0:c41d29f1e0fb | 29 | touch_num_last = 0; |
| dkato | 0:c41d29f1e0fb | 30 | |
| dkato | 1:5bfc165e8f08 | 31 | p_touch->SetCallback(this, &MotionEventConverter::touch_int_callback); |
| dkato | 1:5bfc165e8f08 | 32 | p_touch->Reset(); |
| dkato | 0:c41d29f1e0fb | 33 | t.reset(); |
| dkato | 0:c41d29f1e0fb | 34 | t.start(); |
| dkato | 0:c41d29f1e0fb | 35 | |
| dkato | 0:c41d29f1e0fb | 36 | while (1) { |
| dkato | 1:5bfc165e8f08 | 37 | sem_touch_int.wait(); |
| dkato | 0:c41d29f1e0fb | 38 | // Get Coordinates |
| dkato | 0:c41d29f1e0fb | 39 | touch_num = p_touch->GetCoordinates(MotionEvent::TOUCH_NUM_MAX, &touch_pos[0]); |
| dkato | 0:c41d29f1e0fb | 40 | time_cnt = t.read_ms(); |
| dkato | 0:c41d29f1e0fb | 41 | event.clearPointerCount(); |
| dkato | 0:c41d29f1e0fb | 42 | if ((touch_num_last == 0) && (touch_num >= MULTI_TOUCH_NUM)) { |
| dkato | 0:c41d29f1e0fb | 43 | // Two points are pressed at the same time |
| dkato | 0:c41d29f1e0fb | 44 | event.setPosData(0, touch_pos[0].x, touch_pos[0].y); |
| dkato | 0:c41d29f1e0fb | 45 | event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_DOWN, time_cnt); |
| dkato | 1:5bfc165e8f08 | 46 | _callback.call(event); |
| dkato | 0:c41d29f1e0fb | 47 | |
| dkato | 0:c41d29f1e0fb | 48 | event.setPosData(1, touch_pos[1].x, touch_pos[1].y); |
| dkato | 0:c41d29f1e0fb | 49 | event.setActionInfo((1 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_DOWN, time_cnt); |
| dkato | 1:5bfc165e8f08 | 50 | _callback.call(event); |
| dkato | 0:c41d29f1e0fb | 51 | } else if ((touch_num_last == 0) && (touch_num == 1)) { |
| dkato | 0:c41d29f1e0fb | 52 | // It was pressed only one point |
| dkato | 0:c41d29f1e0fb | 53 | if (touch_pos[0].valid != false) { |
| dkato | 0:c41d29f1e0fb | 54 | pidx = 0; |
| dkato | 0:c41d29f1e0fb | 55 | } else { |
| dkato | 0:c41d29f1e0fb | 56 | pidx = 1; |
| dkato | 0:c41d29f1e0fb | 57 | } |
| dkato | 0:c41d29f1e0fb | 58 | event.setPosData(pidx, touch_pos[pidx].x, touch_pos[pidx].y); |
| dkato | 0:c41d29f1e0fb | 59 | event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_DOWN, time_cnt); |
| dkato | 1:5bfc165e8f08 | 60 | _callback.call(event); |
| dkato | 0:c41d29f1e0fb | 61 | } else if ((touch_num_last == 1) && (touch_num == 2)) { |
| dkato | 0:c41d29f1e0fb | 62 | // The second point is pressed |
| dkato | 0:c41d29f1e0fb | 63 | event.setPosData(0, touch_pos[0].x, touch_pos[0].y); |
| dkato | 0:c41d29f1e0fb | 64 | event.setPosData(1, touch_pos[1].x, touch_pos[1].y); |
| dkato | 0:c41d29f1e0fb | 65 | if (touch_pos[0].valid != touch_pos_last[0].valid) { |
| dkato | 0:c41d29f1e0fb | 66 | pidx = 0; |
| dkato | 0:c41d29f1e0fb | 67 | } else { |
| dkato | 0:c41d29f1e0fb | 68 | pidx = 1; |
| dkato | 0:c41d29f1e0fb | 69 | } |
| dkato | 0:c41d29f1e0fb | 70 | event.setActionInfo((pidx << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_DOWN, time_cnt); |
| dkato | 1:5bfc165e8f08 | 71 | _callback.call(event); |
| dkato | 0:c41d29f1e0fb | 72 | } else if ((touch_num_last >= MULTI_TOUCH_NUM) && (touch_num == 0)) { |
| dkato | 0:c41d29f1e0fb | 73 | // Two points are released at the same time |
| dkato | 0:c41d29f1e0fb | 74 | event.setPosData(0, touch_pos_last[0].x, touch_pos_last[0].y); |
| dkato | 0:c41d29f1e0fb | 75 | event.setPosData(1, touch_pos_last[1].x, touch_pos_last[1].y); |
| dkato | 0:c41d29f1e0fb | 76 | event.setActionInfo((1 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_UP, time_cnt); |
| dkato | 1:5bfc165e8f08 | 77 | _callback.call(event); |
| dkato | 0:c41d29f1e0fb | 78 | |
| dkato | 0:c41d29f1e0fb | 79 | event.clearPointerCount(); |
| dkato | 0:c41d29f1e0fb | 80 | event.setPosData(0, touch_pos_last[0].x, touch_pos_last[0].y); |
| dkato | 0:c41d29f1e0fb | 81 | event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_UP, time_cnt); |
| dkato | 1:5bfc165e8f08 | 82 | _callback.call(event); |
| dkato | 0:c41d29f1e0fb | 83 | } else if ((touch_num_last == 1) && (touch_num == 0)) { |
| dkato | 0:c41d29f1e0fb | 84 | // The last of one point is released |
| dkato | 0:c41d29f1e0fb | 85 | if (touch_pos[0].valid != touch_pos_last[0].valid) { |
| dkato | 0:c41d29f1e0fb | 86 | pidx = 0; |
| dkato | 0:c41d29f1e0fb | 87 | } else { |
| dkato | 0:c41d29f1e0fb | 88 | pidx = 1; |
| dkato | 0:c41d29f1e0fb | 89 | } |
| dkato | 0:c41d29f1e0fb | 90 | event.setPosData(pidx, touch_pos_last[pidx].x, touch_pos_last[pidx].y); |
| dkato | 0:c41d29f1e0fb | 91 | event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_UP, time_cnt); |
| dkato | 1:5bfc165e8f08 | 92 | _callback.call(event); |
| dkato | 0:c41d29f1e0fb | 93 | } else if ((touch_num_last >= MULTI_TOUCH_NUM) && (touch_num == 1)) { |
| dkato | 0:c41d29f1e0fb | 94 | // The second point is released |
| dkato | 0:c41d29f1e0fb | 95 | pidx = 0; |
| dkato | 0:c41d29f1e0fb | 96 | if (touch_pos[0].valid != false) { |
| dkato | 0:c41d29f1e0fb | 97 | event.setPosData(0, touch_pos[0].x, touch_pos[0].y); |
| dkato | 0:c41d29f1e0fb | 98 | } else { |
| dkato | 0:c41d29f1e0fb | 99 | event.setPosData(0, touch_pos_last[0].x, touch_pos_last[0].y); |
| dkato | 0:c41d29f1e0fb | 100 | } |
| dkato | 0:c41d29f1e0fb | 101 | if (touch_pos[1].valid != false) { |
| dkato | 0:c41d29f1e0fb | 102 | event.setPosData(1, touch_pos[1].x, touch_pos[1].y); |
| dkato | 0:c41d29f1e0fb | 103 | } else { |
| dkato | 0:c41d29f1e0fb | 104 | event.setPosData(1, touch_pos_last[1].x, touch_pos_last[1].y); |
| dkato | 0:c41d29f1e0fb | 105 | pidx = 1; |
| dkato | 0:c41d29f1e0fb | 106 | } |
| dkato | 0:c41d29f1e0fb | 107 | event.setActionInfo((pidx << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_UP, time_cnt); |
| dkato | 1:5bfc165e8f08 | 108 | _callback.call(event); |
| dkato | 0:c41d29f1e0fb | 109 | } else if (touch_num != 0) { |
| dkato | 0:c41d29f1e0fb | 110 | // Check whether the position has moved |
| dkato | 0:c41d29f1e0fb | 111 | bool moved = false; |
| dkato | 0:c41d29f1e0fb | 112 | for (int i = 0; i < MotionEvent::TOUCH_NUM_MAX; i++) { |
| dkato | 0:c41d29f1e0fb | 113 | if (touch_pos[i].valid != false) { |
| dkato | 0:c41d29f1e0fb | 114 | event.setPosData(i, touch_pos[i].x, touch_pos[i].y); |
| dkato | 0:c41d29f1e0fb | 115 | if ((touch_pos[i].x != touch_pos_last[i].x) || (touch_pos[i].y != touch_pos_last[i].y)) { |
| dkato | 0:c41d29f1e0fb | 116 | moved = true; |
| dkato | 0:c41d29f1e0fb | 117 | pidx = i; |
| dkato | 0:c41d29f1e0fb | 118 | } |
| dkato | 0:c41d29f1e0fb | 119 | } |
| dkato | 0:c41d29f1e0fb | 120 | } |
| dkato | 0:c41d29f1e0fb | 121 | if (moved != false) { |
| dkato | 0:c41d29f1e0fb | 122 | if (touch_num >= MULTI_TOUCH_NUM) { |
| dkato | 0:c41d29f1e0fb | 123 | pidx = 0; |
| dkato | 0:c41d29f1e0fb | 124 | } |
| dkato | 0:c41d29f1e0fb | 125 | event.setActionInfo((pidx << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_MOVE, time_cnt); |
| dkato | 1:5bfc165e8f08 | 126 | _callback.call(event); |
| dkato | 0:c41d29f1e0fb | 127 | } |
| dkato | 0:c41d29f1e0fb | 128 | } else { |
| dkato | 0:c41d29f1e0fb | 129 | // do nothing |
| dkato | 0:c41d29f1e0fb | 130 | } |
| dkato | 0:c41d29f1e0fb | 131 | touch_pos_last[0] = touch_pos[0]; |
| dkato | 0:c41d29f1e0fb | 132 | touch_pos_last[1] = touch_pos[1]; |
| dkato | 0:c41d29f1e0fb | 133 | touch_num_last = touch_num; |
| dkato | 0:c41d29f1e0fb | 134 | } |
| dkato | 0:c41d29f1e0fb | 135 | } |
| dkato | 0:c41d29f1e0fb | 136 | |
| dkato | 1:5bfc165e8f08 | 137 | void MotionEventConverter::touch_int_callback(void) { |
| dkato | 1:5bfc165e8f08 | 138 | sem_touch_int.release(); |
| dkato | 1:5bfc165e8f08 | 139 | } |
| dkato | 0:c41d29f1e0fb | 140 | |
| dkato | 1:5bfc165e8f08 | 141 | |
| dkato | 1:5bfc165e8f08 | 142 |