summaryrefslogtreecommitdiffstats
path: root/Graphic_Equalizer/src/eventhandler.hcc
blob: 983f67b5c3282656de5dcfb5013745f3a397ba68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*! \file eventhandler.hcc
 *
 * \section generic	This modules coordinates all events. Thus for example
 *			when the mousehandler registers a 'click' we coordinate
 *			the actions that follow that click.
 *
 * \section project Project information.
 * Project Graphic Equalizer\n
 * \author O.M. Schinagl
 * \date 20041110
 * \version 0.1
 *
 * \section copyright Copyright
 * Copyright ©2004 Koninklijke Philips Electronics N.V. All rights reserved
 *
 * \section history Change history
 * 20041110: O.M. Schinagl\n	Initial version
 *
 ********************************************************************/

/******** System Includes *************/
#include <stdlib.hch>

#include "pal_master.hch"

/******** Application Includes ********/
#include "configuration.hch"
#include "audio.hch"
#include "mouse_shared.hch"
#include "eventhandler_shared.hch"
#include "display_shared.hch"
#include "eventhandler.hch"
#include "events.hch"
#include "display.hch"

#if HAVE_DEBUG
	#include "debug.hch"
#endif



/*
 */
extern chan unsigned 1 maskupdate_notification;
extern chan unsigned 1 event_notification;


/*! \fn		void eventhandler_main(audiodata_t *audiodata, events_t *events, mousedata_t *mousedata);
 * \brief	
 * 
 * \param	*audiodata	pointer to struct for setting equalizer levels.
 * \param	*events	pointer to struct with all events for display states.
 * \param	*mousedata	pointer to struct with mouse data for X and Y
 *				coordinates.
 *
 * \return	Never Returns.
 * \retval	void
 */
