MCPcopy Create free account
hub / github.com/ClusterM/flipper_rc / width_encode

Function width_encode

custom_components/flipper_rc/pulse.py:163–202  ·  view source on GitHub ↗

Encode a list of bytes into pulses and gaps based on given timings. Args: values (list of int): List of byte values to encode. leading_pulse (int): Duration of the leading pulse. leading_gap (int): Duration of the gap following the leading pulse. gap (int):

(values, leading_pulse, leading_gap, gap, pulse_0, pulse_1, bit_length=None, msb_first=False)

Source from the content-addressed store, hash-verified

161 return pulses
162
163def width_encode(values, leading_pulse, leading_gap, gap, pulse_0, pulse_1, bit_length=None, msb_first=False):
164 """
165 Encode a list of bytes into pulses and gaps based on given timings.
166
167 Args:
168 values (list of int): List of byte values to encode.
169 leading_pulse (int): Duration of the leading pulse.
170 leading_gap (int): Duration of the gap following the leading pulse.
171 gap (int): Duration of the gap between pulses.
172 pulse_0 (int): Duration of the pulse representing a '0' bit.
173 pulse_1 (int): Duration of the pulse representing a '1' bit.
174 bit_length (int, optional): Total number of bits to encode. If None, encode all bits in `values`. Defaults to None.
175 msb_first (bool, optional): If True, encode the most significant bit first. If False, encode the least significant bit first. Defaults to False.
176
177 Returns:
178 list of int: List of pulse and gap durations representing the encoded values.
179
180 Raises:
181 ValueError: If `bit_length` is greater than the number of bits in `values`.
182 """
183 # Encode a list of bytes into pulses/gaps based on given timings
184 if bit_length is not None and bit_length > len(values) * 8:
185 raise ValueError(f"bit_length {bit_length} is greater than the number of bits in values")
186 pulses = []
187 pulses.append(leading_pulse)
188 pulses.append(leading_gap)
189 total = 0
190 for i in values:
191 for bit in range(8):
192 if msb_first:
193 pulses.append(pulse_1 if (i & (1 << (7 - bit))) > 0 else pulse_0)
194 else:
195 pulses.append(pulse_1 if (i & (1 << bit)) > 0 else pulse_0)
196 pulses.append(gap)
197 total += 1
198 if bit_length is not None and total >= bit_length:
199 break
200 if bit_length is not None and total >= bit_length:
201 break
202 return pulses
203

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected