-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightDeckController.m
215 lines (160 loc) · 9.27 KB
/
LightDeckController.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
//
// LightDeckController.m
// ArduinoSerial
//
// Created by Florian Maurer on 2/26/12.
// Copyright (c) 2012 2215 22nd St. All rights reserved.
//
#import "LightDeckController.h"
#import "DMXChannel.h"
#import "AMSerialPortList.h"
#import "AMSerialPortAdditions.h"
@interface LightDeckController()
//- (void)initPort;
@end
@implementation LightDeckController
@synthesize activeChannels;
@synthesize dmxchannels = _dmxchannels;
@synthesize port = _port;
@synthesize deviceName;
- (id) init {
self.deviceName = @"";
if (self = [super init]) {
// Initialize dmx channel values
self.dmxchannels = [[[DMXChannels alloc] init] autorelease];
//listen for change to lights from httpServer
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(lightsChanged:) name:@"PostReceived" object:nil];
//listen for change to different device selection
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(selectedDevice:) name:@"DeviceSelected" object:nil];
//[self startServer];
/// set up notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didAddPorts:) name:AMSerialPortListDidAddPortsNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRemovePorts:) name:AMSerialPortListDidRemovePortsNotification object:nil];
/// initialize port list to arm notifications
[AMSerialPortList sharedPortList];
if(self.port) {
[self initPort];
}
if([self.port isOpen]) {
//[port writeString:[self.dmxchannels generateSerialString] usingEncoding:NSASCIIStringEncoding error:NULL];
//NSLog(@"%@", [self.dmxchannels generateSerialString]);
//NSMutableData *serialData = [self.dmxchannels generateSerialData];
//[port writeData:[NSMutableData dataWithBytes:& serialData length:sizeof(serialData)] error:NULL];
//u_char test[] = {0x07, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff, 0xde, 0xad, 0xbe, 0xef};
NSError *writeError;
//[port writeData:[NSMutableData dataWithBytes:test length:12] error:&writeError];
//[port writeData:[NSMutableData dataWithData:serialData] error:&writeError];
/*if (writeError) {
NSLog(@"Write error: %@",writeError);
}*/
//NSLog(@"%@", [[NSString alloc] initWithBytes:&serialData length:sizeof(serialData) encoding:NSASCIIStringEncoding]);
}
return self;
}
return self;
}
- (void)awakeFromNib {
}
//event handler when event occurs
-(void)lightsChanged: (NSNotification *) notification
{
[self setLights:notification.userInfo];
}
- (void)selectedDevice: (NSNotification *) notification{
self.deviceName = [notification.userInfo objectForKey:@"dev"];
NSLog(@"%@",self.deviceName);
}
-(void) setLights:(NSDictionary*)parameters {
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
for (NSNumber *lightNumber in [parameters objectForKey:@"lights"]) {
for( NSString *aKey in parameters )
{
if ([aKey isEqualToString:@"brightness"]){
NSNumber *tempBrightness = [f numberFromString:[parameters objectForKey:aKey]];
/* The following line is a hack to make it work: it assumes all the lights have identical channels to multiply by 7
The correct solution would be another function setValueOf:dmxChannel forLight:identifier which uses the below method
(lightNum -1 ) * 7 + channel
*/
[self.dmxchannels asdf];
[self.dmxchannels setChannel:[NSNumber numberWithInt:[lightNumber intValue]*7] toValue:tempBrightness];
//NSLog(@"%@",tempBrightness);
}
if ([aKey isEqualToString:@"color"]){
NSString *tempColor = [parameters objectForKey:aKey];
if ([tempColor isEqualToString:@"red"]) {
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+1] toValue:[NSNumber numberWithInt:1]];
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+2] toValue:[NSNumber numberWithInt:0]];
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+3] toValue:[NSNumber numberWithInt:0]];
}
if ([tempColor isEqualToString:@"purple"]) {
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+1] toValue:[NSNumber numberWithInt:1]];
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+2] toValue:[NSNumber numberWithInt:0]];
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+3] toValue:[NSNumber numberWithInt:1]];
}
if ([tempColor isEqualToString:@"blue"]) {
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+1] toValue:[NSNumber numberWithInt:0]];
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+2] toValue:[NSNumber numberWithInt:0]];
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+3] toValue:[NSNumber numberWithInt:1]];
}
if ([tempColor isEqualToString:@"teal"]) {
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+1] toValue:[NSNumber numberWithInt:0]];
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+2] toValue:[NSNumber numberWithInt:1]];
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+3] toValue:[NSNumber numberWithInt:1]];
}
if ([tempColor isEqualToString:@"green"]) {
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+1] toValue:[NSNumber numberWithInt:0]];
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+2] toValue:[NSNumber numberWithInt:1]];
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+3] toValue:[NSNumber numberWithInt:0]];
}
if ([tempColor isEqualToString:@"white"]) {
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+1] toValue:[NSNumber numberWithInt:1]];
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+2] toValue:[NSNumber numberWithInt:1]];
[self.dmxchannels setChannel:[NSNumber numberWithInt:([lightNumber intValue]-1)*7+3] toValue:[NSNumber numberWithInt:1]];
}
//NSLog(@"%@",tempBrightness);
}
}
}
[self sendDMXSerialString];
/*if (writeError) {
NSLog(@"Write error: %@",writeError);
}
NSLog(@"%@", [[NSString alloc] initWithBytes:&serialData length:sizeof(serialData) encoding:NSASCIIStringEncoding]);
*/
[f release];
}
-(void) sendDMXSerialString {
NSMutableData *serialData = [self.dmxchannels generateSerialData];
NSError *writeError;
[self.port writeData:[NSMutableData dataWithData:serialData] error:&writeError];
}
# pragma mark Serial Port Stuff
- (void)initPort
{
NSString *currentDevName = self.deviceName;
if (![currentDevName isEqualToString:[self.port bsdPath]]) {
[self.port close];
[self setPort:[[[AMSerialPort alloc] init:currentDevName withName:currentDevName type:(NSString*)CFSTR(kIOSerialBSDModemType)] autorelease]];
[self.port setDelegate:self];
if ([self.port open]) {
//Then I suppose we connected!
NSLog(@"successfully connected");
//[connectButton setEnabled:NO];
//[sendButton setEnabled:YES];
//[serialScreenMessage setStringValue:@"Connection Successful!"];
//TODO: Set appropriate baud rate here.
//The standard speeds defined in termios.h are listed near
//the top of AMSerialPort.h. Those can be preceeded with a 'B' as below. However, I've had success
//with non standard rates (such as the one for the MIDI protocol). Just omit the 'B' for those.
[self.port setSpeed:B115200];
// listen for data in a separate thread
[self.port readDataInBackground];
} else { // an error occured while creating port
NSLog(@"error connecting");
//[serialScreenMessage setStringValue:@"Error Trying to Connect..."];
[self setPort:nil];
}
}
}
@end