-
Notifications
You must be signed in to change notification settings - Fork 0
/
DMXChannels.m
77 lines (57 loc) · 2.39 KB
/
DMXChannels.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
//
// DMXChannels.m
// ArduinoSerial
//
// Created by Florian Maurer on 2/10/12.
// Copyright (c) 2012 2215 22nd St. All rights reserved.
//
#import "DMXChannels.h"
#import "DMXChannel.h"
@implementation DMXChannels
@synthesize channelValues;
@synthesize numberOf;
- (id) init {
if (self = [super init]) {
channelValues = [NSMutableDictionary new];
numberOf = 0;
// Path to the plist (in the application bundle)
NSString *path = [[NSBundle mainBundle] pathForResource:
@"defaultDMXValues" ofType:@"plist"];
// Build the array from the plist
NSMutableDictionary *defaultDmx = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
// Create DMXChannel objects from plist entries and store them into channelValues
for (NSDictionary *tempChannel in [defaultDmx objectForKey:@"channels"]){
DMXChannel* newChannel = [[DMXChannel alloc] init];
newChannel.channel = [tempChannel objectForKey:@"channel"];
newChannel.value = [tempChannel objectForKey:@"value"];
newChannel.description = [tempChannel objectForKey:@"description"];
[channelValues setObject:newChannel forKey:newChannel.channel];
numberOf++;
//NSLog(@"--%@", [tempChannel objectForKey:@"description"]);
}
}
return self;
}
- (void) dealloc
{
[channelValues release];
[super dealloc];
}
- (NSMutableData *) generateSerialData {
//Initialize empty char[] that will hold the serial message
NSMutableData * serialBytes = [[NSMutableData alloc] init];
//First character is the number of channels about to be sent
u_char numChannels = (u_char)self.numberOf;
[serialBytes appendBytes:&numChannels length:sizeof(numChannels)];
for (int i=1;i<=self.numberOf;i++){
//get i'th channel from dictionary of channelValues
DMXChannel* tempChannel = [self.channelValues objectForKey:[NSNumber numberWithInt:i]];
u_char channelValue = (u_char)[tempChannel.value intValue];
[serialBytes appendBytes:&channelValue length:sizeof(channelValue)];
}
//framing bytes
const u_char framingbytes[4] = "\xde\xad\xbe\xef";
[serialBytes appendData:[NSData dataWithBytes:framingbytes length:sizeof(framingbytes)]];
return serialBytes;
}
@end