# Path: ripyl/util/bitops.py
# def split_bits(n, num_bits):
#     '''Convert integer to a list of bits (MSB-first)
# 
#     n (int)
#         The number to convert to bits.
# 
#     num_bits (int)
#         The number of bits in the result.
# 
#     Returns a list of ints representing each bit in n.
#     '''
#     bits = [0] * num_bits
#     for i in xrange(num_bits-1, -1, -1):
#         bits[i] = n & 0x01
#         n >>= 1
#         
#     return bits
# 
# def join_bits(bits):
#     '''Convert an array of bits (MSB first) to an integer word
# 
#     bits (sequence of ints)
#         The bits to be merged into an integer
# 
#     Returns an int representing the bits contained in the bits parameter.
#     '''
#     word = 0
#     for b in bits:
#         word = (word << 1) | b
#         
#     return word


import functools
import ripyl.decode as decode
import ripyl.streaming as stream
import ripyl.protocol.infrared as ir
from ripyl.util.bitops import split_bits, join_bits
                break  # Not the beginning of a bit

            bit_starts.append(es.cur_time - ts)

            ts = es.advance_to_edge()
            if time_is_nearly(ts, one_t): # 0-bit
                msg_bits.append(0)

            elif time_is_nearly(ts, 2 * one_t): #1-bit
                msg_bits.append(1)

            else:
                break

        bit_starts.append(prev_edge) # End of last bit
        #print('### last bit:', es.cur_time)

        if len(msg_bits) in (12, 15, 20):
            cmd = join_bits(reversed(msg_bits[0:7]))
            cmd_range = (bit_starts[0], bit_starts[7])

            if len(msg_bits) == 12 or len(msg_bits) == 20:
                device = join_bits(reversed(msg_bits[7:12]))
                device_range = (bit_starts[7], bit_starts[12])
            else: # 15-bit command
                device = join_bits(reversed(msg_bits[7:15]))
                device_range = (bit_starts[7], bit_starts[15])

            extended = None
            if len(msg_bits) == 20: # 20-bit extended format

# Path: ripyl/util/bitops.py
# def split_bits(n, num_bits):
#     '''Convert integer to a list of bits (MSB-first)
# 
#     n (int)
#         The number to convert to bits.
# 
#     num_bits (int)
#         The number of bits in the result.
# 
#     Returns a list of ints representing each bit in n.
#     '''
#     bits = [0] * num_bits
#     for i in xrange(num_bits-1, -1, -1):
#         bits[i] = n & 0x01
#         n >>= 1
#         
#     return bits
# 
# def join_bits(bits):
#     '''Convert an array of bits (MSB first) to an integer word
# 
#     bits (sequence of ints)
#         The bits to be merged into an integer
# 
#     Returns an int representing the bits contained in the bits parameter.
#     '''
#     word = 0
#     for b in bits:
#         word = (word << 1) | b
#         
#     return word


# Path: ripyl/protocol/infrared/sirc.py
import functools
import ripyl.decode as decode
import ripyl.streaming as stream
import ripyl.protocol.infrared as ir
from ripyl.util.bitops import split_bits, join_bits

                break  # Not the beginning of a bit

            bit_starts.append(es.cur_time - ts)

            ts = es.advance_to_edge()
            if time_is_nearly(ts, one_t): # 0-bit
                msg_bits.append(0)

            elif time_is_nearly(ts, 2 * one_t): #1-bit
                msg_bits.append(1)

            else:
                break

        bit_starts.append(prev_edge) # End of last bit
        #print('### last bit:', es.cur_time)

        if len(msg_bits) in (12, 15, 20):
            cmd = join_bits(reversed(msg_bits[0:7]))
            cmd_range = (bit_starts[0], bit_starts[7])

            if len(msg_bits) == 12 or len(msg_bits) == 20:
                device = join_bits(reversed(msg_bits[7:12]))
                device_range = (bit_starts[7], bit_starts[12])
            else: # 15-bit command
                device = join_bits(reversed(msg_bits[7:15]))
                device_range = (bit_starts[7], bit_starts[15])

            extended = None
            if len(msg_bits) == 20: # 20-bit extended format

                extended = join_bits(reversed(msg_bits[12:20]))