forked from teamon/SumoDebugger
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppController.m
481 lines (391 loc) · 12.4 KB
/
AppController.m
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
//
// AppController.m
// SumoDebugger
//
// Created by Tymon Tobolski on 10-01-16.
// Copyright 2010 Politechnika Wrocławska. All rights reserved.
//
#import "AppController.h"
#import "AMSerialPortList.h"
#import "JoystickView.h"
#define DefaultPortPath @"DefaultPortPath"
@implementation AppController
- (void)awakeFromNib
{
groundSensors[0].displayCheckBox = ground0DisplayCheckBox;
groundSensors[1].displayCheckBox = ground1DisplayCheckBox;
groundSensors[2].displayCheckBox = ground2DisplayCheckBox;
groundSensors[3].displayCheckBox = ground3DisplayCheckBox;
groundSensors[0].activeCheckBox = ground0ActiveCheckBox;
groundSensors[1].activeCheckBox = ground1ActiveCheckBox;
groundSensors[2].activeCheckBox = ground2ActiveCheckBox;
groundSensors[3].activeCheckBox = ground3ActiveCheckBox;
for(int i=0 ; i<GROUND_COUNT; i++) {
[groundSensors[i].activeCheckBox setAction:@selector(activateGroundSensor:)];
}
distanceSensors[0].label = dist0ValueLabel;
distanceSensors[1].label = dist1ValueLabel;
distanceSensors[2].label = dist2ValueLabel;
distanceSensors[3].label = dist3ValueLabel;
distanceSensors[4].label = dist4ValueLabel;
distanceSensors[5].label = dist5ValueLabel;
distanceSensors[0].levelIndicator = dist0LevelIndicator;
distanceSensors[1].levelIndicator = dist1LevelIndicator;
distanceSensors[2].levelIndicator = dist2LevelIndicator;
distanceSensors[3].levelIndicator = dist3LevelIndicator;
distanceSensors[4].levelIndicator = dist4LevelIndicator;
distanceSensors[5].levelIndicator = dist5LevelIndicator;
distanceSensors[0].activeCheckBox = dist0ActiveCheckBox;
distanceSensors[1].activeCheckBox = dist1ActiveCheckBox;
distanceSensors[2].activeCheckBox = dist2ActiveCheckBox;
distanceSensors[3].activeCheckBox = dist3ActiveCheckBox;
distanceSensors[4].activeCheckBox = dist4ActiveCheckBox;
distanceSensors[5].activeCheckBox = dist5ActiveCheckBox;
distanceSensors[0].historyView = dist0HistoryView;
distanceSensors[1].historyView = dist1HistoryView;
distanceSensors[2].historyView = dist2HistoryView;
distanceSensors[3].historyView = dist3HistoryView;
distanceSensors[4].historyView = dist4HistoryView;
distanceSensors[5].historyView = dist5HistoryView;
for(int i=0 ; i<DISTANCE_COUNT; i++) {
[distanceSensors[i].activeCheckBox setAction:@selector(activateDistanceSensor:)];
}
engines[0].slider = engine0Slider;
engines[1].slider = engine1Slider;
engines[0].label = engine0ValueLabel;
engines[1].label = engine1ValueLabel;
[engines[0].slider setAction:@selector(setEnginePower:)];
[engines[1].slider setAction:@selector(setEnginePower:)];
engineMode = kNormal;
// serial port
preferences = [[NSUserDefaults standardUserDefaults] retain];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didAddPorts:)
name:AMSerialPortListDidAddPortsNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRemovePorts:)
name:AMSerialPortListDidRemovePortsNotification
object:nil];
[AMSerialPortList sharedPortList];
[self updatePortList];
NSString *defaultPort = [preferences objectForKey:DefaultPortPath];
if(defaultPort && ![defaultPort isEqualToString: @""])
{
[portListPopUpButton selectItemWithTitle: defaultPort];
[self initPortFor: defaultPort];
}
// joystick
joystick.list = [[DDHidJoystick allJoysticks] retain];
[joystick.list makeObjectsPerformSelector: @selector(setDelegate:)
withObject: self];
if([joystick.list count] > 0){
joystick.current = [joystick.list objectAtIndex: 0];
[joystick.current startListening];
NSArray * buttons = [joystick.current buttonElements];
for(DDHidElement *e in buttons){
NSLog(@"%@", [e usage]);
}
}
}
- (void)windowWillClose:(NSNotification *)notification
{
[self closePort];
[NSApp terminate:self];
}
- (void) setPort:(AMSerialPort *)newPort
{
id old = nil;
if(newPort != port)
{
old = port;
port = [newPort retain];
[old release];
}
}
- (AMSerialPort *)port
{
return port;
}
- (void) openPort
{
[self initPortFor:[portListPopUpButton titleOfSelectedItem]];
if([port isOpen]) return;
if([port bsdPath] == nil){
[self log:@"[ERROR] Empty bsdPath"];
[port close];
return;
}
[self log:@"[INFO] Attempting to open port"];
if([port open])
{
[self log:[@"[INFO] Port opened for device " stringByAppendingString:[port bsdPath]]];
[port readDataInBackground];
}
else
{
[self log:[@"[ERROR] Couldn`t open port for devie " stringByAppendingString:[port bsdPath]]];
[self setPort:nil];
}
}
- (void) closePort
{
[port stopReadInBackground];
[port clearError];
[port close];
}
- (void) initPortFor:(NSString *)portPath
{
if(![portPath isEqualToString:[port bsdPath]])
{
[port close];
[self setPort:[[[AMSerialPort alloc] init:portPath
withName:portPath
type:(NSString*)CFSTR(kIOSerialBSDModemType)] autorelease]];
[port setDelegate:self];
[port setSpeed:B9600];
}
}
- (void) log:(NSString *) text
{
[outputTextView insertText:[text stringByAppendingString:@"\n"]];
}
- (void) debuglog:(NSString *) text
{
[debugTextView insertText:[text stringByAppendingString:@"\n"]];
}
- (void) updatePortList
{
[portListPopUpButton removeAllItems];
NSEnumerator *enumerator = [AMSerialPortList portEnumerator];
AMSerialPort *aPort;
while(aPort = [enumerator nextObject])
{
[portListPopUpButton addItemWithTitle:[aPort bsdPath]];
}
[self log:@"[INFO] Port list updated"];
}
// port stuff
- (void)serialPortReadData:(NSDictionary *)dataDictionary
{
AMSerialPort *sendPort = [dataDictionary objectForKey:@"serialPort"];
NSData *data = [dataDictionary objectForKey:@"data"];
if([data length] > 0)
{
NSString *text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
for(NSString *c in [text componentsSeparatedByString:@"\n"]){
[self parseInput:c];
}
[text release];
[sendPort readDataInBackground];
}
else
{
[self closePort];
[self log:@"[INFO] Port closed"];
}
}
- (void) parseInput:(NSString *)input
{
if([input isEqualToString:@""]) return;
if([[input substringWithRange:NSMakeRange(0,1)] isEqualToString:@"^"]){ // debug /^../
[self debuglog:input];
return;
}
[self log:[@"[INPUT] " stringByAppendingString:input]];
NSArray *chunks = [input componentsSeparatedByString: @":"];
if([chunks count] < (GROUND_COUNT + DISTANCE_COUNT + ENGINE_COUNT + 1)) return;
int value;
// ground sensors
for(int i=0 ; i<GROUND_COUNT; i++) {
if ([[chunks objectAtIndex:i] intValue] == 1) [groundSensors[i].displayCheckBox setState:NSOnState];
else [groundSensors[i].displayCheckBox setState:NSOffState];
}
// distance sensors
DistanceHistoryView *view;
for(int i=0; i<DISTANCE_COUNT; i++){
value = [[chunks objectAtIndex:(i+GROUND_COUNT)] intValue];
[distanceSensors[i].label setIntValue:value];
[distanceSensors[i].levelIndicator setIntValue:(value*50/1023)];
view = distanceSensors[i].historyView;
[view addValue:value];
}
// engines
if(engineMode == kNormal){
for(int i=0; i<ENGINE_COUNT; i++){
value = [[chunks objectAtIndex:(i+GROUND_COUNT+DISTANCE_COUNT)] intValue];
[engines[i].label setIntValue:value];
[engines[i].slider setIntValue:value];
}
}
// other
//chunks[i]
}
- (void)didAddPorts:(NSNotification *)theNotification
{
[self log:@"[INFO] New port found"];
[self updatePortList];
NSString *defaultPort = [preferences objectForKey:DefaultPortPath];
if([[portListPopUpButton itemTitles] containsObject:defaultPort]){
[self log:[@"[INFO] Selecting default port:" stringByAppendingString:defaultPort]];
[portListPopUpButton selectItemWithTitle:defaultPort];
}
}
- (void)didRemovePorts:(NSNotification *)theNotification
{
[self log:@"[INFO] Port removed"];
[self updatePortList];
}
- (void) send:(NSString *)msg
{
[self log:[@"[OUTPUT] " stringByAppendingString:msg]];
NSError *error;
if(port && [port isOpen]) [port writeString:msg usingEncoding:NSASCIIStringEncoding error:&error];
else [self log:@"[ERROR] Port not opened"];
}
// actions
-(IBAction) startStopReading:(id)sender
{
if([[sender title] isEqualToString:@"Connect"])
{
[self openPort];
[sender setTitle:@"Disconnect"];
}
else
{
[self closePort];
[sender setTitle:@"Connect"];
}
}
-(IBAction) selectPort:(id)sender
{
[preferences setObject:[sender titleOfSelectedItem] forKey:DefaultPortPath];
}
-(IBAction) setNormalEngineMode:(id)sender
{
engineMode = kNormal;
for(int i=0; i<ENGINE_COUNT; i++) [engines[i].slider setEnabled:NO];
[self send:@"M0\n"];
}
-(IBAction) setGUIEngineMode:(id)sender
{
engineMode = kGUI;
for(int i=0; i<ENGINE_COUNT; i++) [engines[i].slider setEnabled:YES];
[self send:@"M1\n"];
}
-(IBAction) setJoystickEngineMode:(id)sender
{
engineMode = kJoystick;
for(int i=0; i<ENGINE_COUNT; i++) [engines[i].slider setEnabled:NO];
[self send:@"M1\n"];
}
-(IBAction) sendStart:(id)sender { [self send:@"!"]; }
-(IBAction) sendReset:(id)sender { [self send:@"*"]; }
-(IBAction) sendNewline:(id)sender { [self send:@"\n"]; }
-(IBAction) sendCustom:(id)sender { [self send:[customSendTextField stringValue]]; }
-(IBAction) clearLog:(id)sender {
NSFont *font = [outputTextView font];
[outputTextView setString:@""];
[outputTextView setTextColor: [NSColor whiteColor]];
[outputTextView setFont:font];
[debugTextView setString:@""];
[debugTextView setTextColor: [NSColor whiteColor]];
[debugTextView setFont:font];
}
-(IBAction) activateGroundSensor:(id)sender
{
for(int i=0 ; i<GROUND_COUNT; i++) {
if([groundSensors[i].activeCheckBox isEqual:sender]){
if([sender state] == NSOnState) [self send:[NSString stringWithFormat:@"G%d1\n", i]];
else [self send:[NSString stringWithFormat:@"G%d0\n", i]];
break;
}
}
}
-(IBAction) activateDistanceSensor:(id)sender
{
for(int i=0 ; i<DISTANCE_COUNT; i++) {
if([distanceSensors[i].activeCheckBox isEqual:sender]){
if([sender state] == NSOnState) [self send:[NSString stringWithFormat:@"D%d1\n", i]];
else [self send:[NSString stringWithFormat:@"D%d0\n", i]];
break;
}
}
}
-(IBAction) setEnginePower:(id)sender
{
if(engineMode == kNormal) return;
for(int i=0 ; i<ENGINE_COUNT; i++) {
if([engines[i].slider isEqual:sender]){
[self send:[NSString stringWithFormat:@"E%d%04d\n", i, [sender intValue]]];
[engines[i].label setIntValue:[sender intValue]];
break;
}
}
}
-(void)setEnginePower:(int)engine_id withValue:(int)value
{
if(engineMode == kNormal) return;
[self send:[NSString stringWithFormat:@"E%d%04d\n", engine_id, value]];
[engines[engine_id].label setIntValue:value];
[engines[engine_id].slider setIntValue:value];
}
-(void) changeEnginePower:(int)engine_id withValue:(int)value
{
if(engineMode == kNormal) return;
value += [engines[engine_id].label intValue];
if(value > 100) value = 100;
else if(value < -100) value = -100;
[self send:[NSString stringWithFormat:@"E%d%04d\n", engine_id, value]];
[engines[engine_id].label setIntValue:value];
[engines[engine_id].slider setIntValue:value];
}
- (void) setEnginePowerFromJoystick
{
if(engineMode != kJoystick) return;
int e0,e1;
if(joystick.yValue >= 0){
if(joystick.xValue >= 0){
e0 = joystick.yValue;
e1 = joystick.yValue - joystick.xValue;
} else {
e0 = joystick.yValue + joystick.xValue;
e1 = joystick.yValue;
}
} else {
if(joystick.xValue >= 0){
e0 = joystick.yValue;
e1 = joystick.yValue + joystick.xValue;
} else {
e0 = joystick.yValue - joystick.xValue;
e1 = joystick.yValue;
}
}
[self setEnginePower: 0 withValue: e0];
[self setEnginePower: 1 withValue: e1];
}
- (void) ddhidJoystick:(DDHidJoystick *)theJoystick stick:(unsigned)stick xChanged:(int)value;
{
joystick.xValue = value * 100 / 65536;
[joystickView setX: joystick.xValue];
[self setEnginePowerFromJoystick];
}
- (void) ddhidJoystick:(DDHidJoystick *)theJoystick stick:(unsigned)stick yChanged:(int)value;
{
joystick.yValue = -value * 100 / 65536;
[joystickView setY: joystick.yValue];
[self setEnginePowerFromJoystick];
}
- (void) ddhidJoystick:(DDHidJoystick *)theJoystick stick:(unsigned)stick otherAxis:(unsigned)otherAxis valueChanged:(int)value;
{
joystick.zValue = value * 100 / 65536;
}
- (void) ddhidJoystick:(DDHidJoystick *)theJoystick buttonDown:(unsigned)buttonNumber;
{
NSLog(@"Button %d down", buttonNumber);
}
- (void) ddhidJoystick:(DDHidJoystick *)theJoystick buttonUp:(unsigned)buttonNumber;
{
NSLog(@"Button %d up", buttonNumber);
}
@end