Browse Source

Merge pull request #5 from dokthar/fix_bcd

fix BCD fields encoding
main
Kate Temkin 4 years ago
committed by GitHub
parent
commit
37371d8f39
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 6 deletions
  1. +6
    -6
      usb_protocol/types/descriptor.py

+ 6
- 6
usb_protocol/types/descriptor.py View File

@@ -87,13 +87,13 @@ class BCDFieldAdapter(construct.Adapter):

def _encode(self, obj, context, path):

# Ensure the data is parseable.
if (obj * 100) % 1:
raise AssertionError("BCD fields must be in the format XX.YY")

# Break the object down into its component parts...
integer = int(obj)
percent = int((obj * 100) % 100)
integer = int(obj) % 100
percent = int(round(obj * 100)) % 100

# ... make sure nothing is lost during conversion...
if float(f"{integer:02}.{percent:02}") != obj:
raise AssertionError("BCD fields must be in the format XX.YY")

# ... and squish them into an integer.
return int(f"{integer:02}{percent:02}", 16)


Loading…
Cancel
Save