summaryrefslogtreecommitdiff
path: root/firmware/target/arm/gigabeat/meg-fx/button-meg-fx.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/gigabeat/meg-fx/button-meg-fx.c')
-rw-r--r--firmware/target/arm/gigabeat/meg-fx/button-meg-fx.c141
1 files changed, 105 insertions, 36 deletions
diff --git a/firmware/target/arm/gigabeat/meg-fx/button-meg-fx.c b/firmware/target/arm/gigabeat/meg-fx/button-meg-fx.c
index 88a1b4de09..210febb7db 100644
--- a/firmware/target/arm/gigabeat/meg-fx/button-meg-fx.c
+++ b/firmware/target/arm/gigabeat/meg-fx/button-meg-fx.c
@@ -27,58 +27,127 @@
27#include "adc.h" 27#include "adc.h"
28#include "system.h" 28#include "system.h"
29 29
30static bool headphones_detect;
31
32static int const remote_buttons[] =
33{
34 BUTTON_NONE, /* Headphones connected - remote disconnected */
35 BUTTON_SELECT,
36 BUTTON_MENU, /* could be changed to BUTTON_A */
37 BUTTON_LEFT,
38 BUTTON_RIGHT,
39 BUTTON_UP, /* could be changed to BUTTON_VOL_UP */
40 BUTTON_DOWN, /* could be changed to BUTTON_VOL_DOWN */
41 BUTTON_NONE, /* Remote control attached - no buttons pressed */
42 BUTTON_NONE, /* Nothing in the headphone socket */
43};
44
45
46
47
48
30void button_init_device(void) 49void button_init_device(void)
31{ 50{
32 /* Power, Remote Play & Hold switch */ 51 /* Power, Remote Play & Hold switch */
33} 52}
34 53
35bool button_hold(void) 54
55
56inline bool button_hold(void)
36{ 57{
37 return (GPGDAT & (1 << 15)); 58 return (GPGDAT & (1 << 15));
38} 59}
39 60
40int button_read_device(void)
41{
42 int btn = BUTTON_NONE;
43 int touchpad = GPJDAT;
44 int buttons = GPGDAT;
45
46 /* Check for hold first */
47 if (buttons & (1 << 15))
48 return btn;
49 61
50 /* the side buttons */
51 if (buttons & (1 << 0))
52 btn |= BUTTON_POWER;
53
54 if (buttons & (1 << 1))
55 btn |= BUTTON_MENU;
56
57 if (buttons & (1 << 2))
58 btn |= BUTTON_VOL_UP;
59
60 if (buttons & (1 << 3))
61 btn |= BUTTON_VOL_DOWN;
62
63 if (buttons & (1 << 4))
64 btn |= BUTTON_A;
65 62
63int button_read_device(void)
64{
65 int btn;
66 int touchpad;
67 int buttons;
68 static int lastbutton;
69 unsigned short remote_adc;
70
71 /* Check for hold first - exit if asserted with no button pressed */
72 if (button_hold())
73 return BUTTON_NONE;
74
75 /* See header for ADC values when remote control buttons are pressed */
76 /* Only one button can be sensed at a time on the remote. */
77 /* Need to filter the remote button because the ADC is so fast */
78 remote_adc = adc_read(ADC_HPREMOTE);
79 btn = remote_buttons[(remote_adc + 64) / 128];
80 if (btn != lastbutton)
81 {
82 /* if the buttons dont agree twice in a row, then its none */
83 lastbutton = btn;
84 btn = BUTTON_NONE;
85 }
86
87 /*
88 * Code can be added that overrides the side buttons when the remote is
89 * plugged in: Check for remote_adc > 64 && remote_adc < 930 then skip
90 * reading the side and touch volume buttons, right, left, up, down, etc.
91 * but keep reading the Power and 'A'.
92 * For now, the buttons from remote and side and touch are used together.
93 */
94
95
96 /* the side buttons - Check before doing all of the work on each bit */
97 buttons = GPGDAT & 0x1F;
98 if (buttons)
99 {
100 if (buttons & (1 << 0))
101 btn |= BUTTON_POWER;
102
103 if (buttons & (1 << 1))
104 btn |= BUTTON_MENU;
105
106 if (buttons & (1 << 2))
107 btn |= BUTTON_VOL_UP;
108
109 if (buttons & (1 << 3))
110 btn |= BUTTON_VOL_DOWN;
111
112 if (buttons & (1 << 4))
113 btn |= BUTTON_A;
114 }
115
66 /* the touchpad */ 116 /* the touchpad */
67 if (touchpad & (1 << 0)) 117 touchpad = GPJDAT & 0x10C9;
68 btn |= BUTTON_UP; 118 if (touchpad)
119 {
120 if (touchpad & (1 << 0))
121 btn |= BUTTON_UP;
69 122
70 if (touchpad & (1 << 12)) 123 if (touchpad & (1 << 12))
71 btn |= BUTTON_RIGHT; 124 btn |= BUTTON_RIGHT;
72 125
73 if (touchpad & (1 << 6)) 126 if (touchpad & (1 << 6))
74 btn |= BUTTON_DOWN; 127 btn |= BUTTON_DOWN;
75 128
76 if (touchpad & (1 << 7)) 129 if (touchpad & (1 << 7))
77 btn |= BUTTON_LEFT; 130 btn |= BUTTON_LEFT;
78
79 if (touchpad & (1 << 3))
80 btn |= BUTTON_SELECT;
81 131
132 if (touchpad & (1 << 3))
133 btn |= BUTTON_SELECT;
134 }
135
82 return btn; 136 return btn;
83} 137}
84 138
139
140
141bool headphones_inserted(void)
142{
143 unsigned short remote_adc = adc_read(ADC_HPREMOTE);
144 if (remote_adc != ADC_READ_ERROR)
145 {
146 /* If there is nothing in the headphone socket, the ADC reads high */
147 if (remote_adc < 940)
148 headphones_detect = true;
149 else
150 headphones_detect = false;
151 }
152 return headphones_detect;
153}