The rapper class to move it like MotionEvent in Android.

Files at this revision

API Documentation at this revision

Comitter:
dkato
Date:
Wed Jun 29 05:33:35 2016 +0000
Parent:
0:c41d29f1e0fb
Commit message:
update

Changed in this revision

MotionEventConverter.cpp Show annotated file Show diff for this revision Revisions of this file
MotionEventConverter.h Show annotated file Show diff for this revision Revisions of this file
--- a/MotionEventConverter.cpp	Tue Jun 28 13:06:53 2016 +0000
+++ b/MotionEventConverter.cpp	Wed Jun 29 05:33:35 2016 +0000
@@ -18,19 +18,23 @@
 
 #define MULTI_TOUCH_NUM         (2)
 
-void MotionEventConverter::Process(TouchKey * p_touch, int (*cb_func)(MotionEvent event)) {
+MotionEventConverter::MotionEventConverter() : sem_touch_int(0) {
+}
+
+void MotionEventConverter::touch_process(TouchKey * p_touch) {
     memset(touch_pos, 0, sizeof(touch_pos));
     memset(touch_pos_last, 0, sizeof(touch_pos_last));
     time_cnt = 0;
     touch_num = 0;
     touch_num_last = 0;
 
-    p_touch->Init();
+    p_touch->SetCallback(this, &MotionEventConverter::touch_int_callback);
+    p_touch->Reset();
     t.reset();
     t.start();
 
     while (1) {
-        p_touch->WaitInt();
+        sem_touch_int.wait();
         // Get Coordinates
         touch_num = p_touch->GetCoordinates(MotionEvent::TOUCH_NUM_MAX, &touch_pos[0]);
         time_cnt = t.read_ms();
@@ -39,11 +43,11 @@
             // Two points are pressed at the same time
             event.setPosData(0, touch_pos[0].x, touch_pos[0].y);
             event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_DOWN, time_cnt);
-            cb_func(event);
+            _callback.call(event);
 
             event.setPosData(1, touch_pos[1].x, touch_pos[1].y);
             event.setActionInfo((1 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_DOWN, time_cnt);
-            cb_func(event);
+            _callback.call(event);
         } else if ((touch_num_last == 0) && (touch_num == 1)) {
             // It was pressed only one point
             if (touch_pos[0].valid != false) {
@@ -53,7 +57,7 @@
             }
             event.setPosData(pidx, touch_pos[pidx].x, touch_pos[pidx].y);
             event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_DOWN, time_cnt);
-            cb_func(event);
+            _callback.call(event);
         } else if ((touch_num_last == 1) && (touch_num == 2)) {
             // The second point is pressed
             event.setPosData(0, touch_pos[0].x, touch_pos[0].y);
@@ -64,18 +68,18 @@
                 pidx = 1;
             }
             event.setActionInfo((pidx << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_DOWN, time_cnt);
-            cb_func(event);
+            _callback.call(event);
         } else if ((touch_num_last >= MULTI_TOUCH_NUM) && (touch_num == 0)) {
             // Two points are released at the same time
             event.setPosData(0, touch_pos_last[0].x, touch_pos_last[0].y);
             event.setPosData(1, touch_pos_last[1].x, touch_pos_last[1].y);
             event.setActionInfo((1 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_UP, time_cnt);
-            cb_func(event);
+            _callback.call(event);
 
             event.clearPointerCount();
             event.setPosData(0, touch_pos_last[0].x, touch_pos_last[0].y);
             event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_UP, time_cnt);
-            cb_func(event);
+            _callback.call(event);
         } else if ((touch_num_last == 1) && (touch_num == 0)) {
             // The last of one point is released
             if (touch_pos[0].valid != touch_pos_last[0].valid) {
@@ -85,7 +89,7 @@
             }
             event.setPosData(pidx, touch_pos_last[pidx].x, touch_pos_last[pidx].y);
             event.setActionInfo((0 << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_UP, time_cnt);
-            cb_func(event);
+            _callback.call(event);
         } else if ((touch_num_last >= MULTI_TOUCH_NUM) && (touch_num == 1)) {
             // The second point is released
             pidx = 0;
@@ -101,7 +105,7 @@
                 pidx = 1;
             }
             event.setActionInfo((pidx << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_POINTER_UP, time_cnt);
-            cb_func(event);
+            _callback.call(event);
         } else if (touch_num != 0) {
             // Check whether the position has moved
             bool moved = false;
@@ -119,7 +123,7 @@
                     pidx = 0;
                 }
                 event.setActionInfo((pidx << MotionEvent::ACTION_POINTER_INDEX_SHIFT) | ACTION_MOVE, time_cnt);
-                cb_func(event);
+                _callback.call(event);
             }
         } else {
             // do nothing
@@ -130,4 +134,9 @@
     }
 }
 
+void MotionEventConverter::touch_int_callback(void) {
+    sem_touch_int.release();
+}
 
+
+
--- a/MotionEventConverter.h	Tue Jun 28 13:06:53 2016 +0000
+++ b/MotionEventConverter.h	Wed Jun 29 05:33:35 2016 +0000
@@ -32,22 +32,48 @@
 class MotionEventConverter {
 
 public:
+    /** Constructor
+     *
+     */
+    MotionEventConverter();
+
     /** Run the touch panel process.
      *
+     *  @param fptr A pointer to a void function
+     *  @param p_touch pointer of TouchKey class
+     */
+    void Process(int (*fptr)(MotionEvent event), TouchKey * p_touch) {
+        _callback.attach(fptr);
+        touch_process(p_touch);
+    }
+
+    /** Run the touch panel process.
      *
-     * @param cb_func Callback function.
+     *  @param tptr pointer to the object to call the member function on
+     *  @param mptr pointer to the member function to be called
+     *  @param p_touch pointer of TouchKey class
      */
-    void Process(TouchKey * p_touch, int (*cb_func)(MotionEvent event));
+    template<typename T>
+    void Process(T* tptr, int (T::*mptr)(MotionEvent event), TouchKey * p_touch) {
+        _callback.attach(tptr, mptr);
+        touch_process(p_touch);
+    }
 
 private:
+    TouchKey *            p_touch;
     MotionEventCtl        event;
     Timer                 t;
+    Semaphore             sem_touch_int;
     TouchKey::touch_pos_t touch_pos[MotionEvent::TOUCH_NUM_MAX];
     TouchKey::touch_pos_t touch_pos_last[MotionEvent::TOUCH_NUM_MAX];
     uint32_t              time_cnt;
     int                   pidx;
     uint8_t               touch_num;
     uint8_t               touch_num_last;
+    FunctionPointerArg1<int, MotionEvent>  _callback;
+
+    void touch_process(TouchKey * p_touch);
+    void touch_int_callback(void);
 };
 
 #endif