inline void eventhandler_main(audiodata_t *audiodata, events_t *events, mousedata_t *mousedata, skindata_t *skindata) {
	mpram {
		ram unsigned 4 write[768];	
		rom unsigned 4 read[768];	
	} equalizer_levels with { block = "BlockRAM"};

	unsigned 11 reference_point, reference_point_right;
	unsigned 10 preset_offset;
	unsigned 6 gain_counter;
	unsigned 5 old_volume, old_audio_player_volume;
	unsigned 5 volume;
	unsigned 4 gain;
	unsigned 4 index_offset, index_end;
	unsigned 4 equalizer_mode_local;
	unsigned 1 newmaskupdate, eventupdate;

	macro proc set_mode(newmode)
  	{
		events->mode = (events->mode[4]) ? (newmode | EVENTS_MODE_AUDIO_PLAYER): newmode;
	} 

	audiodata->display_log = TRUE;
	events->image = IMAGE_SKIN;
	events->button_demo_state = FALSE;
	events->button_reset_state = FALSE;
	events->saturated = FALSE;
	events->mode = 1;
	events->locked_gain = FALSE;

	volume = change_volume_from_coordinate(volumecontrol_table_inv[0x18], events, skindata);
	old_audio_player_volume = volume;
	old_volume = volume;
	gain = change_inputgain_from_coordinate(inputgain_table_inv[0xf], events, skindata);
	load_preset(LOAD_PRESET_RESET, equalizer_levels.write);
	reload_equalizer(events, &equalizer_levels.write[preset_offset]);
	


	par {
		while (TRUE) {
			event_notification ? eventupdate;

			if (audiodata->saturated) {
				gain_counter--;
				if (!gain_counter) {
					gain--;
					change_inputgain_from_coordinate(inputgain_table_inv[gain], events, skindata);
					events->saturated = TRUE;
				}
			}
		}
		while (TRUE) {
		
			maskupdate_notification ? newmaskupdate;
			/*
			 * First we determine what mousestate we currently have. Then
			 * we check where we are to decide what to do.
			 */
			switch (mousedata->state) {
				case MOUSE_STATE_ON_PRESS:
					/*
					 * If we are displaying the graphic visual,
					 * any press returns to the application.
					 */
					if (IMAGE_GRAPH == events->image) {
						events->image = IMAGE_SKIN;
						events->mask = 0;
						set_mode(EVENTS_MODE_SKIN);
					} else {
						delay;
					}

					/*
					 */
					if (BUTTON_MODE == events->mask) {
						if (IMAGE_GRAPH == events->image) {
							events->image = IMAGE_SKIN;
							set_mode(EVENTS_MODE_SKIN);
						} else {
							events->image = IMAGE_GRAPH;
							set_mode(EVENTS_MODE_GRAPH);
						}
					} else {
						delay;
					}

					/*
					 * If the current mask equals the help button
					 * we set display mode to help.
					 */
					if ((!events->mode[2]) && (BUTTON_HELP == events->mask)) {
						/*
						 * Change the mode to help if it's
						 * currently not set, otherwise go back
						 * to skin mode.
						 */
						if (events->mode[1]) {
							events->image = IMAGE_SKIN;
							set_mode(EVENTS_MODE_SKIN);
						} else {
							events->image = IMAGE_HELP;
							set_mode(EVENTS_MODE_HELP);
						}
					} else {
						delay;
					}

					if ((!events->mode[1]) && (BUTTON_ABOUT == events->mask)) {
						/*
						 * Change the mode to about if it's
						 * currently not set, otherwise go back
						 * to skin mode.
						 */
						if (events->mode[2]) {
							events->image = IMAGE_SKIN;
							set_mode(EVENTS_MODE_SKIN);
						} else {
							events->image_about = IMAGE_ABOUT_TOP_TASS;
							events->image = IMAGE_ABOUT;
							set_mode(EVENTS_MODE_ABOUT);
						}
					} else {
						delay;
					}

					if (BUTTON_EXIT_TO_SKIN == events->mask) {
						/*
						 */
						events->image = IMAGE_SKIN;
						set_mode(EVENTS_MODE_SKIN);
					} else {
						delay;
					}

					/*
					 */
					if ((BUTTON_URL_FONTYS <= events->mask) && (events->mask <= BUTTON_URL_DETAILS)) {
						events->image_about = (events->mask -BUTTON_URL_FONTYS) <- 5;
					} else {
						delay;
					}

					/*
					 * The Preset buttons span from 1 to 6 so if
					 * the mask one of those, we'll change the
					 * pointer to point to the current preset.
					 */
					if ((BUTTON_PRESET_1 <= events->mask) && (events->mask <= BUTTON_PRESET_6)) {
						/*
						 * The active preset is determined by
						 * the mask minus an offset. Hence
						 * ranging our active preset from 0 to
						 * 6.
						 */
						events->active_preset = 0 @ (events->mask -BUTTON_PRESET_1);
						/*
						 * Each equalizer is 128 bands wide,
						 * thus we need to add 128 * the preset
						 * for each different preset. This
						 * offset is calculated here.
						 */
						preset_offset = events->active_preset << 7;
						/*
						 * We set the pointer to the active
						 * part of the array by using the
						 * preset offset as our index. Hence
						 * depending on the selected preset
						 * we point to 0, 128, 256, 384, 512
						 * or 640.
						 */
						audiodata->equalizer_levels_ptr = &equalizer_levels.read[preset_offset];
						/*
						 * Reload the equalizer bars
						 * from our buffer into
						 * the display memory.
						 */
						reload_equalizer(events, &equalizer_levels.write[preset_offset]);
					} else {
						delay;
					}

					if ((BUTTON_PRECISE <= events->mask) && (events->mask <= BUTTON_CONVEX_FULL)) {
						events->equalizer_mode = (events->mask -BUTTON_PRECISE) <-4;
					} else {
						delay;
					}

					/*
					 * If the demo button was pressed load the
					 * demo preset values into the presets.
					 */
					if (BUTTON_DEMO == events->mask) {
						events->button_demo_state = TRUE;
						load_preset(LOAD_PRESET_DEMO, equalizer_levels.write);
						reload_equalizer(events, &equalizer_levels.write[preset_offset]);
					}

					/*
					 * If the reset button was pressed load the
					 * reset values into the presets.
					 */             
					if (BUTTON_RESET == events->mask) {
						events->button_reset_state = TRUE;
						load_preset(LOAD_PRESET_RESET, equalizer_levels.write);
						reload_equalizer(events, &equalizer_levels.write[preset_offset]);
					}

					/*
					 * If the current mask equals the log button,
					 * we flip the display_log bit.
					 */
					if (BUTTON_LOG == events->mask) {
						audiodata->display_log = !audiodata->display_log;
					} else {
						delay;
					}

					if (BUTTON_INPUT_SELECT == events->mask) {					
						if (events->mode[4]) {
							/*
							 * Disable the audio player
							 */
							events->mode &= (~EVENTS_MODE_AUDIO_PLAYER);
							old_audio_player_volume = volume;
							volume = old_volume;
							change_volume_from_coordinate(volumecontrol_table_inv[volume], events, skindata);
							audiodata->player_state = PLAYER_DISABLED;
							events->locked_gain = FALSE;
						} else {
							/*
 							 * Enable the audio player
							 */
							events->mode |= EVENTS_MODE_AUDIO_PLAYER;
							old_volume = volume;
							volume = old_audio_player_volume;
							change_volume_from_coordinate(volumecontrol_table_inv[volume], events, skindata);
							audiodata->player_state = PLAYING;
							events->locked_gain = TRUE;
						}
					} else {
						delay;
					}



					if ((BUTTON_PAUSE == events->mask) && (audiodata->player_state != STOPPED)) {
						if (PLAYING == audiodata->player_state) {
							audiodata->player_state = PAUSED;
						} else {
							audiodata->player_state = PLAYING;
						}
					} else {
						delay;
					}

					if (BUTTON_PLAY == events->mask) {
						audiodata->player_state = PLAYING;
					} else {
						delay;
					}

					if (BUTTON_STOP == events->mask) {
						audiodata->player_state = STOPPED;
					} else {
						delay;
					}

					if (BUTTON_NEXT_TRACK == events->mask) {
						audiodata->player_state = NEXT_TRACK;
						events->next_track_state = TRUE;
					} else {
						delay;
					}

					if (BUTTON_PREV_TRACK == events->mask) {
						audiodata->player_state = PREV_TRACK;
						events->prev_track_state = TRUE;
					} else {
						delay;
					}

					if (BUTTON_REPEAT == events->mask) {
							audiodata->player_mode = !audiodata->player_mode;
					} else {
						delay;
					}

					break;

				case MOUSE_STATE_DOWN:
					/*
					 * If we are in the volume area we update the
					 * volume level for both channels.
					 */
					if (AREA_VOLUME_YAXIS == events->mask) {
						/*
						 * Change the volume depending
						 * on the y position.
						 */
						volume = change_volume_from_coordinate(mousedata->y, events, skindata);
					} else {
						delay;
					}

					if ((AREA_INPUTGAIN_YAXIS == events->mask) && (!events->locked_gain)) {
						/*
						 * Change the inputgain
						 * depending on the y position.
						 * We store the 'set' gain to
						 * be used with the automatic
						 * input gain detection.
						 */
						gain = change_inputgain_from_coordinate(mousedata->y, events, skindata);
						/*
						 * We manually update the
						 * inputgain so we change the
						 * color of the slider.
						 */
						events->saturated = FALSE;
					} else {
						delay;
					}

					if ((AREA_EQUALIZER_MIN <= events->mask) && (events->mask <= AREA_EQUALIZER_MAX)) {

						if (EQUALIZERMODE_PRECISE == events->equalizer_mode) {
							events->locked_display = TRUE;
							equalizer_levels.write[preset_offset +(0 @ events->mask) -AREA_EQUALIZER_MIN] = equalizer_table[(mousedata->y -382) <- 10];
							events->equalizer_display[(events->mask -AREA_EQUALIZER_MIN) <- 7] = 0 @ (mousedata->y);
							events->locked_display = FALSE;
						} else {
						
							equalizer_mode_local = events->equalizer_mode +1;

							events->locked_display = TRUE;
							index_offset = ((equalizer_mode_local << 1)); /* delay; */

							if (events->mask < (AREA_EQUALIZER_MIN +(0 @ equalizer_mode_local))) {
								reference_point = events->equalizer_display[AREA_EQUALIZER_MIN -AREA_EQUALIZER_MIN];
								index_offset -= (((0 @ equalizer_mode_local) -(events->mask -AREA_EQUALIZER_MIN)) <- 4);
							} else {
								reference_point = events->equalizer_display[(events->mask - (0 @ equalizer_mode_local) -AREA_EQUALIZER_MIN) <- 7];
								index_offset -= 1;
							}

							if (events->mask > (AREA_EQUALIZER_MAX -(0 @ equalizer_mode_local))) {
								reference_point_right = events->equalizer_display[AREA_EQUALIZER_MAX -AREA_EQUALIZER_MIN];
								index_end = (((0 @ equalizer_mode_local) -(AREA_EQUALIZER_MAX -events->mask)) <- 4) -1;
							} else {
								reference_point_right = events->equalizer_display[(events->mask + (0 @ equalizer_mode_local) -AREA_EQUALIZER_MIN) <- 7];
								index_end = 0;
							}
							events->locked_display = FALSE;

							for (; index_offset != index_end; index_offset--) {
								unsigned 11 average_bar;
								unsigned 7 equalizer_index;
								unsigned 4 bar_index;
							
								if (index_offset == equalizer_mode_local) {
									reference_point = reference_point_right;
								} else {
									delay;
								}

								if (index_offset > equalizer_mode_local) 
								{
									bar_index = (index_offset -equalizer_mode_local);			
									equalizer_index = ((events->mask -(0 @ bar_index) -AREA_EQUALIZER_MIN) <- 7);
								} else 
								{
									bar_index = (equalizer_mode_local -index_offset);
									equalizer_index = ((events->mask +(0 @ bar_index) -AREA_EQUALIZER_MIN) <- 7);	
								}

								if (reference_point > mousedata->y) {
									average_bar = mousedata->y +((reference_point -mousedata->y) >> (equalizer_mode_local -bar_index));
								} else {
								 	average_bar = mousedata->y -((mousedata->y -reference_point) >> (equalizer_mode_local -bar_index));
								}
													
								events->locked_display = TRUE;
								equalizer_levels.write[preset_offset +(0 @ equalizer_index)] = equalizer_table[(average_bar -382) <- 10];
								events->equalizer_display[equalizer_index] = average_bar;
								events->locked_display = FALSE;						
							}
						}
					} else {
						delay;
					}
					
					if (BUTTON_DEMO != events->mask) {
						events->button_demo_state = FALSE;
					}
					if (BUTTON_RESET != events->mask) {
						events->button_reset_state = FALSE;
					}

					if (BUTTON_NEXT_TRACK != events->mask) {
						events->next_track_state = FALSE;
					}

					if (BUTTON_PREV_TRACK != events->mask) {
						events->prev_track_state = FALSE;
					} 

					break;

				case MOUSE_STATE_ON_RELEASE:
					events->button_demo_state = FALSE;
					events->button_reset_state = FALSE;
					events->next_track_state = FALSE;
					events->prev_track_state = FALSE;
					break;

				default:
					break;
			}
		}
	}
} /* --- eventhandler_main() --- */



/*
 * Volume Control lookuptable.
 * TODO: This table is now hardcoded. To ensure full skinability this table
 * should be dynamically loaded.
 */
ram unsigned 5 volumecontrol_table[46] = {31, 30, 30, 29, 28, 28, 27, 26, 25, 25, 24, 23, 23, 22, 21, 21, 20, 19, 19, 18, 17, 17, 16, 15, 14, 14, 13, 12, 12, 11, 10, 10, 9, 8, 8, 7, 6, 6, 5, 4, 3, 3, 2, 1, 1, 0};
ram unsigned 11 volumecontrol_table_inv[32] = {111, 110, 108, 107, 104, 102, 101, 99, 98, 96, 95, 94, 92, 91, 89, 86, 85, 83, 82, 81, 79, 78, 76, 75, 73, 72, 70, 69, 67, 66};

ram unsigned 4 inputgain_table[46] = {15, 15, 15, 15, 14, 14, 14, 14, 13, 13, 13, 13, 12, 12, 12, 12, 11, 11, 11, 11, 10, 10, 10, 10, 9, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 0};
ram unsigned 11 inputgain_table_inv[16] = {111, 108, 105, 102, 99, 96, 93, 90, 87, 84, 81, 78, 75, 72, 69, 66};