Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
805 - 1
// ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (C) 2010, Frank Blumenberg
3
//
4
// See License.txt for complete licensing and attribution information.
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
//
12
// The above copyright notice and this permission notice shall be included in all
13
// copies or substantial portions of the Software.
14
//
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
// THE SOFTWARE.
22
//
23
// ///////////////////////////////////////////////////////////////////////////////
24
 
25
#import "AnalogValues.h"
26
#import "TTGlobalCorePaths.h"
27
#import "TTCorePreprocessorMacros.h"
28
 
29
#import "MKConnectionController.h"
30
#import "NSData+MKCommandEncode.h"
31
#import "MKDatatypes.h"
32
#import "MKDataConstants.h"
33
 
34
 
35
#define kAnalogLabelFile @"analoglables.plist"
36
 
37
@interface AnalogValues (Private)
38
- (void) analogLabelNotification:(NSNotification *)aNotification;
39
- (void) debugValueNotification:(NSNotification *)aNotification;
40
- (void) requestAnalogLabelForIndex:(NSInteger)index;
41
- (void) requestDebugData;
42
 
43
- (void) loadLabels;
44
- (void) saveLabels;
45
- (void) initNotifications;
46
 
47
@end
48
 
49
@implementation AnalogValues
50
 
51
@synthesize delegate = _delegate;
52
 
53
- (id) init
54
{
55
  self = [super init];
56
  if (self != nil) {
57
 
58
    debugData = [[NSMutableArray alloc] initWithCapacity:kMaxDebugData];
59
    for (int i = 0; i < kMaxDebugData; i++) {
60
      [debugData addObject:[NSNumber numberWithInt:0]];
61
    }
62
 
63
    [self loadLabels];
64
    [self initNotifications];
65
  }
66
 
67
  return self;
68
}  
69
 
70
- (void) dealloc {
71
  NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
72
  [nc removeObserver:self];
73
 
74
  [analogLabels release];
75
  [debugData release];
76
  [super dealloc];
77
}
78
 
79
-(void) loadLabels {
80
 
81
  NSString *filePath = TTPathForDocumentsResource(kAnalogLabelFile);
82
 
83
  if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
84
 
85
    DLog(@"Load the analog labels from %@",filePath);
86
 
87
    analogLabels = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
88
  }
89
  else {
90
 
91
    analogLabels = [[NSMutableArray alloc] initWithCapacity:4];
92
    for(int d=0;d<4;d++) {
93
 
94
      NSMutableArray* analogLabelsDevice = [NSMutableArray arrayWithCapacity:kMaxDebugData];
95
      for (int i = 0; i < kMaxDebugData; i++) {
96
        [analogLabelsDevice addObject:[NSString stringWithFormat:@"Analog%d", i]];
97
      }
98
      [analogLabels addObject:analogLabelsDevice];
99
    }
100
 
101
    [self saveLabels];
102
  }  
103
}
104
 
105
-(void) saveLabels {
106
 
107
  NSString *filePath = TTPathForDocumentsResource(kAnalogLabelFile);
108
  [analogLabels writeToFile:filePath atomically:NO];
109
}
110
 
111
-(void) initNotifications {
112
 
113
  NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
114
 
115
  [nc addObserver:self
116
         selector:@selector(analogLabelNotification:)
117
             name:MKDebugLabelNotification
118
           object:nil];
119
 
120
  [nc addObserver:self
121
         selector:@selector(debugValueNotification:)
122
             name:MKDebugDataNotification
123
           object:nil];
124
 
125
}
126
 
127
//////////////////////////////////////////////////////////////////////////////////////////////
128
#pragma mark -
129
 
130
- (void) requestAnalogLabelForIndex:(NSInteger)theIndex {
131
  MKConnectionController * cCtrl = [MKConnectionController sharedMKConnectionController];
132
  uint8_t index = theIndex;
133
 
134
  NSData * data = [NSData dataWithCommand:MKCommandDebugLabelRequest
135
                               forAddress:MKAddressAll
136
                         payloadWithBytes:&index
137
                                   length:1];
138
 
139
  [cCtrl sendRequest:data];
140
}
141
 
142
- (void) analogLabelNotification:(NSNotification *)aNotification {
143
 
144
  NSString * label = [[aNotification userInfo] objectForKey:kMKDataKeyLabel];
145
  NSInteger index = [[[aNotification userInfo] objectForKey:kMKDataKeyIndex] intValue];
146
  NSUInteger address=[MKConnectionController sharedMKConnectionController].currentDevice;
147
 
148
  if ([label length] > 0) {
149
    //    label=[NSString stringWithFormat:@"%@ %d",label,[label length]];
150
    NSMutableArray* a=[analogLabels objectAtIndex:address];
151
    [a replaceObjectAtIndex:index withObject:label];
152
  }
153
 
154
  DLog(@"([%d][%d] %@",address , index, label);
155
 
156
  if (index < (kMaxDebugData-1)) {
157
    [self requestAnalogLabelForIndex:index+1];
158
  }
159
  else {
160
    [self saveLabels];
161
  }
162
 
163
  [self.delegate didReceiveLabelForIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
164
}
165
 
166
//////////////////////////////////////////////////////////////////////////////////////////////
167
#pragma mark -
168
 
169
- (void) requestDebugData {
170
  MKConnectionController * cCtrl = [MKConnectionController sharedMKConnectionController];
171
  uint8_t interval = 50;
172
 
173
  NSData * data = [NSData dataWithCommand:MKCommandDebugValueRequest
174
                               forAddress:MKAddressAll
175
                         payloadWithBytes:&interval
176
                                   length:1];
177
 
178
  [cCtrl sendRequest:data];
179
}
180
 
181
- (void) debugValueNotification:(NSNotification *)aNotification {
182
  NSArray * newDebugData = [[aNotification userInfo] objectForKey:kMKDataKeyDebugData];
183
 
184
  for (int i = 0; i < [newDebugData count]; i++) {
185
    [debugData replaceObjectAtIndex:i withObject:[newDebugData objectAtIndex:i]];
186
  }
187
 
188
  if (debugResponseCounter++ > 4 ) {
189
    [self requestDebugData];
190
    debugResponseCounter = 0;
191
  }
192
 
193
  [self.delegate didReceiveValues];
194
}
195
 
196
//////////////////////////////////////////////////////////////////////////////////////////////
197
#pragma mark -
198
 
199
-(void) reloadAll {
200
 
201
  [self performSelector:@selector(requestDebugData) withObject:self afterDelay:0.1];
202
 
203
  [self requestAnalogLabelForIndex:0];
204
}
205
 
206
-(NSUInteger) count {
207
  return [debugData count];
208
}
209
 
210
-(NSString*) labelAtIndexPath:(NSIndexPath *)indexPath {
211
  NSUInteger address=[MKConnectionController sharedMKConnectionController].currentDevice;
212
  NSMutableArray* a=[analogLabels objectAtIndex:address];
213
 
214
  return [a objectAtIndex:indexPath.row];
215
}
216
 
217
-(NSString*) valueAtIndexPath:(NSIndexPath *)indexPath {
218
  return [[debugData objectAtIndex:indexPath.row] description];
219
}
220
 
221
@end