summaryrefslogtreecommitdiff
path: root/firmware/target/arm/imx233/samsung-ypz5/button-ypz5.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/imx233/samsung-ypz5/button-ypz5.c')
-rw-r--r--firmware/target/arm/imx233/samsung-ypz5/button-ypz5.c273
1 files changed, 273 insertions, 0 deletions
diff --git a/firmware/target/arm/imx233/samsung-ypz5/button-ypz5.c b/firmware/target/arm/imx233/samsung-ypz5/button-ypz5.c
new file mode 100644
index 0000000000..cee82e2e9f
--- /dev/null
+++ b/firmware/target/arm/imx233/samsung-ypz5/button-ypz5.c
@@ -0,0 +1,273 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2013 by Lorenzo Miori
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20
21#include "system.h"
22#include "lcd.h"
23#include "string.h"
24#include "pinctrl-imx233.h"
25#include "power-imx233.h"
26#include "button-lradc-imx233.h"
27#include "button-target.h"
28
29#ifndef BOOTLOADER
30#include "touchscreen.h"
31#include "touchscreen-imx233.h"
32#include "button.h"
33#include "font.h"
34#include "action.h"
35#endif
36
37struct imx233_button_lradc_mapping_t imx233_button_lradc_mapping[] =
38{
39 {455, BUTTON_VOL_UP},
40 {900, BUTTON_VOL_DOWN},
41 {1410, BUTTON_BACK},
42 {1868, BUTTON_FF},
43 {2311, BUTTON_REW},
44 {2700, 0},
45 {3300, IMX233_BUTTON_LRADC_END},
46};
47
48#ifndef BOOTLOADER
49static int last_x = 0;
50static int last_y = 0;
51static bool touching = false;
52#endif /* BOOTLOADER */
53
54#ifndef BOOTLOADER
55
56/* Touchpad extra pin initialization
57 * Strange facts:
58 * 1. In the fully working sample I have, it seems that pins
59 * must be all set to low
60 * 2. In the other sample without LCD, it seems (by measurement) that
61 * not all the pins are set to low! Actually, I still need to see if
62 * touchpad works in this other sample.
63*/
64void touchpad_pin_setup(void)
65{
66 /* TX+ */
67 imx233_pinctrl_acquire(0, 25, "touchpad X+ power low");
68 imx233_pinctrl_set_function(0, 25, PINCTRL_FUNCTION_GPIO);
69 imx233_pinctrl_set_drive(0, 25, PINCTRL_DRIVE_4mA);
70 imx233_pinctrl_enable_gpio(0, 25, true);
71
72 /* TY+ */
73 imx233_pinctrl_acquire(0, 26, "touchpad Y+ power high");
74 imx233_pinctrl_set_function(0, 26, PINCTRL_FUNCTION_GPIO);
75 imx233_pinctrl_set_drive(0, 26, PINCTRL_DRIVE_4mA);
76 imx233_pinctrl_enable_gpio(0, 26, true);
77
78 /* TY- */
79 imx233_pinctrl_acquire(1, 21, "touchpad Y- power low");
80 imx233_pinctrl_set_function(1, 21, PINCTRL_FUNCTION_GPIO);
81 imx233_pinctrl_set_drive(1, 21, PINCTRL_DRIVE_4mA);
82 imx233_pinctrl_enable_gpio(1, 21, true);
83
84 /* TX- */
85 imx233_pinctrl_acquire(3, 15, "touchpad X- power high");
86 imx233_pinctrl_set_function(3, 15, PINCTRL_FUNCTION_GPIO);
87 imx233_pinctrl_set_drive(3, 15, PINCTRL_DRIVE_4mA);
88 imx233_pinctrl_enable_gpio(3, 15, true);
89
90}
91#endif /* BOOTLOADER */
92
93void button_init_device(void)
94{
95 imx233_button_lradc_init();
96#ifndef BOOTLOADER
97 touchpad_pin_setup();
98 /* Now that is powered up, proceed with touchpad initialization */
99 imx233_touchscreen_init();
100 imx233_touchscreen_enable(true);
101#endif /* BOOTLOADER */
102}
103
104bool button_hold(void)
105{
106 return imx233_button_lradc_hold();
107}
108
109/* X, Y, RadiusX, RadiusY */
110#define TOUCH_UP 2400, 1050, 650, 250
111#define TOUCH_DOWN 2057, 3320, 500, 350
112#define TOUCH_LEFT 3581, 2297, 300, 350
113#define TOUCH_RIGHT 1000, 2100, 400, 700
114#define TOUCH_CENTER 2682, 2167, 335, 276
115
116bool coord_in_radius(int x, int y, int cx, int cy, int rx, int ry)
117{
118 return ((x >= cx - rx && x <= cx + rx) && (y >= cy - ry && y <= cy + ry));
119}
120
121int button_read_device(void)
122{
123 int res = 0;
124
125 switch(imx233_power_read_pswitch())
126 {
127 case 1: res |= BUTTON_POWER; break;
128 case 3: res |= BUTTON_SELECT; break;
129 }
130
131#ifndef BOOTLOADER
132 touching = imx233_touchscreen_get_touch(&last_x, &last_y);
133 if(touching)
134 {
135 if (coord_in_radius(last_x, last_y, TOUCH_LEFT))
136 {
137 res |= BUTTON_LEFT;
138 }
139 else if (coord_in_radius(last_x, last_y, TOUCH_RIGHT))
140 {
141 res |= BUTTON_RIGHT;
142 }
143 else if (coord_in_radius(last_x, last_y, TOUCH_DOWN))
144 {
145 res |= BUTTON_DOWN;
146 }
147 else if (coord_in_radius(last_x, last_y, TOUCH_UP))
148 {
149 res |= BUTTON_UP;
150 }
151 }
152#endif /* BOOTLOADER */
153 return imx233_button_lradc_read(res);
154}
155
156#ifndef BOOTLOADER
157
158#define MAX_ENTRIES 100
159#define VIEWPORT_HEIGHT 100
160#define VIEWPORT_WIDTH 100
161#define MAX_X 3700
162#define MAX_Y 3700
163#define ADAPT_TO_VIEWPORT(cx, cy, rx, ry) ((float)(cx) / MAX_X) * VIEWPORT_WIDTH, \
164 ((float)(cy) / MAX_Y) * VIEWPORT_HEIGHT, \
165 ((float)(rx) / MAX_X) * VIEWPORT_WIDTH, \
166 ((float)(ry) / MAX_Y) * VIEWPORT_HEIGHT
167static void draw_calibration_rect(int cx, int cy, int rx, int ry)
168{
169 if (coord_in_radius(last_x, last_y, cx, cy, rx, ry))
170 lcd_set_drawinfo(DRMODE_SOLID, LCD_RGBPACK(0xff, 0xff, 0xff), LCD_BLACK);
171 else
172 lcd_set_drawinfo(DRMODE_SOLID, LCD_RGBPACK(0xff, 0, 0), LCD_BLACK);
173 lcd_drawrect(ADAPT_TO_VIEWPORT(cx - rx, cy - ry, 2 * rx, 2 * ry));
174}
175
176bool button_debug_screen(void)
177{
178 int last = 0;
179 struct point_t
180 {
181 int x;
182 int y;
183 };
184 struct point_t last_entries[MAX_ENTRIES];
185 struct viewport report_vp;
186
187 memset(&report_vp, 0, sizeof(report_vp));
188 report_vp.x = (LCD_WIDTH - VIEWPORT_WIDTH) / 2;
189 report_vp.y = (LCD_HEIGHT - VIEWPORT_HEIGHT) / 2;
190 report_vp.width = VIEWPORT_WIDTH;
191 report_vp.height = VIEWPORT_HEIGHT;
192
193 lcd_setfont(FONT_SYSFIXED);
194 lcd_clear_display();
195
196 while(1)
197 {
198 int button = get_action(CONTEXT_STD, HZ / 10);
199 switch(button)
200 {
201 case ACTION_STD_OK:
202 case ACTION_STD_MENU:
203 lcd_set_viewport(NULL);
204 lcd_setfont(FONT_UI);
205 lcd_clear_display();
206 return true;
207 case ACTION_STD_CANCEL:
208 lcd_set_viewport(NULL);
209 lcd_setfont(FONT_UI);
210 lcd_clear_display();
211 return false;
212 }
213
214 lcd_set_viewport(NULL);
215 lcd_putsf(0, 1, "(%d,%d) %s", last_x, last_y, touching ? "touching!" : "");
216 lcd_putsf(0, 0, "Type %s", imx233_pinctrl_get_gpio(0, 31) ? "CAP" : "REG");
217 lcd_set_viewport(&report_vp);
218 lcd_set_drawinfo(DRMODE_SOLID, LCD_RGBPACK(0, 0, 0xff), LCD_BLACK);
219 lcd_drawrect(0, 0, 100, 100);
220 float percent_x = ((float)(last_x) / MAX_X);
221 float percent_y = ((float)(last_y) / MAX_Y);
222 if (touching)
223 {
224 lcd_set_viewport(NULL);
225 if (last < MAX_ENTRIES)
226 {
227 last_entries[last].x = last_x;
228 last_entries[last].y = last_y;
229 last++;
230 lcd_putsf(0, 17, "Recording: %d captures left", MAX_ENTRIES - last);
231 }
232 else
233 {
234 int min_x = 9999;
235 int min_y = 9999;
236 int max_x = -1;
237 int max_y = -1;
238 int median_x = 0;
239 int median_y = 0;
240 for (int i = 0; i < MAX_ENTRIES; i++)
241 {
242 min_x = MIN(min_x, last_entries[i].x);
243 min_y = MIN(min_y, last_entries[i].y);
244 max_x = MAX(max_x, last_entries[i].x);
245 max_y = MAX(max_y, last_entries[i].y);
246 median_x += last_entries[i].x;
247 median_y += last_entries[i].y;
248 }
249 median_x /= MAX_ENTRIES;
250 median_y /= MAX_ENTRIES;
251 lcd_putsf(0, 17, "center(%d,%d)", median_x, median_y);
252 lcd_putsf(0, 18, "radius(%d,%d)", median_x / 2, median_y / 2);
253 }
254 lcd_set_viewport(&report_vp);
255 lcd_set_drawinfo(DRMODE_SOLID, LCD_RGBPACK(0xff, 0x8c, 0), LCD_BLACK);
256 lcd_fillrect(VIEWPORT_WIDTH * percent_x, VIEWPORT_HEIGHT * percent_y, 2, 2);
257 }
258
259 /* Draw current calibration settings */
260 lcd_set_viewport(&report_vp);
261 draw_calibration_rect(TOUCH_UP);
262 draw_calibration_rect(TOUCH_DOWN);
263 draw_calibration_rect(TOUCH_CENTER);
264 draw_calibration_rect(TOUCH_LEFT);
265 draw_calibration_rect(TOUCH_RIGHT);
266
267 lcd_update();
268 yield();
269 }
270
271 return true;
272}
273#endif