2008-10-19 14:11:01 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Barry Wardell
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
#include "powermgmt.h"
|
|
|
|
|
2009-04-13 14:08:43 +00:00
|
|
|
|
2008-10-19 14:11:01 +00:00
|
|
|
static bool hold_button = false;
|
2009-04-10 15:19:59 +00:00
|
|
|
#ifndef BOOTLOADER
|
2008-10-19 14:11:01 +00:00
|
|
|
static bool hold_button_old = false;
|
2009-02-16 20:02:09 +00:00
|
|
|
#endif
|
2009-04-20 20:11:01 +00:00
|
|
|
static unsigned short _dbop_din = 0;
|
2009-04-29 20:50:12 +00:00
|
|
|
/* read_missed is true if buttons could not
|
|
|
|
* be read (see lcd_button_support) */
|
|
|
|
static bool read_missed = false;
|
2009-02-16 19:50:52 +00:00
|
|
|
|
2009-04-29 20:50:12 +00:00
|
|
|
#define WHEEL_REPEAT_INTERVAL (HZ/4)
|
2009-04-13 14:08:43 +00:00
|
|
|
/* in the lcd driver */
|
2009-02-18 17:43:38 +00:00
|
|
|
extern bool lcd_button_support(void);
|
2008-10-19 14:11:01 +00:00
|
|
|
|
|
|
|
void button_init_device(void)
|
|
|
|
{
|
2009-02-16 19:50:52 +00:00
|
|
|
|
2008-10-19 14:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool button_hold(void)
|
|
|
|
{
|
2009-02-16 20:02:09 +00:00
|
|
|
return hold_button;
|
2008-10-19 14:11:01 +00:00
|
|
|
}
|
|
|
|
|
2009-04-13 14:08:43 +00:00
|
|
|
#if !defined(BOOTLOADER) && defined(HAVE_SCROLLWHEEL)
|
2009-04-20 20:11:01 +00:00
|
|
|
static void scrollwheel(unsigned short dbop_din)
|
2008-10-19 14:11:01 +00:00
|
|
|
{
|
2009-04-13 14:08:43 +00:00
|
|
|
/* current wheel values, parsed from dbop and the resulting button */
|
|
|
|
unsigned wheel_value = 0;
|
|
|
|
unsigned btn = BUTTON_NONE;
|
|
|
|
/* old wheel values */
|
|
|
|
static unsigned old_wheel_value = 0;
|
2009-04-13 17:10:52 +00:00
|
|
|
static unsigned old_btn = BUTTON_NONE;
|
2009-04-13 14:08:43 +00:00
|
|
|
|
|
|
|
/* getting BUTTON_REPEAT works like this: Remember when the btn value was
|
|
|
|
* posted to the button_queue last, and if it was recent enough, generate
|
|
|
|
* BUTTON_REPEAT
|
|
|
|
*/
|
|
|
|
static long last_wheel_post = 0;
|
|
|
|
/* Repeat is used for the scrollwheel acceleration. If high enough then
|
|
|
|
* jump over some items */
|
|
|
|
static unsigned repeat = 0;
|
|
|
|
/* we omit 1 of 2 posts to the button_queue, that works better, so count */
|
|
|
|
static int counter = 0;
|
|
|
|
/* Read wheel
|
2009-02-16 19:50:52 +00:00
|
|
|
* Bits 13 and 14 of DBOP_DIN change as follows:
|
|
|
|
* Clockwise rotation 00 -> 01 -> 11 -> 10 -> 00
|
|
|
|
* Counter-clockwise 00 -> 10 -> 11 -> 01 -> 00
|
|
|
|
*/
|
2009-04-13 14:08:43 +00:00
|
|
|
static const unsigned char wheel_tbl[2][4] =
|
|
|
|
{
|
|
|
|
{ 2, 0, 3, 1 }, /* Clockwise rotation */
|
|
|
|
{ 1, 3, 0, 2 }, /* Counter-clockwise */
|
|
|
|
};
|
2009-04-13 17:10:52 +00:00
|
|
|
|
|
|
|
if(hold_button)
|
|
|
|
{
|
|
|
|
repeat = counter = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-13 14:08:43 +00:00
|
|
|
wheel_value = dbop_din & (1<<13|1<<14);
|
2009-04-13 10:28:06 +00:00
|
|
|
wheel_value >>= 13;
|
|
|
|
|
2009-02-16 19:50:52 +00:00
|
|
|
if (old_wheel_value == wheel_tbl[0][wheel_value])
|
|
|
|
btn = BUTTON_SCROLL_FWD;
|
|
|
|
else if (old_wheel_value == wheel_tbl[1][wheel_value])
|
|
|
|
btn = BUTTON_SCROLL_BACK;
|
|
|
|
|
|
|
|
if (btn != BUTTON_NONE)
|
|
|
|
{
|
2009-04-13 17:10:52 +00:00
|
|
|
if (btn != old_btn)
|
2009-02-16 19:50:52 +00:00
|
|
|
{
|
2009-04-13 14:08:43 +00:00
|
|
|
/* direction reversals nullify repeats */
|
2009-04-13 17:10:52 +00:00
|
|
|
old_btn = btn;
|
2009-04-13 14:08:43 +00:00
|
|
|
repeat = counter = 0;
|
2009-02-16 19:50:52 +00:00
|
|
|
}
|
2009-04-19 17:05:36 +00:00
|
|
|
/* wheel_delta will cause lists to jump over items,
|
|
|
|
* we want this for fast scrolling, but we must keep it accurate
|
|
|
|
* for slow scrolling */
|
|
|
|
int wheel_delta = 0;
|
2009-04-29 20:50:12 +00:00
|
|
|
/* generate repeats if quick enough, scroll slightly too */
|
2009-04-19 17:05:36 +00:00
|
|
|
if (TIME_BEFORE(current_tick, last_wheel_post + WHEEL_REPEAT_INTERVAL))
|
2009-02-16 19:50:52 +00:00
|
|
|
{
|
2009-04-19 17:05:36 +00:00
|
|
|
btn |= BUTTON_REPEAT;
|
2009-04-29 20:50:12 +00:00
|
|
|
wheel_delta = repeat>>1;
|
2009-04-19 17:05:36 +00:00
|
|
|
}
|
2009-02-16 19:50:52 +00:00
|
|
|
|
2009-04-29 20:50:12 +00:00
|
|
|
repeat += 3;
|
2009-04-19 17:05:36 +00:00
|
|
|
/* the wheel is more reliable if we don't send ever change,
|
|
|
|
* every 2th is basically one "physical click" is
|
|
|
|
* 1 item in the rockbox menus */
|
|
|
|
if (++counter >= 2 && queue_empty(&button_queue))
|
|
|
|
{
|
|
|
|
buttonlight_on();
|
|
|
|
backlight_on();
|
|
|
|
queue_post(&button_queue, btn, ((wheel_delta+1)<<24));
|
|
|
|
/* message posted - reset count & last post to the queue */
|
|
|
|
counter = 0;
|
|
|
|
last_wheel_post = current_tick;
|
2009-02-16 19:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-29 20:50:12 +00:00
|
|
|
if (repeat > 0 && !read_missed)
|
2009-04-13 14:08:43 +00:00
|
|
|
repeat--;
|
|
|
|
|
2009-02-16 19:50:52 +00:00
|
|
|
old_wheel_value = wheel_value;
|
2008-10-19 14:11:01 +00:00
|
|
|
}
|
2009-04-13 14:08:43 +00:00
|
|
|
#endif /* !defined(BOOTLOADER) && defined(HAVE_SCROLLWHEEL) */
|
2008-10-19 14:11:01 +00:00
|
|
|
|
2009-04-20 20:11:01 +00:00
|
|
|
unsigned short button_read_dbop(void)
|
2008-10-19 14:11:01 +00:00
|
|
|
{
|
2009-02-16 19:50:52 +00:00
|
|
|
/*write a red pixel */
|
2009-02-18 17:43:38 +00:00
|
|
|
if (!lcd_button_support())
|
2009-04-29 20:50:12 +00:00
|
|
|
{
|
|
|
|
read_missed = true;
|
|
|
|
}
|
|
|
|
if (!read_missed)
|
|
|
|
{
|
|
|
|
/* Set up dbop for input */
|
|
|
|
while (!(DBOP_STAT & (1<<10))); /* Wait for fifo to empty */
|
|
|
|
DBOP_CTRL |= (1<<19); /* Tri-state DBOP on read cycle */
|
|
|
|
DBOP_CTRL &= ~(1<<16); /* disable output (1:write enabled) */
|
|
|
|
DBOP_TIMPOL_01 = 0xe167e167; /* Set Timing & Polarity regs 0 & 1 */
|
|
|
|
DBOP_TIMPOL_23 = 0xe167006e; /* Set Timing & Polarity regs 2 & 3 */
|
|
|
|
|
|
|
|
DBOP_CTRL |= (1<<15); /* start read */
|
|
|
|
while (!(DBOP_STAT & (1<<16))); /* wait for valid data */
|
|
|
|
|
|
|
|
_dbop_din = DBOP_DIN; /* Read dbop data*/
|
|
|
|
|
|
|
|
/* Reset dbop for output */
|
|
|
|
DBOP_TIMPOL_01 = 0x6e167; /* Set Timing & Polarity regs 0 & 1 */
|
|
|
|
DBOP_TIMPOL_23 = 0xa167e06f; /* Set Timing & Polarity regs 2 & 3 */
|
|
|
|
DBOP_CTRL |= (1<<16); /* Enable output (0:write disable) */
|
|
|
|
DBOP_CTRL &= ~(1<<19); /* Tri-state when no active write */
|
|
|
|
}
|
2009-04-13 14:08:43 +00:00
|
|
|
#if !defined(BOOTLOADER) && defined(HAVE_SCROLLWHEEL)
|
2009-04-13 10:28:06 +00:00
|
|
|
scrollwheel(_dbop_din);
|
2009-04-13 14:08:43 +00:00
|
|
|
#endif
|
2009-04-29 20:50:12 +00:00
|
|
|
read_missed = false;
|
2009-02-18 17:43:38 +00:00
|
|
|
return _dbop_din;
|
2008-10-19 14:11:01 +00:00
|
|
|
}
|
|
|
|
|
2009-04-20 20:11:01 +00:00
|
|
|
unsigned short button_dbop_data(void)
|
2009-02-18 17:46:11 +00:00
|
|
|
{
|
|
|
|
return _dbop_din;
|
|
|
|
}
|
|
|
|
|
2008-10-19 14:11:01 +00:00
|
|
|
/*
|
|
|
|
* Get button pressed from hardware
|
|
|
|
*/
|
|
|
|
int button_read_device(void)
|
|
|
|
{
|
2009-02-16 19:50:52 +00:00
|
|
|
int btn = BUTTON_NONE;
|
|
|
|
/* read buttons from dbop */
|
2009-04-20 20:11:01 +00:00
|
|
|
unsigned short dbop = button_read_dbop();
|
2009-04-10 15:19:59 +00:00
|
|
|
|
2009-02-16 19:50:52 +00:00
|
|
|
/* hold button */
|
|
|
|
if(dbop & (1<<12))
|
|
|
|
{
|
|
|
|
hold_button = true;
|
|
|
|
return btn;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hold_button = false;
|
|
|
|
}
|
2009-04-10 15:19:59 +00:00
|
|
|
|
2009-02-16 19:50:52 +00:00
|
|
|
if (dbop & (1<<8))
|
|
|
|
btn |= BUTTON_POWER;
|
2009-04-10 15:19:59 +00:00
|
|
|
|
|
|
|
if (!(dbop & (1<<15)))
|
|
|
|
btn |= BUTTON_REC;
|
2009-02-16 19:50:52 +00:00
|
|
|
|
|
|
|
/* Set afsel, so that we can read our buttons */
|
|
|
|
GPIOC_AFSEL &= ~(1<<2|1<<3|1<<4|1<<5|1<<6);
|
|
|
|
/* set dir so we can read our buttons (but reset the C pins first) */
|
|
|
|
GPIOB_DIR &= ~(1<<4);
|
|
|
|
GPIOC_DIR |= (1<<2|1<<3|1<<4|1<<5|1<<6);
|
|
|
|
GPIOC_PIN(2) |= (1<<2);
|
|
|
|
GPIOC_PIN(3) |= (1<<3);
|
|
|
|
GPIOC_PIN(4) |= (1<<4);
|
|
|
|
GPIOC_PIN(5) |= (1<<5);
|
|
|
|
GPIOC_PIN(6) |= (1<<6);
|
|
|
|
|
|
|
|
GPIOC_DIR &= ~(1<<2|1<<3|1<<4|1<<5|1<<6);
|
|
|
|
|
2009-04-26 11:05:53 +00:00
|
|
|
int delay = 8; /* small delay needed to read buttons correctly */
|
2009-02-16 19:50:52 +00:00
|
|
|
while(delay--);
|
|
|
|
|
|
|
|
/* direct GPIO connections */
|
|
|
|
if (!GPIOC_PIN(2))
|
|
|
|
btn |= BUTTON_UP;
|
|
|
|
if (!GPIOC_PIN(3))
|
|
|
|
btn |= BUTTON_LEFT;
|
|
|
|
if (!GPIOC_PIN(4))
|
|
|
|
btn |= BUTTON_SELECT;
|
|
|
|
if (!GPIOC_PIN(5))
|
|
|
|
btn |= BUTTON_RIGHT;
|
2009-04-10 15:19:59 +00:00
|
|
|
if (!GPIOC_PIN(6))
|
|
|
|
btn |= BUTTON_DOWN;
|
2009-02-16 19:50:52 +00:00
|
|
|
|
|
|
|
/* return to settings needed for lcd */
|
|
|
|
GPIOC_DIR |= (1<<2|1<<3|1<<4|1<<5|1<<6);
|
|
|
|
GPIOC_AFSEL |= (1<<2|1<<3|1<<4|1<<5|1<<6);
|
2009-04-10 15:19:59 +00:00
|
|
|
|
|
|
|
#ifndef BOOTLOADER
|
2008-10-19 14:11:01 +00:00
|
|
|
/* light handling */
|
|
|
|
if (hold_button != hold_button_old)
|
|
|
|
{
|
|
|
|
hold_button_old = hold_button;
|
|
|
|
backlight_hold_changed(hold_button);
|
|
|
|
}
|
|
|
|
#endif /* BOOTLOADER */
|
|
|
|
/* The int_btn variable is set in the button interrupt handler */
|
2009-02-16 19:50:52 +00:00
|
|
|
return btn;
|
2008-10-19 14:11:01 +00:00
|
|
|
}
|