summaryrefslogtreecommitdiffstats
path: root/Graphic_Equalizer/src/eventhandler.hcc
blob: 2502518b9ac9c3b7ee93422f54694c87273b1a30 (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
/*! \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 "display_shared.hch"
#include "eventhandler_shared.hch"
#include "eventhandler.hch"

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

extern chan unsigned 1 maskupdate_notification;

/*! \fn		void eventhandler_main(audiodata_t *audiodata, events_t *events, mousedata_t *mousedata);
 * \brief	
 * 
 * \param	events_t *events	pointer to struct with all events.
 * \param	mousedata_t *mousedata	pointer to struct with mouse data.
 *
 * \return	Never Returns.
 * \retval	void
 */
void eventhandler_main(audiodata_t *audiodata, events_t *events, mousedata_t *mousedata) {
	mpram {
		ram unsigned 4 write[768];	
		rom unsigned 4 read[768];	
	} equalizer_levels with { block = "BlockRAM"};

	unsigned 10 preset_offset;
	unsigned 7 equalizer_bands;
	unsigned 5 volume_left;
	unsigned 1 newmaskupdate;

	for (;;) {
		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 the current mask equals the help button
				 * we flip the help bit.
				 */
				if (BUTTON_HELP == events->mask) {
					events->help = !events->help;
				} 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.write[preset_offset];

					/*
					 * Reverse fill
					 */
					equalizer_bands = 0;
					do {
						events->equalizer_position[equalizer_bands] = equalizer_table_inv[audiodata->equalizer_levels_ptr[equalizer_bands]];
						equalizer_bands++;
					} while (equalizer_bands);
				} else {
					delay;
				}

				if ((BUTTON_1 <= events->mask) && (events->mask <= BUTTON_5)) {
					events->equalizer_mode = (events->mask -BUTTON_1) <- 3;
				} else {
					delay;
				}

				/*
				 * 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;
				}

				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) {
					/*
					 * Copy the current Y cursor position
					 * to the events struct. This we then
					 * later use for drawing purposes.
					 */
					events->volume_position = 0 @ mousedata->y;
					/*
					 * Look the relative y-coordinate up in
					 * the volumecontrol lookup table. We
					 * make a local copy here because the
					 * RC200 call doesn't behave nicely
					 * when passing anything else.
					 */
					volume_left = volumecontrol_table[((mousedata->y) -65) <- 6];
					/*
					 * We feel that volume gets softer the
					 * closer it gets to the 0, and louder
					 * as it approaches 0x1f. The SetVolume
					 * RC200 call does this in an unnatural
					 * reverse way. Therefor we fix it.
					 */
					RC200AudioOutSetVolume(!volume_left, 0x1f -volume_left, 0x1f -volume_left);
				} else {
					delay;
				}

				if ((AREA_EQUALIZER_MIN <= events->mask) && (events->mask <= AREA_EQUALIZER_MAX)) {
					/*
					 * Copy the current Y position into a
					 * store for the current equalizer bar.
					 * The index is determined by the mask
					 * minus the maskoffset.
					 */
					events->equalizer_position[((events->mask -AREA_EQUALIZER_MIN) <- 7)] = mousedata->y;
print_string("Eventsmask: ");print_hex_value(0 @ ((events->mask -AREA_EQUALIZER_MIN) <- 7));print_eol();
					/*
					 * We look our current possition up in
					 * the lookup table. We determin our
					 * position via the current Y position
					 * (minus an offset of course). We
					 * store the result in the array of
					 * equalizer levels. The index we get
					 * from the current mask. We add the
					 * size of our equalizer bands times
					 * the active preset.
					 *
					 * TODO: lock equalizer store with a
					 * semaphore!
					 */
					audiodata->equalizer_levels_ptr[(events->mask -AREA_EQUALIZER_MIN) <- 7] = equalizer_table[(mousedata->y -382) <- 7];
				} else {
					delay;
				}
				break;
		}
	}
} /* --- eventhandler_main() --- */



/*
 * Volume Control lookuptable.
 * TODO: This table is now hardcoded. To ensure full skinability this table
 * should be dynamically loaded.
 */
rom unsigned 5 volumecontrol_table[46] = {31, 31, 30, 30, 29, 29, 28, 28, 27, 27, 26, 26, 25, 25, 24,  24, 23, 23, 22, 22, 21, 21, 20, 20, 19, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0};

/*
 * Equalizer lookuptabes.
 * TODO: This table is now hardcoded. To ensure full skinability this table
 * should be dynamically loaded.
 */
rom unsigned 4 equalizer_table[66] = {15, 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, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0};
rom unsigned 9 equalizer_table_inv[16] = {448, 444, 440, 436, 432, 428, 424, 420, 416, 412, 408, 404, 400, 396, 392, 382};