Jonathan Gordon | cad563b | 2006-10-29 10:26:41 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2006 by Jonathan Gordon |
| 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. |
Jonathan Gordon | cad563b | 2006-10-29 10:26:41 +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 | ****************************************************************************/ |
| 21 | |
Jonathan Gordon | cad563b | 2006-10-29 10:26:41 +0000 | [diff] [blame] | 22 | #include "config.h" |
| 23 | #include "cpu.h" |
| 24 | #include "system.h" |
| 25 | #include "button.h" |
Jonathan Gordon | cad563b | 2006-10-29 10:26:41 +0000 | [diff] [blame] | 26 | #include "backlight.h" |
| 27 | #include "adc.h" |
Jonathan Gordon | cad563b | 2006-10-29 10:26:41 +0000 | [diff] [blame] | 28 | #include "lcd-remote.h" |
Jonathan Gordon | cad563b | 2006-10-29 10:26:41 +0000 | [diff] [blame] | 29 | |
| 30 | void button_init_device(void) |
| 31 | { |
| 32 | /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs */ |
| 33 | GPIO1_FUNCTION |= 0x00100062; |
| 34 | GPIO1_ENABLE &= ~0x00100060; |
| 35 | } |
| 36 | |
| 37 | bool button_hold(void) |
| 38 | { |
| 39 | return (GPIO1_READ & 0x00000002)?true:false; |
| 40 | } |
| 41 | |
| 42 | bool remote_button_hold_only(void) |
| 43 | { |
| 44 | if(remote_type() == REMOTETYPE_H300_NONLCD) |
| 45 | return adc_scan(ADC_REMOTE)<0x0d; /* hold should be 0x00 */ |
| 46 | else |
| 47 | return (GPIO1_READ & 0x00100000)?true:false; |
| 48 | } |
| 49 | |
| 50 | /* returns true only if there is remote present */ |
| 51 | bool remote_button_hold(void) |
| 52 | { |
| 53 | /* H300's NON-LCD remote doesn't set the "remote present" bit */ |
| 54 | if(remote_type() == REMOTETYPE_H300_NONLCD) |
| 55 | return remote_button_hold_only(); |
| 56 | else |
| 57 | return ((GPIO_READ & 0x40000000) == 0)?remote_button_hold_only():false; |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Get button pressed from hardware |
| 62 | */ |
| 63 | int button_read_device(void) |
| 64 | { |
| 65 | int btn = BUTTON_NONE; |
| 66 | int data; |
| 67 | static bool hold_button = false; |
| 68 | static bool remote_hold_button = false; |
| 69 | static int prev_data = 0xff; |
| 70 | static int last_valid = 0xff; |
| 71 | bool hold_button_old; |
| 72 | bool remote_hold_button_old; |
| 73 | |
| 74 | /* normal buttons */ |
| 75 | hold_button_old = hold_button; |
| 76 | hold_button = button_hold(); |
| 77 | |
| 78 | |
| 79 | #ifndef BOOTLOADER |
| 80 | if (hold_button != hold_button_old) |
| 81 | backlight_hold_changed(hold_button); |
| 82 | #endif |
| 83 | |
| 84 | if (!hold_button) |
| 85 | { |
| 86 | data = adc_scan(ADC_BUTTONS); |
| 87 | |
| 88 | /* ADC debouncing: Only accept new reading if it's |
| 89 | * stable (+/-1). Use latest stable value otherwise. */ |
| 90 | if ((unsigned)(data - prev_data + 1) <= 2) |
| 91 | last_valid = data; |
| 92 | prev_data = data; |
| 93 | data = last_valid; |
| 94 | |
| 95 | if (data < 0xf0) |
| 96 | { |
| 97 | if (data < 0x80) |
| 98 | if (data < 0x30) |
| 99 | if (data < 0x18) |
| 100 | btn = BUTTON_SELECT; |
| 101 | else |
| 102 | btn = BUTTON_UP; |
| 103 | else |
| 104 | if (data < 0x50) |
| 105 | btn = BUTTON_LEFT; |
| 106 | else |
| 107 | btn = BUTTON_DOWN; |
| 108 | else |
| 109 | if (data < 0xb0) |
| 110 | if (data < 0xa0) |
| 111 | btn = BUTTON_RIGHT; |
| 112 | else |
| 113 | btn = BUTTON_OFF; |
| 114 | else |
| 115 | if (data < 0xd0) |
| 116 | btn = BUTTON_MODE; |
| 117 | else |
| 118 | btn = BUTTON_REC; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* remote buttons */ |
| 123 | remote_hold_button_old = remote_hold_button; |
| 124 | remote_hold_button = remote_button_hold_only(); |
| 125 | |
| 126 | #ifndef BOOTLOADER |
| 127 | if (remote_hold_button != remote_hold_button_old) |
| 128 | remote_backlight_hold_changed(remote_hold_button); |
| 129 | #endif |
| 130 | |
| 131 | if (!remote_hold_button) |
| 132 | { |
| 133 | data = adc_scan(ADC_REMOTE); |
| 134 | switch (remote_type()) |
| 135 | { |
| 136 | case REMOTETYPE_H100_LCD: |
| 137 | if (data < 0xf5) |
| 138 | { |
| 139 | if (data < 0x73) |
| 140 | if (data < 0x3f) |
| 141 | if (data < 0x25) |
| 142 | if(data < 0x0c) |
| 143 | btn |= BUTTON_RC_STOP; |
| 144 | else |
| 145 | btn |= BUTTON_RC_VOL_DOWN; |
| 146 | else |
| 147 | btn |= BUTTON_RC_MODE; |
| 148 | else |
| 149 | if (data < 0x5a) |
| 150 | btn |= BUTTON_RC_VOL_UP; |
| 151 | else |
| 152 | btn |= BUTTON_RC_BITRATE; |
| 153 | else |
| 154 | if (data < 0xa8) |
| 155 | if (data < 0x8c) |
| 156 | btn |= BUTTON_RC_REC; |
| 157 | else |
| 158 | btn |= BUTTON_RC_SOURCE; |
| 159 | else |
| 160 | if (data < 0xdf) |
| 161 | if(data < 0xc5) |
| 162 | btn |= BUTTON_RC_FF; |
| 163 | else |
| 164 | btn |= BUTTON_RC_MENU; |
| 165 | else |
| 166 | btn |= BUTTON_RC_REW; |
| 167 | } |
| 168 | break; |
| 169 | case REMOTETYPE_H300_LCD: |
| 170 | if (data < 0xf5) |
| 171 | { |
| 172 | if (data < 0x73) |
| 173 | if (data < 0x42) |
| 174 | if (data < 0x27) |
| 175 | if(data < 0x0c) |
| 176 | btn |= BUTTON_RC_VOL_DOWN; |
| 177 | else |
| 178 | btn |= BUTTON_RC_FF; |
| 179 | else |
| 180 | btn |= BUTTON_RC_STOP; |
| 181 | else |
| 182 | if (data < 0x5b) |
| 183 | btn |= BUTTON_RC_MODE; |
| 184 | else |
| 185 | btn |= BUTTON_RC_REC; |
| 186 | else |
| 187 | if (data < 0xab) |
| 188 | if (data < 0x8e) |
| 189 | btn |= BUTTON_RC_ON; |
| 190 | else |
| 191 | btn |= BUTTON_RC_BITRATE; |
| 192 | else |
| 193 | if (data < 0xde) |
| 194 | if(data < 0xc5) |
| 195 | btn |= BUTTON_RC_SOURCE; |
| 196 | else |
| 197 | btn |= BUTTON_RC_VOL_UP; |
| 198 | else |
| 199 | btn |= BUTTON_RC_REW; |
| 200 | } |
| 201 | break; |
| 202 | case REMOTETYPE_H300_NONLCD: |
| 203 | if (data < 0xf1) |
| 204 | { |
| 205 | if (data < 0x7d) |
| 206 | if (data < 0x25) |
| 207 | btn |= BUTTON_RC_FF; |
| 208 | else |
| 209 | btn |= BUTTON_RC_REW; |
| 210 | else |
| 211 | if (data < 0xd5) |
| 212 | btn |= BUTTON_RC_VOL_DOWN; |
| 213 | else |
| 214 | btn |= BUTTON_RC_VOL_UP; |
| 215 | } |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | data = GPIO1_READ; |
| 221 | if (!hold_button && ((data & 0x20) == 0)) |
| 222 | btn |= BUTTON_ON; |
| 223 | if (!remote_hold_button && ((data & 0x40) == 0)) |
| 224 | switch(remote_type()) |
| 225 | { |
| 226 | case REMOTETYPE_H100_LCD: |
| 227 | case REMOTETYPE_H300_NONLCD: |
| 228 | btn |= BUTTON_RC_ON; |
| 229 | break; |
| 230 | case REMOTETYPE_H300_LCD: |
| 231 | btn |= BUTTON_RC_MENU; |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | return btn; |
| 236 | } |