blob: c9011051005bc1e6e4ae3e58329f4c4668a2e15e [file] [log] [blame]
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 by Linus Nielsen Feltzing
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +000021#include "config.h"
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +000022#include "system.h"
23#include "button.h"
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +000024#include "backlight.h"
25#include "adc.h"
Michael Sevakisc4a0d452006-10-28 23:10:45 +000026#include "lcd-remote-target.h"
27
28/* have buttons scan by default */
29static bool button_scan_on = true;
30static bool hold_button = false;
31static bool remote_hold_button = false;
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +000032
33void button_init_device(void)
34{
35 /* Power, Remote Play & Hold switch */
36 GPIO_FUNCTION |= 0x0e000000;
37 GPIO_ENABLE &= ~0x0e000000;
38}
39
Michael Sevakisc4a0d452006-10-28 23:10:45 +000040void button_enable_scan(bool enable)
41{
42 button_scan_on = enable;
43}
44
45bool button_scan_enabled(void)
46{
47 return button_scan_on;
48}
49
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +000050bool button_hold(void)
51{
Michael Sevakisc4a0d452006-10-28 23:10:45 +000052 return (GPIO_READ & 0x08000000) == 0;
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +000053}
54
55bool remote_button_hold(void)
56{
Michael Sevakisc4a0d452006-10-28 23:10:45 +000057 return remote_hold_button;
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +000058}
59
60int button_read_device(void)
61{
Michael Sevakisc4a0d452006-10-28 23:10:45 +000062 int btn = BUTTON_NONE;
Rani Hodc9f59e62006-08-08 22:03:56 +000063 bool hold_button_old;
Michael Sevakis3d2e10b2006-09-10 02:00:40 +000064 bool remote_hold_button_old;
Jens Arnold611737b2007-03-05 20:14:41 +000065 static int prev_data = 0xff;
66 static int last_valid = 0xff;
Michael Sevakisc4a0d452006-10-28 23:10:45 +000067 int data;
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +000068
69 /* normal buttons */
Rani Hodc9f59e62006-08-08 22:03:56 +000070 hold_button_old = hold_button;
71 hold_button = button_hold();
72
Rani Hod7d4d6ce2006-08-08 22:43:40 +000073#ifndef BOOTLOADER
Rani Hodc9f59e62006-08-08 22:03:56 +000074 /* give BL notice if HB state chaged */
75 if (hold_button != hold_button_old)
76 backlight_hold_changed(hold_button);
Rani Hod7d4d6ce2006-08-08 22:43:40 +000077#endif
Rani Hodc9f59e62006-08-08 22:03:56 +000078
Michael Sevakisc4a0d452006-10-28 23:10:45 +000079 if (button_scan_on && !hold_button)
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +000080 {
81 data = adc_scan(ADC_BUTTONS);
Michael Sevakisc4a0d452006-10-28 23:10:45 +000082
Jens Arnold611737b2007-03-05 20:14:41 +000083 /* ADC debouncing: Only accept new reading if it's
84 * stable (+/-1). Use latest stable value otherwise. */
85 if ((unsigned)(data - prev_data + 1) <= 2)
86 last_valid = data;
87 prev_data = data;
88 data = last_valid;
89
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +000090 if (data < 0xf0)
91 {
92 if(data < 0x7c)
93 if(data < 0x42)
94 btn = BUTTON_LEFT;
95 else
96 if(data < 0x62)
97 btn = BUTTON_RIGHT;
98 else
99 btn = BUTTON_SELECT;
100 else
101 if(data < 0xb6)
102 if(data < 0x98)
103 btn = BUTTON_REC;
104 else
105 btn = BUTTON_PLAY;
106 else
107 if(data < 0xd3)
108 btn = BUTTON_DOWN;
109 else
110 btn = BUTTON_UP;
111 }
112 }
113
114 /* remote buttons */
Michael Sevakisc4a0d452006-10-28 23:10:45 +0000115 data = remote_detect() ? adc_scan(ADC_REMOTE) : 0xff;
Rani Hodc9f59e62006-08-08 22:03:56 +0000116
Michael Sevakisc4a0d452006-10-28 23:10:45 +0000117 remote_hold_button_old = remote_hold_button;
Michael Sevakis3d2e10b2006-09-10 02:00:40 +0000118 remote_hold_button = data < 0x17;
119
120#ifndef BOOTLOADER
121 if (remote_hold_button != remote_hold_button_old)
122 remote_backlight_hold_changed(remote_hold_button);
123#endif
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +0000124
Michael Sevakisc4a0d452006-10-28 23:10:45 +0000125 if (!remote_hold_button)
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +0000126 {
127 if (data < 0xee)
128 {
129 if(data < 0x7a)
130 if(data < 0x41)
Linus Nielsen Feltzingdffcb9f2006-08-17 14:32:23 +0000131 btn |= BUTTON_RC_FF;
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +0000132 else
133 if(data < 0x61)
Linus Nielsen Feltzingdffcb9f2006-08-17 14:32:23 +0000134 btn |= BUTTON_RC_REW;
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +0000135 else
136 btn |= BUTTON_RC_MODE;
137 else
138 if(data < 0xb4)
139 if(data < 0x96)
140 btn |= BUTTON_RC_REC;
141 else
142 btn |= BUTTON_RC_MENU;
143 else
144 if(data < 0xd1)
145 btn |= BUTTON_RC_VOL_UP;
146 else
147 btn |= BUTTON_RC_VOL_DOWN;
148 }
149 }
150
151 data = GPIO_READ;
Michael Sevakisb8043fc2006-09-26 23:35:44 +0000152
153 /* hold and power are mutually exclusive */
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +0000154 if (!(data & 0x04000000))
155 btn |= BUTTON_POWER;
156
Michael Sevakisb8043fc2006-09-26 23:35:44 +0000157 /* remote play button should be dead if hold */
158 if (!remote_hold_button && !(data & 0x02000000))
Linus Nielsen Feltzingc8492192006-07-27 13:27:31 +0000159 btn |= BUTTON_RC_PLAY;
160
161 return btn;
162}