Skip to content

Commit

Permalink
fixed tests and status error
Browse files Browse the repository at this point in the history
  • Loading branch information
kkonteh97 committed May 23, 2024
1 parent 98ce33c commit 22762f3
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 83 deletions.
5 changes: 5 additions & 0 deletions Sources/SwiftOBD2/commands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public extension DecodeResult {
if case let .troubleCode(res) = self { return res as [TroubleCode] }
return nil
}

var measurementMonitor: Monitor? {
if case let .measurementMonitor(res) = self { return res as Monitor }
return nil
}
}

public struct CommandProperties {
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftOBD2/decoders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public enum Decoders: Equatable {
case .status:
return decodeStatus(data)
case .temp:
return decodeTemp(data, unit: unit)
return tempDecoder(data, unit: unit)
case .percent:
return percentDecoder(data)
case .currentCentered:
Expand Down Expand Up @@ -556,7 +556,7 @@ func percentCenteredDecoder(_ data: Data) -> Result<DecodeResult, DecodeError> {

// -128 to 128 mA
func currentCenteredDecoder(_ data: Data) -> Result<DecodeResult, DecodeError> {
var value = Double(bytesToInt(data[2 ... 3]))
var value = Double(bytesToInt(data.dropFirst(2)))
value = (value / 256.0) - 128.0
return .success(.measurementResult(MeasurementResult(value: value, unit: UnitElectricCurrent.milliamperes)))
}
Expand Down Expand Up @@ -597,7 +597,7 @@ func airStatusDecoder(_ data: Data) -> Result<DecodeResult, DecodeError> {
return .failure(.invalidData)
}

func decodeTemp(_ data: Data, unit: MeasurementUnit) -> Result<DecodeResult, DecodeError> {
func tempDecoder(_ data: Data, unit: MeasurementUnit) -> Result<DecodeResult, DecodeError> {
let value = Double(bytesToInt(data)) - 40.0
return .success(.measurementResult(MeasurementResult(value: value, unit: UnitTemperature.celsius)))
}
Expand All @@ -623,7 +623,7 @@ func decodeStatus(_ data: Data) -> Result<DecodeResult, DecodeError> {
// [# DTC] X [supprt] [~ready]

// convert to binaryarray
let bits = BitArray(data: data.dropFirst())
let bits = BitArray(data: data)

var output = Status()
output.MIL = bits.binaryArray[0] == 1
Expand Down
Loading

0 comments on commit 22762f3

Please sign in to comment.