From 910c890ab07157cdf4710194d98a45a7fcf4cee3 Mon Sep 17 00:00:00 2001
From: Uros Majstorovic <majstor@majstor.org>
Date: Mon, 8 Jun 2020 14:25:36 +0200
Subject: touch extended tracking refactor

---
 code/fe310/eos/eve/eve.c          |  6 ++--
 code/fe310/eos/eve/eve_platform.c |  2 +-
 code/fe310/eos/eve/eve_platform.h |  2 +-
 code/fe310/eos/eve/eve_touch.c    | 72 +++++++++++++++++++++++++++------------
 code/fe310/eos/eve/eve_touch.h    | 20 +++++++++--
 code/fe310/eos/eve/eve_track.c    | 55 +++++++++---------------------
 code/fe310/eos/eve/eve_track.h    | 17 ++-------
 7 files changed, 91 insertions(+), 83 deletions(-)

(limited to 'code')

diff --git a/code/fe310/eos/eve/eve.c b/code/fe310/eos/eve/eve.c
index 8d8a15a..0ab5427 100644
--- a/code/fe310/eos/eve/eve.c
+++ b/code/fe310/eos/eve/eve.c
@@ -381,9 +381,9 @@ int eve_init(int pwr_on) {
         eve_active();
     }
 
-    eve_init_touch();
-    eve_init_track();
-    eve_init_platform();
+    eve_touch_init();
+    eve_track_init();
+    eve_platform_init();
 
     return EVE_OK;
 }
diff --git a/code/fe310/eos/eve/eve_platform.c b/code/fe310/eos/eve/eve_platform.c
index 2c6c319..598b932 100644
--- a/code/fe310/eos/eve/eve_platform.c
+++ b/code/fe310/eos/eve/eve_platform.c
@@ -47,7 +47,7 @@ uint64_t eve_time_get_tick(void) {
     return eos_time_get_tick();
 }
 
