Jens Arnold | 8876018 | 2007-03-05 00:04:00 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2006 by Linus Nielsen Feltzing |
| 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 | #include "config.h" |
| 20 | #include "system.h" |
| 21 | #include "button.h" |
| 22 | #include "backlight.h" |
| 23 | #include "adc.h" |
| 24 | #include "lcd-remote-target.h" |
| 25 | |
| 26 | /* have buttons scan by default */ |
| 27 | static bool button_scan_on = true; |
| 28 | static bool hold_button = false; |
| 29 | static bool remote_hold_button = false; |
| 30 | |
| 31 | void button_init_device(void) |
| 32 | { |
| 33 | /* Power, Remote Play & Hold switch */ |
| 34 | GPIO_FUNCTION |= 0x0e000000; |
| 35 | GPIO_ENABLE &= ~0x0e000000; |
| 36 | } |
| 37 | |
| 38 | void button_enable_scan(bool enable) |
| 39 | { |
| 40 | button_scan_on = enable; |
| 41 | } |
| 42 | |
| 43 | bool button_scan_enabled(void) |
| 44 | { |
| 45 | return button_scan_on; |
| 46 | } |
| 47 | |
| 48 | bool button_hold(void) |
| 49 | { |
| 50 | return (GPIO_READ & 0x08000000) == 0; |
| 51 | } |
| 52 | |
| 53 | bool remote_button_hold(void) |
| 54 | { |
| 55 | return remote_hold_button; |
| 56 | } |
| 57 | |
| 58 | int button_read_device(void) |
| 59 | { |
| 60 | int btn = BUTTON_NONE; |
| 61 | bool hold_button_old; |
| 62 | bool remote_hold_button_old; |
Jens Arnold | 611737b | 2007-03-05 20:14:41 +0000 | [diff] [blame^] | 63 | static int prev_data = 0xff; |
| 64 | static int last_valid = 0xff; |
Jens Arnold | 8876018 | 2007-03-05 00:04:00 +0000 | [diff] [blame] | 65 | int data; |
| 66 | |
| 67 | /* normal buttons */ |
| 68 | hold_button_old = hold_button; |
| 69 | hold_button = button_hold(); |
| 70 | |
| 71 | #ifndef BOOTLOADER |
| 72 | /* give BL notice if HB state chaged */ |
| 73 | if (hold_button != hold_button_old) |
| 74 | backlight_hold_changed(hold_button); |
| 75 | #endif |
| 76 | |
| 77 | if (button_scan_on && !hold_button) |
| 78 | { |
| 79 | data = adc_scan(ADC_BUTTONS); |
| 80 | |
Jens Arnold | 611737b | 2007-03-05 20:14:41 +0000 | [diff] [blame^] | 81 | /* ADC debouncing: Only accept new reading if it's |
| 82 | * stable (+/-1). Use latest stable value otherwise. */ |
| 83 | if ((unsigned)(data - prev_data + 1) <= 2) |
| 84 | last_valid = data; |
| 85 | prev_data = data; |
| 86 | data = last_valid; |
| 87 | |
Jens Arnold | 8876018 | 2007-03-05 00:04:00 +0000 | [diff] [blame] | 88 | if (data < 0xf0) |
| 89 | { |
| 90 | if(data < 0x7c) |
| 91 | if(data < 0x42) |
| 92 | btn = BUTTON_LEFT; |
| 93 | else |
| 94 | if(data < 0x62) |
| 95 | btn = BUTTON_RIGHT; |
| 96 | else |
| 97 | btn = BUTTON_SELECT; |
| 98 | else |
| 99 | if(data < 0xb6) |
| 100 | if(data < 0x98) |
| 101 | btn = BUTTON_PLAY; |
| 102 | else |
| 103 | btn = BUTTON_REC; |
| 104 | else |
| 105 | if(data < 0xd3) |
| 106 | btn = BUTTON_DOWN; |
| 107 | else |
| 108 | btn = BUTTON_UP; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* remote buttons */ |
| 113 | data = remote_detect() ? adc_scan(ADC_REMOTE) : 0xff; |
| 114 | |
| 115 | remote_hold_button_old = remote_hold_button; |
| 116 | remote_hold_button = data < 0x17; |
| 117 | |
| 118 | #ifndef BOOTLOADER |
| 119 | if (remote_hold_button != remote_hold_button_old) |
| 120 | remote_backlight_hold_changed(remote_hold_button); |
| 121 | #endif |
| 122 | |
| 123 | if (!remote_hold_button) |
| 124 | { |
| 125 | if (data < 0xee) |
| 126 | { |
| 127 | if(data < 0x7a) |
| 128 | if(data < 0x41) |
| 129 | btn |= BUTTON_RC_FF; |
| 130 | else |
| 131 | if(data < 0x61) |
| 132 | btn |= BUTTON_RC_REW; |
| 133 | else |
| 134 | btn |= BUTTON_RC_MODE; |
| 135 | else |
| 136 | if(data < 0xb4) |
| 137 | if(data < 0x96) |
| 138 | btn |= BUTTON_RC_REC; |
| 139 | else |
| 140 | btn |= BUTTON_RC_MENU; |
| 141 | else |
| 142 | if(data < 0xd1) |
| 143 | btn |= BUTTON_RC_VOL_UP; |
| 144 | else |
| 145 | btn |= BUTTON_RC_VOL_DOWN; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | data = GPIO_READ; |
| 150 | |
| 151 | /* hold and power are mutually exclusive */ |
| 152 | if (!(data & 0x04000000)) |
| 153 | btn |= BUTTON_POWER; |
| 154 | |
| 155 | /* remote play button should be dead if hold */ |
| 156 | if (!remote_hold_button && !(data & 0x02000000)) |
| 157 | btn |= BUTTON_RC_PLAY; |
| 158 | |
| 159 | return btn; |
| 160 | } |