simplify and cleanup usb_serial.c (FS#10149 by Tomer Shalev)
[kugel-rb.git] / apps / gui / pitchscreen.c
blobb34824ebadb5eb1a4358f99b971a533c3d82ed2e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdbool.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include "config.h"
26 #include "sprintf.h"
27 #include "action.h"
28 #include "dsp.h"
29 #include "sound.h"
30 #include "pcmbuf.h"
31 #include "lang.h"
32 #include "icons.h"
33 #include "screens.h"
34 #include "statusbar.h"
35 #include "viewport.h"
36 #include "font.h"
37 #include "system.h"
38 #include "misc.h"
39 #include "pitchscreen.h"
41 #define PITCH_MODE_ABSOLUTE 1
42 #define PITCH_MODE_SEMITONE -PITCH_MODE_ABSOLUTE
43 #define ICON_BORDER 12 /* icons are currently 7x8, so add ~2 pixels */
44 /* on both sides when drawing */
47 #define PITCH_MAX 2000
48 #define PITCH_MIN 500
49 #define PITCH_SMALL_DELTA 1
50 #define PITCH_BIG_DELTA 10
51 #define PITCH_NUDGE_DELTA 20
54 static int pitch_mode = PITCH_MODE_ABSOLUTE; /* 1 - absolute, -1 - semitone */
56 enum
58 PITCH_TOP = 0,
59 PITCH_MID,
60 PITCH_BOTTOM,
61 PITCH_ITEM_COUNT,
64 static void pitchscreen_fix_viewports(struct viewport *parent,
65 struct viewport pitch_viewports[PITCH_ITEM_COUNT])
67 int i, height;
68 height = font_get(parent->font)->height;
69 for (i = 0; i < PITCH_ITEM_COUNT; i++)
71 pitch_viewports[i] = *parent;
72 pitch_viewports[i].height = height;
74 pitch_viewports[PITCH_TOP].y += ICON_BORDER;
76 pitch_viewports[PITCH_MID].x += ICON_BORDER;
77 pitch_viewports[PITCH_MID].width = parent->width - ICON_BORDER*2;
78 pitch_viewports[PITCH_MID].height = height * 2;
79 pitch_viewports[PITCH_MID].y += parent->height / 2 -
80 pitch_viewports[PITCH_MID].height / 2;
82 pitch_viewports[PITCH_BOTTOM].y += parent->height - height - ICON_BORDER;
85 /* must be called before pitchscreen_draw, or within
86 * since it neither clears nor updates the display */
87 static void pitchscreen_draw_icons (struct screen *display,
88 struct viewport *parent)
90 display->set_viewport(parent);
91 display->mono_bitmap(bitmap_icons_7x8[Icon_UpArrow],
92 parent->width/2 - 3,
93 2, 7, 8);
94 display->mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
95 parent->width /2 - 3,
96 parent->height - 10, 7, 8);
97 display->mono_bitmap(bitmap_icons_7x8[Icon_FastForward],
98 parent->width - 10,
99 parent->height /2 - 4, 7, 8);
100 display->mono_bitmap(bitmap_icons_7x8[Icon_FastBackward],
102 parent->height /2 - 4, 7, 8);
103 display->update_viewport();
106 static void pitchscreen_draw (struct screen *display, int max_lines,
107 struct viewport pitch_viewports[PITCH_ITEM_COUNT], int pitch)
109 unsigned char* ptr;
110 unsigned char buf[32];
111 int width_val, w, h;
112 bool show_lang_pitch;
114 /* Hide "Pitch up/Pitch down" for a small screen */
115 if (max_lines >= 5)
117 /* UP: Pitch Up */
118 display->set_viewport(&pitch_viewports[PITCH_TOP]);
119 if (pitch_mode == PITCH_MODE_ABSOLUTE) {
120 ptr = str(LANG_PITCH_UP);
121 } else {
122 ptr = str(LANG_PITCH_UP_SEMITONE);
124 display->getstringsize(ptr,&w,&h);
125 display->clear_viewport();
126 /* draw text */
127 display->putsxy((pitch_viewports[PITCH_TOP].width / 2) -
128 (w / 2), 0, ptr);
129 display->update_viewport();
131 /* DOWN: Pitch Down */
132 display->set_viewport(&pitch_viewports[PITCH_BOTTOM]);
133 if (pitch_mode == PITCH_MODE_ABSOLUTE) {
134 ptr = str(LANG_PITCH_DOWN);
135 } else {
136 ptr = str(LANG_PITCH_DOWN_SEMITONE);
138 display->getstringsize(ptr,&w,&h);
139 display->clear_viewport();
140 /* draw text */
141 display->putsxy((pitch_viewports[PITCH_BOTTOM].width / 2) -
142 (w / 2), 0, ptr);
143 display->update_viewport();
145 display->set_viewport(&pitch_viewports[PITCH_MID]);
147 snprintf((char *)buf, sizeof(buf), "%s", str(LANG_PITCH));
148 display->getstringsize(buf,&w,&h);
149 /* lets hide LANG_PITCH for smaller screens */
150 display->clear_viewport();
151 if ((show_lang_pitch = (max_lines >= 3)))
152 display->putsxy((pitch_viewports[PITCH_MID].width / 2) - (w / 2),
153 0, buf);
155 /* "XXX.X%" */
156 snprintf((char *)buf, sizeof(buf), "%d.%d%%",
157 pitch / 10, pitch % 10 );
158 display->getstringsize(buf,&width_val,&h);
159 display->putsxy((pitch_viewports[PITCH_MID].width / 2) - (width_val / 2),
160 (show_lang_pitch? h : h/2), buf);
162 /* What's wider? LANG_PITCH or the value?
163 * Only interesting if LANG_PITCH is actually drawn */
164 if (show_lang_pitch && width_val > w)
165 w = width_val;
167 /* Let's treat '+' and '-' as equally wide
168 * This saves a getstringsize call
169 * Also, it wouldn't look nice if -2% shows up, but +2% not */
170 display->getstringsize("+2%",&width_val,&h);
171 w += width_val*2;
172 /* hide +2%/-2% for a narrow screens */
173 if (w <= pitch_viewports[PITCH_MID].width)
175 /* RIGHT: +2% */
176 display->putsxy(pitch_viewports[PITCH_MID].width - width_val, h /2, "+2%");
177 /* LEFT: -2% */
178 display->putsxy(0, h / 2, "-2%");
180 display->update_viewport();
181 display->set_viewport(NULL);
184 static int pitch_increase(int pitch, int delta, bool allow_cutoff)
186 int new_pitch;
188 if (delta < 0) {
189 if (pitch + delta >= PITCH_MIN) {
190 new_pitch = pitch + delta;
191 } else {
192 if (!allow_cutoff) {
193 return pitch;
195 new_pitch = PITCH_MIN;
197 } else if (delta > 0) {
198 if (pitch + delta <= PITCH_MAX) {
199 new_pitch = pitch + delta;
200 } else {
201 if (!allow_cutoff) {
202 return pitch;
204 new_pitch = PITCH_MAX;
206 } else {
207 /* delta == 0 -> no real change */
208 return pitch;
210 sound_set_pitch(new_pitch);
212 return new_pitch;
215 /* Factor for changing the pitch one half tone up.
216 The exact value is 2^(1/12) = 1.05946309436
217 But we use only integer arithmetics, so take
218 rounded factor multiplied by 10^5=100,000. This is
219 enough to get the same promille values as if we
220 had used floating point (checked with a spread
221 sheet).
223 #define PITCH_SEMITONE_FACTOR 105946L
225 /* Some helpful constants. K is the scaling factor for SEMITONE.
226 N is for more accurate rounding
227 KN is K * N
229 #define PITCH_K_FCT 100000UL
230 #define PITCH_N_FCT 10
231 #define PITCH_KN_FCT 1000000UL
233 static int pitch_increase_semitone(int pitch, bool up)
235 uint32_t tmp;
236 uint32_t round_fct; /* How much to scale down at the end */
237 tmp = pitch;
238 if (up) {
239 tmp = tmp * PITCH_SEMITONE_FACTOR;
240 round_fct = PITCH_K_FCT;
241 } else {
242 tmp = (tmp * PITCH_KN_FCT) / PITCH_SEMITONE_FACTOR;
243 round_fct = PITCH_N_FCT;
245 /* Scaling down with rounding */
246 tmp = (tmp + round_fct / 2) / round_fct;
247 return pitch_increase(pitch, tmp - pitch, false);
251 returns:
252 0 on exit
253 1 if USB was connected
256 int gui_syncpitchscreen_run(void)
258 int button, i;
259 int pitch = sound_get_pitch();
260 int new_pitch, delta = 0;
261 bool nudged = false;
262 bool exit = false;
263 /* should maybe be passed per parameter later, not needed for now */
264 struct viewport parent[NB_SCREENS];
265 struct viewport pitch_viewports[NB_SCREENS][PITCH_ITEM_COUNT];
266 int max_lines[NB_SCREENS];
268 /* initialize pitchscreen vps */
269 FOR_NB_SCREENS(i)
271 screens[i].clear_display();
272 viewport_set_defaults(&parent[i], i);
273 max_lines[i] = viewport_get_nb_lines(&parent[i]);
274 pitchscreen_fix_viewports(&parent[i], pitch_viewports[i]);
276 /* also, draw the icons now, it's only needed once */
277 pitchscreen_draw_icons(&screens[i], &parent[i]);
279 #if CONFIG_CODEC == SWCODEC
280 pcmbuf_set_low_latency(true);
281 #endif
283 while (!exit)
285 FOR_NB_SCREENS(i)
286 pitchscreen_draw(&screens[i], max_lines[i],
287 pitch_viewports[i], pitch);
288 button = get_action(CONTEXT_PITCHSCREEN,HZ);
289 switch (button) {
290 case ACTION_PS_INC_SMALL:
291 delta = PITCH_SMALL_DELTA;
292 break;
294 case ACTION_PS_INC_BIG:
295 delta = PITCH_BIG_DELTA;
296 break;
298 case ACTION_PS_DEC_SMALL:
299 delta = -PITCH_SMALL_DELTA;
300 break;
302 case ACTION_PS_DEC_BIG:
303 delta = -PITCH_BIG_DELTA;
304 break;
306 case ACTION_PS_NUDGE_RIGHT:
307 new_pitch = pitch_increase(pitch, PITCH_NUDGE_DELTA, false);
308 nudged = (new_pitch != pitch);
309 pitch = new_pitch;
310 break;
312 case ACTION_PS_NUDGE_RIGHTOFF:
313 if (nudged) {
314 pitch = pitch_increase(pitch, -PITCH_NUDGE_DELTA, false);
316 nudged = false;
317 break;
319 case ACTION_PS_NUDGE_LEFT:
320 new_pitch = pitch_increase(pitch, -PITCH_NUDGE_DELTA, false);
321 nudged = (new_pitch != pitch);
322 pitch = new_pitch;
323 break;
325 case ACTION_PS_NUDGE_LEFTOFF:
326 if (nudged) {
327 pitch = pitch_increase(pitch, PITCH_NUDGE_DELTA, false);
329 nudged = false;
330 break;
332 case ACTION_PS_RESET:
333 pitch = 1000;
334 sound_set_pitch( pitch );
335 break;
337 case ACTION_PS_TOGGLE_MODE:
338 pitch_mode = -pitch_mode;
339 break;
341 case ACTION_PS_EXIT:
342 exit = true;
343 break;
345 default:
346 if(default_event_handler(button) == SYS_USB_CONNECTED)
347 return 1;
348 break;
350 if(delta)
352 if (pitch_mode == PITCH_MODE_ABSOLUTE) {
353 pitch = pitch_increase(pitch, delta, true);
354 } else {
355 pitch = pitch_increase_semitone(pitch, delta > 0 ? true:false);
358 delta = 0;
361 #if CONFIG_CODEC == SWCODEC
362 pcmbuf_set_low_latency(false);
363 #endif
364 return 0;