|
1 | | -'''bitstream.py: Contains BitStream class which handles reading streams of data bit by bit ''' |
2 | | - |
3 | | -########################################################################################### |
4 | | -# Copyright 2025 Garmin International, Inc. |
5 | | -# Licensed under the Flexible and Interoperable Data Transfer (FIT) Protocol License; you |
6 | | -# may not use this file except in compliance with the Flexible and Interoperable Data |
7 | | -# Transfer (FIT) Protocol License. |
8 | | -########################################################################################### |
9 | | -# ****WARNING**** This file is auto-generated! Do NOT edit this file. |
10 | | -# Profile Version = 21.188.0Release |
11 | | -# Tag = production/release/21.188.0-0-g55050f8 |
12 | | -############################################################################################ |
13 | | - |
14 | | - |
15 | | -from . import fit as FIT |
16 | | - |
17 | | - |
18 | | -class BitStream: |
19 | | - ''' |
20 | | - A class that represents a stream of binary data from a chunk of data. |
21 | | -
|
22 | | - Attributes: |
23 | | - _array: The stream of data in an array structure. |
24 | | - _current_array_position: Current position in data array. |
25 | | - _bits_per_position: Number of bits per step through the data. |
26 | | - _current_byte: Position of the current byte being read in the data. |
27 | | - _current_bit: Position of the current bit being read in the data. |
28 | | - _bits_available: Remaining number of bits left unread in the data. |
29 | | - ''' |
30 | | - def __init__(self, data, base_type): |
31 | | - self._array = None |
32 | | - self._current_array_position = 0 |
33 | | - self._bits_per_position = 0 |
34 | | - self._current_byte = 0 |
35 | | - self._current_bit = 0 |
36 | | - self._bits_available = 0 |
37 | | - |
38 | | - self._array = data if isinstance(data, list) else [data] |
39 | | - base_type_size = FIT.BASE_TYPE_DEFINITIONS[base_type]['size'] |
40 | | - self._bits_per_position = base_type_size * 8 |
41 | | - self.reset() |
42 | | - |
43 | | - def bits_available(self): |
44 | | - '''Returns the number of bits left in the data.''' |
45 | | - return self._bits_available |
46 | | - |
47 | | - def has_bits_available(self): |
48 | | - '''Returns true if the data has bits available.''' |
49 | | - return self._bits_available > 0 |
50 | | - |
51 | | - def reset(self): |
52 | | - '''Resets the bitstream to the start of the data and resets the bits available.''' |
53 | | - self._current_array_position = 0 |
54 | | - self._bits_available = self._bits_per_position * len(self._array) |
55 | | - self.__next_byte() |
56 | | - |
57 | | - def read_bit(self): |
58 | | - '''Reads the next bit if possible.''' |
59 | | - if self.has_bits_available() is False: |
60 | | - self.__raise_error() |
61 | | - |
62 | | - if self._current_bit >= self._bits_per_position: |
63 | | - self.__next_byte() |
64 | | - |
65 | | - bit = self._current_byte & 0x01 |
66 | | - self._current_byte = (self._current_byte >> 1) |
67 | | - self._current_bit += 1 |
68 | | - self._bits_available -= 1 |
69 | | - |
70 | | - return bit |
71 | | - |
72 | | - def read_bits(self, number_bits_to_read): |
73 | | - '''Reads the specificed number of bits if possible.''' |
74 | | - value = 0 |
75 | | - |
76 | | - for i in range(number_bits_to_read): |
77 | | - value |= self.read_bit() << i |
78 | | - |
79 | | - return value |
80 | | - |
81 | | - def __next_byte(self): |
82 | | - if self._current_array_position >= len(self._array): |
83 | | - self.__raise_error() |
84 | | - |
85 | | - self._current_byte = self._array[self._current_array_position] |
86 | | - self._current_array_position += 1 |
87 | | - self._current_bit = 0 |
88 | | - |
89 | | - def __raise_error(self): |
90 | | - raise IndexError('FIT Runtime Error, no bits available.') |
| 1 | +'''bitstream.py: Contains BitStream class which handles reading streams of data bit by bit ''' |
| 2 | + |
| 3 | +########################################################################################### |
| 4 | +# Copyright 2026 Garmin International, Inc. |
| 5 | +# Licensed under the Flexible and Interoperable Data Transfer (FIT) Protocol License; you |
| 6 | +# may not use this file except in compliance with the Flexible and Interoperable Data |
| 7 | +# Transfer (FIT) Protocol License. |
| 8 | +########################################################################################### |
| 9 | +# ****WARNING**** This file is auto-generated! Do NOT edit this file. |
| 10 | +# Profile Version = 21.194.0Release |
| 11 | +# Tag = production/release/21.194.0-0-g65135fc |
| 12 | +############################################################################################ |
| 13 | + |
| 14 | + |
| 15 | +from . import fit as FIT |
| 16 | + |
| 17 | + |
| 18 | +class BitStream: |
| 19 | + ''' |
| 20 | + A class that represents a stream of binary data from a chunk of data. |
| 21 | +
|
| 22 | + Attributes: |
| 23 | + _array: The stream of data in an array structure. |
| 24 | + _current_array_position: Current position in data array. |
| 25 | + _bits_per_position: Number of bits per step through the data. |
| 26 | + _current_byte: Position of the current byte being read in the data. |
| 27 | + _current_bit: Position of the current bit being read in the data. |
| 28 | + _bits_available: Remaining number of bits left unread in the data. |
| 29 | + ''' |
| 30 | + def __init__(self, data, base_type): |
| 31 | + self._array = None |
| 32 | + self._current_array_position = 0 |
| 33 | + self._bits_per_position = 0 |
| 34 | + self._current_byte = 0 |
| 35 | + self._current_bit = 0 |
| 36 | + self._bits_available = 0 |
| 37 | + |
| 38 | + self._array = data if isinstance(data, list) else [data] |
| 39 | + base_type_size = FIT.BASE_TYPE_DEFINITIONS[base_type]['size'] |
| 40 | + self._bits_per_position = base_type_size * 8 |
| 41 | + self.reset() |
| 42 | + |
| 43 | + def bits_available(self): |
| 44 | + '''Returns the number of bits left in the data.''' |
| 45 | + return self._bits_available |
| 46 | + |
| 47 | + def has_bits_available(self): |
| 48 | + '''Returns true if the data has bits available.''' |
| 49 | + return self._bits_available > 0 |
| 50 | + |
| 51 | + def reset(self): |
| 52 | + '''Resets the bitstream to the start of the data and resets the bits available.''' |
| 53 | + self._current_array_position = 0 |
| 54 | + self._bits_available = self._bits_per_position * len(self._array) |
| 55 | + self.__next_byte() |
| 56 | + |
| 57 | + def read_bit(self): |
| 58 | + '''Reads the next bit if possible.''' |
| 59 | + if self.has_bits_available() is False: |
| 60 | + self.__raise_error() |
| 61 | + |
| 62 | + if self._current_bit >= self._bits_per_position: |
| 63 | + self.__next_byte() |
| 64 | + |
| 65 | + bit = self._current_byte & 0x01 |
| 66 | + self._current_byte = (self._current_byte >> 1) |
| 67 | + self._current_bit += 1 |
| 68 | + self._bits_available -= 1 |
| 69 | + |
| 70 | + return bit |
| 71 | + |
| 72 | + def read_bits(self, number_bits_to_read): |
| 73 | + '''Reads the specificed number of bits if possible.''' |
| 74 | + value = 0 |
| 75 | + |
| 76 | + for i in range(number_bits_to_read): |
| 77 | + value |= self.read_bit() << i |
| 78 | + |
| 79 | + return value |
| 80 | + |
| 81 | + def __next_byte(self): |
| 82 | + if self._current_array_position >= len(self._array): |
| 83 | + self.__raise_error() |
| 84 | + |
| 85 | + self._current_byte = self._array[self._current_array_position] |
| 86 | + self._current_array_position += 1 |
| 87 | + self._current_bit = 0 |
| 88 | + |
| 89 | + def __raise_error(self): |
| 90 | + raise IndexError('FIT Runtime Error, no bits available.') |
0 commit comments