2008-11-01 10:29:23 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-12-31 21:02:56 +00:00
|
|
|
* Copyright (C) 2008 by Thomas Martitz
|
|
|
|
* Copyright (C) 2008 by Dominik Wenger
|
2008-11-01 10:29:23 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
#include "button.h"
|
2008-12-31 21:02:56 +00:00
|
|
|
#include "button-target.h"
|
2008-11-01 10:29:23 +00:00
|
|
|
#include "backlight.h"
|
|
|
|
|
2009-06-22 22:24:58 +00:00
|
|
|
|
|
|
|
#ifdef SANSA_FUZE
|
|
|
|
#define DBOP_BIT15_BUTTON BUTTON_HOME
|
|
|
|
#define WHEEL_REPEAT_INTERVAL (HZ/5)
|
|
|
|
#define WHEEL_COUNTER_DIV 4
|
|
|
|
#define ACCEL_INCREMENT 2
|
|
|
|
#define ACCEL_SHIFT 2
|
2009-09-07 23:48:51 +00:00
|
|
|
#define BUTTON_DELAY 60
|
2009-06-22 22:24:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SANSA_E200V2
|
|
|
|
#define DBOP_BIT15_BUTTON BUTTON_REC
|
2009-06-02 01:31:16 +00:00
|
|
|
#define WHEEL_REPEAT_INTERVAL (HZ/5)
|
2009-06-22 22:24:58 +00:00
|
|
|
#define WHEEL_COUNTER_DIV 2
|
|
|
|
#define ACCEL_INCREMENT 3
|
|
|
|
#define ACCEL_SHIFT 1
|
2009-09-05 03:50:11 +00:00
|
|
|
#define BUTTON_DELAY 20
|
2009-06-22 22:24:58 +00:00
|
|
|
|
|
|
|
/* read_missed is true if buttons could not
|
|
|
|
* be read (see lcd_button_support) */
|
|
|
|
static bool read_missed = false;
|
|
|
|
|
|
|
|
#endif
|
2009-06-02 01:31:16 +00:00
|
|
|
|
2008-11-01 10:29:23 +00:00
|
|
|
/* Buttons */
|
|
|
|
static bool hold_button = false;
|
2009-02-19 21:20:42 +00:00
|
|
|
#ifndef BOOTLOADER
|
2008-11-01 10:29:23 +00:00
|
|
|
static bool hold_button_old = false;
|
2009-02-19 21:20:42 +00:00
|
|
|
#endif
|
2009-04-20 20:11:01 +00:00
|
|
|
static unsigned short _dbop_din = BUTTON_NONE;
|
2009-02-17 02:36:48 +00:00
|
|
|
|
2009-02-19 21:20:42 +00:00
|
|
|
/* in the lcd driver */
|
|
|
|
extern bool lcd_button_support(void);
|
2008-11-01 10:29:23 +00:00
|
|
|
|
|
|
|
void button_init_device(void)
|
|
|
|
{
|
2009-02-19 21:20:42 +00:00
|
|
|
GPIOA_DIR |= (1<<1);
|
|
|
|
GPIOA_PIN(1) = (1<<1);
|
2008-12-31 21:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(BOOTLOADER) && defined(HAVE_SCROLLWHEEL)
|
2009-04-20 20:11:01 +00:00
|
|
|
static void scrollwheel(unsigned short dbop_din)
|
2008-12-31 21:02:56 +00:00
|
|
|
{
|
2009-03-13 16:18:56 +00:00
|
|
|
/* current wheel values, parsed from dbop and the resulting button */
|
2009-10-01 00:28:03 +00:00
|
|
|
unsigned wheel_value = 0;
|
|
|
|
unsigned btn = BUTTON_NONE;
|
2009-03-13 16:18:56 +00:00
|
|
|
/* old wheel values */
|
2009-02-19 21:20:42 +00:00
|
|
|
static unsigned old_wheel_value = 0;
|
2009-04-13 17:10:52 +00:00
|
|
|
static unsigned old_btn = BUTTON_NONE;
|
2009-03-13 16:18:56 +00:00
|
|
|
|
2009-06-02 01:31:16 +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
|
|
|
|
*/
|
2009-10-01 00:28:03 +00:00
|
|
|
static long last_wheel_post = 0;
|
2009-06-02 01:31:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Providing wheel acceleration works as follows: We increment accel
|
|
|
|
* by 2 if the wheel was turned, and decrement it by 1 each tick
|
|
|
|
* (no matter if it was turned), that means: the longer and faster you turn,
|
|
|
|
* the higher accel will be. accel>>2 will actually posted to the button_queue
|
2008-12-31 21:02:56 +00:00
|
|
|
*/
|
2009-10-01 00:28:03 +00:00
|
|
|
static int accel = 0;
|
2009-06-02 01:31:16 +00:00
|
|
|
/* We only post every 4th action, as this matches better with the physical
|
|
|
|
* clicks of the wheel */
|
2009-10-01 00:28:03 +00:00
|
|
|
static int counter = 0;
|
2008-12-31 21:02:56 +00:00
|
|
|
/* Read wheel
|
2009-10-09 23:27:38 +00:00
|
|
|
* Bits 13 and 14 of DBOP_DIN change as follows (Gray Code):
|
2008-12-31 21:02:56 +00:00
|
|
|
* Clockwise rotation 00 -> 01 -> 11 -> 10 -> 00
|
|
|
|
* Counter-clockwise 00 -> 10 -> 11 -> 01 -> 00
|
2009-10-01 00:28:03 +00:00
|
|
|
*
|
|
|
|
* For easy look-up, actual wheel values act as indicies also,
|
|
|
|
* which is why the table seems to be not ordered correctly
|
2008-12-31 21:02:56 +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)
|
|
|
|
{
|
2009-06-02 01:31:16 +00:00
|
|
|
accel = counter = 0;
|
2009-04-13 17:10:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-01 00:28:03 +00:00
|
|
|
wheel_value = (dbop_din >> 13) & (1<<1|1<<0);
|
2008-12-31 21:02:56 +00:00
|
|
|
|
2009-02-19 21:20:42 +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;
|
2009-10-09 23:27:38 +00:00
|
|
|
else if (old_wheel_value != wheel_value && accel > ACCEL_INCREMENT)
|
|
|
|
{ /* if no button is read and wheel_value changed, assume old_btn */
|
2009-10-01 00:28:03 +00:00
|
|
|
btn = old_btn;
|
|
|
|
}
|
|
|
|
/* else btn = BUTTON_NONE */
|
2009-02-19 21:20:42 +00:00
|
|
|
|
|
|
|
if (btn != BUTTON_NONE)
|
|
|
|
{
|
2009-10-09 23:27:38 +00:00
|
|
|
if (btn != old_btn)
|
|
|
|
{
|
2009-06-02 01:31:16 +00:00
|
|
|
/* direction reversals nullify acceleration and counters */
|
2009-04-13 17:10:52 +00:00
|
|
|
old_btn = btn;
|
2009-06-02 01:31:16 +00:00
|
|
|
accel = counter = 0;
|
2009-02-19 21:20:42 +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-06-02 01:31:16 +00:00
|
|
|
/* generate BUTTON_REPEAT if quick enough, scroll slightly faster too*/
|
|
|
|
if (TIME_BEFORE(current_tick, last_wheel_post + WHEEL_REPEAT_INTERVAL))
|
2008-12-31 21:02:56 +00:00
|
|
|
{
|
2009-04-19 17:05:36 +00:00
|
|
|
btn |= BUTTON_REPEAT;
|
2009-06-22 22:24:58 +00:00
|
|
|
wheel_delta = accel>>ACCEL_SHIFT;
|
2009-04-19 17:05:36 +00:00
|
|
|
}
|
2009-03-13 16:18:56 +00:00
|
|
|
|
2009-06-22 22:24:58 +00:00
|
|
|
accel += ACCEL_INCREMENT;
|
2009-03-13 16:18:56 +00:00
|
|
|
|
2009-06-02 01:31:16 +00:00
|
|
|
/* the wheel is more reliable if we don't send every change,
|
2009-06-22 22:24:58 +00:00
|
|
|
* every WHEEL_COUNTER_DIVth is basically one "physical click"
|
|
|
|
* which should make up 1 item in lists */
|
|
|
|
if (++counter >= WHEEL_COUNTER_DIV && queue_empty(&button_queue))
|
2009-04-19 17:05:36 +00:00
|
|
|
{
|
|
|
|
buttonlight_on();
|
|
|
|
backlight_on();
|
|
|
|
queue_post(&button_queue, btn, ((wheel_delta+1)<<24));
|
2009-06-02 01:31:16 +00:00
|
|
|
/* message posted - reset count and remember post */
|
2009-04-19 17:05:36 +00:00
|
|
|
counter = 0;
|
2009-06-02 01:31:16 +00:00
|
|
|
last_wheel_post = current_tick;
|
2008-12-31 21:02:56 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-22 22:24:58 +00:00
|
|
|
if (accel > 0
|
|
|
|
#ifdef SANSA_E200V2
|
|
|
|
&& !read_missed /* decrement only if reading buttons was successful */
|
|
|
|
#endif
|
|
|
|
)
|
2009-06-02 01:31:16 +00:00
|
|
|
accel--;
|
|
|
|
|
2008-12-31 21:02:56 +00:00
|
|
|
old_wheel_value = wheel_value;
|
|
|
|
}
|
|
|
|
#endif /* !defined(BOOTLOADER) && defined(SCROLLWHEEL) */
|
|
|
|
|
2008-11-01 10:29:23 +00:00
|
|
|
bool button_hold(void)
|
|
|
|
{
|
2008-12-31 21:02:56 +00:00
|
|
|
return hold_button;
|
2008-11-01 10:29:23 +00:00
|
|
|
}
|
|
|
|
|
2009-03-30 19:57:42 +00:00
|
|
|
static void button_delay(void)
|
|
|
|
{
|
2009-06-22 22:24:58 +00:00
|
|
|
int i = BUTTON_DELAY;
|
2009-06-02 01:31:16 +00:00
|
|
|
while(i--) asm volatile ("nop\n");
|
2009-03-30 19:57:42 +00:00
|
|
|
}
|
|
|
|
|
2009-04-20 20:11:01 +00:00
|
|
|
unsigned short button_read_dbop(void)
|
2008-12-31 21:02:56 +00:00
|
|
|
{
|
2009-06-22 22:24:58 +00:00
|
|
|
#ifdef SANSA_FUZE
|
2009-02-19 22:54:48 +00:00
|
|
|
/* skip home and power reading if lcd_button_support was blocked,
|
2009-06-22 22:24:58 +00:00
|
|
|
* since the dbop bit 15 is invalid then, and use the old value instead
|
|
|
|
* -20 (arbitary value) indicates valid home&power button read
|
|
|
|
* (fuze only) */
|
2009-02-19 22:54:48 +00:00
|
|
|
int old_home_power = -20;
|
2009-06-22 22:24:58 +00:00
|
|
|
#endif
|
2009-02-19 21:20:42 +00:00
|
|
|
if(!lcd_button_support())
|
2009-02-19 22:54:48 +00:00
|
|
|
{
|
2009-06-22 22:24:58 +00:00
|
|
|
#if defined(SANSA_FUZE)
|
2009-02-19 22:54:48 +00:00
|
|
|
old_home_power = (_dbop_din & (1<<15|1<<8));
|
2009-06-22 22:24:58 +00:00
|
|
|
#elif defined(SANSA_E200V2)
|
|
|
|
read_missed = true;
|
|
|
|
#endif
|
2009-02-19 22:54:48 +00:00
|
|
|
}
|
2009-02-19 21:20:42 +00:00
|
|
|
|
2009-06-22 22:24:58 +00:00
|
|
|
#ifdef SANSA_E200V2
|
|
|
|
if (!read_missed) /* read buttons only if lcd_button_support was not blocked */
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Set up dbop for input */
|
|
|
|
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 */
|
|
|
|
|
|
|
|
button_delay();
|
|
|
|
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 */
|
|
|
|
}
|
2008-11-24 20:51:05 +00:00
|
|
|
|
2009-06-22 22:24:58 +00:00
|
|
|
#ifdef SANSA_FUZE
|
2009-02-19 22:54:48 +00:00
|
|
|
/* write back old values if blocked */
|
|
|
|
if (old_home_power != -20)
|
|
|
|
{
|
|
|
|
_dbop_din |= old_home_power & 1<<15;
|
|
|
|
_dbop_din &= 0xfeff|(old_home_power & 1<<8);
|
|
|
|
}
|
2009-06-22 22:24:58 +00:00
|
|
|
#endif
|
|
|
|
|
2009-04-13 10:28:06 +00:00
|
|
|
#if defined(HAVE_SCROLLWHEEL) && !defined(BOOTLOADER)
|
|
|
|
/* read wheel on bit 13 & 14, but sent to the button queue seperately */
|
|
|
|
scrollwheel(_dbop_din);
|
|
|
|
#endif
|
2009-06-22 22:24:58 +00:00
|
|
|
|
|
|
|
#ifdef SANSA_E200V2
|
|
|
|
read_missed = false;
|
|
|
|
#endif
|
|
|
|
|
2009-02-19 21:20:42 +00:00
|
|
|
return _dbop_din;
|
2009-02-17 02:36:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* for the debug menu */
|
2009-04-20 20:11:01 +00:00
|
|
|
unsigned short button_dbop_data(void)
|
2009-02-17 02:36:48 +00:00
|
|
|
{
|
|
|
|
return _dbop_din;
|
2008-12-31 21:02:56 +00:00
|
|
|
}
|
|
|
|
|
2009-02-17 02:36:48 +00:00
|
|
|
static int button_gpio(void)
|
2008-11-01 10:29:23 +00:00
|
|
|
{
|
2009-02-17 02:36:48 +00:00
|
|
|
int btn = BUTTON_NONE;
|
2008-12-31 21:02:56 +00:00
|
|
|
if(hold_button)
|
2009-02-17 02:36:48 +00:00
|
|
|
return btn;
|
2009-09-01 00:42:22 +00:00
|
|
|
|
|
|
|
/* disable DBOP output while changing GPIO pins that share lines with it */
|
|
|
|
DBOP_CTRL &= ~(1<<16);
|
|
|
|
button_delay();
|
|
|
|
|
2008-12-31 21:02:56 +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);
|
2009-02-17 02:36:48 +00:00
|
|
|
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);
|
2008-12-31 21:02:56 +00:00
|
|
|
|
|
|
|
GPIOC_DIR &= ~(1<<2|1<<3|1<<4|1<<5|1<<6);
|
|
|
|
|
|
|
|
/* small delay needed to read buttons correctly */
|
2009-06-02 01:31:16 +00:00
|
|
|
button_delay();
|
2008-11-24 20:51:05 +00:00
|
|
|
|
|
|
|
/* direct GPIO connections */
|
2008-12-31 21:02:56 +00:00
|
|
|
if (!GPIOC_PIN(3))
|
2009-02-17 02:36:48 +00:00
|
|
|
btn |= BUTTON_LEFT;
|
2008-12-31 21:02:56 +00:00
|
|
|
if (!GPIOC_PIN(2))
|
2009-02-17 02:36:48 +00:00
|
|
|
btn |= BUTTON_UP;
|
2008-11-24 20:51:05 +00:00
|
|
|
if (!GPIOC_PIN(6))
|
2009-02-17 02:36:48 +00:00
|
|
|
btn |= BUTTON_DOWN;
|
2008-11-24 20:51:05 +00:00
|
|
|
if (!GPIOC_PIN(5))
|
2009-02-17 02:36:48 +00:00
|
|
|
btn |= BUTTON_RIGHT;
|
2008-11-24 20:51:05 +00:00
|
|
|
if (!GPIOC_PIN(4))
|
2009-02-17 02:36:48 +00:00
|
|
|
btn |= BUTTON_SELECT;
|
2008-11-24 20:51:05 +00:00
|
|
|
/* return to settings needed for lcd */
|
2008-12-31 21:02:56 +00:00
|
|
|
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-09-01 00:42:22 +00:00
|
|
|
|
|
|
|
DBOP_CTRL |= (1<<16); /* enable output again */
|
2009-02-17 02:36:48 +00:00
|
|
|
return btn;
|
2008-12-31 21:02:56 +00:00
|
|
|
}
|
2009-02-17 02:36:48 +00:00
|
|
|
|
2008-11-01 10:29:23 +00:00
|
|
|
/*
|
|
|
|
* Get button pressed from hardware
|
|
|
|
*/
|
|
|
|
int button_read_device(void)
|
|
|
|
{
|
2009-02-19 21:20:42 +00:00
|
|
|
int btn = BUTTON_NONE;
|
2009-04-20 20:11:01 +00:00
|
|
|
unsigned short dbop = button_read_dbop();
|
2009-06-22 22:24:58 +00:00
|
|
|
#ifdef SANSA_FUZE
|
2009-03-13 16:18:56 +00:00
|
|
|
static unsigned power_counter = 0;
|
2009-06-22 22:24:58 +00:00
|
|
|
#endif
|
2009-02-19 21:20:42 +00:00
|
|
|
/* hold button */
|
|
|
|
if(dbop & (1<<12))
|
|
|
|
{
|
2009-06-22 22:24:58 +00:00
|
|
|
#ifdef SANSA_FUZE
|
2009-03-13 16:18:56 +00:00
|
|
|
power_counter = HZ;
|
2009-06-22 22:24:58 +00:00
|
|
|
#endif
|
2009-02-19 21:20:42 +00:00
|
|
|
hold_button = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hold_button = false;
|
2009-06-22 22:24:58 +00:00
|
|
|
#ifdef SANSA_FUZE
|
2009-02-19 21:20:42 +00:00
|
|
|
/* read power on bit 8, but not if hold button was just released, since
|
|
|
|
* you basically always hit power due to the slider mechanism after releasing
|
2009-06-22 22:24:58 +00:00
|
|
|
* (fuze only)
|
2009-06-02 01:31:16 +00:00
|
|
|
* hold (wait 1 sec) */
|
2009-03-13 16:18:56 +00:00
|
|
|
if (power_counter)
|
|
|
|
power_counter--;
|
2009-06-22 22:24:58 +00:00
|
|
|
#endif
|
|
|
|
if (dbop & (1<<8)
|
|
|
|
#ifdef SANSA_FUZE
|
|
|
|
&& !power_counter
|
|
|
|
#endif
|
|
|
|
)
|
2009-02-19 21:20:42 +00:00
|
|
|
btn |= BUTTON_POWER;
|
|
|
|
/* read home on bit 15 */
|
|
|
|
if (!(dbop & (1<<15)))
|
2009-06-22 22:24:58 +00:00
|
|
|
btn |= DBOP_BIT15_BUTTON;
|
|
|
|
|
2009-02-19 21:20:42 +00:00
|
|
|
btn |= button_gpio();
|
|
|
|
}
|
|
|
|
|
2008-12-31 21:02:56 +00:00
|
|
|
#ifndef BOOTLOADER
|
2008-11-01 10:29:23 +00:00
|
|
|
/* light handling */
|
|
|
|
if (hold_button != hold_button_old)
|
|
|
|
{
|
|
|
|
hold_button_old = hold_button;
|
|
|
|
backlight_hold_changed(hold_button);
|
|
|
|
}
|
|
|
|
#endif /* BOOTLOADER */
|
|
|
|
|
2009-02-19 21:20:42 +00:00
|
|
|
return btn;
|
2008-11-01 10:29:23 +00:00
|
|
|
}
|