This repository was archived by the owner on Nov 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAppController.m
More file actions
460 lines (371 loc) · 11.1 KB
/
AppController.m
File metadata and controls
460 lines (371 loc) · 11.1 KB
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
//
// AppController.m
// MusicController
//
// Created by David Becerril Valle on 6/20/08.
// Copyright 2008 DaveBV.es. All rights reserved.
//
#import "AppController.h"
@implementation AppController
-(void) awakeFromNib
{
// WiiMote
discovery = [[WiiRemoteDiscovery alloc] init];
[discovery setDelegate:self];
[textStatusWii setStringValue:@"Press \"Discover\" to find a Wiimote"];
[bDiscovery setEnabled:YES];
// OSC
[remoteOSCAddress setStringValue:@"127.0.0.1"] ;
[OSCport setIntValue:30200];
[OSCdestinationPath setStringValue:[OSCobjeto OSCdestinationPath]] ;
}
-(id) init {
[super init];
// Iniciamos los eventos midi
_midiEvts = [[MidiEventOutput alloc] init];
[_midiEvts setChannel:15-1];
NSLog(@"Init");
midiDevicesArray = [[MidiDevice getAllMidiDevices] retain];
NSLog(@"Array iniciado");
if (0 == [midiDevicesArray count])
{
// No MIDI devices. Blarg im ded!
[[NSAlert alertWithMessageText:@"No MIDI endpoints available" defaultButton:nil
alternateButton:nil otherButton:nil
informativeTextWithFormat:@"Enable some MIDI devices in Audio MIDI Setup and relaunch WiiToMidi."]
runModal];
[NSApp terminate:self];
}
_midiTimer = nil;
// Escoger el "destino" midi
_midiDest = [[midiDevicesArray objectAtIndex:0] midiEndpoint];
// Creamos el Cliente y el puerto
MIDIClientCreate(CFSTR("MusicController"), NULL, NULL, &_midi);
MIDIOutputPortCreate(_midi, CFSTR("Output"), &_midiOut);
// WiiControls
_WiiControls = [[WiiControls alloc] init];
// Logica
_generadorEv = [[GeneradorEv alloc] init];
// OSC
OSCobjeto = [[OSCEventClass alloc] init] ;
return self;
}
- (void) dealloc
{
[wii release];
[discovery release];
[super dealloc];
}
- (IBAction)doDiscovery:(id)sender
{
// CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.5, NO);
[discovery start];
[textStatusWii setStringValue:@"Please press 1 button and 2 button simultaneously"];
[discoverySpinner startAnimation:self];
[bDiscovery setEnabled:NO];
}
- (void) wiiRemoteDisconnected:(IOBluetoothDevice*)device {
[wii release];
wii = nil;
[textStatusWii setStringValue:@"Wiimote Lost"];
[bDiscovery setEnabled:YES];
// Desactivamos el Timer
[self setMidiTimer:nil];
}
- (IBAction)doCalibration:(id)sender{
//id config = [mappingController selection];
if ([sender tag] == 0){
x1 = tmpAccX;
y1 = tmpAccY;
z1 = tmpAccZ;
NSLog(@"Calibrando A");
}
if ([sender tag] == 1){
x2 = tmpAccX;
y2 = tmpAccY;
z2 = tmpAccZ;
NSLog(@"Calibrando Exp port");
}
if ([sender tag] == 2){
x3 = tmpAccX;
y3 = tmpAccY;
z3 = tmpAccZ;
NSLog(@"Calibrando Left Side");
}
x0 = (x1 + x2) / 2.0;
y0 = (y1 + y3) / 2.0;
z0 = (z2 + z3) / 2.0;
[_WiiControls setAccX_zero:x0];
[_WiiControls setAccY_zero:y0];
[_WiiControls setAccZ_zero:z0];
[_WiiControls setAccX_1g:x3];
[_WiiControls setAccY_1g:y2];
[_WiiControls setAccZ_1g:z1];
}
//#pragma mark -
//#pragma mark WiiRemoteDiscovery delegates
- (void) WiiRemoteDiscoveryError:(int)code {
[discoverySpinner stopAnimation:self];
[textStatusWii setStringValue:@"WiiRemoteDiscovery error"];
[bDiscovery setEnabled:YES];
// Desactivamos el Timer
[self setMidiTimer:nil];
}
- (void) willStartWiimoteConnections {
[textStatusWii setStringValue:@"WiiRemote discovered."];
[bDiscovery setEnabled:NO];
}
- (void) WiiRemoteDiscovered:(WiiRemote*)wiimote {
// [discovery stop];
// the wiimote must be retained because the discovery provides us with an autoreleased object
wii = [wiimote retain];
[wiimote setDelegate:self];
// Logica
[wii setLEDEnabled1:1 enabled2:0 enabled3:0 enabled4:1] ;
[textStatusWii setStringValue:@"Connected to WiiRemote"];
[discoverySpinner stopAnimation:self];
// Desacivo el botón de "discover"
[bDiscovery setEnabled:NO];
//[wiimote setLEDEnabled1:YES enabled2:NO enabled3:NO enabled4:YES];
[wiimote setMotionSensorEnabled:YES];
// [wiimote setIRSensorEnabled:YES];
// Activamos el timer para mandar midi
[self setMidiTimer:[NSTimer scheduledTimerWithTimeInterval:0.016666
target:self
selector:@selector(sendMidi:)
userInfo:nil
repeats:TRUE]];
}
// Botones Wii
- (void) buttonChanged:(WiiButtonType)type isPressed:(BOOL)isPressed{
switch (type) {
case WiiRemoteAButton:
[_WiiControls setAButton:isPressed];
[_generadorEv manipular:_WiiControls
mandarloA:_midiEvts
tipoBoton:type
mandarOSC: OSCobjeto];
break;
case WiiRemoteBButton:
[_WiiControls setBButton:isPressed];
[_generadorEv manipular: _WiiControls
mandarloA: _midiEvts
tipoBoton: type
mandarOSC: OSCobjeto];
break;
case WiiRemoteOneButton:
[_WiiControls setOneButton:isPressed];
[_generadorEv manipular:_WiiControls
mandarloA:_midiEvts
tipoBoton:type
mandarOSC: OSCobjeto];
break;
case WiiRemoteTwoButton:
[_WiiControls setTwoButton:isPressed];
[_generadorEv manipular:_WiiControls
mandarloA:_midiEvts
tipoBoton:type
mandarOSC:OSCobjeto];
break;
case WiiRemoteMinusButton:
[_WiiControls setMinusButton:isPressed];
[_generadorEv cambiaModo:_WiiControls
conWiimote:wii
isPressed:isPressed];
[_generadorEv manipular:_WiiControls
mandarloA:_midiEvts
tipoBoton:type
mandarOSC: OSCobjeto];
break;
case WiiRemoteHomeButton:
[_WiiControls setHomeButton:isPressed];
[_generadorEv manipular:_WiiControls
mandarloA:_midiEvts
tipoBoton:type
mandarOSC: OSCobjeto];
break;
case WiiRemotePlusButton:
[_WiiControls setPlusButton:isPressed];
[_generadorEv cambiaModo:_WiiControls
conWiimote:wii
isPressed:isPressed];
[_generadorEv manipular:_WiiControls
mandarloA:_midiEvts
tipoBoton:type
mandarOSC: OSCobjeto];
break;
case WiiRemoteUpButton:
[_WiiControls setUpButton:isPressed];
[_generadorEv manipular:_WiiControls
mandarloA:_midiEvts
tipoBoton:type
mandarOSC: OSCobjeto];
break;
case WiiRemoteDownButton:
[_WiiControls setDownButton:isPressed];
[_generadorEv manipular:_WiiControls
mandarloA:_midiEvts
tipoBoton:type
mandarOSC: OSCobjeto];
break;
case WiiRemoteLeftButton:
[_WiiControls setLeftButton:isPressed];
[_generadorEv manipular:_WiiControls
mandarloA:_midiEvts
tipoBoton:type
mandarOSC: OSCobjeto];
break;
case WiiRemoteRightButton:
[_WiiControls setRightButton:isPressed];
[_generadorEv manipular:_WiiControls
mandarloA:_midiEvts
tipoBoton:type
mandarOSC: OSCobjeto];
break;
default:
break;
}
}
- (void) accelerationChanged:(WiiAccelerationSensorType)type accX:(unsigned short)accX accY:(unsigned short)accY accZ:(unsigned short)accZ{
tmpAccX = accX;
tmpAccY = accY;
tmpAccZ = accZ;
if ([_WiiControls manualCalib]) {
x0 = [_WiiControls accX_zero];
x3 = [_WiiControls accX_1g];
y0 = [_WiiControls accY_zero];
y2 = [_WiiControls accY_1g];
z0 = [_WiiControls accZ_zero];
z1 = [_WiiControls accZ_1g];
}
else {
WiiAccCalibData data = [wii accCalibData:WiiRemoteAccelerationSensor];
x0 = data.accX_zero;
x3 = data.accX_1g;
y0 = data.accY_zero;
y2 = data.accY_1g;
z0 = data.accZ_zero;
z1 = data.accZ_1g;
}
double ax = (double)(accX - x0) / (x3 - x0);
double ay = (double)(accY - y0) / (y2 - y0);
double az = (double)(accZ - z0) / (z1 - z0) * (-1.0);
double roll;
if (az < 0) {
roll = atan(ax) * 180.0 / 3.14 * 2;
}
else if(ax > 0) {
roll = 180 - atan(ax) * 180.0 / 3.14 * 2;
}
else {
roll = - 180 - atan(ax) * 180.0 / 3.14 * 2;
}
double pitch = atan(ay) * 180.0 / 3.14 * 2;
[_WiiControls setAceleracionx:ax];
[_WiiControls setAceleraciony:ay];
[_WiiControls setAceleracionz:az];
[_WiiControls setRoll:roll];
[_WiiControls setPitch:pitch];
[_generadorEv manipularAcc:_WiiControls
mandarloA:_midiEvts
mandarOSC:OSCobjeto];
}
- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *) sender
{
NSLog(@"Se terminó");
[wii closeConnection];
return NSTerminateNow;
}
// MIDI
-(IBAction) midiDeviceChanged:(NSPopUpButton*)button {
NSLog(@"Se ha seleccionado %@",
[[midiDevicesArray objectAtIndex:[button indexOfSelectedItem]] midiDeviceName]);
_midiDest = [[midiDevicesArray objectAtIndex:[button indexOfSelectedItem]] midiEndpoint];
}
-(id) setMidiTimer:(NSTimer*)timer
{
[_midiTimer invalidate];
[_midiTimer autorelease];
_midiTimer = [timer retain];
return self;
}
-(void) sendMidi:(NSTimer*)timer{
if (timer != _midiTimer) return;
//NSLog(@"Entra en sendMidi");
MIDISend(_midiOut, _midiDest, [_midiEvts list]);
[_midiEvts clearList];
}
// OSC
-(IBAction)connectOSC:(id)sender
{
if (wii == nil) {
NSLog(@"No Wiimote detected\n") ;
return;
}
[OSCobjeto init_mod_OSCsettings:[remoteOSCAddress stringValue] atPort:[OSCport intValue]] ;
}
-(IBAction)disconnectOSC:(id)sender
{
[OSCobjeto disconnect_OSC] ;
}
-(IBAction)setupField:(id)sender
{
switch ([sender tag]){
case 0:
[OSCobjeto change_OSCsettings:[sender stringValue] atPort:(int) -1] ;
break;
case 1:
[OSCobjeto change_OSCsettings:nil atPort:[sender intValue]] ;
break;
case 3:
[OSCobjeto setOSCdestinationPath:[sender stringValue]] ;
break;
}
}
-(IBAction)testOSC:(id)sender
{
[OSCobjeto sendOSCPacket_float:[NSArray arrayWithObjects: [NSNumber numberWithFloat:16], [NSNumber numberWithFloat:146], nil]
typeOSC:[NSString stringWithFormat:@"tipoOSC"]];
}
// Mode override
-(IBAction)overrideMode:(id)sender
{
if (wii == nil){
[sender setState:NSOffState] ;
NSLog(@"No Wiimote detected\n") ;
return;
}
NSLog(@"%d",[sender state]);
//[sender setState:NSOnState];
//
switch ([sender state]) {
case NSOnState:
[_generadorEv setModo_ant:[_generadorEv modo]] ;
[_generadorEv cambiaModo:5 conWiimote:wii] ;
break;
case NSOffState:
[_generadorEv cambiaModo:[_generadorEv modo_ant] conWiimote:wii] ;
break;
}
}
// Link to my web
-(IBAction) abrirEnlace:(id)sender{
if ([sender tag] == 0){
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://davebv.com"]] ;
[[NSWorkspace sharedWorkspace] openURL:url];
}
if ([sender tag] == 1){
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.davebv.com/music-controller-midi-and-osc-wiimote"]];
[[NSWorkspace sharedWorkspace] openURL:url];
}
if ([sender tag] == 2){
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://davebv.com"]];
[[NSWorkspace sharedWorkspace] openURL:url];
}
}
// Close last window
//-(BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
//{
//return YES;
//}
@end