Fuzev2: Scrollwheel works like a charm :)
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-fuzev2 / button-fuzev2.c
blob4848d9e7d9bbfa054fa03ef205bd8763c71c92b8
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"
27 extern void scrollwheel(unsigned wheel_value);
29 #ifdef HAS_BUTTON_HOLD
30 static bool hold_button = false;
31 #endif
32 void button_init_device(void)
33 { /* activate the wheel */
34 volatile int i;
35 GPIOB_DIR |= 1<<4;
36 for(i = 20; i; i--) nop;
37 GPIOB_PIN(4) = 0x10;
40 unsigned read_GPIOA_67(void)
42 unsigned ret = 0;
43 volatile int i;
44 DBOP_CTRL |= 1<<19;
45 for(i = 20; i; i--) nop;
46 GPIOA_DIR &= ~0xc0;
47 for(i = 20; i; i--) nop;
48 if (GPIOA_PIN(6) != 0)
49 ret = 1<<0;
50 for(i = 20; i; i--) nop;
51 if (GPIOA_PIN(7) != 0)
52 ret |= 1<<1;
53 DBOP_CTRL &= ~(1<<19);
54 for(i = 20; i; i--) nop;
55 return ret;
58 void get_scrollwheel(void)
60 #if defined(HAVE_SCROLLWHEEL) && !defined(BOOTLOADER)
61 /* scroll wheel handling */
62 scrollwheel(read_GPIOA_67());
63 #endif
67 * Get button pressed from hardware
71 int button_read_device(void)
73 int btn = 0;
74 volatile int delay;
75 static bool hold_button_old = false;
76 static long power_counter = 0;
77 unsigned gpiod = GPIOD_DATA;
78 unsigned gpioa_dir = GPIOA_DIR;
79 unsigned gpiod6;
80 get_scrollwheel();
81 for(delay = 500; delay; delay--) nop;
82 CCU_IO &= ~(1<<12);
83 for(delay=8;delay;delay--) nop;
84 GPIOB_DIR |= 1<<3;
85 GPIOB_PIN(3) = 1<<3;
86 GPIOC_DIR = 0;
87 GPIOB_DIR &= ~(1<<1);
88 GPIOB_DIR |= 1<<0;
89 GPIOB_PIN(0) = 1;
90 for(delay = 500; delay; delay--)
91 nop;
92 gpiod6 = GPIOD_PIN(6);
93 GPIOB_PIN(0) = 0;
94 for(delay = 240; delay; delay--)
95 nop;
96 GPIOD_DIR = 0xff;
97 GPIOA_DIR &= ~(1<<6|1<<7);
98 GPIOD_DATA = 0;
99 GPIOD_DIR = 0;
100 if (GPIOC_PIN(1) & 1<<1)
101 btn |= BUTTON_DOWN;
102 if (GPIOC_PIN(2) & 1<<2)
103 btn |= BUTTON_UP;
104 if (GPIOC_PIN(3) & 1<<3)
105 btn |= BUTTON_LEFT;
106 if (GPIOC_PIN(4) & 1<<4)
107 btn |= BUTTON_SELECT;
108 if (GPIOC_PIN(5) & 1<<5)
109 btn |= BUTTON_RIGHT;
110 if (GPIOB_PIN(1) & 1<<1)
111 btn |= BUTTON_HOME;
112 if (gpiod6 & 1<<6)
113 { /* power/hold is on the same pin. we know it's hold if the bit isn't
114 * set now anymore */
115 if (GPIOD_PIN(6) & 1<<6)
117 hold_button = false;
118 btn |= BUTTON_POWER;
120 else
122 hold_button = true;
126 GPIOD_DIR = 0xff;
127 GPIOD_DATA = gpiod;
128 GPIOA_DIR = gpioa_dir;
129 GPIOD_DIR = 0;
130 CCU_IO |= 1<<12;
131 #ifdef HAS_BUTTON_HOLD
132 #ifndef BOOTLOADER
133 /* light handling */
134 if (hold_button != hold_button_old)
136 hold_button_old = hold_button;
137 backlight_hold_changed(hold_button);
139 #else
140 (void)hold_button_old;
141 #endif
142 if (hold_button)
144 power_counter = HZ;
145 return 0;
147 /* read power, but not if hold button was just released, since
148 * you basically always hit power due to the slider mechanism after releasing
149 * (fuze only)
151 else if (power_counter > 0)
153 power_counter--;
154 btn &= ~BUTTON_POWER;
156 #endif
157 return btn;
160 #ifdef HAS_BUTTON_HOLD
161 bool button_hold(void)
163 return hold_button;
165 #endif