Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
larsblumberg committed May 12, 2016
1 parent 8b8215e commit 689b15e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion SDK/BLEDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class BLEDevice: NSObject, CBPeripheralDelegate {
guard peripheral.state == .Disconnected else { return false }
centralManager.connectPeripheral(peripheral, options: nil)
connectionTimeoutTimer?.invalidate()
connectionTimeoutTimer = NSTimer.scheduledTimerWithTimeInterval(connectionTimeoutInterval, target: self, selector: "didConnectTimeout", userInfo: nil, repeats: false)
connectionTimeoutTimer = NSTimer.scheduledTimerWithTimeInterval(connectionTimeoutInterval, target: self, selector: #selector(self.didConnectTimeout), userInfo: nil, repeats: false)
return true
}

Expand Down
2 changes: 1 addition & 1 deletion SDK/BLEDiscoveryManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private class UnreachableDevicesDetector {
previouslyDiscoveredDevices = Set()
currentlyDiscoveredDevices = Set()
unreachableDevicesDetectionTimer?.invalidate()
unreachableDevicesDetectionTimer = NSTimer.scheduledTimerWithTimeInterval(minDetectionInterval + 0.5, target: self, selector: "removeUnreachableDevices", userInfo: nil, repeats: true)
unreachableDevicesDetectionTimer = NSTimer.scheduledTimerWithTimeInterval(minDetectionInterval + 0.5, target: self, selector: #selector(self.removeUnreachableDevices), userInfo: nil, repeats: true)
}

func stop() {
Expand Down
9 changes: 5 additions & 4 deletions SDK/NuimoBluetoothController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private class LEDMatrixWriter {
// When the matrix write response is not retrieved within 500ms we assume the response to have timed out
dispatch_async(dispatch_get_main_queue()) {
self.writeMatrixResponseTimeoutTimer?.invalidate()
self.writeMatrixResponseTimeoutTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "didRetrieveMatrixWriteResponse", userInfo: nil, repeats: false)
self.writeMatrixResponseTimeoutTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: #selector(self.didRetrieveMatrixWriteResponse), userInfo: nil, repeats: false)
}
}

Expand Down Expand Up @@ -298,11 +298,11 @@ private extension NuimoGestureEvent {

private extension NuimoLEDMatrix {
var matrixBytes: [UInt8] {
return bits
return leds
.chunk(8)
.map{ $0
.enumerate()
.map{(i: Int, b: Bit) -> Int in return b == Bit.Zero ? 0 : 1 << i}
.map{(i: Int, b: Bool) -> Int in return b ? 1 << i : 0}
.reduce(UInt8(0), combine: {(s: UInt8, v: Int) -> UInt8 in s + UInt8(v)})
}
}
Expand All @@ -317,7 +317,8 @@ private extension SequenceType {
var i = n
self.forEach {
chunk.append($0)
if --i == 0 {
i -= 1
if i == 0 {
chunks.append(chunk)
chunk.removeAll(keepCapacity: true)
i = n
Expand Down
10 changes: 5 additions & 5 deletions SDK/NuimoLEDMatrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ public let NuimoLEDMatrixDefaultLEDOffCharacter = NuimoLEDMatrixLEDOffCharacters
public let NuimoLEDMatrixDefaultLEDOnCharacter: Character = "."

public class NuimoLEDMatrix: NSObject {
public let bits: [Bit]
public let leds: [Bool]

public init(matrix: NuimoLEDMatrix) {
bits = matrix.bits
leds = matrix.leds
}

public init(string: String) {
bits = string
leds = string
// Cut off after count of LEDs
.substringToIndex(string.startIndex.advancedBy(min(string.characters.count, NuimoLEDMatrixLEDCount)))
// Right fill up to count of LEDs
.stringByPaddingToLength(NuimoLEDMatrixLEDCount, withString: " ", startingAtIndex: 0)
.characters
.map{NuimoLEDMatrixLEDOffCharacters.contains($0) ? Bit.Zero : Bit.One}
.map{!NuimoLEDMatrixLEDOffCharacters.contains($0)}
}

//TODO: Have only one init(progress) method and pass presentation style as 2nd argument
Expand All @@ -53,7 +53,7 @@ public class NuimoLEDMatrix: NSObject {
}

public func ==(left: NuimoLEDMatrix, right: NuimoLEDMatrix) -> Bool {
return left.bits == right.bits
return left.leds == right.leds
}

public func ==(left: NuimoLEDMatrix?, right: NuimoLEDMatrix) -> Bool {
Expand Down

0 comments on commit 689b15e

Please sign in to comment.