BolusCarelinkMessageBody.swift 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // BolusCarelinkMessageBody.swift
  3. // Naterade
  4. //
  5. // Created by Nathan Racklyeft on 3/5/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import Foundation
  9. public class BolusCarelinkMessageBody: CarelinkLongMessageBody {
  10. public convenience init(units: Double, insulinBitPackingScale: Int = 10) {
  11. let length: Int
  12. let scrollRate: Int
  13. if insulinBitPackingScale >= 40 {
  14. length = 2
  15. // 40-stroke pumps scroll faster for higher unit values
  16. switch units {
  17. case let u where u > 10:
  18. scrollRate = 4
  19. case let u where u > 1:
  20. scrollRate = 2
  21. default:
  22. scrollRate = 1
  23. }
  24. } else {
  25. length = 1
  26. scrollRate = 1
  27. }
  28. let strokes = Int(units * Double(insulinBitPackingScale / scrollRate)) * scrollRate
  29. let data = Data(hexadecimalString: String(format: "%02x%0\(2 * length)x", length, strokes))!
  30. self.init(rxData: data)!
  31. }
  32. }