2006-10-14 12:14:23 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Barry Wardell
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Taken from button-h10.c by Barry Wardell and reverse engineering by MrH. */
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
#include "button.h"
|
|
|
|
#include "backlight.h"
|
|
|
|
|
2007-07-22 21:02:24 +00:00
|
|
|
#define WHEEL_REPEAT_INTERVAL 300000
|
|
|
|
#define WHEEL_FAST_ON_INTERVAL 20000
|
|
|
|
#define WHEEL_FAST_OFF_INTERVAL 60000
|
2007-05-11 08:09:49 +00:00
|
|
|
|
|
|
|
/* Clickwheel */
|
2007-05-25 22:41:44 +00:00
|
|
|
#ifndef BOOTLOADER
|
2007-05-11 08:09:49 +00:00
|
|
|
static unsigned int old_wheel_value = 0;
|
|
|
|
static unsigned int wheel_repeat = BUTTON_NONE;
|
|
|
|
static unsigned int wheel_click_count = 0;
|
2007-07-22 21:02:24 +00:00
|
|
|
static unsigned int wheel_delta = 0;
|
2007-05-11 08:09:49 +00:00
|
|
|
static int wheel_fast_mode = 0;
|
2007-07-22 21:02:24 +00:00
|
|
|
static unsigned long last_wheel_usec = 0;
|
|
|
|
static unsigned long wheel_velocity = 0;
|
|
|
|
static long last_wheel_post = 0;
|
|
|
|
static long next_backlight_on = 0;
|
2007-05-11 08:09:49 +00:00
|
|
|
/* Buttons */
|
|
|
|
static bool hold_button = false;
|
|
|
|
static bool hold_button_old = false;
|
2007-06-03 14:31:42 +00:00
|
|
|
#define _button_hold() hold_button
|
|
|
|
#else
|
|
|
|
#define _button_hold() ((GPIOF_INPUT_VAL & 0x80) != 0)
|
2007-05-25 22:41:44 +00:00
|
|
|
#endif /* BOOTLOADER */
|
2007-05-11 08:09:49 +00:00
|
|
|
static int int_btn = BUTTON_NONE;
|
2006-10-14 12:14:23 +00:00
|
|
|
|
2007-06-03 15:17:01 +00:00
|
|
|
void button_int(void);
|
|
|
|
|
2006-10-14 12:14:23 +00:00
|
|
|
void button_init_device(void)
|
|
|
|
{
|
2006-12-16 00:21:41 +00:00
|
|
|
/* Enable all buttons */
|
2007-05-11 08:09:49 +00:00
|
|
|
GPIOF_OUTPUT_EN &= ~0xff;
|
2006-10-14 12:14:23 +00:00
|
|
|
GPIOF_ENABLE |= 0xff;
|
2007-06-03 14:31:42 +00:00
|
|
|
|
2006-12-18 19:08:41 +00:00
|
|
|
/* Scrollwheel light - enable control through GPIOG pin 7 and set timeout */
|
|
|
|
GPIOG_OUTPUT_EN |= 0x80;
|
2007-05-11 08:09:49 +00:00
|
|
|
GPIOG_ENABLE = 0x80;
|
|
|
|
|
2007-05-25 22:41:44 +00:00
|
|
|
#ifndef BOOTLOADER
|
2007-06-03 15:17:01 +00:00
|
|
|
/* Mask these before performing init ... because init has possibly
|
|
|
|
occurred before */
|
|
|
|
GPIOF_INT_EN &= ~0xff;
|
|
|
|
GPIOH_INT_EN &= ~0xc0;
|
|
|
|
|
2007-06-03 14:31:42 +00:00
|
|
|
/* Get current tick before enabling button interrupts */
|
2007-07-22 21:02:24 +00:00
|
|
|
last_wheel_usec = USEC_TIMER;
|
|
|
|
last_wheel_post = last_wheel_usec;
|
2007-06-03 14:31:42 +00:00
|
|
|
|
2007-05-11 08:09:49 +00:00
|
|
|
GPIOH_ENABLE |= 0xc0;
|
|
|
|
GPIOH_OUTPUT_EN &= ~0xc0;
|
|
|
|
|
|
|
|
/* Read initial buttons */
|
2007-06-03 15:17:01 +00:00
|
|
|
button_int();
|
|
|
|
GPIOF_INT_CLR = 0xff;
|
2007-05-11 08:09:49 +00:00
|
|
|
|
2006-12-16 00:21:41 +00:00
|
|
|
/* Read initial wheel value (bit 6-7 of GPIOH) */
|
|
|
|
old_wheel_value = GPIOH_INPUT_VAL & 0xc0;
|
2007-05-11 08:09:49 +00:00
|
|
|
GPIOH_INT_LEV = (GPIOH_INT_LEV & ~0xc0) | (old_wheel_value ^ 0xc0);
|
2007-06-03 15:17:01 +00:00
|
|
|
GPIOH_INT_CLR = 0xc0;
|
2007-05-11 08:09:49 +00:00
|
|
|
|
2007-06-03 14:31:42 +00:00
|
|
|
/* Enable button interrupts */
|
2007-06-03 15:17:01 +00:00
|
|
|
GPIOF_INT_EN |= 0xff;
|
|
|
|
GPIOH_INT_EN |= 0xc0;
|
2007-05-11 08:09:49 +00:00
|
|
|
|
2007-06-03 14:31:42 +00:00
|
|
|
CPU_INT_EN = HI_MASK;
|
2007-06-17 00:07:23 +00:00
|
|
|
CPU_HI_INT_EN = GPIO1_MASK;
|
2007-05-25 22:41:44 +00:00
|
|
|
#endif /* BOOTLOADER */
|
2006-10-14 12:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool button_hold(void)
|
|
|
|
{
|
2007-06-03 14:31:42 +00:00
|
|
|
return _button_hold();
|
2007-05-11 08:09:49 +00:00
|
|
|
}
|
|
|
|
|
2007-05-25 22:41:44 +00:00
|
|
|
/* clickwheel */
|
|
|
|
#ifndef BOOTLOADER
|
2007-05-11 08:09:49 +00:00
|
|
|
void clickwheel_int(void)
|
|
|
|
{
|
|
|
|
/* Read wheel
|
|
|
|
* Bits 6 and 7 of GPIOH change as follows:
|
|
|
|
* Clockwise rotation 01 -> 00 -> 10 -> 11
|
|
|
|
* Counter-clockwise 11 -> 10 -> 00 -> 01
|
|
|
|
*
|
|
|
|
* This is equivalent to wheel_value of:
|
|
|
|
* Clockwise rotation 0x40 -> 0x00 -> 0x80 -> 0xc0
|
|
|
|
* Counter-clockwise 0xc0 -> 0x80 -> 0x00 -> 0x40
|
|
|
|
*/
|
|
|
|
static const unsigned char wheel_tbl[2][4] =
|
|
|
|
{
|
|
|
|
/* 0x00 0x40 0x80 0xc0 */ /* Wheel value */
|
|
|
|
{ 0x40, 0xc0, 0x00, 0x80 }, /* Clockwise rotation */
|
|
|
|
{ 0x80, 0x00, 0xc0, 0x40 }, /* Counter-clockwise */
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned int wheel_value;
|
|
|
|
|
|
|
|
wheel_value = GPIOH_INPUT_VAL & 0xc0;
|
|
|
|
GPIOH_INT_LEV = (GPIOH_INT_LEV & ~0xc0) | (wheel_value ^ 0xc0);
|
2007-06-03 14:31:42 +00:00
|
|
|
GPIOH_INT_CLR = GPIOH_INT_STAT & 0xc0;
|
2007-05-11 08:09:49 +00:00
|
|
|
|
|
|
|
if (!hold_button)
|
|
|
|
{
|
|
|
|
unsigned int btn = BUTTON_NONE;
|
|
|
|
|
|
|
|
if (old_wheel_value == wheel_tbl[0][wheel_value >> 6])
|
|
|
|
btn = BUTTON_SCROLL_DOWN;
|
|
|
|
else if (old_wheel_value == wheel_tbl[1][wheel_value >> 6])
|
|
|
|
btn = BUTTON_SCROLL_UP;
|
|
|
|
|
|
|
|
if (btn != BUTTON_NONE)
|
|
|
|
{
|
2007-07-22 21:02:24 +00:00
|
|
|
int repeat = 1; /* assume repeat */
|
|
|
|
unsigned long usec = USEC_TIMER;
|
|
|
|
unsigned v = (usec - last_wheel_usec) & 0x7fffffff;
|
|
|
|
|
|
|
|
/* wheel velocity in 0.24 fixed point - clicks/uS */
|
|
|
|
|
|
|
|
/* velocity cap to 18 bits to allow up to x16 scaling */
|
|
|
|
v = (v < 0x40) ? 0xffffff / 0x40 : 0xffffff / v;
|
|
|
|
|
|
|
|
/* some velocity filtering to smooth things out */
|
|
|
|
wheel_velocity = (7*wheel_velocity + v) / 8;
|
2007-05-11 08:09:49 +00:00
|
|
|
|
|
|
|
if (btn != wheel_repeat)
|
|
|
|
{
|
2007-07-22 21:02:24 +00:00
|
|
|
/* direction reversals nullify all fast mode states */
|
2007-05-11 08:09:49 +00:00
|
|
|
wheel_repeat = btn;
|
|
|
|
repeat =
|
|
|
|
wheel_fast_mode =
|
2007-07-22 21:02:24 +00:00
|
|
|
wheel_velocity =
|
2007-05-11 08:09:49 +00:00
|
|
|
wheel_click_count = 0;
|
|
|
|
}
|
|
|
|
|
2007-07-22 21:02:24 +00:00
|
|
|
if (wheel_fast_mode != 0)
|
2007-05-11 08:09:49 +00:00
|
|
|
{
|
2007-07-22 21:02:24 +00:00
|
|
|
/* fast OFF happens immediately when velocity drops below
|
|
|
|
threshold */
|
|
|
|
if (TIME_AFTER(usec,
|
|
|
|
last_wheel_usec + WHEEL_FAST_OFF_INTERVAL))
|
2007-05-11 08:09:49 +00:00
|
|
|
{
|
2007-07-22 21:02:24 +00:00
|
|
|
/* moving out of fast mode */
|
2007-05-11 08:09:49 +00:00
|
|
|
wheel_fast_mode = 0;
|
2007-07-22 21:02:24 +00:00
|
|
|
/* reset velocity */
|
|
|
|
wheel_velocity = 0;
|
|
|
|
/* wheel_delta is always 1 in slow mode */
|
|
|
|
wheel_delta = 1;
|
2007-05-11 08:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-07-22 21:02:24 +00:00
|
|
|
/* fast ON gets filtered to avoid inadvertent jumps to fast mode */
|
|
|
|
if (repeat && wheel_velocity > 0xffffff/WHEEL_FAST_ON_INTERVAL)
|
|
|
|
{
|
|
|
|
/* moving into fast mode */
|
|
|
|
wheel_fast_mode = 1 << 31;
|
|
|
|
wheel_click_count = 0;
|
|
|
|
wheel_velocity = 0xffffff/WHEEL_FAST_OFF_INTERVAL;
|
|
|
|
}
|
2007-05-11 08:09:49 +00:00
|
|
|
else if (++wheel_click_count < 2)
|
2007-07-22 21:02:24 +00:00
|
|
|
{
|
2007-05-11 08:09:49 +00:00
|
|
|
btn = BUTTON_NONE;
|
2007-07-22 21:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* wheel_delta is always 1 in slow mode */
|
|
|
|
wheel_delta = 1;
|
2007-05-11 08:09:49 +00:00
|
|
|
}
|
|
|
|
|
2007-07-22 21:02:24 +00:00
|
|
|
if (TIME_AFTER(usec, next_backlight_on))
|
2007-05-11 08:09:49 +00:00
|
|
|
{
|
2007-07-22 21:02:24 +00:00
|
|
|
/* poke backlight to turn it on or maintain it no more often
|
|
|
|
than every 1/4 second*/
|
|
|
|
next_backlight_on = usec + 1000000/4;
|
2007-05-11 08:09:49 +00:00
|
|
|
backlight_on();
|
|
|
|
button_backlight_on();
|
|
|
|
}
|
2007-05-25 22:41:44 +00:00
|
|
|
|
2007-05-11 08:09:49 +00:00
|
|
|
if (btn != BUTTON_NONE)
|
|
|
|
{
|
|
|
|
wheel_click_count = 0;
|
|
|
|
|
2007-07-22 21:02:24 +00:00
|
|
|
/* generate repeats if quick enough */
|
|
|
|
if (repeat && TIME_BEFORE(usec,
|
|
|
|
last_wheel_post + WHEEL_REPEAT_INTERVAL))
|
2007-05-11 08:09:49 +00:00
|
|
|
btn |= BUTTON_REPEAT;
|
|
|
|
|
2007-07-22 21:02:24 +00:00
|
|
|
last_wheel_post = usec;
|
2007-05-11 08:09:49 +00:00
|
|
|
|
|
|
|
if (queue_empty(&button_queue))
|
2007-07-22 21:02:24 +00:00
|
|
|
{
|
|
|
|
queue_post(&button_queue, btn, wheel_fast_mode |
|
|
|
|
(wheel_delta << 24) | wheel_velocity);
|
|
|
|
/* message posted - reset delta */
|
|
|
|
wheel_delta = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* skipped post - increment delta */
|
|
|
|
if (++wheel_delta > 0x7f)
|
|
|
|
wheel_delta = 0x7f;
|
|
|
|
}
|
2007-05-11 08:09:49 +00:00
|
|
|
}
|
|
|
|
|
2007-07-22 21:02:24 +00:00
|
|
|
last_wheel_usec = usec;
|
2007-05-11 08:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
old_wheel_value = wheel_value;
|
|
|
|
}
|
2007-05-25 22:41:44 +00:00
|
|
|
#endif /* BOOTLOADER */
|
2007-05-11 08:09:49 +00:00
|
|
|
|
2007-05-25 22:41:44 +00:00
|
|
|
/* device buttons */
|
2007-05-11 08:09:49 +00:00
|
|
|
void button_int(void)
|
|
|
|
{
|
|
|
|
unsigned char state;
|
|
|
|
|
2007-05-25 22:41:44 +00:00
|
|
|
int_btn = BUTTON_NONE;
|
2007-05-11 08:09:49 +00:00
|
|
|
|
|
|
|
state = GPIOF_INPUT_VAL & 0xff;
|
|
|
|
|
2007-05-25 22:41:44 +00:00
|
|
|
#ifndef BOOTLOADER
|
2007-05-11 08:09:49 +00:00
|
|
|
GPIOF_INT_LEV = (GPIOF_INT_LEV & ~0xff) | (state ^ 0xff);
|
2007-06-03 14:31:42 +00:00
|
|
|
GPIOF_INT_CLR = GPIOF_INT_STAT;
|
2007-05-11 08:09:49 +00:00
|
|
|
|
|
|
|
hold_button = (state & 0x80) != 0;
|
2007-06-03 14:31:42 +00:00
|
|
|
#endif
|
2007-05-11 08:09:49 +00:00
|
|
|
|
2007-06-03 14:31:42 +00:00
|
|
|
if (!_button_hold())
|
2007-05-11 08:09:49 +00:00
|
|
|
{
|
|
|
|
/* Read normal buttons */
|
|
|
|
if ((state & 0x01) == 0) int_btn |= BUTTON_REC;
|
|
|
|
if ((state & 0x02) == 0) int_btn |= BUTTON_DOWN;
|
|
|
|
if ((state & 0x04) == 0) int_btn |= BUTTON_RIGHT;
|
|
|
|
if ((state & 0x08) == 0) int_btn |= BUTTON_LEFT;
|
|
|
|
if ((state & 0x10) == 0) int_btn |= BUTTON_SELECT; /* The centre button */
|
|
|
|
if ((state & 0x20) == 0) int_btn |= BUTTON_UP; /* The "play" button */
|
|
|
|
if ((state & 0x40) != 0) int_btn |= BUTTON_POWER;
|
|
|
|
}
|
2006-10-14 12:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get button pressed from hardware
|
|
|
|
*/
|
|
|
|
int button_read_device(void)
|
|
|
|
{
|
2007-05-25 22:41:44 +00:00
|
|
|
#ifdef BOOTLOADER
|
|
|
|
/* Read buttons directly in the bootloader */
|
|
|
|
button_int();
|
|
|
|
#else
|
2006-10-14 12:14:23 +00:00
|
|
|
/* light handling */
|
|
|
|
if (hold_button != hold_button_old)
|
|
|
|
{
|
2007-05-11 08:09:49 +00:00
|
|
|
hold_button_old = hold_button;
|
2006-10-14 12:14:23 +00:00
|
|
|
backlight_hold_changed(hold_button);
|
|
|
|
}
|
2007-05-25 22:41:44 +00:00
|
|
|
#endif /* BOOTLOADER */
|
2006-10-14 12:14:23 +00:00
|
|
|
|
2007-05-11 08:09:49 +00:00
|
|
|
/* The int_btn variable is set in the button interrupt handler */
|
|
|
|
return int_btn;
|
2006-10-14 12:14:23 +00:00
|
|
|
}
|