1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
diff --git a/apps/gui/bitmap/list.c b/apps/gui/bitmap/list.c
index 4323bb0..ec9365c 100644
--- a/apps/gui/bitmap/list.c
+++ b/apps/gui/bitmap/list.c
@@ -52,9 +52,10 @@ static struct viewport list_text[NB_SCREENS], title_text[NB_SCREENS];
 #define SCROLL_BEGIN_THRESHOLD 3 

 static enum {
-    SCROLL_NONE,    /* no scrolling */
-    SCROLL_BAR,     /* scroll by using the scrollbar */
-    SCROLL_SWIPE,   /* scroll by wiping over the screen */
+    SCROLL_NONE,            /* no scrolling */
+    SCROLL_BAR,             /* scroll by using the scrollbar */
+    SCROLL_SWIPE,           /* scroll by wiping over the screen */
+    SCROLL_KINETIC,         /* state after releasing swipe */
 } scroll_mode;

 static int y_offset;
@@ -162,12 +163,13 @@ void list_draw(struct screen *display, struct gui_synclist *list)
         list_text_vp->height -= line_height;
     }

+    const int nb_lines = viewport_get_nb_lines(list_text_vp);

     start = list_start_item;
-    end = start + viewport_get_nb_lines(list_text_vp);
+    end = start + nb_lines;

 #ifdef HAVE_TOUCHSCREEN
-    if (list->selected_item == 0)
+    if (list->selected_item == 0 || (list->nb_items < nb_lines))
         y_offset = 0; /* reset in case it's a new list */

     int draw_offset = y_offset;
@@ -187,12 +189,11 @@ void list_draw(struct screen *display, struct gui_synclist *list)
 #endif

     /* draw the scrollbar if its needed */
-    if (global_settings.scrollbar &&
-        viewport_get_nb_lines(list_text_vp) < list->nb_items)
+    if (global_settings.scrollbar && nb_lines < list->nb_items)
     {
         struct viewport vp = *list_text_vp;
         vp.width = SCROLLBAR_WIDTH;
-        vp.height = line_height * viewport_get_nb_lines(list_text_vp);
+        vp.height = line_height * nb_lines;
         vp.x = parent->x;
         list_text_vp->width -= SCROLLBAR_WIDTH;
         if (scrollbar_in_left)
@@ -202,7 +203,7 @@ void list_draw(struct screen *display, struct gui_synclist *list)
         display->set_viewport(&vp);
         gui_scrollbar_draw(display,
                 (scrollbar_in_left? 0: 1), 0, SCROLLBAR_WIDTH-1, vp.height,
-                list->nb_items, list_start_item, list_start_item + end-start,
+                list->nb_items, list_start_item, list_start_item + nb_lines,
                 VERTICAL);
     }
     else if (show_title)
@@ -359,8 +360,7 @@ static bool released = false;
  */
 static int last_position=0;

