FS#10796 - Clip - prevent power switch activation when coming out of hold
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-clip / button-clip.c
blob53ebfa12954dd5dbc5c6653937f92eac0d6ccddb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 Bertrik Sikken
11 * Copyright (C) 2008 François Dinel
12 * Copyright (C) 2008 Rafaël Carré
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "system.h"
24 #include "button-target.h"
25 #include "as3525.h"
26 #ifndef BOOTLOADER
27 #include "backlight.h"
28 #endif
30 /* The Sansa Clip uses a button matrix that is scanned by selecting one of
31 three rows and reading back the button states from the columns.
33 In this driver, the row is changed at each call (i.e. once per tick).
34 In one tick, column data from one row is read back and then the next row
35 is selected for the following tick. This mechanism ensures that there is
36 plenty time between selecting a row and reading the columns, avoiding the
37 need for explicit delays.
41 void button_init_device(void)
43 GPIOA_DIR &= ~((1<<7) | (1<<3));
44 GPIOB_DIR &= ~((1<<2) | (1<<1) | (1<<0));
45 GPIOC_PIN(4) = 0;
46 GPIOC_PIN(5) = 0;
47 GPIOC_PIN(6) = 0;
48 GPIOC_DIR |= ((1<<6) | (1<<5) | (1<<4));
50 /* get initial readings */
51 button_read_device();
52 button_read_device();
53 button_read_device();
56 int button_read_device(void)
58 static int row = 0;
59 static int buttons = 0;
60 static unsigned power_counter = 0;
62 if(button_hold())
64 power_counter = HZ;
65 return 0;
68 /* direct GPIO connections */
69 /* read power, but not if hold button was just released, since
70 * you basically always hit power due to the slider mechanism after
71 * releasing hold (wait 1 sec) */
72 if (power_counter)
73 power_counter--;
75 if (GPIOA_PIN(7) && !power_counter)
76 buttons |= BUTTON_POWER;
77 else
78 buttons &= ~BUTTON_POWER;
80 /* This is a keypad using C4-C6 as columns and B0-B2 as rows */
81 switch (row) {
83 case 0:
84 buttons &= ~(BUTTON_VOL_UP | BUTTON_UP);
86 (void)GPIOB_PIN(0); /* C4B0 is unused */
88 if (GPIOB_PIN(1))
89 buttons |= BUTTON_VOL_UP;
91 if (GPIOB_PIN(2))
92 buttons |= BUTTON_UP;
94 GPIOC_PIN(4) = 0;
95 GPIOC_PIN(5) = (1<<5);
96 row++;
97 break;
99 case 1:
100 buttons &= ~(BUTTON_LEFT | BUTTON_SELECT | BUTTON_RIGHT);
102 if (GPIOB_PIN(0))
103 buttons |= BUTTON_LEFT;
105 if (GPIOB_PIN(1))
106 buttons |= BUTTON_SELECT;
108 if (GPIOB_PIN(2))
109 buttons |= BUTTON_RIGHT;
111 GPIOC_PIN(5) = 0;
112 GPIOC_PIN(6) = (1<<6);
113 row++;
114 break;
116 case 2:
117 buttons &= ~(BUTTON_DOWN | BUTTON_VOL_DOWN | BUTTON_HOME);
119 if (GPIOB_PIN(0))
120 buttons |= BUTTON_DOWN;
122 if (GPIOB_PIN(1))
123 buttons |= BUTTON_VOL_DOWN;
125 if (GPIOB_PIN(2))
126 buttons |= BUTTON_HOME;
128 GPIOC_PIN(6) = 0;
129 GPIOC_PIN(4) = (1<<4);
131 default:
132 row = 0;
133 break;
136 return buttons;
139 bool button_hold(void)
141 #ifndef BOOTLOADER
142 static bool hold_button_old = false;
143 #endif
144 bool hold_button = (GPIOA_PIN(3) != 0);
146 #ifndef BOOTLOADER
147 /* light handling */
148 if (hold_button != hold_button_old)
150 hold_button_old = hold_button;
151 backlight_hold_changed(hold_button);
153 #endif /* BOOTLOADER */
155 return hold_button;