Skip to content

Commit

Permalink
Add Neewer BH-30S RGB light support (#57)
Browse files Browse the repository at this point in the history
* Add Neewer BH-30S RGB light support

* fix a rgb command issue.

* update

* add a debug method
  • Loading branch information
keefo authored Apr 29, 2024
1 parent bce35b2 commit 049a95e
Show file tree
Hide file tree
Showing 13 changed files with 442 additions and 128 deletions.
46 changes: 34 additions & 12 deletions NeewerLite/NeewerLite/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,30 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
}
let sat = cmdParameter.saturation()
let brr = cmdParameter.brightness()
func act(_ viewObj: DeviceViewObject) {
if viewObj.isON && viewObj.device.supportRGB {
viewObj.changeToHSIMode()
viewObj.updateHSI(hue: hueVal, sat: sat, brr: brr)
func act(_ viewObj: DeviceViewObject, showAlert: Bool) {
if viewObj.isON {
if viewObj.device.supportRGB {
viewObj.changeToHSIMode()
viewObj.updateHSI(hue: hueVal, sat: sat, brr: brr)
} else {
if showAlert {
let alert = NSAlert()
alert.messageText = "This light does not support RGB"
alert.informativeText = "\(viewObj.device.nickName)"
alert.alertStyle = .informational
alert.addButton(withTitle: "OK")
alert.runModal()
}
}
}
}

if let lightname = cmdParameter.lightName() {
self.viewObjects.forEach {
if lightname.caseInsensitiveCompare($0.device.userLightName.value) == .orderedSame { act($0) }
if lightname.caseInsensitiveCompare($0.device.userLightName.value) == .orderedSame { act($0, showAlert: true) }
}
} else {
self.viewObjects.forEach { act($0) }
self.viewObjects.forEach { act($0, showAlert: false) }
}
self.statusItemIcon = .on
}))
Expand All @@ -321,19 +332,30 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
}
let brr = cmdParameter.brightness()

func act(_ viewObj: DeviceViewObject) {
if viewObj.isON && viewObj.device.supportRGB {
viewObj.changeToSCEMode()
viewObj.changeToSCE(sceneId!, brr)
func act(_ viewObj: DeviceViewObject, showAlert: Bool) {
if viewObj.isON {
if viewObj.device.supportRGB {
viewObj.changeToSCEMode()
viewObj.changeToSCE(sceneId!, brr)
} else {
if showAlert {
let alert = NSAlert()
alert.messageText = "This light does not support RGB"
alert.informativeText = "\(viewObj.device.nickName)"
alert.alertStyle = .informational
alert.addButton(withTitle: "OK")
alert.runModal()
}
}
}
}

if let lightname = cmdParameter.lightName() {
self.viewObjects.forEach {
if lightname.caseInsensitiveCompare($0.device.userLightName.value) == .orderedSame { act($0) }
if lightname.caseInsensitiveCompare($0.device.userLightName.value) == .orderedSame { act($0, showAlert: true) }
}
} else {
self.viewObjects.forEach { act($0) }
self.viewObjects.forEach { act($0, showAlert: false) }
}
self.statusItemIcon = .on
}))
Expand Down
30 changes: 17 additions & 13 deletions NeewerLite/NeewerLite/Common/ColorUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,28 @@ extension NSColor {
alpha: 1.0
)
}

convenience init(hex: String, alpha: Float) {
// Handle two types of literals: 0x and # prefixed
var cleanedString = ""
if hex.hasPrefix("0x") {
cleanedString = String(hex[hex.index(cleanedString.startIndex, offsetBy: 2)..<hex.endIndex])
} else if hex.hasPrefix("#") {
cleanedString = String(hex[hex.index(cleanedString.startIndex, offsetBy: 1)..<hex.endIndex])
} else if hex.count == 6 {
cleanedString = hex
var cleanedString = hex.hasPrefix("0x") ? String(hex.dropFirst(2)) : hex
cleanedString = hex.hasPrefix("#") ? String(hex.dropFirst(1)) : cleanedString

// Normalize short hex code to full 6 characters
if cleanedString.count == 3 {
cleanedString = cleanedString.map { String(repeating: $0, count: 2) }.joined()
}
// Ensure it only contains valid hex characters 0
let validHexPattern = "[a-fA-F0-9]+"
if cleanedString.conformsTo(validHexPattern) {

if cleanedString.range(of: "^[a-fA-F0-9]{6}$", options: .regularExpression) != nil {
var rgbValue: UInt64 = 0
Scanner(string: cleanedString).scanHexInt64(&rgbValue)
self.init(hex: rgbValue, alpha: 1)
self.init(hex: rgbValue, alpha: alpha)
} else {
fatalError("Unable to parse color?")
let alert = NSAlert()
alert.messageText = "Error"
alert.informativeText = "Invalid hex color format \(hex)"
alert.alertStyle = .warning
alert.addButton(withTitle: "OK")
alert.runModal()
self.init(hex: 0, alpha: alpha)
}
}
}
10 changes: 6 additions & 4 deletions NeewerLite/NeewerLite/ContentManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ContentManager {
// Image Cache Directory
private lazy var cacheDirectory: URL = {
let appSupportURL = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
let cacheURL = appSupportURL.appendingPathComponent("LightImageCache")
let cacheURL = appSupportURL.appendingPathComponent("NeewerLite/LightImageCache")
if !fileManager.fileExists(atPath: cacheURL.path) {
try? fileManager.createDirectory(at: cacheURL, withIntermediateDirectories: true, attributes: nil)
}
Expand Down Expand Up @@ -195,9 +195,11 @@ class ContentManager {
}
}
}
let lights = databaseCache?.lights ?? []
if let found = lights.first(where: { $0.type == lightType }) {
return found.image
if let safeCache = databaseCache {
let lights = safeCache.lights
if let found = lights.first(where: { $0.type == lightType }) {
return found.image
}
}
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion NeewerLite/NeewerLite/Model/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ struct CommandParameter {
func saturation() -> Double {
if let val = components.queryItems?.first(where: { $0.name == "Saturation" })?.value {
if let sat = Double(val) {
if sat > 1.0 {
return sat / 100.0
}
return sat
} else {
return 1.0
Expand Down Expand Up @@ -185,4 +188,3 @@ public enum TabId: String {
case source = "sourceTab"
case scene = "sceTab"
}

Loading

0 comments on commit 049a95e

Please sign in to comment.