# 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 ripyl.decode as decode
import ripyl.streaming as stream
import ripyl.protocol.infrared as ir
from ripyl.util.bitops import split_bits, join_bits
        bit_starts = [0.0, 0.0]
        same_count = 1
        prev_state = 1
        while True:
            es.advance()
            coded_bits.append(es.cur_state())
            bit_starts.append(es.cur_time - pulse_width / 2.0)
            if es.cur_state() == prev_state:
                same_count += 1
            else:
                same_count = 1

            if same_count > 2:
                break

            prev_state = es.cur_state()

        msg_end_time = es.cur_time - pulse_width

        if len(coded_bits) >= 14 * 2:
            # Decode Manchester
            # The second bit of each pair is the same as the decoded bit
            msg_bits = coded_bits[1:28:2]
            mb_starts = bit_starts[::2]

            #print('$$$ coded_bits:', coded_bits)
            #print('$$$ msg_bits:', msg_bits)

            toggle = msg_bits[2]
            addr = join_bits(msg_bits[3:8])

# 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/rc5.py
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

        bit_starts = [0.0, 0.0]
        same_count = 1
        prev_state = 1
        while True:
            es.advance()
            coded_bits.append(es.cur_state())
            bit_starts.append(es.cur_time - pulse_width / 2.0)
            if es.cur_state() == prev_state:
                same_count += 1
            else:
                same_count = 1

            if same_count > 2:
                break

            prev_state = es.cur_state()

        msg_end_time = es.cur_time - pulse_width

        if len(coded_bits) >= 14 * 2:
            # Decode Manchester
            # The second bit of each pair is the same as the decoded bit
            msg_bits = coded_bits[1:28:2]
            mb_starts = bit_starts[::2]

            #print('$$$ coded_bits:', coded_bits)
            #print('$$$ msg_bits:', msg_bits)

            toggle = msg_bits[2]
            addr = join_bits(msg_bits[3:8])

            cmd = join_bits([0 if msg_bits[1] else 1] + msg_bits[8:14])