Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: Don't read LED state after write if not supported #90

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,671 changes: 832 additions & 839 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Example/Tests/NormalBehaviorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ class NormalBehaviorTest: XCTestCase {
// The `onLedStateDidChange` handler will be called after the initial state has been read.
let ledEnabled = XCTestExpectation(description: "LED Enabled")
let ledObserver = target!.onLedStateDidChange { isOn in
if isOn {
if isOn == true {
ledEnabled.fulfill()
} else {
} else if isOn == false {
// Simulate toggling the switch.
blinkyViewController.ledToggleSwitch.setOn(true, animated: true)
blinkyViewController.ledToggleSwitchDidChange(blinkyViewController.ledToggleSwitch)
Expand All @@ -123,7 +123,7 @@ class NormalBehaviorTest: XCTestCase {
let buttonPressed = XCTestExpectation(description: "Button pressed")
let buttonReleased = XCTestExpectation(description: "Button released")
let buttonObserver = target!.onButtonStateDidChange { isPressed in
if isPressed {
if isPressed == true {
// Simulate releasing the BTN1 on DK.
blinky.simulateValueUpdate(Data([0x00]), for: .buttonCharacteristic)
buttonPressed.fulfill()
Expand Down
4 changes: 2 additions & 2 deletions Example/Tests/ResetTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ class ResetTest: XCTestCase {
buttonPressed.isInverted = true

let ledObserver = target!.onLedStateDidChange { isOn in
if !isOn {
if isOn == false {
ledDisabled.fulfill()
}
}
let buttonObserver = target!.onButtonStateDidChange { isPressed in
if isPressed {
if isPressed == true {
buttonPressed.fulfill()
} else {
buttonReleased.fulfill()
Expand Down
6 changes: 4 additions & 2 deletions Example/nRFBlinky/Models/BlinkyPeripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class BlinkyPeripheral: NSObject, CBPeripheralDelegate {
basePeripheral.readValue(for: ledCharacteristic)
} else {
print("Can't read LED state")
post(.ledState(of: self, didChangeTo: false))
post(.ledState(of: self, didChangeTo: nil))
}
}
}
Expand Down Expand Up @@ -381,6 +381,8 @@ class BlinkyPeripheral: NSObject, CBPeripheralDelegate {
print("Writing value failed: \(error.localizedDescription)")
}
// LED value has been written, let's read it to confirm.
readLEDValue()
if characteristic.properties.contains(.read) {
readLEDValue()
}
}
}
20 changes: 10 additions & 10 deletions Example/nRFBlinky/Models/BlinkyPeripheralEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ extension Notification {
}

static func ledState(of blinkyPeripheral: BlinkyPeripheral,
didChangeTo isOn: Bool) -> Notification {
didChangeTo isOn: Bool?) -> Notification {
return Notification(name: .ledState, object: blinkyPeripheral,
userInfo: ["isOn": isOn])
userInfo: isOn.map { ["isOn": $0] } ?? nil )
}

static func buttonState(of blinkyPeripheral: BlinkyPeripheral,
didChangeTo isPressed: Bool) -> Notification {
didChangeTo isPressed: Bool?) -> Notification {
return Notification(name: .buttonState, object: blinkyPeripheral,
userInfo: ["isPressed": isPressed])
userInfo: isPressed.map { ["isPressed": $0] } ?? nil)
}

}
Expand Down Expand Up @@ -139,19 +139,19 @@ extension BlinkyPeripheral {
}
}

func onLedStateDidChange(do action: @escaping (Bool) -> ()) -> NSObjectProtocol {
func onLedStateDidChange(do action: @escaping (Bool?) -> ()) -> NSObjectProtocol {
return on(.ledState) { notification in
if let userInfo = notification.userInfo,
let isOn = userInfo["isOn"] as? Bool {
if let userInfo = notification.userInfo {
let isOn = userInfo["isOn"] as? Bool
action(isOn)
}
}
}

func onButtonStateDidChange(do action: @escaping (Bool) -> ()) -> NSObjectProtocol {
func onButtonStateDidChange(do action: @escaping (Bool?) -> ()) -> NSObjectProtocol {
return on(.buttonState) { notification in
if let userInfo = notification.userInfo,
let isPressed = userInfo["isPressed"] as? Bool {
if let userInfo = notification.userInfo {
let isPressed = userInfo["isPressed"] as? Bool
action(isPressed)
}
}
Expand Down
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/de.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "AUS";
"PRESSED" = "GEDRÜCKT";
"RELEASED" = "LOSGELASSEN";
"UNKNOWN" = "UNBEKANNT";
"Reading..." = "Lese...";
"Unknown Device" = "Unbekanntes Gerät";
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "OFF";
"PRESSED" = "PRESSED";
"RELEASED" = "RELEASED";
"UNKNOWN" = "UNKNOWN";
"Reading..." = "Reading...";
"Unknown Device" = "Unknown Device";
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/es.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "APAGADO";
"PRESSED" = "PULSADO";
"RELEASED" = "LIBRE";
"UNKNOWN" = "DESCONOCIDO";
"Reading..." = "Leyendo...";
"Unknown Device" = "Dispositivo Desconocido";
2 changes: 1 addition & 1 deletion Example/nRFBlinky/UI/es.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/* Class = "UILabel"; text = "2. Make sure the coin cell battery has power."; ObjectID = "rmw-BF-cDo"; */
"rmw-BF-cDo.text" = "2. Asegúrate de que la pila de botón da corriente.";

/* Class = "UILabel"; text = "OFF"; ObjectID = "sXe-8K-21Q"; */
/* Class = "UILabel"; text = "UNKNOWN"; ObjectID = "sXe-8K-21Q"; */
"sXe-8K-21Q.text" = "DESCONOCIDO";

/* Class = "UILabel"; text = "<Peripheral Name>"; ObjectID = "ufl-Iw-mhn"; */
Expand Down
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/fi.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "SAMMUTA";
"PRESSED" = "PAINETTU";
"RELEASED" = "VAPAUTETTU";
"UNKNOWN" = "TUNTEMATON";
"Reading..." = "Lukee...";
"Unknown Device" = "Tuntematon laite";
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/fr.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "OFF";
"PRESSED" = "APPUYÉ";
"RELEASED" = "RELACHÉ";
"UNKNOWN" = "INCONNU";
"Reading..." = "Lecture en cours...";
"Unknown Device" = "Appareil Inconnu";
2 changes: 1 addition & 1 deletion Example/nRFBlinky/UI/fr.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/* Class = "UILabel"; text = "2. Make sure the coin cell battery has power."; ObjectID = "rmw-BF-cDo"; */
"rmw-BF-cDo.text" = "2. Assurez-vous que la pile bouton n'est pas déchargée.";

/* Class = "UILabel"; text = "OFF"; ObjectID = "sXe-8K-21Q"; */
/* Class = "UILabel"; text = "UNKNOWN"; ObjectID = "sXe-8K-21Q"; */
"sXe-8K-21Q.text" = "INCONNU";

/* Class = "UILabel"; text = "Nordic Blinky"; ObjectID = "ufl-Iw-mhn"; */
Expand Down
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/it.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "SPENTO";
"PRESSED" = "PREMUTO";
"RELEASED" = "ALZATO";
"UNKNOWN" = "SCONOSCIUTO";
"Reading..." = "Lettura...";
"Unknown Device" = "Dispositivo sconosciuto";
2 changes: 1 addition & 1 deletion Example/nRFBlinky/UI/it.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/* Class = "UILabel"; text = "2. Make sure the coin cell battery has power."; ObjectID = "rmw-BF-cDo"; */
"rmw-BF-cDo.text" = "2. Assicurarsi che la batteria sia carica.";

/* Class = "UILabel"; text = "OFF"; ObjectID = "sXe-8K-21Q"; */
/* Class = "UILabel"; text = "UNKNOWN"; ObjectID = "sXe-8K-21Q"; */
"sXe-8K-21Q.text" = "SCONOSCIUTO";

/* Class = "UILabel"; text = "Nordic Blinky"; ObjectID = "ufl-Iw-mhn"; */
Expand Down
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/ko.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "꺼짐";
"PRESSED" = "활성화됨";
"RELEASED" = "비활성화됨";
"UNKNOWN" = "알 수 없음";
"Reading..." = "읽는 중...";
"Unknown Device" = "알 수 없는 기기";
2 changes: 1 addition & 1 deletion Example/nRFBlinky/UI/ko.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/* Class = "UILabel"; text = "2. Make sure the coin cell battery has power."; ObjectID = "rmw-BF-cDo"; */
"rmw-BF-cDo.text" = "2. Coin cell의 배터리가 충분한지 확인하세요.";

/* Class = "UILabel"; text = "OFF"; ObjectID = "sXe-8K-21Q"; */
/* Class = "UILabel"; text = "UNKNOWN"; ObjectID = "sXe-8K-21Q"; */
"sXe-8K-21Q.text" = "알 수 없음";

/* Class = "UILabel"; text = "Nordic Blinky"; ObjectID = "ufl-Iw-mhn"; */
Expand Down
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/mr.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "बंद";
"PRESSED" = "दाबले";
"RELEASED" = "सोडले";
"UNKNOWN" = "अज्ञात";
"Reading..." = "वाचले जात आहे...";
"Unknown Device" = "अज्ञात उपकरण";
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/nb.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "AV";
"PRESSED" = "NEDTRYKKET";
"RELEASED" = "INAKTIV";
"UNKNOWN" = "UKJENT";
"Reading..." = "Leser...";
"Unknown Device" = "Ukjent Enhet";
2 changes: 1 addition & 1 deletion Example/nRFBlinky/UI/nb.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/* Class = "UILabel"; text = "2. Make sure the coin cell battery has power."; ObjectID = "rmw-BF-cDo"; */
"rmw-BF-cDo.text" = "2. Sørg for at det er strøm i batteriet.";

/* Class = "UILabel"; text = "OFF"; ObjectID = "sXe-8K-21Q"; */
/* Class = "UILabel"; text = "UNKNOWN"; ObjectID = "sXe-8K-21Q"; */
"sXe-8K-21Q.text" = "UKJENT";

/* Class = "UILabel"; text = "Nordic Blinky"; ObjectID = "ufl-Iw-mhn"; */
Expand Down
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/pl.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"OFF" = "WYŁĄCZONE";
"PRESSED" = "WCIŚNIĘTY";
"RELEASED" = "ZWOLNIONY";
"UNKNOWN" = "NIEZNANY";
"Reading..." = "Pobieranie...";
"Unknown Device" = "Nieznane urządzenie";

2 changes: 1 addition & 1 deletion Example/nRFBlinky/UI/pl.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/* Class = "UILabel"; text = "2. Make sure the coin cell battery has power."; ObjectID = "rmw-BF-cDo"; */
"rmw-BF-cDo.text" = "2. Upewnij się, że bateria nie jest wyczerpana.";

/* Class = "UILabel"; text = "OFF"; ObjectID = "sXe-8K-21Q"; */
/* Class = "UILabel"; text = "UNKNOWN"; ObjectID = "sXe-8K-21Q"; */
"sXe-8K-21Q.text" = "NIEZNANY";

/* Class = "UILabel"; text = "Nordic Blinky"; ObjectID = "ufl-Iw-mhn"; */
Expand Down
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/pt-BR.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "DESLIGADO";
"PRESSED" = "PRESSIONADO";
"RELEASED" = "LIBERADO";
"UNKNOWN" = "DESCONHECIDO";
"Reading..." = "Lendo...";
"Unknown Device" = "Dispositivo desconhecido";
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/ro.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "OPRIT";
"PRESSED" = "APASAT";
"RELEASED" = "NEAPĂSAT";
"UNKNOWN" = "NECUNOSCUT";
"Reading..." = "Se actualizează starea...";
"Unknown Device" = "Dispozitiv necunoscut";
4 changes: 2 additions & 2 deletions Example/nRFBlinky/UI/ro.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
/* Class = "UILabel"; text = "2. Make sure the coin cell battery has power."; ObjectID = "rmw-BF-cDo"; */
"rmw-BF-cDo.text" = "2. Asigura-te că bateria de tip moneda e încărcată.";

/* Class = "UILabel"; text = "OFF"; ObjectID = "sXe-8K-21Q"; */
"sXe-8K-21Q.text" = "OPRIT";
/* Class = "UILabel"; text = "UNKNOWN"; ObjectID = "sXe-8K-21Q"; */
"sXe-8K-21Q.text" = "NECUNOSCUT";

/* Class = "UILabel"; text = "Nordic Blinky"; ObjectID = "ufl-Iw-mhn"; */
"ufl-Iw-mhn.text" = "Nordic Blinky";
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/ru.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "ВЫКЛЮЧЕНО";
"PRESSED" = "НАЖАТА";
"RELEASED" = "ОТПУЩЕНА";
"UNKNOWN" = "НЕИЗВЕСТНО";
"Reading..." = "Чтение...";
"Unknown Device" = "Неизвестное устройство";
2 changes: 1 addition & 1 deletion Example/nRFBlinky/UI/ru.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/* Class = "UILabel"; text = "2. Make sure the coin cell battery has power."; ObjectID = "rmw-BF-cDo"; */
"rmw-BF-cDo.text" = "2. Убедитесь, что таблетка (миниатюрный элемент питания) имеет заряд.";

/* Class = "UILabel"; text = "OFF"; ObjectID = "sXe-8K-21Q"; */
/* Class = "UILabel"; text = "UNKNOWN"; ObjectID = "sXe-8K-21Q"; */
"sXe-8K-21Q.text" = "НЕИЗВЕСТНО";

/* Class = "UILabel"; text = "Nordic Blinky"; ObjectID = "ufl-Iw-mhn"; */
Expand Down
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/uk.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "ВИМКНЕНО";
"PRESSED" = "НАТИСНУТО";
"RELEASED" = "ВIДПУЩЕНО";
"UNKNOWN" = "НЕВІДОМО";
"Reading..." = "Читання...";
"Unknown Device" = "Невідомий пристрій";
2 changes: 1 addition & 1 deletion Example/nRFBlinky/UI/uk.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/* Class = "UILabel"; text = "2. Make sure the coin cell battery has power."; ObjectID = "rmw-BF-cDo"; */
"rmw-BF-cDo.text" = "2. Переконайтеся, що батарейка (мініатюрний елемент живлення) має заряд.";

/* Class = "UILabel"; text = "OFF"; ObjectID = "sXe-8K-21Q"; */
/* Class = "UILabel"; text = "UNKNOWN"; ObjectID = "sXe-8K-21Q"; */
"sXe-8K-21Q.text" = "НЕВІДОМО";

/* Class = "UILabel"; text = "Nordic Blinky"; ObjectID = "ufl-Iw-mhn"; */
Expand Down
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/vi.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "TẮT";
"PRESSED" = "ĐÃ ẤN";
"RELEASED" = "ĐÃ NHẢ";
"UNKNOWN" = "KHÔNG XÁC ĐỊNH";
"Reading..." = "Đang đọc...";
"Unknown Device" = "Thiết bị chưa nhận dạng";
1 change: 1 addition & 0 deletions Example/nRFBlinky/UI/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"OFF" = "关";
"PRESSED" = "按下";
"RELEASED" = "释放";
"UNKNOWN" = "未知状态";
"Reading..." = "读取...";
"Unknown Device" = "未知设备";
Original file line number Diff line number Diff line change
Expand Up @@ -164,24 +164,31 @@ private extension BlinkyViewController {
}
}

func ledStateChanged(isOn: Bool) {
func ledStateChanged(isOn: Bool?) {
DispatchQueue.main.async {
if isOn {
switch isOn {
case true:
self.ledStateLabel.text = "ON".localized
self.ledToggleSwitch.setOn(true, animated: true)
} else {
case false:
self.ledStateLabel.text = "OFF".localized
self.ledToggleSwitch.setOn(false, animated: true)
default:
self.ledStateLabel.text = "UNKNOWN".localized
self.ledToggleSwitch.setOn(false, animated: true)
}
}
}

func buttonStateChanged(isPressed: Bool) {
func buttonStateChanged(isPressed: Bool?) {
DispatchQueue.main.async {
if isPressed {
switch isPressed {
case true:
self.buttonStateLabel.text = "PRESSED".localized
} else {
case false:
self.buttonStateLabel.text = "RELEASED".localized
default:
self.buttonStateLabel.text = "UNKNOWN".localized
}
self.buttonTapHapticFeedback()
}
Expand Down