Skip to content

Commit

Permalink
Added brightnessTimer again
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMisfit68 committed Dec 18, 2023
1 parent 8e4ba84 commit 0815014
Showing 1 changed file with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
//
// DimmerTimer.swift
// BrightnessTimer.swift
// HAPiNest
//
// Created by Jan Verrept on 12/12/2023.
// Copyright © 2023 Jan Verrept. All rights reserved.
//

import Foundation
import JVCocoa
import SoftPLC

protocol Dimmer:AnyObject{
var brightness: Float { get set }
protocol DimmedLight:AnyObject{

var brightness: Int? { get set }
var brightnessTimer:BrightnessTimer!{get set}

}

// Foundation.Timer is a class cluster and it's not meant to be subclassed directly, so
// use a helper class a.k.a. composition instead of inheritance.
class DimmerTimer {
weak var dimmer: Dimmer?
private var timer: Timer?
class BrightnessTimer {

weak var dimmer: DimmedLight?
// Public property for time to make a 100% change
var timeForFullChange: TimeInterval = 10.0 // Default to 10 seconds for a full range change

var targetBrightness: Float? {
public var timeFor100percentChange: TimeInterval = 5.0 // Default to 5 seconds for a full range change
private var timer: Timer?

var targetBrightness: Int? {
didSet {
if targetBrightness == nil {
stop()
Expand All @@ -32,31 +36,37 @@ class DimmerTimer {
}
}

var brightness: Float {
get { return dimmer?.brightness ?? 0.0 }
var brightness: Int {
get { return dimmer?.brightness ?? 0}
set { dimmer?.brightness = newValue }
}

init(dimmer: Dimmer) {
init(dimmer: DimmedLight) {
self.dimmer = dimmer
}

private func adjustBrightness() {
// Interval for 1% change
let intervalForOnePercentChange = timeForFullChange / 100
let intervalForOnePercentChange = timeFor100percentChange / 100

timer?.invalidate() // Invalidate any existing timer
timer = Timer.scheduledTimer(withTimeInterval: intervalForOnePercentChange, repeats: true) { [weak self] _ in
guard let self = self, let targetBrightness = self.targetBrightness else { return }

#warning("DEBUGPRINT") // TODO: - remove temp print statement
print("🐞\tI'm dimming")

// Apply a small 1% adjustment at the time in either direction
let onePercentAdjustment:Float = (targetBrightness >= self.brightness) ? 0.01 : -0.01
if (self.brightness+onePercentAdjustment) >= targetBrightness || (self.brightness+onePercentAdjustment) <= targetBrightness{
self.brightness = targetBrightness
self.stop()
} else {
self.brightness += onePercentAdjustment
}
let onePercentAdjustment:Int = (targetBrightness >= self.brightness) ? 1 : -1

// Apply the adjustment only if it does not overshoot the target brightness
if (onePercentAdjustment == 1) && (self.brightness+onePercentAdjustment >= targetBrightness) ||
(onePercentAdjustment == -1) && (self.brightness+onePercentAdjustment <= targetBrightness) {
self.brightness = targetBrightness
self.stop()
} else {
self.brightness += onePercentAdjustment
}

}
}
Expand Down

0 comments on commit 0815014

Please sign in to comment.