Skip to content

Commit

Permalink
day 250
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurrium committed Feb 24, 2022
1 parent be910a3 commit f161679
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions bluetooth-poc/PeripheralListView.swift
Expand Up @@ -52,6 +52,13 @@ final class PeripheralListViewState: NSObject, ObservableObject {

private var previousCrankEventTime: UInt16?
private var previousCumulativeCrankRevolutions: UInt16?
private var isStoppedCounter = 0 {
didSet {
if isStoppedCounter > 2 {
cadence = 0
}
}
}
private var cscValue: [UInt8]? {
didSet {
guard let value = cscValue else { return }
Expand All @@ -72,18 +79,20 @@ final class PeripheralListViewState: NSObject, ObservableObject {
duration = crankEventTime - previousCrankEventTime
}

if duration == 0 { return }

// FIXME: どこかがおかしい
cadence = Int(round(
(Double(cumulativeCrankRevolutions - previousCumulativeCrankRevolutions) * 60)
/
(Double(duration) / 1024)
))
} else {
previousCumulativeCrankRevolutions = cumulativeCrankRevolutions
previousCrankEventTime = crankEventTime
if duration > 0 {
isStoppedCounter = 0
cadence = Int(round(
(Double(cumulativeCrankRevolutions - previousCumulativeCrankRevolutions) * 60)
/
(Double(duration) / 1024)
))
} else {
isStoppedCounter += 1
}
}

previousCumulativeCrankRevolutions = cumulativeCrankRevolutions
previousCrankEventTime = crankEventTime
}
}

Expand Down

0 comments on commit f161679

Please sign in to comment.