-void eve_init_platform(void) {
+void eve_platform_init(void) {
     eos_evtq_set_handler(EOS_EVT_UI, handle_evt);
     eos_timer_set_handler(EOS_TIMER_ETYPE_UI, handle_time);
 
diff --git a/code/fe310/eos/eve/eve_platform.h b/code/fe310/eos/eve/eve_platform.h
index 5d67020..0fb8081 100644
--- a/code/fe310/eos/eve/eve_platform.h
+++ b/code/fe310/eos/eve/eve_platform.h
@@ -24,4 +24,4 @@ void eve_timer_set(uint32_t ms);
 void eve_timer_clear(void);
 uint64_t eve_time_get_tick(void);
 
-void eve_init_platform(void);
\ No newline at end of file
+void eve_platform_init(void);
\ No newline at end of file
diff --git a/code/fe310/eos/eve/eve_touch.c b/code/fe310/eos/eve/eve_touch.c
index 76fd526..cbeae87 100644
--- a/code/fe310/eos/eve/eve_touch.c
+++ b/code/fe310/eos/eve/eve_touch.c
@@ -2,7 +2,6 @@
 #include <string.h>
 
 #include "eve.h"
-#include "eve_platform.h"
 
 #define EVE_THRESHOLD_X         5
 #define EVE_THRESHOLD_Y         5
@@ -21,6 +20,7 @@ static uint8_t _tag0;
 
 static EVETouch _touch[EVE_MAX_TOUCH];
 static EVETouchTimer _touch_timer;
+static EVEExtTracker _ext_tracker;
 
 static eve_touch_handler_t _touch_handler;
 static void *_touch_handler_param;
@@ -49,17 +49,8 @@ static const uint32_t _reg_track[] = {
     REG_TRACKER_4
 };
 
-void eve_init_touch(void) {
-    int i;
-
-    for (i=0; i<EVE_MAX_TOUCH; i++) {
-        EVETouch *touch = &_touch[i];
-        touch->x = EVE_NOTOUCH;
-        touch->y = EVE_NOTOUCH;
-    }
-    eve_write8(REG_INT_MASK, _intr_mask);
-    eve_write8(REG_INT_EN, 0x01);
-    while(eve_read8(REG_INT_FLAGS));
+static inline void _touch_evt_clear(EVETouch *touch) {
+    touch->evt &= ~EVE_TOUCH_EVT_MASK;
 }
 
 static void _touch_timer_set(uint8_t tag, uint8_t idx, uint16_t evt, int x0, int y0, uint32_t to) {
@@ -92,7 +83,7 @@ void eve_handle_touch(void) {
         uint64_t now = 0;
         EVETouch *touch = &_touch[i];
 
-        touch->evt &= ~EVE_TOUCH_EVT_MASK;
+        _touch_evt_clear(touch);
         touch_xy = i < 4 ? eve_read32(_reg_touch[i]) : (((uint32_t)eve_read16(REG_CTOUCH_TOUCH4_X) << 16) | eve_read16(REG_CTOUCH_TOUCH4_Y));
 
         if (touch_xy != 0x80008000) {
@@ -113,8 +104,8 @@ void eve_handle_touch(void) {
                         }
                     }
                     if (_touch_timer.evt & EVE_TOUCH_ETYPE_TRACK) {
-                        EVETracker *tr= eve_track_get_tracker();
-                        if (tr->stop) tr->stop(&_touch_timer, touch);
+                        touch->evt |= EVE_TOUCH_ETYPE_TRACK_STOP;
+                        if (_ext_tracker.stop) _ext_tracker.stop(&_touch_timer, touch);
                     }
                     if (_touch_handler && (touch->evt & EVE_TOUCH_EVT_MASK)) {
                         _touch_handler(_touch_handler_param, _touch_timer.tag, i);
@@ -159,9 +150,8 @@ void eve_handle_touch(void) {
                 }
                 if (touch->tracker.tag && touch->tracker.track) {
                     if (!_touch_timer.tag && (_tag_opt[touch->tracker.tag] & EVE_TOUCH_OPT_TRACK_EXT)) {
-                        EVETracker *tr= eve_track_get_tracker();
                         _touch_timer_set(touch->tracker.tag, i, EVE_TOUCH_ETYPE_TRACK, touch->x, touch->y, EVE_TIMEOUT_TRACK);
-                        if (tr->init) tr->init(&_touch_timer, touch);
+                        if (_ext_tracker.init) _ext_tracker.init(&_touch_timer, touch);
                     } else {
                         touch->evt |= EVE_TOUCH_ETYPE_TRACK_STOP;
                     }
@@ -283,15 +273,15 @@ void eve_handle_time(void) {
             int _x = touch->x;
             int _y = touch->y;
 
-            touch->evt &= ~EVE_TOUCH_EVT_MASK;
+            _touch_evt_clear(touch);
             touch->evt |= _touch_timer.evt;
             if (touch->evt & EVE_TOUCH_ETYPE_TRACK) {
-                EVETracker *tr= eve_track_get_tracker();
-                if (tr->tick) more = tr->tick(&_touch_timer, touch);
+                if (_ext_tracker.tick) more = _ext_tracker.tick(&_touch_timer, touch);
                 if (more) {
                     eve_timer_set(EVE_TIMEOUT_TRACK);
-                } else if (tr->stop) {
-                    tr->stop(&_touch_timer, touch);
+                } else {
+                    touch->evt |= EVE_TOUCH_ETYPE_TRACK_STOP;
+                    if (_ext_tracker.stop) _ext_tracker.stop(&_touch_timer, touch);
                 }
             }
 
@@ -304,6 +294,19 @@ void eve_handle_time(void) {
     }
 }
 
+void eve_touch_init(void) {
+    int i;
+
+    for (i=0; i<EVE_MAX_TOUCH; i++) {
+        EVETouch *touch = &_touch[i];
+        touch->x = EVE_NOTOUCH;
+        touch->y = EVE_NOTOUCH;
+    }
+    eve_write8(REG_INT_MASK, _intr_mask);
+    eve_write8(REG_INT_EN, 0x01);
+    while(eve_read8(REG_INT_FLAGS));
+}
+
 void eve_touch_set_handler(eve_touch_handler_t handler, void *param) {
     _touch_handler = handler;
     _touch_handler_param = param;
@@ -362,3 +365,28 @@ void eve_touch_clear_opt(void) {
 EVETouchTimer *eve_touch_get_timer(void) {
     return &_touch_timer;
 }
+
+void eve_etrack_set(eve_etrack_init_t init, eve_etrack_tick_t tick, eve_etrack_stop_t stop, void *param) {
+    _ext_tracker.init = init;
+    _ext_tracker.tick = tick;
+    _ext_tracker.stop = stop;
+    _touch_timer.p = param;
+}
+
+void eve_etrack_start(int i) {
+    EVETouch *touch = &_touch[i];
+
+    _touch_timer_set(touch->tracker.tag, i, EVE_TOUCH_ETYPE_TRACK, touch->x, touch->y, EVE_TIMEOUT_TRACK);
+    if (_ext_tracker.init) _ext_tracker.init(&_touch_timer, touch);
+}
+
+void eve_etrack_stop(void) {
+    if (_ext_tracker.stop) {
+        EVETouch *touch = &_touch[_touch_timer.idx];
+
+        _touch_evt_clear(touch);
+        touch->evt |= EVE_TOUCH_ETYPE_TRACK_STOP;
+        _ext_tracker.stop(&_touch_timer, touch);
+    }
+    _touch_timer_clear();
+}
\ No newline at end of file
diff --git a/code/fe310/eos/eve/eve_touch.h b/code/fe310/eos/eve/eve_touch.h
index 1382ddc..656dd2f 100644
--- a/code/fe310/eos/eve/eve_touch.h
+++ b/code/fe310/eos/eve/eve_touch.h
@@ -44,6 +44,8 @@
 #define EVE_TOUCH_OPT_TRACK_MASK        (EVE_TOUCH_OPT_TRACK        | EVE_TOUCH_OPT_TRACK_REG)
 #define EVE_TOUCH_OPT_TIMER_MASK        (EVE_TOUCH_OPT_LPRESS       | EVE_TOUCH_OPT_DTAP)
 
+typedef void (*eve_touch_handler_t) (void *, uint8_t, int);
+
 typedef struct EVETouch {
     int x;
     int y;
@@ -72,15 +74,27 @@ typedef struct EVETouchTimer {
     void *p;
 } EVETouchTimer;
 
-typedef void (*eve_touch_handler_t) (void *, uint8_t, int);
-
-void eve_init_touch(void);
 void eve_handle_touch(void);
 void eve_handle_time(void);
 
+void eve_touch_init(void);
 void eve_touch_set_handler(eve_touch_handler_t handler, void *handler_param);
 EVETouch *eve_touch_evt(uint8_t tag0, int touch_idx, uint8_t tag_min, uint8_t tag_max, uint16_t *evt);
 void eve_touch_set_opt(uint8_t tag, uint8_t opt);
 uint8_t eve_touch_get_opt(uint8_t tag);
 void eve_touch_clear_opt(void);
 EVETouchTimer *eve_touch_get_timer(void);
+
+typedef void (*eve_etrack_init_t) (EVETouchTimer *, EVETouch *);
+typedef int  (*eve_etrack_tick_t) (EVETouchTimer *, EVETouch *);
+typedef void (*eve_etrack_stop_t) (EVETouchTimer *, EVETouch *);
+
+typedef struct EVEExtTracker {
+    eve_etrack_init_t init;
+    eve_etrack_tick_t tick;
+    eve_etrack_stop_t stop;
+} EVEExtTracker;
+
+void eve_etrack_set(eve_etrack_init_t init, eve_etrack_tick_t tick, eve_etrack_stop_t stop, void *param);
+void eve_etrack_start(int i);
+void eve_etrack_stop(void);
diff --git a/code/fe310/eos/eve/eve_track.c b/code/fe310/eos/eve/eve_track.c
index dccedad..97ef72b 100644
--- a/code/fe310/eos/eve/eve_track.c
+++ b/code/fe310/eos/eve/eve_track.c
@@ -2,45 +2,24 @@
 #include <math.h>
 
 #include "eve.h"
-#include "eve_platform.h"
 
-static EVETracker _tracker;
-
-void eve_init_track(void) {
-    eve_track_set_handler(eve_track_inert_init, eve_track_inert_tick, eve_track_stop, NULL);
-}
-
-EVETracker *eve_track_get_tracker(void) {
-    return &_tracker;
-}
-
-void eve_track_set_handler(eve_track_init_t init, eve_track_tick_t tick, eve_track_stop_t stop, void *param) {
-    if (stop == NULL) stop = eve_track_stop;
-
-    _tracker.init = init;
-    _tracker.tick = tick;
-    _tracker.stop = stop;
-    eve_touch_get_timer()->p = param;
+void eve_track_init(void) {
+    eve_etrack_set(eve_track_inert_init, eve_track_inert_tick, NULL, NULL);
 }
 
 void eve_track_set(uint8_t type, void *param) {
     switch (type) {
         case EVE_TRACK_TYPE_INERT:
-            eve_track_set_handler(eve_track_inert_init, eve_track_inert_tick, eve_track_stop, NULL);
+            eve_etrack_set(eve_track_inert_init, eve_track_inert_tick, NULL, NULL);
             break;
         case EVE_TRACK_TYPE_OSC:
-            eve_track_set_handler(NULL, eve_track_osc_tick, eve_track_stop, param);
+            eve_etrack_set(NULL, eve_track_osc_tick, NULL, param);
             break;
         default:
             break;
     }
 }
 
-void eve_track_stop(EVETouchTimer *timer, EVETouch *touch) {
-    touch->evt |= EVE_TOUCH_ETYPE_TRACK_STOP;
-}
-
-
 void eve_track_inert_init(EVETouchTimer *timer, EVETouch *touch) {
     double d = sqrt(touch->vx * touch->vx + touch->vy * touch->vy);
     int fc = (double)(EVE_RTC_FREQ) * d / EVE_TRACK_FRICTION;
@@ -62,6 +41,19 @@ int eve_track_inert_tick(EVETouchTimer *timer, EVETouch *touch) {
     return more;
 }
 
+void eve_track_osc_init(EVETrackOsc *p, int x, int y, uint32_t T, double d, uint32_t t_max) {
+    double f0 = 2 * M_PI / (T * EVE_RTC_FREQ / 1000);
+
+    if (d < 0) d = 0;
+    if (d > 1) d = 1;
+    p->x = x;
+    p->y = y;
+    p->f = d ? f0 * sqrt(1 - d * d) : f0;
+    p->d = d;
+    p->a = -d * f0;
+    p->t_max = t_max;
+}
+
 int eve_track_osc_tick(EVETouchTimer *timer, EVETouch *touch) {
     EVETrackOsc *p = (EVETrackOsc *)timer->p;
     int dt = eve_time_get_tick() - touch->t;
@@ -83,16 +75,3 @@ int eve_track_osc_tick(EVETouchTimer *timer, EVETouch *touch) {
     touch->y = p->y + ay * cos(p->f * dt);
     return more;
 }
-
-void eve_track_osc_init(EVETrackOsc *p, int x, int y, uint32_t T, double d, uint32_t t_max) {
-    double f0 = 2 * M_PI / (T * EVE_RTC_FREQ / 1000);
-
-    if (d < 0) d = 0;
-    if (d > 1) d = 1;
-    p->x = x;
-    p->y = y;
-    p->f = d ? f0 * sqrt(1 - d * d) : f0;
-    p->d = d;
-    p->a = -d * f0;
-    p->t_max = t_max;
-}
diff --git a/code/fe310/eos/eve/eve_track.h b/code/fe310/eos/eve/eve_track.h
index 07ddb44..a5fd113 100644
--- a/code/fe310/eos/eve/eve_track.h
+++ b/code/fe310/eos/eve/eve_track.h
@@ -4,16 +4,6 @@
 #define EVE_TRACK_TYPE_OSC              2
 #define EVE_TRACK_FRICTION              500
 
-typedef void (*eve_track_init_t) (EVETouchTimer *, EVETouch *);
-typedef int  (*eve_track_tick_t) (EVETouchTimer *, EVETouch *);
-typedef void (*eve_track_stop_t) (EVETouchTimer *, EVETouch *);
-
-typedef struct EVETracker {
-    eve_track_init_t init;
-    eve_track_tick_t tick;
-    eve_track_stop_t stop;
-} EVETracker;
-
 typedef struct EVETrackOsc {
     int x;
     int y;
@@ -23,13 +13,10 @@ typedef struct EVETrackOsc {
     uint32_t t_max;
 } EVETrackOsc;
 
-void eve_init_track(void);
-EVETracker *eve_track_get_tracker(void);
-void eve_track_set_handler(eve_track_init_t init, eve_track_tick_t tick, eve_track_stop_t stop, void *param);
+void eve_track_init(void);
 void eve_track_set(uint8_t type, void *param);
-void eve_track_stop(EVETouchTimer *timer, EVETouch *touch);
 
 void eve_track_inert_init(EVETouchTimer *timer, EVETouch *touch);
 int eve_track_inert_tick(EVETouchTimer *timer, EVETouch *touch);
-int eve_track_osc_tick(EVETouchTimer *timer, EVETouch *touch);
 void eve_track_osc_init(EVETrackOsc *p, int x, int y, uint32_t T, double d, uint32_t t_max);
+int eve_track_osc_tick(EVETouchTimer *timer, EVETouch *touch);
-- 
cgit v1.2.3