summaryrefslogtreecommitdiff
path: root/firmware/drivers/button.c
diff options
context:
space:
mode:
authorBjörn Stenberg <bjorn@haxx.se>2002-04-23 09:13:23 +0000
committerBjörn Stenberg <bjorn@haxx.se>2002-04-23 09:13:23 +0000
commitc6773e1436ba3adda0a6456dbe54ba1320a095be (patch)
tree172c21804499aa84fcab2f755596a3272cfbd59a /firmware/drivers/button.c
parent34f948291202bdb42391bbc1200326e1aca00e5c (diff)
downloadrockbox-c6773e1436ba3adda0a6456dbe54ba1320a095be.tar.gz
rockbox-c6773e1436ba3adda0a6456dbe54ba1320a095be.zip
Moved driver to 'drivers' subdir
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@189 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/drivers/button.c')
-rw-r--r--firmware/drivers/button.c197
1 files changed, 197 insertions, 0 deletions
diff --git a/firmware/drivers/button.c b/firmware/drivers/button.c
new file mode 100644
index 0000000000..a1abccd32a
--- /dev/null
+++ b/firmware/drivers/button.c
@@ -0,0 +1,197 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Daniel Stenberg
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19/*
20 * Archos Jukebox Recorder button functions
21 */
22
23#include "config.h"
24#include "sh7034.h"
25#include "button.h"
26
27#ifdef HAVE_RECORDER_KEYPAD
28
29/* AJBR buttons are connected to the CPU as follows:
30 *
31 * ON and OFF are connected to separate port B input pins.
32 *
33 * F1, F2, F3, and UP are connected to the AN4 analog input, each through
34 * a separate voltage divider. The voltage on AN4 depends on which button
35 * (or none, or a combination) is pressed.
36 *
37 * DOWN, PLAY, LEFT, and RIGHT are likewise connected to AN5. */
38
39/* Button voltage levels on AN4 and AN5 */
40#define LEVEL1 50
41#define LEVEL2 125
42#define LEVEL3 175
43#define LEVEL4 225
44
45/* Number of calls to get_button for a button to stop bouncing
46 * and to be considered held down.
47 * Should really use a hardware timer for this.
48 */
49#define BOUNCE_COUNT 200
50#define HOLD_COUNT 10000
51
52static int last; /* Last button pressed */
53static int count; /* Number of calls button has been down */
54
55
56/*
57 *Initialize buttons
58 */
59void button_init()
60{
61#ifndef SIMULATOR
62 /* Set PB4 and PB8 as input pins */
63 PBCR1 &= 0xfffc; /* PB8MD = 00 */
64 PBCR2 &= 0xfcff; /* PB4MD = 00 */
65 PBIOR &= ~(PBDR_BTN_ON|PBDR_BTN_OFF); /* Inputs */
66
67 /* Set A/D to scan AN4 and AN5.
68 * This needs to be changed to scan other analog pins
69 * for battery level, etc. */
70 ADCSR = 0;
71 ADCR = 0;
72 ADCSR = ADCSR_ADST | ADCSR_SCAN | 0x5;
73#endif
74 last = BUTTON_NONE;
75 count = 0;
76}
77
78/*
79 * Get button pressed from hardware
80 */
81static int get_raw_button (void)
82{
83 /* Check port B pins for ON and OFF */
84 int data = PBDR;
85 if ((data & PBDR_BTN_ON) == 0)
86 return BUTTON_ON;
87 else if ((data & PBDR_BTN_OFF) == 0)
88 return BUTTON_OFF;
89
90 /* Check AN4 pin for F1-3 and UP */
91 data = ADDRAH;
92 if (data >= LEVEL4)
93 return BUTTON_F3;
94 else if (data >= LEVEL3)
95 return BUTTON_UP;
96 else if (data >= LEVEL2)
97 return BUTTON_F2;
98 else if (data >= LEVEL1)
99 return BUTTON_F1;
100
101 /* Check AN5 pin for DOWN, PLAY, LEFT, RIGHT */
102 data = ADDRBH;
103 if (data >= LEVEL4)
104 return BUTTON_DOWN;
105 else if (data >= LEVEL3)
106 return BUTTON_PLAY;
107 else if (data >= LEVEL2)
108 return BUTTON_LEFT;
109 else if (data >= LEVEL1)
110 return BUTTON_RIGHT;
111
112 return BUTTON_NONE;
113}
114
115/*
116 * Get the currently pressed button.
117 * Returns one of BUTTON_xxx codes, with possibly a modifier bit set.
118 * No modifier bits are set when the button is first pressed.
119 * BUTTON_HELD bit is while the button is being held.
120 * BUTTON_REL bit is set when button has been released.
121 */
122int button_get(void)
123{
124 int btn = get_raw_button();
125 int ret;
126
127 /* Last button pressed is still down */
128 if (btn != BUTTON_NONE && btn == last) {
129 count++;
130 if (count == BOUNCE_COUNT)
131 return btn;
132 else if (count >= HOLD_COUNT)
133 return btn | BUTTON_HELD;
134 else
135 return BUTTON_NONE;
136 }
137
138 /* Last button pressed now released */
139 if (btn == BUTTON_NONE && last != BUTTON_NONE)
140 ret = last | BUTTON_REL;
141 else
142 ret = BUTTON_NONE;
143
144 last = btn;
145 count = 0;
146 return ret;
147}
148
149#elif HAVE_PLAYER_KEYPAD
150
151/* The player has all buttons on port pins:
152
153 LEFT: PC0
154 RIGHT: PC2
155 PLAY: PC3
156 STOP: PA11
157 ON: PA5
158 MENU: PC1
159*/
160
161void button_init(void)
162{
163 /* set port pins as input */
164 PAIOR &= ~0x820;
165}
166
167int button_get(void)
168{
169 int porta = PADR;
170 int portc = PCDR;
171 int btn = 0;
172
173 if ( portc & 1 )
174 btn |= BUTTON_LEFT;
175 if ( portc & 2 )
176 btn |= BUTTON_MENU;
177 if ( portc & 4 )
178 btn |= BUTTON_RIGHT;
179 if ( portc & 8 )
180 btn |= BUTTON_PLAY | BUTTON_UP;
181 if ( porta & 0x20 )
182 btn |= BUTTON_ON;
183 if ( porta & 0x800 )
184 btn |= BUTTON_STOP | BUTTON_DOWN;
185
186 return btn;
187}
188
189#endif
190
191
192
193/* -----------------------------------------------------------------
194 * local variables:
195 * eval: (load-file "rockbox-mode.el")
196 * end:
197 */