# Path: ripyl/util/enum.py
# class Enum(object):
#     '''Base class for enumeration classes
#     
#     This provides a name() class method that will return the string
#     attribute name for an encoded enumeration value.
#     
#     e.g.
#     
#     >>> class Colors(Enum):
#     ...     blue = 1
#     ...     red = 2
#     ...     green = 3
#         
#     >>> Colors.name(Colors.red)
#     'red'
#     
#     >>> Colors.name(1, full_name=True)
#     'Colors.blue'
# 
#     "Instantiating" the Enum sub-class calls the name() method as well:
#     
#     >>> Colors(3, full_name=False)
#     'green'
#     
#     If necesssary you can use non-integers as the values as long as they are hashable:
#     
#     >>> class MathConst(Enum):
#     ...     pi = 3.2     # in certain US states :)
#     ...     e = 2.71828
#         
#     >>> MathConst.pi
#     3.2
#     
#     '''
# 
#     @classmethod
#     def name(cls, value, full_name=False):
#         '''Lookup the enumeration name with the provided value
# 
#         value (hashable)
#             A hashable Enum value (typically int) to find a name for.
# 
#         full_name (bool)
#             Include full name of Enum object in result
# 
#         Returns a string for the Enum attribute associated with value.
#         '''
#         
#         try:
#             enum_lookup = cls._enum_lookup
#         except AttributeError:
#             # Build inverse dict of class attributes with their values as key
#             enum_lookup = dict((v, k) for k, v in cls.__dict__.items() if k[0] != '_')
#             cls._enum_lookup = enum_lookup
#         
#         try:
#             aname = enum_lookup[value]
#             prefix = cls.__name__ + '.' if full_name else ''
#             return prefix + aname
# 
#         except KeyError:
#             return 'unknown'
# 
#     def __new__(cls, value, full_name=False):
#         '''Override class instantitation to perform name lookup instead'''
#         return cls.name(value, full_name)
# 
# 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


import inspect
from ripyl.decode import *
from ripyl.streaming import *
from ripyl.util.enum import Enum
from ripyl.util.bitops import split_bits
    merged_code = ((a*256 + b)*256 +c)*256 + d
    pid_bits = split_bits(merged_code, 32)

    pids = []
    for i, b in enumerate(pid_bits):
        if b == 1:
            pids.append(i+1+offset)

    return pids

def _get_status(a, b, c, d):
    '''Decode response for sid 0x01, pid 0x01'''
    r = {}
    r['DTC count'] = a & 0x7F
    r['MIL status'] = True if a & 0x80 else False

    r['spark ignition'] = False if b & 0x08 else True
    test_available = [bool(v) for v in split_bits(b & 0x07, 3)]
    test_incomplete = [bool(v) for v in split_bits((b & 0x70) >> 4, 3)]
    common_tests = ['misfire', 'fuel system', 'components']
    tests = {}
    for tname, ta, tc in zip(common_tests, test_available, test_incomplete):
        #print('## test', tname, ta, tc)
        tests[tname] = (ta, tc)

    if r['spark ignition']:
        spark_tests = ['catalyst', 'heated catalyst', 'evap. system', \
            'secondary air system', 'A/C refrigerant', 'oxygen sensor', \
            'oxygen sensor heater', 'EGR system']
        test_available = [bool(v) for v in split_bits(c, 8)]

# Path: ripyl/util/enum.py
# class Enum(object):
#     '''Base class for enumeration classes
#     
#     This provides a name() class method that will return the string
#     attribute name for an encoded enumeration value.
#     
#     e.g.
#     
#     >>> class Colors(Enum):
#     ...     blue = 1
#     ...     red = 2
#     ...     green = 3
#         
#     >>> Colors.name(Colors.red)
#     'red'
#     
#     >>> Colors.name(1, full_name=True)
#     'Colors.blue'
# 
#     "Instantiating" the Enum sub-class calls the name() method as well:
#     
#     >>> Colors(3, full_name=False)
#     'green'
#     
#     If necesssary you can use non-integers as the values as long as they are hashable:
#     
#     >>> class MathConst(Enum):
#     ...     pi = 3.2     # in certain US states :)
#     ...     e = 2.71828
#         
#     >>> MathConst.pi
#     3.2
#     
#     '''
# 
#     @classmethod
#     def name(cls, value, full_name=False):
#         '''Lookup the enumeration name with the provided value
# 
#         value (hashable)
#             A hashable Enum value (typically int) to find a name for.
# 
#         full_name (bool)
#             Include full name of Enum object in result
# 
#         Returns a string for the Enum attribute associated with value.
#         '''
#         
#         try:
#             enum_lookup = cls._enum_lookup
#         except AttributeError:
#             # Build inverse dict of class attributes with their values as key
#             enum_lookup = dict((v, k) for k, v in cls.__dict__.items() if k[0] != '_')
#             cls._enum_lookup = enum_lookup
#         
#         try:
#             aname = enum_lookup[value]
#             prefix = cls.__name__ + '.' if full_name else ''
#             return prefix + aname
# 
#         except KeyError:
#             return 'unknown'
# 
#     def __new__(cls, value, full_name=False):
#         '''Override class instantitation to perform name lookup instead'''
#         return cls.name(value, full_name)
# 
# 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


# Path: ripyl/protocol/obd2.py
import inspect
from ripyl.decode import *
from ripyl.streaming import *
from ripyl.util.enum import Enum
from ripyl.util.bitops import split_bits

    merged_code = ((a*256 + b)*256 +c)*256 + d
    pid_bits = split_bits(merged_code, 32)

    pids = []
    for i, b in enumerate(pid_bits):
        if b == 1:
            pids.append(i+1+offset)

    return pids

def _get_status(a, b, c, d):
    '''Decode response for sid 0x01, pid 0x01'''
    r = {}
    r['DTC count'] = a & 0x7F
    r['MIL status'] = True if a & 0x80 else False

    r['spark ignition'] = False if b & 0x08 else True
    test_available = [bool(v) for v in split_bits(b & 0x07, 3)]
    test_incomplete = [bool(v) for v in split_bits((b & 0x70) >> 4, 3)]
    common_tests = ['misfire', 'fuel system', 'components']
    tests = {}
    for tname, ta, tc in zip(common_tests, test_available, test_incomplete):
        #print('## test', tname, ta, tc)
        tests[tname] = (ta, tc)

    if r['spark ignition']:
        spark_tests = ['catalyst', 'heated catalyst', 'evap. system', \
            'secondary air system', 'A/C refrigerant', 'oxygen sensor', \
            'oxygen sensor heater', 'EGR system']
        test_available = [bool(v) for v in split_bits(c, 8)]

        test_incomplete = [bool(v) for v in split_bits(d, 8)]