Fuzev2: Preliminary button support. Scrollwheel does not work yet.
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-fuzev2 / button-fuzev2.c
blob5bd8afebc127f1b854b62c2586b0e9bf2b39c282
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 by Thomas Martitz
12 * 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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include "system.h"
24 #include "button.h"
25 #include "backlight.h"
28 * TODO: Scrollwheel!
31 #ifdef HAS_BUTTON_HOLD
32 static bool hold_button = false;
33 #endif
34 void button_init_device(void)
39 * Get button pressed from hardware
42 int button_read_device(void)
44 int btn = 0;
45 volatile int delay;
46 static bool hold_button_old = false;
47 static long power_counter = 0;
48 unsigned gpiod = GPIOD_DATA;
49 unsigned gpioa_dir = GPIOA_DIR;
50 unsigned gpiod6;
51 for(delay = 500; delay; delay--) nop;
52 CCU_IO &= ~(1<<12);
53 for(delay=8;delay;delay--) nop;
54 GPIOB_DIR |= 1<<3;
55 GPIOB_PIN(3) = 1<<3;
56 GPIOC_DIR = 0;
57 GPIOB_DIR &= ~(1<<1);
58 GPIOB_DIR |= 1<<0;
59 GPIOB_PIN(0) = 1;
60 for(delay = 500; delay; delay--)
61 nop;
62 gpiod6 = GPIOD_PIN(6);
63 GPIOB_PIN(0) = 0;
64 for(delay = 240; delay; delay--)
65 nop;
66 GPIOD_DIR = 0xff;
67 GPIOA_DIR &= ~(1<<6|1<<7);
68 GPIOD_DATA = 0;
69 GPIOD_DIR = 0;
70 if (GPIOC_PIN(1) & 1<<1)
71 btn |= BUTTON_DOWN;
72 if (GPIOC_PIN(2) & 1<<2)
73 btn |= BUTTON_UP;
74 if (GPIOC_PIN(3) & 1<<3)
75 btn |= BUTTON_LEFT;
76 if (GPIOC_PIN(4) & 1<<4)
77 btn |= BUTTON_SELECT;
78 if (GPIOC_PIN(5) & 1<<5)
79 btn |= BUTTON_RIGHT;
80 if (GPIOB_PIN(1) & 1<<1)
81 btn |= BUTTON_HOME;
82 if (gpiod6 & 1<<6)
83 { /* power/hold is on the same pin. we know it's hold if the bit isn't
84 * set now anymore */
85 if (GPIOD_PIN(6) & 1<<6)
87 hold_button = false;
88 btn |= BUTTON_POWER;
90 else
92 hold_button = true;
96 GPIOD_DIR = 0xff;
97 GPIOD_DATA = gpiod;
98 GPIOA_DIR = gpioa_dir;
99 GPIOD_DIR = 0;
100 CCU_IO |= 1<<12;
101 #ifdef HAS_BUTTON_HOLD
102 /* light handling */
103 if (hold_button != hold_button_old)
105 hold_button_old = hold_button;
106 backlight_hold_changed(hold_button);
108 if (hold_button)
110 power_counter = HZ;
111 return 0;
113 /* read power, but not if hold button was just released, since
114 * you basically always hit power due to the slider mechanism after releasing
115 * (fuze only)
117 else if (power_counter > 0)
119 power_counter--;
120 btn &= ~BUTTON_POWER;
122 #endif
123 return btn;
126 #ifdef HAS_BUTTON_HOLD
127 bool button_hold(void)
129 return hold_button;
131 #endif