1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2009-2010 by Thomas Martitz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
25 #include "backlight.h"
27 void scrollwheel(unsigned int wheel_value
)
30 /* current wheel values, parsed from dbop and the resulting button */
31 unsigned btn
= BUTTON_NONE
;
32 /* old wheel values */
33 static unsigned old_wheel_value
= 0;
34 static unsigned old_btn
= BUTTON_NONE
;
37 * Getting BUTTON_REPEAT works like this: Remember when the btn value was
38 * posted to the button_queue last, and if it was recent enough, generate
41 static long last_wheel_post
= 0;
44 * Providing wheel acceleration works as follows: We increment accel
45 * by 2 if the wheel was turned, and decrement it by 1 each tick
46 * (no matter if it was turned), that means: the longer and faster you turn,
47 * the higher accel will be. accel>>2 will actually posted to the button_queue
50 /* We only post every 4th action, as this matches better with the physical
51 * clicks of the wheel */
52 static int counter
= 0;
54 * Bits 13 and 14 of DBOP_DIN change as follows (Gray Code):
55 * Clockwise rotation 00 -> 01 -> 11 -> 10 -> 00
56 * Counter-clockwise 00 -> 10 -> 11 -> 01 -> 00
58 * For easy look-up, actual wheel values act as indicies also,
59 * which is why the table seems to be not ordered correctly
61 static const unsigned char wheel_tbl
[2][4] =
63 { 2, 0, 3, 1 }, /* Clockwise rotation */
64 { 1, 3, 0, 2 }, /* Counter-clockwise */
73 if (old_wheel_value
== wheel_tbl
[0][wheel_value
])
74 btn
= BUTTON_SCROLL_FWD
;
75 else if (old_wheel_value
== wheel_tbl
[1][wheel_value
])
76 btn
= BUTTON_SCROLL_BACK
;
77 else if (old_wheel_value
!= wheel_value
&& accel
> ACCEL_INCREMENT
)
78 { /* if no button is read and wheel_value changed, assume old_btn */
81 /* else btn = BUTTON_NONE */
83 if (btn
!= BUTTON_NONE
)
87 /* direction reversals nullify acceleration and counters */
91 /* wheel_delta will cause lists to jump over items,
92 * we want this for fast scrolling, but we must keep it accurate
93 * for slow scrolling */
95 /* generate BUTTON_REPEAT if quick enough, scroll slightly faster too*/
96 if (TIME_BEFORE(current_tick
, last_wheel_post
+ WHEEL_REPEAT_INTERVAL
))
99 wheel_delta
= accel
>>ACCEL_SHIFT
;
102 accel
+= ACCEL_INCREMENT
;
104 /* the wheel is more reliable if we don't send every change,
105 * every WHEEL_COUNTER_DIVth is basically one "physical click"
106 * which should make up 1 item in lists */
107 if (++counter
>= WHEEL_COUNTER_DIV
&& queue_empty(&button_queue
))
111 queue_post(&button_queue
, btn
, ((wheel_delta
+1)<<24));
112 /* message posted - reset count and remember post */
114 last_wheel_post
= current_tick
;
120 old_wheel_value
= wheel_value
;