-
-static int gui_synclist_touchscreen_scrollbar(struct gui_synclist * gui_list,
+static int scrollbar_scroll(struct gui_synclist * gui_list,
                                               int y)
 {
     const int screen = screens[SCREEN_MAIN].screen_type;
@@ -391,25 +391,104 @@ static int gui_synclist_touchscreen_scrollbar(struct gui_synclist * gui_list,
     return ACTION_NONE;
 }

+/* kinetic scrolling, based on
+ *
+ * v = a*t + v0 and ds = v*dt
+ *
+ * In each (fixed interval) timeout, the list is advanced by ds, then
+ * the v is reduced by a.
+ * This way we get a linear and smooth deceleration of the scrolling
+ *
+ * As v is the difference of distance per time unit, v is passed (as
+ * pixels moved since the last call) to the scrolling function which takes
+ * care of the pixel accurate drawing
+ *
+ * v0 is dertermined by averaging the last 4 movements of the list
+ * (the pixel and time difference is used to compute each v)
+ *
+ * influenced by http://stechz.com/tag/kinetic/
+ * We take the easy and smooth first approach (until section "Drawbacks"),
+ * since its drawbacks don't apply for us since our timers seem to be
+ * relatively accurate
+ */
+
+
+#define SIGN(a) ((a) < 0 ? -1 : 1)
+/* these could possibly be configurable */
+#define RELOAD_INTERVAL (HZ/25)
+#define DECELERATION (1000*RELOAD_INTERVAL/HZ)
+
+/* this array holds data to compute the initial velocity v0 */
+static struct kinetic_info {
+    int difference;
+    long ticks;
+} kinetic_data[4];
+static size_t cur_idx;
+
+static struct cb_data {
+    struct gui_synclist *list;  /* current list */
+    int velocity;               /* in pixel/s */
+} cb_data;
+
+/* data member points to the above struct */
+static struct timeout kinetic_tmo;
+
+static bool is_kinetic_over(void)
+{
+    return !cb_data.velocity && (scroll_mode == SCROLL_KINETIC);
+}
+
 /*
- * returns the number of pixel scrolled since the last call
+ * collect data about how fast the list is moved in order to compute
+ * the initial velocity from it later */
+static void kinetic_stats_collect(const int difference)
+{
+    static long last_tick;
+    /* collect velocity statistics */
+    kinetic_data[cur_idx].difference = difference;
+    kinetic_data[cur_idx].ticks = current_tick - last_tick;
+
+    last_tick = current_tick;
+    cur_idx += 1;
+    if (cur_idx >= ARRAYLEN(kinetic_data))
+        cur_idx = 0; /* rewind the index */
+}
+
+/*
+ * resets the statistic */
+static void kinetic_stats_reset(void)
+{
+    memset(kinetic_data, 0, sizeof(kinetic_data));
+    cur_idx = 0;
+}
+
+/* cancels all currently active kinetic scrolling */
+static void kinetic_force_stop(void)
+{
+    timeout_cancel(&kinetic_tmo);
+    kinetic_stats_reset();
+}
+
+/*
+ * returns false if scrolling should be stopped entirely
+ *
+ * otherwise it returns true even if it didn't actually scroll,
+ * but scrolling mode shouldn't be changed
  **/
-static int gui_synclist_touchscreen_scrolling(struct gui_synclist * gui_list, int line_height, int position)
+static bool swipe_scroll(struct gui_synclist * gui_list, int line_height, int difference)
 {
     /* fixme */
     const enum screen_type screen = screens[SCREEN_MAIN].screen_type;
     const int nb_lines = viewport_get_nb_lines(&list_text[screen]);
-    /* in pixels */
-    const int difference = position - last_position;

     /* make selecting items easier */
     if (abs(difference) < SCROLL_BEGIN_THRESHOLD && scroll_mode == SCROLL_NONE)
-        return 0;
+        return false;

     /* does the list even scroll? if no, return but still show
      * the caller that we would scroll */
     if (nb_lines >= gui_list->nb_items)
-        return difference;
+        return true;

     const int old_start = gui_list->start_item[screen];
     int new_start_item = -1;
@@ -420,7 +499,8 @@ static int gui_synclist_touchscreen_scrolling(struct gui_synclist * gui_list, in
      || (old_start == (gui_list->nb_items - nb_lines) && difference < 0))
     {
         y_offset = 0;
-        return difference;
+        gui_list->start_item[screen] = old_start;
+        return scroll_mode != SCROLL_KINETIC; /* stop kinetic at the edges */
     }

     /* add up y_offset over time and translate to lines
@@ -441,10 +521,77 @@ static int gui_synclist_touchscreen_scrolling(struct gui_synclist * gui_list, in
         /* set new_start_item to 0 if it's negative */
         if(new_start_item < 0)
             new_start_item = 0;
+
         gui_list->start_item[screen] = new_start_item;
     }

-    return difference;
+    return true;
+}
+
+static int kinetic_callback(struct timeout *tmo)
+{
+    /* cancel if screen was pressed */
+    if (scroll_mode != SCROLL_KINETIC)
+        return 0;
+
+    struct cb_data *data = (struct cb_data*)tmo->data;
+    int line_height = font_get(data->list->parent[0]->font)->height;
+    /* ds = v*dt */
+    int pixel_diff = data->velocity * RELOAD_INTERVAL / HZ;
+    /* remember signedness to detect stopping */
+    int old_sign = SIGN(data->velocity);
+    /* advance the list */
+    if (!swipe_scroll(data->list, line_height, pixel_diff))
+    {
+        /* nothing to scroll? */
+        data->velocity = 0;
+    }
+    else
+    {
+        /* decelerate by a fixed amount
+         * decrementing v0 over time by the deceleration is
+         * equivalent to computing v = a*t + v0 */
+        data->velocity -= SIGN(data->velocity)*DECELERATION;
+        if (SIGN(data->velocity) != old_sign)
+            data->velocity = 0;
+    }
+
+    /* stop if the velocity hit or crossed zero */
+    if (!data->velocity)
+        kinetic_stats_reset();
+    /* let get_action() timeout, which loads to a
+     * gui_synclist_draw() call from the main thread */
+    queue_post(&button_queue, BUTTON_TOUCHSCREEN, 0);
+    return data->velocity ? RELOAD_INTERVAL : 0; /* cancel or reload */
+}
+
+/*
+ * computes the initial velocity v0 and sets up the timer */
+static bool kinetic_setup_scroll(struct gui_synclist *list)
+{
+    /* compute initial velocity */
+    int i, _i, v0, len = ARRAYLEN(kinetic_data);
+    for(i = 0, _i = 0, v0 = 0; i < len; i++)
+    {   /* in pixel/s */
+        if (kinetic_data[i].ticks > 0)
+        {
+            v0 += kinetic_data[i].difference*HZ/kinetic_data[i].ticks;
+            _i++;
+        }
+    }
+    if (_i > 0)
+        v0 /= _i;
+    else
+        v0 = 0;
+
+    if (v0 != 0)
+    {
+        cb_data.list = list;
+        cb_data.velocity = v0;
+        timeout_register(&kinetic_tmo, kinetic_callback, RELOAD_INTERVAL, (intptr_t)&cb_data);
+        return true;
+    }
+    return false;
 }

 unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
@@ -461,11 +608,22 @@ unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
     const bool show_cursor = !global_settings.cursor_style &&
                         gui_list->show_selection_marker;
     const bool on_title_clicked = show_title && y < line_height;
+    const bool cancelled_kinetic = (scroll_mode == SCROLL_KINETIC
+                        && button != ACTION_NONE && button != ACTION_UNKNOWN
+                        && !is_kinetic_over());
     int icon_width = 0;
     int line, list_width = list_text_vp->width;

+    released = (button&BUTTON_REL) != 0;
+
     if (button == ACTION_NONE || button == ACTION_UNKNOWN)
+    {
+        /* this happens when we hit edges of the list while kinetic scrolling,
+         * but not when manually cancelling */
+        if (scroll_mode == SCROLL_KINETIC)
+            return ACTION_REDRAW;
         return ACTION_NONE;
+    }

     /* x and y are relative to info_vp */
     if (global_settings.show_icons)
@@ -473,25 +631,23 @@ unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
     if (show_cursor)
         icon_width += get_icon_width(screen);

-    released = (button&BUTTON_REL) != 0;
-
-    if (button == BUTTON_NONE)
-        return ACTION_NONE;
-
     if (on_title_clicked)
     {
-        if (x < icon_width)
+        if (scroll_mode == SCROLL_NONE || is_kinetic_over())
         {
-            /* Top left corner is GO_TO_ROOT */
-            if (button == BUTTON_REL)
-                return ACTION_STD_MENU;
-            else if (button == (BUTTON_REPEAT|BUTTON_REL))
-                return ACTION_STD_CONTEXT;
-            return ACTION_NONE;
+            if (x < icon_width)
+            {
+                /* Top left corner is GO_TO_ROOT */
+                if (button == BUTTON_REL)
+                    return ACTION_STD_MENU;
+                else if (button == (BUTTON_REPEAT|BUTTON_REL))
+                    return ACTION_STD_CONTEXT;
+                return ACTION_NONE;
+            }
+            else /* click on title text is cancel */
+                if (button == BUTTON_REL)
+                    return ACTION_STD_CANCEL;
         }
-        else /* click on title text is cancel */
-            if (button == BUTTON_REL && scroll_mode == SCROLL_NONE)
-                return ACTION_STD_CANCEL;
     }
     else /* list area clicked */
     {
@@ -520,7 +676,7 @@ unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
             (on_scrollbar_clicked || scroll_mode == SCROLL_BAR))
         {
             scroll_mode = SCROLL_BAR;
-            return gui_synclist_touchscreen_scrollbar(gui_list, y);
+            return scrollbar_scroll(gui_list, y);
         }

         /* |--------------------------------------------------------|
@@ -540,7 +696,13 @@ unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
             /* selection needs to be corrected if an items are only
              * partially visible */
             line = (actual_y - y_offset) / line_height;
-            
+
+            if (cancelled_kinetic)
+            {
+                kinetic_force_stop();
+                scroll_mode = SCROLL_SWIPE;
+            }
+
             /* Pressed below the list*/
             if (list_start_item + line >= gui_list->nb_items)
             {
@@ -550,7 +712,7 @@ unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
                 return ACTION_NONE;
             }

-            if (released)
+            if (released && !cancelled_kinetic)
             {
                 /* Pen was released anywhere on the screen */
                 last_position = 0;
@@ -571,18 +733,22 @@ unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
                 {
                     /* we were scrolling
                      *  -> reset scrolling but do nothing else */
-                    scroll_mode = SCROLL_NONE;
+                    if (scroll_mode == SCROLL_SWIPE)
+                    {
+                        if (kinetic_setup_scroll(gui_list))
+                            scroll_mode = SCROLL_KINETIC;
+                    }
+                    if (scroll_mode != SCROLL_KINETIC)
+                        scroll_mode = SCROLL_NONE;
                     return ACTION_NONE;
                 }
             }
             else
             {   /* pen is on the screen */
-                int result = 0;
-                bool redraw = false;
-
+                bool redraw = false, result = false;
                 /* beginning of list interaction denoted by release in
                  * the previous call */
-                if (old_released)
+                if (old_released || is_kinetic_over())
                 {
                     scroll_mode = SCROLL_NONE;
                     redraw = true;
@@ -593,7 +759,12 @@ unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
                 if (last_position == 0)
                     last_position = actual_y;
                 else
-                    result = gui_synclist_touchscreen_scrolling(gui_list, line_height, actual_y);
+                {
+                    /* record speed data in case we do kinetic scrolling */
+                    int diff = actual_y - last_position;
+                    kinetic_stats_collect(diff);
+                    result = swipe_scroll(gui_list, line_height, diff);
+                }

                 /* Start scrolling once the pen is moved without
                  * releasing it inbetween */
@@ -602,13 +773,12 @@ unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
                     redraw = true;
                     scroll_mode = SCROLL_SWIPE;
                 }
-
                 last_position = actual_y;

                 return redraw ? ACTION_REDRAW:ACTION_NONE;
             }
         }
     }
-    return ACTION_NONE;
+    return ACTION_REDRAW;
 }
 #endif
diff --git a/firmware/export/config.h b/firmware/export/config.h
index 8892600..7e1df9f 100644
--- a/firmware/export/config.h
+++ b/firmware/export/config.h
@@ -901,6 +901,13 @@ Lyre prototype 1 */
 #endif
 #endif /* HAVE_HEADPHONE_DETECTION */

+#ifdef HAVE_TOUCHSCREEN
+/* Timeout objects required if headphone detection is enabled */
+#ifndef INCLUDE_TIMEOUT_API
+#define INCLUDE_TIMEOUT_API
+#endif
+#endif /* HAVE_HEADPHONE_DETECTION */
+
 #if defined(HAVE_USB_CHARGING_ENABLE) && defined(HAVE_USBSTACK)
 /* USB charging support in the USB stack requires timeout objects */
 #ifndef INCLUDE_TIMEOUT_API