Skip to content

Commit

Permalink
Incorporated custom Kelvin value
Browse files Browse the repository at this point in the history
From:
antoinelamy@098f2aa4c5358ce19dad
ce1f35b0b7af2632737f

It’s a shame our Forks have diverged as much as they have.
  • Loading branch information
kylehowells committed Jun 3, 2015
1 parent 08da387 commit 4f50055
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 14 deletions.
38 changes: 35 additions & 3 deletions HAPAccessoryKit Headers/HAKCharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,38 @@

@class HAKService, HAKUUID, HAKValueConstraints, NSArray, NSHashTable, NSNumber, NSString;

typedef NS_ENUM(NSUInteger, HAKCharacteristicFormat) {
HAKCharacteristicFormatNone,
HAKCharacteristicFormatBool,
HAKCharacteristicFormatInt,
HAKCharacteristicFormatUInt8,
HAKCharacteristicFormatUInt16,
HAKCharacteristicFormatUInt32,
HAKCharacteristicFormatUInt64,
HAKCharacteristicFormatFloat,
HAKCharacteristicFormatString,
HAKCharacteristicFormatDate,
HAKCharacteristicFormatTLV8,
HAKCharacteristicFormatData,
HAKCharacteristicFormatArray,
HAKCharacteristicFormatDictionary
};

typedef NS_ENUM(NSUInteger, HAKCharacteristicUnit) {
HAKCharacteristicUnitNone,
HAKCharacteristicUnitCelcius,
HAKCharacteristicUnitPercentage,
HAKCharacteristicUnitArcDegrees
};

typedef NS_OPTIONS(NSUInteger, HAKCharacteristicPermission) {
HAKCharacteristicPermissionReadable = 1 << 0,
HAKCharacteristicPermissionWritable = 1 << 1,
HAKCharacteristicPermissionUpdatable = 1 << 2,
};



