Skip to content

Commit 049a95e

Browse files
authored
Add Neewer BH-30S RGB light support (#57)
* Add Neewer BH-30S RGB light support * fix a rgb command issue. * update * add a debug method
1 parent bce35b2 commit 049a95e

File tree

13 files changed

+442
-128
lines changed

13 files changed

+442
-128
lines changed

NeewerLite/NeewerLite/AppDelegate.swift

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -297,19 +297,30 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
297297
}
298298
let sat = cmdParameter.saturation()
299299
let brr = cmdParameter.brightness()
300-
func act(_ viewObj: DeviceViewObject) {
301-
if viewObj.isON && viewObj.device.supportRGB {
302-
viewObj.changeToHSIMode()
303-
viewObj.updateHSI(hue: hueVal, sat: sat, brr: brr)
300+
func act(_ viewObj: DeviceViewObject, showAlert: Bool) {
301+
if viewObj.isON {
302+
if viewObj.device.supportRGB {
303+
viewObj.changeToHSIMode()
304+
viewObj.updateHSI(hue: hueVal, sat: sat, brr: brr)
305+
} else {
306+
if showAlert {
307+
let alert = NSAlert()
308+
alert.messageText = "This light does not support RGB"
309+
alert.informativeText = "\(viewObj.device.nickName)"
310+
alert.alertStyle = .informational
311+
alert.addButton(withTitle: "OK")
312+
alert.runModal()
313+
}
314+
}
304315
}
305316
}
306317

307318
if let lightname = cmdParameter.lightName() {
308319
self.viewObjects.forEach {
309-
if lightname.caseInsensitiveCompare($0.device.userLightName.value) == .orderedSame { act($0) }
320+
if lightname.caseInsensitiveCompare($0.device.userLightName.value) == .orderedSame { act($0, showAlert: true) }
310321
}
311322
} else {
312-
self.viewObjects.forEach { act($0) }
323+
self.viewObjects.forEach { act($0, showAlert: false) }
313324
}
314325
self.statusItemIcon = .on
315326
}))
@@ -321,19 +332,30 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
321332
}
322333
let brr = cmdParameter.brightness()
323334

324-
func act(_ viewObj: DeviceViewObject) {
325-
if viewObj.isON && viewObj.device.supportRGB {
326-
viewObj.changeToSCEMode()
327-
viewObj.changeToSCE(sceneId!, brr)
335+
func act(_ viewObj: DeviceViewObject, showAlert: Bool) {
336+
if viewObj.isON {
337+
if viewObj.device.supportRGB {
338+
viewObj.changeToSCEMode()
339+
viewObj.changeToSCE(sceneId!, brr)
340+
} else {
341+
if showAlert {
342+
let alert = NSAlert()
343+
alert.messageText = "This light does not support RGB"
344+
alert.informativeText = "\(viewObj.device.nickName)"
345+
alert.alertStyle = .informational
346+
alert.addButton(withTitle: "OK")
347+
alert.runModal()
348+
}
349+
}
328350
}
329351
}
330352

331353
if let lightname = cmdParameter.lightName() {
332354
self.viewObjects.forEach {
333-
if lightname.caseInsensitiveCompare($0.device.userLightName.value) == .orderedSame { act($0) }
355+
if lightname.caseInsensitiveCompare($0.device.userLightName.value) == .orderedSame { act($0, showAlert: true) }
334356
}
335357
} else {
336-
self.viewObjects.forEach { act($0) }
358+
self.viewObjects.forEach { act($0, showAlert: false) }
337359
}
338360
self.statusItemIcon = .on
339361
}))

NeewerLite/NeewerLite/Common/ColorUtils.swift

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,28 @@ extension NSColor {
116116
alpha: 1.0
117117
)
118118
}
119+
119120
convenience init(hex: String, alpha: Float) {
120-
// Handle two types of literals: 0x and # prefixed
121-
var cleanedString = ""
122-
if hex.hasPrefix("0x") {
123-
cleanedString = String(hex[hex.index(cleanedString.startIndex, offsetBy: 2)..<hex.endIndex])
124-
} else if hex.hasPrefix("#") {
125-
cleanedString = String(hex[hex.index(cleanedString.startIndex, offsetBy: 1)..<hex.endIndex])
126-
} else if hex.count == 6 {
127-
cleanedString = hex
121+
var cleanedString = hex.hasPrefix("0x") ? String(hex.dropFirst(2)) : hex
122+
cleanedString = hex.hasPrefix("#") ? String(hex.dropFirst(1)) : cleanedString
123+
124+
// Normalize short hex code to full 6 characters
125+
if cleanedString.count == 3 {
126+
cleanedString = cleanedString.map { String(repeating: $0, count: 2) }.joined()
128127
}
129-
// Ensure it only contains valid hex characters 0
130-
let validHexPattern = "[a-fA-F0-9]+"
131-
if cleanedString.conformsTo(validHexPattern) {
128+
129+
if cleanedString.range(of: "^[a-fA-F0-9]{6}$", options: .regularExpression) != nil {
132130
var rgbValue: UInt64 = 0
133131
Scanner(string: cleanedString).scanHexInt64(&rgbValue)
134-
self.init(hex: rgbValue, alpha: 1)
132+
self.init(hex: rgbValue, alpha: alpha)
135133
} else {
136-
fatalError("Unable to parse color?")
134+
let alert = NSAlert()
135+
alert.messageText = "Error"
136+
alert.informativeText = "Invalid hex color format \(hex)"
137+
alert.alertStyle = .warning
138+
alert.addButton(withTitle: "OK")
139+
alert.runModal()
140+
self.init(hex: 0, alpha: alpha)
137141
}
138142
}
139143
}

NeewerLite/NeewerLite/ContentManager.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class ContentManager {
6262
// Image Cache Directory
6363
private lazy var cacheDirectory: URL = {
6464
let appSupportURL = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
65-
let cacheURL = appSupportURL.appendingPathComponent("LightImageCache")
65+
let cacheURL = appSupportURL.appendingPathComponent("NeewerLite/LightImageCache")
6666
if !fileManager.fileExists(atPath: cacheURL.path) {
6767
try? fileManager.createDirectory(at: cacheURL, withIntermediateDirectories: true, attributes: nil)
6868
}
@@ -195,9 +195,11 @@ class ContentManager {
195195
}
196196
}
197197
}
198-
let lights = databaseCache?.lights ?? []
199-
if let found = lights.first(where: { $0.type == lightType }) {
200-
return found.image
198+
if let safeCache = databaseCache {
199+
let lights = safeCache.lights
200+
if let found = lights.first(where: { $0.type == lightType }) {
201+
return found.image
202+
}
201203
}
202204
return nil
203205
}

NeewerLite/NeewerLite/Model/Command.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ struct CommandParameter {
8383
func saturation() -> Double {
8484
if let val = components.queryItems?.first(where: { $0.name == "Saturation" })?.value {
8585
if let sat = Double(val) {
86+
if sat > 1.0 {
87+
return sat / 100.0
88+
}
8689
return sat
8790
} else {
8891
return 1.0
@@ -185,4 +188,3 @@ public enum TabId: String {
185188
case source = "sourceTab"
186189
case scene = "sceTab"
187190
}
188-

0 commit comments

Comments
 (0)