-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitFrameDecoder.mm
61 lines (51 loc) · 1.43 KB
/
BitFrameDecoder.mm
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
// -*- Mode: ObjC -*-
//
// Copyright (C) 2011, Brad Howes. All rights reserved.
//
#import "BitFrameDecoder.h"
@implementation BitFrameDecoder
@synthesize buttonState, frequency, observer;
+ (NSInteger)integerFromBits:(NSString*)bits
{
//
// Given bits are in least-significant first ordering. Ignore the first and last ones since they represent the
// start and stop bits.
//
NSInteger value = 0;
for (NSInteger index = [bits length] - 2; index >= 1; --index) {
value <<= 1;
if ([bits characterAtIndex:index] == 49)
value |= 1;
}
return value;
}
+ (id)create
{
return [[[BitFrameDecoder alloc] init] autorelease];
}
- (id)init
{
if (self = [super init]) {
buttonState = 0;
frequency = 0;
observer = nil;
}
return self;
}
- (void)frameContentBitStream:(NSString*)bits
{
//
// First 10 bits make up the button state
//
buttonState = [BitFrameDecoder integerFromBits:[bits substringWithRange:NSMakeRange(0, 10)]];
//
// The RPM value is encoded in two bytes, MSB first.
//
NSInteger msb = [BitFrameDecoder integerFromBits:[bits substringWithRange:NSMakeRange(10, 10)]];
NSInteger lsb = [BitFrameDecoder integerFromBits:[bits substringWithRange:NSMakeRange(20, 10)]];
frequency = msb * 256 + lsb;
if (observer != nil) {
[observer frameButtonState:buttonState frequency:frequency];
}
}
@end