as3525: make sure we don't use a negative number of sectors
[kugel-rb.git] / firmware / target / arm / as3525 / scrollwheel-as3525.c
blobd67e097ae8ed755716c13b43bc1b526303223759
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include "config.h"
23 #include "button.h"
24 #include "kernel.h"
25 #include "backlight.h"
27 void scrollwheel(unsigned int wheel_value)
29 #ifndef BOOTLOADER
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
39 * BUTTON_REPEAT
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
49 static int accel = 0;
50 /* We only post every 4th action, as this matches better with the physical
51 * clicks of the wheel */
52 static int counter = 0;
53 /* Read wheel
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 */
67 if(button_hold())
69 accel = counter = 0;
70 return;
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 */
79 btn = old_btn;
81 /* else btn = BUTTON_NONE */
83 if (btn != BUTTON_NONE)
85 if (btn != old_btn)
87 /* direction reversals nullify acceleration and counters */
88 old_btn = btn;
89 accel = counter = 0;
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 */
94 int wheel_delta = 0;
95 /* generate BUTTON_REPEAT if quick enough, scroll slightly faster too*/
96 if (TIME_BEFORE(current_tick, last_wheel_post + WHEEL_REPEAT_INTERVAL))
98 btn |= BUTTON_REPEAT;
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))
109 buttonlight_on();
110 backlight_on();
111 queue_post(&button_queue, btn, ((wheel_delta+1)<<24));
112 /* message posted - reset count and remember post */
113 counter = 0;
114 last_wheel_post = current_tick;
117 if (accel > 0)
118 accel--;
120 old_wheel_value = wheel_value;
121 #else
122 (void)wheel_value;
123 #endif