@interface HAKCharacteristic : NSObject <NSCoding, NSCopying>
{
id _value;
Expand All @@ -31,9 +63,9 @@
@property(retain, nonatomic) NSHashTable *subscribedConnectionsTable; // @synthesize subscribedConnectionsTable=_subscribedConnectionsTable;
@property(retain, nonatomic) dispatch_queue_t workQueue; // @synthesize workQueue=_workQueue;
@property(copy, nonatomic) HAKValueConstraints *constraints; // @synthesize constraints=_constraints;
@property(nonatomic) unsigned long long unit; // @synthesize unit=_unit;
@property(readonly, nonatomic) unsigned long long format; // @synthesize format=_format;
@property(nonatomic) unsigned long long permissions; // @synthesize permissions=_permissions;
@property(nonatomic) HAKCharacteristicUnit unit; // @synthesize unit=_unit;
@property(readonly, nonatomic) HAKCharacteristicFormat format; // @synthesize format=_format;
@property(nonatomic) HAKCharacteristicPermission permissions; // @synthesize permissions=_permissions;
@property(readonly, nonatomic) unsigned long long properties; // @synthesize properties=_properties;
@property(copy, nonatomic) NSString *manufacturerDescription; // @synthesize manufacturerDescription=_manufacturerDescription;
@property(copy, nonatomic) NSNumber *instanceID; // @synthesize instanceID=_instanceID;
Expand Down
56 changes: 45 additions & 11 deletions HomeKitBridge/LIFX/HKBLightAccessoryLIFX.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
// Copyright (c) 2014 Kyle Howells. All rights reserved.
//

#import "HAKNumberConstraints.h"
#import "HKBLightAccessoryLIFX.h"

#import <LIFXKit/LIFXKit.h>
#import "HKBLightAccessory+Subclass.h"

@interface HKBLightAccessoryLIFX () <LFXLightObserver>
@property (nonatomic, strong) LFXLight *lifxBulb;

@property (nonatomic, strong) HAKCharacteristic *kelvinCharacteristic;
@end


Expand Down Expand Up @@ -51,14 +54,26 @@ -(instancetype)initWithLightBulb:(LFXLight*)lightBulb{


// TODO: work out how to define custom characteristics and add the kelvin value of LIFX bulbs
//-(void)setupServices{
// [super setupServices];
//
// HAKIntegerCharacteristic *kelvin = [[HAKIntegerCharacteristic alloc] init];
// kelvin.minimumValue = [NSNumber numberWithInt:LFXHSBKColorMinKelvin];
// kelvin.maximumValue = [NSNumber numberWithInt:LFXHSBKColorMaxKelvin];
// [self.lightBulbService addCharacteristic:kelvin];
//}
-(void)setupServices{
[super setupServices];

_kelvinCharacteristic = [[HAKCharacteristic alloc] initWithType:[HAKUUID UUIDWithUUIDString:@"0836D660-26E5-4D17-A714-A15DB6EAC9A5"] properties:11 format:HAKCharacteristicFormatUInt16];
HAKNumberConstraints *kelvinConstraints = [[HAKNumberConstraints alloc] initWithMinimumValue:@(LFXHSBKColorMinKelvin) maximumValue:@(LFXHSBKColorMaxKelvin)];
_kelvinCharacteristic.constraints = kelvinConstraints;

[self.lightBulbService addCharacteristic:_kelvinCharacteristic];
}

-(void)characteristicDidUpdateValue:(HAKCharacteristic*)characteristic
{
[super characteristicDidUpdateValue:characteristic];

if (characteristic == self.kelvinCharacteristic) {
[self updateExternalKelvin:[characteristic.value unsignedIntValue]];
}

//if (characteristic.service == self.lightBulbService) {}
}



Expand All @@ -82,6 +97,10 @@ -(void)light:(LFXLight *)light didChangePowerState:(LFXPowerState)powerState{

#pragma mark - Update HomeKit to LIFX values

-(void)updateHKPowerState{
[self updateHomeKitPowerState:(self.lifxBulb.powerState == LFXPowerStateOn)];
}

-(void)updateHKColorValues{
// - Conversion: LIFX HomeKit
// hue; // [0, 360] - [0, 360]
Expand All @@ -91,11 +110,17 @@ -(void)updateHKColorValues{
[self updateHomeKitBrightness:(self.lifxBulb.color.brightness * 100)];
[self updateHomeKitSaturation:(self.lifxBulb.color.saturation * 100)];
[self updateHomeKitHue:self.lifxBulb.color.hue];
}
-(void)updateHKPowerState{
[self updateHomeKitPowerState:(self.lifxBulb.powerState == LFXPowerStateOn)];

[self updateHomeKitKelvin:self.lifxBulb.color.kelvin];
}

-(void)updateHomeKitKelvin:(NSUInteger)kelvin{
HAKCharacteristic *characteristic = self.kelvinCharacteristic;

if ([characteristic.constraints validateValue:@(kelvin)]) {
characteristic.value = @(kelvin);
}
}



Expand Down Expand Up @@ -138,6 +163,15 @@ -(void)updateExternalHue:(NSInteger)hue{
[lfxLight setColor:newColor];
}

-(void)updateExternalKelvin:(uint16_t)kelvin{
LFXLight *lfxLight = [self lifxBulb];

LFXHSBKColor *currentColor = [lfxLight color];
LFXHSBKColor *newColor = [LFXHSBKColor colorWithHue:currentColor.hue saturation:currentColor.saturation brightness:currentColor.brightness kelvin:kelvin];

[lfxLight setColor:newColor];
}




Expand Down

3 comments on commit 4f50055

@antoinelamy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it would make sense to merge all this together at some point...

@kylehowells
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, looking at your commit history. You picked it up while I wasn't paying much attention to it. Then I got around to updating my fork, then noticed you had actually been working on your fork.

Btw: Good work, I like particularly liked the discovery service class idea.

@kylehowells
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this might be of interest to you: https://gist.github.com/freerunnering/7851161d83a33e03107d

Please sign in to comment.