# 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
    idle_start (float)
        The amount of idle time before the transmission of messages begins.

    message_interval (float)
        The amount of time between messages.
    
    idle_end (float)
        The amount of idle time after the last message.

    Yields an edge stream of (float, int) pairs. The first element in the iterator
      is the initial state of the stream.
    '''

    t = 0.0
    irtx = 0 # idle-low

    one_t = 600.0e-6 # 600us 1T time
    
    yield (t, irtx) # set initial conditions
    t += idle_start

    for msg in messages:

        msg_bits = list(reversed(split_bits(msg.cmd, 7)))
        if msg.device < 2**5 or msg.extended is not None:
            msg_bits.extend(reversed(split_bits(msg.device, 5)))
        else: # 15-bit command with 8-bit device field
            msg_bits.extend(reversed(split_bits(msg.device, 8)))

        if msg.extended is not None:

# 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

    idle_start (float)
        The amount of idle time before the transmission of messages begins.

    message_interval (float)
        The amount of time between messages.
    
    idle_end (float)
        The amount of idle time after the last message.

    Yields an edge stream of (float, int) pairs. The first element in the iterator
      is the initial state of the stream.
    '''

    t = 0.0
    irtx = 0 # idle-low

    one_t = 600.0e-6 # 600us 1T time
    
    yield (t, irtx) # set initial conditions
    t += idle_start

    for msg in messages:

        msg_bits = list(reversed(split_bits(msg.cmd, 7)))
        if msg.device < 2**5 or msg.extended is not None:
            msg_bits.extend(reversed(split_bits(msg.device, 5)))
        else: # 15-bit command with 8-bit device field
            msg_bits.extend(reversed(split_bits(msg.device, 8)))

        if msg.extended is not None:

            msg_bits.extend(reversed(split_bits(msg.extended, 8)))