Skip to content

Commit

Permalink
#161 Add DataConvertible conformance to GATTNumberOfDigitals
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 11, 2024
1 parent 52c4b99 commit 0610aa6
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions Sources/BluetoothGATT/GATTNumberOfDigitals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,56 @@
// Copyright © 2018 PureSwift. All rights reserved.
//

import Foundation
import Bluetooth

// MARK: - Number of Digitals
/// GATT Number of Digitals Descriptor
///
/// The Characteristic Number of Digitals descriptor is used for defining the number of digitals in a characteristic.
@frozen
public struct GATTNumberOfDigitals: GATTDescriptor, RawRepresentable, Equatable, Hashable {

public static let uuid: BluetoothUUID = .numberOfDigitals

public static let length = 1
public struct GATTNumberOfDigitals: GATTDescriptor, RawRepresentable, Equatable, Hashable, Sendable {

public static var uuid: BluetoothUUID { .numberOfDigitals }

public var rawValue: UInt8

public init(rawValue: UInt8) {

self.rawValue = rawValue
}
}

// MARK: - DataConvertible

extension GATTNumberOfDigitals: DataConvertible {

public init?(data: Data) {

guard data.count == type(of: self).length
public static var length: Int { 1 }

public init?<Data: DataContainer>(data: Data) {
guard data.count == Self.length
else { return nil }

rawValue = data[0]
self.init(rawValue: data[0])
}

public var data: Data {

return Data([rawValue])
public func append<Data>(to data: inout Data) where Data : DataContainer {
data += rawValue
}

public var descriptor: GATTAttribute.Descriptor {

return GATTAttribute.Descriptor(uuid: type(of: self).uuid,
value: data,
permissions: [.read])
}
public var dataLength: Int { Self.length }
}

// MARK: - CustomStringConvertible

extension GATTNumberOfDigitals: CustomStringConvertible {

public var description: String {

return rawValue.description
}
}

// MARK: - ExpressibleByIntegerLiteral

extension GATTNumberOfDigitals: ExpressibleByIntegerLiteral {

public init(integerLiteral value: UInt8) {

self.init(rawValue: value)
}
}

0 comments on commit 0610aa6

Please sign in to comment.