Skip to content

Commit

Permalink
Created function key as a a PLC-class en made it uniform with the oth…
Browse files Browse the repository at this point in the history
…er PLC-classes
  • Loading branch information
TheMisfit68 committed Dec 18, 2023
1 parent 2a1bc75 commit cefe47a
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import JVCocoa
// MARK: - PLC level class
class CirtcuitEnabler:PLCClassAccessoryDelegate, PulsOperatedCircuit, Simulateable{

// Accessory binding
// MARK: - Accessory binding
typealias AccessorySubclass = Accessory.Lightbulb
var characteristicChanged:Bool = false

Expand Down Expand Up @@ -49,7 +49,7 @@ class CirtcuitEnabler:PLCClassAccessoryDelegate, PulsOperatedCircuit, Simulateab
outputSignal.logicalValue = puls
}

// MARK: - Processing
// MARK: - PLC Processing
public func runCycle(){

reevaluate(&powerState, characteristic:accessory.lightbulb.powerState, hardwareFeedback: hardwarePowerState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ import IOTypes
import JVCocoa

// MARK: - PLC level class
class DimmableLight:PLCClassAccessoryDelegate{
// Accessory binding
class DimmableLight:PLCClassAccessoryDelegate, DimmedLight{
// MARK: - Accessory binding
typealias AccessorySubclass = Accessory.Lightbulb
var characteristicChanged: Bool = false

var brightnessTimer: BrightnessTimer!

override init(){
super.init()
self.brightnessTimer = BrightnessTimer(dimmer: self)
}

// MARK: - State
var powerState:Bool? = nil
Expand All @@ -41,14 +48,13 @@ class DimmableLight:PLCClassAccessoryDelegate{
var brightness:Int? = nil{
didSet{
if let brightness = brightness, brightness != oldValue{

if brightness > switchOffLevelDimmer{
previousOnLevel = brightness
}else{
powerState?.reset()
}


}
}
}
Expand Down Expand Up @@ -77,10 +83,11 @@ class DimmableLight:PLCClassAccessoryDelegate{
outputSignal.scaledValue = Float(powerState == true ? (brightness ?? switchOffLevelDimmer+1) : 0)
}

// MARK: - Processing
// MARK: - PLC Processing
public func runCycle() {

reevaluate(&powerState, initialValue: (hardwareBrightness ?? 0 > switchOffLevelDimmer), characteristic:accessory.lightbulb.powerState, hardwareFeedback: nil)

reevaluate(&brightness, characteristic:accessory.lightbulb.brightness, hardwareFeedback:hardwareBrightness)

characteristicChanged.reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import JVCocoa
// MARK: - PLC level class
class Doorlock:PLCClassAccessoryDelegate, PulsOperatedCircuit{

// Accessory binding
// MARK: - Accessory binding
typealias AccessorySubclass = Accessory.LockMechanism
var characteristicChanged:Bool = false

Expand All @@ -43,9 +43,7 @@ class Doorlock:PLCClassAccessoryDelegate, PulsOperatedCircuit{
}

public func assignOutputParameters(){

outputSignal.logicalValue = puls

}

// MARK: - PLC Processing
Expand Down
108 changes: 45 additions & 63 deletions Accessory Delegates/ PLCClassAccessoryDelegates/FunctionKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,79 +16,61 @@ import IOTypes
import JVCocoa


/// A PLC-Class type object that is not an Accessory-Delegate
/// because it has no Accessory associated with it,
/// it only processes hardware-signals
class FunctionKey:PLCClassAccessoryDelegate{

// MARK: - Accessory binding
typealias AccessorySubclass = Accessory.StatelessProgrammableSwitch
var characteristicChanged: Bool = false
var hardwareFeedbackChanged: Bool = false


private var inputPuls:Bool = false
private var inputTriggered:EBool

var clicked:Bool = false{
didSet{
if clicked{
#warning("DEBUGPRINT") // TODO: - remove temp print statement
print("🐞\tClicked")
}
clicked.reset()
}
// MARK: - State
private enum SwitchEvent:UInt8{
case singlePress = 0
case doublePress = 1
case longPress = 2
}

let doubleclikInterval:TimeInterval
let doubleClickTimer:DigitalTimer
var doubleClicked:Bool = false{
private var clicksCounter:Int = 0
private var switchEvent:SwitchEvent? = nil{
didSet{
if doubleClicked{
#warning("DEBUGPRINT") // TODO: - remove temp print statement
print("🐞\tDoubleClicked")
}
doubleClicked.reset()
clicksCounter = 0
longPressTimer.reset()
doubleClickTimer.reset()
}
}

let longPressTime:TimeInterval
let longPressTimer:DigitalTimer
var longPressed:Bool = false{
didSet{
if longPressed{
#warning("DEBUGPRINT") // TODO: - remove temp print statement
print("🐞\tPressedlong")
}
longPressed.reset()
}
}

private var inputPuls:Bool = false
private var inputTriggered:EBool

private let doubleclikInterval:TimeInterval = 10.0
private let doubleClickTimer:DigitalTimer

private let longPressTime:TimeInterval = 20.0
private let longPressTimer:DigitalTimer

override init(){
// Hardware feedback state
// Function keys only have inputs, no controlable outputs and therefore also no associated hardwarefeedback
var hardwareFeedbackChanged:Bool = false

override init(){

self.inputTriggered = EBool(&inputPuls)

self.doubleclikInterval = 1.0
inputTriggered = EBool(&inputPuls)
self.doubleClickTimer = DigitalTimer.OffDelay(time: doubleclikInterval)

self.longPressTime = 2.0
self.longPressTimer = DigitalTimer.OnDelay(time: longPressTime)

super.init()
}

// MARK: - IO-Signal assignment
var inputSignal:DigitalInputSignal{
let ioSymbol:SoftPLC.IOSymbol = .functionKey(circuit:String(localized: "\(instanceName)", table:"AccessoryNames"))
let ioSymbol:SoftPLC.IOSymbol = .functionKey(circuit:String(localized: "\(instanceName)"))
return plc.signal(ioSymbol:ioSymbol) as! DigitalInputSignal
}

// MARK: - PLC Parameter assignment
public func assignInputParameters(){

if let hardwarePuls = inputSignal.logicalValue{
inputPuls = hardwarePuls
}else{
inputPuls = false
}

inputPuls = inputSignal.logicalValue ?? false
}

public func assignOutputParameters(){
Expand All @@ -98,24 +80,24 @@ class FunctionKey:PLCClassAccessoryDelegate{
// MARK: - PLC Processing
public func runCycle() {

let risingEdge = inputTriggered.🔼
doubleClickTimer.input = inputPuls
longPressTimer.input = inputPuls

if risingEdge && !doubleClickTimer.output{
clicked.set()
}else if risingEdge && doubleClickTimer.output{
doubleClicked.set()
}else if longPressTimer.output{
longPressed.set()
if longPressTimer.output{
switchEvent = .longPress
}else if (clicksCounter >= 2) {
switchEvent = .doublePress
}else if (clicksCounter == 1) && !doubleClickTimer.output{
switchEvent = .singlePress
}

// reevaluate(&clicked, characteristic:accessory.programmableSwitchEvent, hardwareFeedback: hardwarePowerState)
// reevaluate(&doubleClicked, characteristic:accessory.outlet.powerState, hardwareFeedback: hardwarePowerState)



doubleClickTimer.input = inputPuls
longPressTimer.input = inputPuls
if inputTriggered.🔼 {
clicksCounter += 1
}else if !doubleClickTimer.output{
clicksCounter = 0
}

reevaluate(&switchEvent, characteristic:accessory.primaryService.programmableSwitchEvent, hardwareFeedback: nil)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import JVCocoa
// MARK: - PLC level class
class GarageDoor:PLCClassAccessoryDelegate, PulsOperatedCircuit{

// Accessory binding
// MARK: - Accessory binding
typealias AccessorySubclass = Accessory.GarageDoorOpener.StatelessGarageDoorOpener
var characteristicChanged: Bool = false

Expand Down
4 changes: 2 additions & 2 deletions Accessory Delegates/ PLCClassAccessoryDelegates/Light.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import OSLog
// MARK: - PLC level class
class Light:PLCClassAccessoryDelegate, PulsOperatedCircuit, Simulateable{

// Accessory binding
// MARK: - Accessory binding
typealias AccessorySubclass = Accessory.Lightbulb
var characteristicChanged:Bool = false

Expand Down Expand Up @@ -50,7 +50,7 @@ class Light:PLCClassAccessoryDelegate, PulsOperatedCircuit, Simulateable{
outputSignal.logicalValue = puls
}

// MARK: - Processing
// MARK: - PLC Processing
public func runCycle(){

reevaluate(&powerState, characteristic:accessory.lightbulb.powerState, hardwareFeedback: hardwarePowerState)
Expand Down
4 changes: 2 additions & 2 deletions Accessory Delegates/ PLCClassAccessoryDelegates/Outlet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import JVCocoa
// MARK: - PLC level class
class Outlet:PLCClassAccessoryDelegate{

// Accessory binding
// MARK: - Accessory binding
typealias AccessorySubclass = Accessory.Outlet
var characteristicChanged: Bool = false

Expand Down Expand Up @@ -58,7 +58,7 @@ class Outlet:PLCClassAccessoryDelegate{
outputSignal.logicalValue = powerState ?? false
}

// MARK: - Processing
// MARK: - PLC Processing
public func runCycle() {

reevaluate(&powerState, characteristic:accessory.outlet.powerState, hardwareFeedback: hardwarePowerState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import OSLog
// MARK: - PLC level class
class SmartSprinkler:PLCClassAccessoryDelegate{

// Accessory binding
// MARK: - Accessory binding
typealias AccessorySubclass = Accessory.SmartSprinkler
var characteristicChanged:Bool = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import JVCocoa
// MARK: - PLC level class
class ToggleableOutlet:PLCClassAccessoryDelegate, PulsOperatedCircuit, Simulateable{

// Accessory binding
// MARK: - Accessory binding
typealias AccessorySubclass = Accessory.Outlet
var characteristicChanged:Bool = false

Expand Down Expand Up @@ -57,7 +57,7 @@ class ToggleableOutlet:PLCClassAccessoryDelegate, PulsOperatedCircuit, Simulatea
}


// MARK: - Processing
// MARK: - PLC Processing
public func runCycle() {

reevaluate(&powerState, characteristic:accessory.outlet.powerState, hardwareFeedback: hardwarePowerState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import JVCocoa
// MARK: - PLC level class
class WindowCovering:PLCClassAccessoryDelegate, PulsOperatedCircuit, Simulateable{

// Accessory binding
// MARK: - Accessory binding
typealias AccessorySubclass = Accessory.WindowCovering
var characteristicChanged:Bool = false

Expand Down
15 changes: 10 additions & 5 deletions Accessory Delegates/MilightAccessoryDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import OSLog
/// Handles characteristic changes for a Homekit Accessory.
/// It uses the MilightDriver to pass those changes to the hardware

class MilightAccessoryDelegate:MilightDriverV6, AccessoryDelegate {

class MilightAccessoryDelegate:MilightDriverV6, AccessoryDelegate, DimmedLight {

var brightnessTimer: BrightnessTimer!

let zone:MilightDriver.Zone
var name: String{
return zone.name
Expand All @@ -25,11 +27,12 @@ class MilightAccessoryDelegate:MilightDriverV6, AccessoryDelegate {
init(ipAddress: String, zone:MilightDriver.Zone){
self.zone = zone
super.init(ipAddress: ipAddress)
self.brightnessTimer = BrightnessTimer(dimmer: self)
}

var characteristicChanged: Bool = false

var brightness:Int = 100{
var brightness:Int? = 100{
didSet{
executeCommand(mode: .rgbwwcw, action: .brightNess, value: brightness, zone: zone)
}
Expand Down Expand Up @@ -66,7 +69,7 @@ class MilightAccessoryDelegate:MilightDriverV6, AccessoryDelegate {
}
}
}

func handleCharacteristicChange<T>(accessory:Accessory,
service: Service,
characteristic: GenericCharacteristic<T>,
Expand All @@ -82,7 +85,9 @@ class MilightAccessoryDelegate:MilightDriverV6, AccessoryDelegate {

case CharacteristicType.brightness:

brightness = characteristic.value as! Int
// brightness = characteristic.value as! Int
self.brightnessTimer.timeFor100percentChange = 1800
self.brightnessTimer.targetBrightness = (characteristic.value as! Int)

case CharacteristicType.hue:

Expand Down
4 changes: 4 additions & 0 deletions HAPiNest.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
DC9963892AD3073000915EAC /* CirtcuitEnabler.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC9963882AD3073000915EAC /* CirtcuitEnabler.swift */; };
DCA37CF926BB29EF003CC3BF /* HAPTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCA37CF826BB29EF003CC3BF /* HAPTests.swift */; };
DCA3DF312B28D97000554A8A /* BrightnessTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCA3DF302B28D97000554A8A /* BrightnessTimer.swift */; };
DCA626B12B3083C900BBDD91 /* ServiceNames.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = DCA626B02B3083C900BBDD91 /* ServiceNames.xcstrings */; };
DCAB996A2A816734005FCC29 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = DCAB99692A816734005FCC29 /* README.md */; };
DCBBC7082B2EE4D80005EE6E /* StatelessProgrammableSwitch.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCBBC7072B2EE4D80005EE6E /* StatelessProgrammableSwitch.swift */; };
DCBCFB5529678657000F0376 /* Enums.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCBCFB5429678657000F0376 /* Enums.swift */; };
Expand Down Expand Up @@ -115,6 +116,7 @@
DCA37CF626BB29EF003CC3BF /* HAPiNestTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = HAPiNestTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
DCA37CF826BB29EF003CC3BF /* HAPTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HAPTests.swift; sourceTree = "<group>"; };
DCA3DF302B28D97000554A8A /* BrightnessTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrightnessTimer.swift; sourceTree = "<group>"; };
DCA626B02B3083C900BBDD91 /* ServiceNames.xcstrings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json.xcstrings; path = ServiceNames.xcstrings; sourceTree = "<group>"; };
DCAB99692A816734005FCC29 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
DCBBC7072B2EE4D80005EE6E /* StatelessProgrammableSwitch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatelessProgrammableSwitch.swift; sourceTree = "<group>"; };
DCBCFB5429678657000F0376 /* Enums.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Enums.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -202,6 +204,7 @@
DC54B6C12570586E00D9169F /* Assets.xcassets */,
DC4D66792A956AC1004C9D30 /* Localizable.xcstrings */,
DC4D667B2A9571BA004C9D30 /* AccessoryNames.xcstrings */,
DCA626B02B3083C900BBDD91 /* ServiceNames.xcstrings */,
DCF7D5AB2AD0D106000010AA /* TVChannelNames.xcstrings */,
DC90DA6A2732828200068839 /* MainWindow */,
DC90DA5D2732817000068839 /* PreferencesWindow */,
Expand Down Expand Up @@ -468,6 +471,7 @@
DCAB996A2A816734005FCC29 /* README.md in Resources */,
DC54B6C22570586E00D9169F /* Assets.xcassets in Resources */,
DC4D667A2A956AC1004C9D30 /* Localizable.xcstrings in Resources */,
DCA626B12B3083C900BBDD91 /* ServiceNames.xcstrings in Resources */,
DC4D667C2A9571BA004C9D30 /* AccessoryNames.xcstrings in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Loading

0 comments on commit cefe47a

Please sign in to comment.