# 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
        msg_end_time = es.cur_time - 2.5 * pulse_width

        #print('$$$ found bits:', len(coded_bits))

        if len(coded_bits) >= 22 * 2:
            # Decode Manchester
            # The first bit of each pair is the same as the decoded bit
            msg_bits = coded_bits[0:44:2]
            mb_starts = bit_starts[::2]

            mode = join_bits(msg_bits[1:4])
            toggle = msg_bits[4]

            if mode == 6: # RC6A message
                if msg_bits[6]: # 15-bit customer field
                    msg_bits = coded_bits[0:76:2]
                    customer = join_bits(msg_bits[7:22])
                    asb = 22 # addr start bit index
                else: # 7-bit customer field
                    msg_bits = coded_bits[0:60:2]
                    customer = join_bits(msg_bits[7:14])
                    asb = 14
            else: # RC6 message
                customer = None
                asb = 6

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



# 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/rc6.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

        msg_end_time = es.cur_time - 2.5 * pulse_width

        #print('$$$ found bits:', len(coded_bits))

        if len(coded_bits) >= 22 * 2:
            # Decode Manchester
            # The first bit of each pair is the same as the decoded bit
            msg_bits = coded_bits[0:44:2]
            mb_starts = bit_starts[::2]

            mode = join_bits(msg_bits[1:4])
            toggle = msg_bits[4]

            if mode == 6: # RC6A message
                if msg_bits[6]: # 15-bit customer field
                    msg_bits = coded_bits[0:76:2]
                    customer = join_bits(msg_bits[7:22])
                    asb = 22 # addr start bit index
                else: # 7-bit customer field
                    msg_bits = coded_bits[0:60:2]
                    customer = join_bits(msg_bits[7:14])
                    asb = 14
            else: # RC6 message
                customer = None
                asb = 6

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



            addr = join_bits(msg_bits[asb:asb+8])