BolusExtraCommand.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // BolusExtraCommand.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 2/24/18.
  6. // Copyright © 2018 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. public struct BolusExtraCommand : MessageBlock {
  10. public let blockType: MessageBlockType = .bolusExtra
  11. public let acknowledgementBeep: Bool
  12. public let completionBeep: Bool
  13. public let programReminderInterval: TimeInterval
  14. public let units: Double
  15. public let timeBetweenPulses: TimeInterval
  16. public let squareWaveUnits: Double
  17. public let squareWaveDuration: TimeInterval
  18. // 17 0d 7c 1770 00030d40 0000 00000000
  19. // 0 1 2 3 5 9 13
  20. public var data: Data {
  21. let beepOptions = (UInt8(programReminderInterval.minutes) & 0x3f) + (completionBeep ? (1<<6) : 0) + (acknowledgementBeep ? (1<<7) : 0)
  22. var data = Data([
  23. blockType.rawValue,
  24. 0x0d,
  25. beepOptions
  26. ])
  27. data.appendBigEndian(UInt16(round(units * 200)))
  28. data.appendBigEndian(UInt32(timeBetweenPulses.hundredthsOfMilliseconds))
  29. let pulseCountX10 = UInt16(round(squareWaveUnits * 200))
  30. data.appendBigEndian(pulseCountX10)
  31. let timeBetweenExtendedPulses = pulseCountX10 > 0 ? squareWaveDuration / Double(pulseCountX10) : 0
  32. data.appendBigEndian(UInt32(timeBetweenExtendedPulses.hundredthsOfMilliseconds))
  33. return data
  34. }
  35. public init(encodedData: Data) throws {
  36. if encodedData.count < 15 {
  37. throw MessageBlockError.notEnoughData
  38. }
  39. acknowledgementBeep = encodedData[2] & (1<<7) != 0
  40. completionBeep = encodedData[2] & (1<<6) != 0
  41. programReminderInterval = TimeInterval(minutes: Double(encodedData[2] & 0x3f))
  42. units = Double(encodedData[3...].toBigEndian(UInt16.self)) / 200
  43. let delayCounts = encodedData[5...].toBigEndian(UInt32.self)
  44. timeBetweenPulses = TimeInterval(hundredthsOfMilliseconds: Double(delayCounts))
  45. let pulseCountX10 = encodedData[9...].toBigEndian(UInt16.self)
  46. squareWaveUnits = Double(pulseCountX10) / 200
  47. let intervalCounts = encodedData[5...].toBigEndian(UInt32.self)
  48. let timeBetweenExtendedPulses = TimeInterval(hundredthsOfMilliseconds: Double(intervalCounts))
  49. squareWaveDuration = timeBetweenExtendedPulses * Double(pulseCountX10) / 10
  50. }
  51. public init(units: Double, timeBetweenPulses: TimeInterval = Pod.secondsPerBolusPulse, squareWaveUnits: Double = 0.0, squareWaveDuration: TimeInterval = 0, acknowledgementBeep: Bool = false, completionBeep: Bool = false, programReminderInterval: TimeInterval = 0) {
  52. self.acknowledgementBeep = acknowledgementBeep
  53. self.completionBeep = completionBeep
  54. self.programReminderInterval = programReminderInterval
  55. self.units = units
  56. self.timeBetweenPulses = timeBetweenPulses
  57. self.squareWaveUnits = squareWaveUnits
  58. self.squareWaveDuration = squareWaveDuration
  59. }
  60. }
  61. extension BolusExtraCommand: CustomDebugStringConvertible {
  62. public var debugDescription: String {
  63. return "BolusExtraCommand(units:\(units), timeBetweenPulses:\(timeBetweenPulses), squareWaveUnits:\(squareWaveUnits), squareWaveDuration:\(squareWaveDuration), acknowledgementBeep:\(acknowledgementBeep), completionBeep:\(completionBeep), programReminderInterval:\(programReminderInterval))"
  64. }
  65. }