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@0:c41d29f1e0fb, 2016-06-28 (annotated)
- Committer:
- dkato
- Date:
- Tue Jun 28 13:06:53 2016 +0000
- Revision:
- 0:c41d29f1e0fb
- Child:
- 1:5bfc165e8f08
first commit
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 | 0:c41d29f1e0fb | 21 | void MotionEventConverter::Process(TouchKey * p_touch, int (*cb_func)(MotionEvent event)) { |
| dkato | 0:c41d29f1e0fb | 22 | memset(touch_pos, 0, sizeof(touch_pos)); |
| dkato | 0:c41d29f1e0fb | 23 | memset(touch_pos_last, 0, sizeof(touch_pos_last)); |
| dkato | 0:c41d29f1e0fb | 24 | time_cnt = 0; |
| dkato | 0:c41d29f1e0fb | 25 | touch_num = 0; |
| dkato | 0:c41d29f1e0fb | 26 | touch_num_last = 0; |
| dkato | 0:c41d29f1e0fb | 27 | |
| dkato | 0:c41d29f1e0fb | 28 | p_touch->Init(); |
| dkato | 0:c41d29f1e0fb | 29 | t.reset(); |
| dkato | 0:c41d29f1e0fb | 30 | t.start(); |
| dkato | 0:c41d29f1e0fb | 31 | |
| dkato | 0:c41d29f1e0fb | 32 | while (1) { |
| dkato | 0:c41d29f1e0fb | 33 | p_touch->WaitInt(); |
| dkato | 0:c41d29f1e0fb | 34 | // Get Coordinates |
| dkato | 0:c41d29f1e0fb | 35 | touch_num = p_touch->GetCoordinates(MotionEvent::TOUCH_NUM_MAX, &touch_pos[0]); |
| dkato | 0:c41d29f1e0fb | 36 | time_cnt = t.read_ms(); |
| dkato | 0:c41d29f1e0fb | 37 | event.clearPointerCount(); |
| dkato | 0:c41d29f1e0fb | 38 | if ((touch_num_last == 0) && (touch_num >= MULTI_TOUCH_NUM)) { |
| dkato | 0:c41d29f1e0fb | 39 | // Two points are pressed at the same time |
| dkato | 0:c41d29f1e0fb | 40 | event.setPosData(0, touch_pos[0].x, touch_pos[0].y); |
| dkato | 0:c41d29f1e0fb | 41 | event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_DOWN, time_cnt); |
| dkato | 0:c41d29f1e0fb | 42 | cb_func(event); |
| dkato | 0:c41d29f1e0fb | 43 | |
| dkato | 0:c41d29f1e0fb | 44 | event.setPosData(1, touch_pos[1].x, touch_pos[1].y); |
| dkato | 0:c41d29f1e0fb | 45 | event.setActionInfo((1 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_DOWN, time_cnt); |
| dkato | 0:c41d29f1e0fb | 46 | cb_func(event); |
| dkato | 0:c41d29f1e0fb | 47 | } else if ((touch_num_last == 0) && (touch_num == 1)) { |
| dkato | 0:c41d29f1e0fb | 48 | // It was pressed only one point |
| dkato | 0:c41d29f1e0fb | 49 | if (touch_pos[0].valid != false) { |
| dkato | 0:c41d29f1e0fb | 50 | pidx = 0; |
| dkato | 0:c41d29f1e0fb | 51 | } else { |
| dkato | 0:c41d29f1e0fb | 52 | pidx = 1; |
| dkato | 0:c41d29f1e0fb | 53 | } |
| dkato | 0:c41d29f1e0fb | 54 | event.setPosData(pidx, touch_pos[pidx].x, touch_pos[pidx].y); |
| dkato | 0:c41d29f1e0fb | 55 | event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_DOWN, time_cnt); |
| dkato | 0:c41d29f1e0fb | 56 | cb_func(event); |
| dkato | 0:c41d29f1e0fb | 57 | } else if ((touch_num_last == 1) && (touch_num == 2)) { |
| dkato | 0:c41d29f1e0fb | 58 | // The second point is pressed |
| dkato | 0:c41d29f1e0fb | 59 | event.setPosData(0, touch_pos[0].x, touch_pos[0].y); |
| dkato | 0:c41d29f1e0fb | 60 | event.setPosData(1, touch_pos[1].x, touch_pos[1].y); |
| dkato | 0:c41d29f1e0fb | 61 | if (touch_pos[0].valid != touch_pos_last[0].valid) { |
| dkato | 0:c41d29f1e0fb | 62 | pidx = 0; |
| dkato | 0:c41d29f1e0fb | 63 | } else { |
| dkato | 0:c41d29f1e0fb | 64 | pidx = 1; |
| dkato | 0:c41d29f1e0fb | 65 | } |
| dkato | 0:c41d29f1e0fb | 66 | event.setActionInfo((pidx << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_DOWN, time_cnt); |
| dkato | 0:c41d29f1e0fb | 67 | cb_func(event); |
| dkato | 0:c41d29f1e0fb | 68 | } else if ((touch_num_last >= MULTI_TOUCH_NUM) && (touch_num == 0)) { |
| dkato | 0:c41d29f1e0fb | 69 | // Two points are released at the same time |
| dkato | 0:c41d29f1e0fb | 70 | event.setPosData(0, touch_pos_last[0].x, touch_pos_last[0].y); |
| dkato | 0:c41d29f1e0fb | 71 | event.setPosData(1, touch_pos_last[1].x, touch_pos_last[1].y); |
| dkato | 0:c41d29f1e0fb | 72 | event.setActionInfo((1 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_UP, time_cnt); |
| dkato | 0:c41d29f1e0fb | 73 | cb_func(event); |
| dkato | 0:c41d29f1e0fb | 74 | |
| dkato | 0:c41d29f1e0fb | 75 | event.clearPointerCount(); |
| dkato | 0:c41d29f1e0fb | 76 | event.setPosData(0, touch_pos_last[0].x, touch_pos_last[0].y); |
| dkato | 0:c41d29f1e0fb | 77 | event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_UP, time_cnt); |
| dkato | 0:c41d29f1e0fb | 78 | cb_func(event); |
| dkato | 0:c41d29f1e0fb | 79 | } else if ((touch_num_last == 1) && (touch_num == 0)) { |
| dkato | 0:c41d29f1e0fb | 80 | // The last of one point is released |
| dkato | 0:c41d29f1e0fb | 81 | if (touch_pos[0].valid != touch_pos_last[0].valid) { |
| dkato | 0:c41d29f1e0fb | 82 | pidx = 0; |
| dkato | 0:c41d29f1e0fb | 83 | } else { |
| dkato | 0:c41d29f1e0fb | 84 | pidx = 1; |
| dkato | 0:c41d29f1e0fb | 85 | } |
| dkato | 0:c41d29f1e0fb | 86 | event.setPosData(pidx, touch_pos_last[pidx].x, touch_pos_last[pidx].y); |
| dkato | 0:c41d29f1e0fb | 87 | event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_UP, time_cnt); |
| dkato | 0:c41d29f1e0fb | 88 | cb_func(event); |
| dkato | 0:c41d29f1e0fb | 89 | } else if ((touch_num_last >= MULTI_TOUCH_NUM) && (touch_num == 1)) { |
| dkato | 0:c41d29f1e0fb | 90 | // The second point is released |
| dkato | 0:c41d29f1e0fb | 91 | pidx = 0; |
| dkato | 0:c41d29f1e0fb | 92 | if (touch_pos[0].valid != false) { |
| dkato | 0:c41d29f1e0fb | 93 | event.setPosData(0, touch_pos[0].x, touch_pos[0].y); |
| dkato | 0:c41d29f1e0fb | 94 | } else { |
| dkato | 0:c41d29f1e0fb | 95 | event.setPosData(0, touch_pos_last[0].x, touch_pos_last[0].y); |
| dkato | 0:c41d29f1e0fb | 96 | } |
| dkato | 0:c41d29f1e0fb | 97 | if (touch_pos[1].valid != false) { |
| dkato | 0:c41d29f1e0fb | 98 | event.setPosData(1, touch_pos[1].x, touch_pos[1].y); |
| dkato | 0:c41d29f1e0fb | 99 | } else { |
| dkato | 0:c41d29f1e0fb | 100 | event.setPosData(1, touch_pos_last[1].x, touch_pos_last[1].y); |
| dkato | 0:c41d29f1e0fb | 101 | pidx = 1; |
| dkato | 0:c41d29f1e0fb | 102 | } |
| dkato | 0:c41d29f1e0fb | 103 | event.setActionInfo((pidx << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_UP, time_cnt); |
| dkato | 0:c41d29f1e0fb | 104 | cb_func(event); |
| dkato | 0:c41d29f1e0fb | 105 | } else if (touch_num != 0) { |
| dkato | 0:c41d29f1e0fb | 106 | // Check whether the position has moved |
| dkato | 0:c41d29f1e0fb | 107 | bool moved = false; |
| dkato | 0:c41d29f1e0fb | 108 | for (int i = 0; i < MotionEvent::TOUCH_NUM_MAX; i++) { |
| dkato | 0:c41d29f1e0fb | 109 | if (touch_pos[i].valid != false) { |
| dkato | 0:c41d29f1e0fb | 110 | event.setPosData(i, touch_pos[i].x, touch_pos[i].y); |
| dkato | 0:c41d29f1e0fb | 111 | if ((touch_pos[i].x != touch_pos_last[i].x) || (touch_pos[i].y != touch_pos_last[i].y)) { |
| dkato | 0:c41d29f1e0fb | 112 | moved = true; |
| dkato | 0:c41d29f1e0fb | 113 | pidx = i; |
| dkato | 0:c41d29f1e0fb | 114 | } |
| dkato | 0:c41d29f1e0fb | 115 | } |
| dkato | 0:c41d29f1e0fb | 116 | } |
| dkato | 0:c41d29f1e0fb | 117 | if (moved != false) { |
| dkato | 0:c41d29f1e0fb | 118 | if (touch_num >= MULTI_TOUCH_NUM) { |
| dkato | 0:c41d29f1e0fb | 119 | pidx = 0; |
| dkato | 0:c41d29f1e0fb | 120 | } |
| dkato | 0:c41d29f1e0fb | 121 | event.setActionInfo((pidx << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_MOVE, time_cnt); |
| dkato | 0:c41d29f1e0fb | 122 | cb_func(event); |
| dkato | 0:c41d29f1e0fb | 123 | } |
| dkato | 0:c41d29f1e0fb | 124 | } else { |
| dkato | 0:c41d29f1e0fb | 125 | // do nothing |
| dkato | 0:c41d29f1e0fb | 126 | } |
| dkato | 0:c41d29f1e0fb | 127 | touch_pos_last[0] = touch_pos[0]; |
| dkato | 0:c41d29f1e0fb | 128 | touch_pos_last[1] = touch_pos[1]; |
| dkato | 0:c41d29f1e0fb | 129 | touch_num_last = touch_num; |
| dkato | 0:c41d29f1e0fb | 130 | } |
| dkato | 0:c41d29f1e0fb | 131 | } |
| dkato | 0:c41d29f1e0fb | 132 | |
| dkato | 0:c41d29f1e0fb | 133 |