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