<commit_before>from BinPy import *


class AnalogBuffer(object):

    """
    Analog Buffer Block
    ===================

    Create an Analog Buffer Bank which buffers the input analog value to the output.
    Enabled input enables the buffer.

    All inputs / outputs / enable are Bus types.

    METHODS
    =======

    set_enable(enable_Bus_1_bit)      : link the enable_Bus_1_bit with the Buffer's enable.

    set_inputs(Bus)                   : link the 'Bus' with the Buffer's inputs.
    set_outputs(Bus)                  : link the 'Bus' with the Buffer's outputs.
    set_inouts(In_Bus, Out_Bus)       : Set the inputs and outputs ( used when the buffer bank width has to be varied )
    set_attenuation(float)            : set the voltage attenuation in decibels.
                                        Voltage attenuation determines the value by which input is attenuated by the buffer block.
    trigger()                         : update the outputs Bus with the value of inputs.

    PROPERTIES
    ==========

    attenuation                       : get the voltage attenuation value ( in db )
    enabled                           : return true if the enable input is high.
    disabled                          : return true if the enable input is low.

    USAGE
    =====

    >>> a = Bus(4)
    >>> a.set_voltage_all(3.5, 4.6, 2.6, 1.1)
    >>> b = Bus(4)
    >>> e = Bus(1)
    >>> e.set_logic(1) # Set enable high

    >>> buf1 = AnalogBuffer(a, b, e, 0.8)

    >>> b.get_voltage_all()
    [ 3.1920379377456842,  4.195249861037184,  2.3712281823253654,  1.0032119232915009 ]

    >>> b.get_logic_all()
    [1, 1, 0, 0]

    >>> b.set_attenuation(0) # Change the attenuation value

    >>> b.get_voltage_all()
    [3.5, 4.6, 2.6, 1.1]

    >>> b.get_logic_all()
    [1, 1, 1, 0]


    NOTE
    ====

    The input width is taken as the width of the Buffer Bank.
    When inputs are changed accordingly the width varies.
    Consequently  the output width needs to match the input width.

    Hence inputs / outputs need to be updated in parallel.

    """

    def __init__(self, inputs, outputs, enable, attenuation=0.0):
        self.inputs = Bus(inputs.width)
        self.outputs = Bus(inputs.width)
        self._enable = Bus(1)

        self.set_attenuation(attenuation)
        self.set_inouts(inputs, outputs)
        self.set_enable(enable)

        self._history = None
        self._enable_history = None

    def trigger(self):

        with AutoUpdater._lock:
            inp_voltages = self.inputs.get_voltage_all()
            cur_enable = bool(self._enable[0])

        if (self._history == inp_voltages) and (self._enable_history == cur_enable):
            return
        # if inputs unchanged...

        self._history = inp_voltages
        self._enable_history = cur_enable

        inp_voltages_atten = [
            float(float(v) * (10 ** (-self._attenuation / 20))) for v in inp_voltages]
        if cur_enable:
            with AutoUpdater._lock:
                self.outputs.set_voltage_all(inp_voltages_atten)

    def set_attenuation(self, attenuation):
        """
        Set the voltage attenuation value in db for the buffer bank.
        Negative value indicates gain.
        """
        if type(attenuation) not in [float, int]:
            raise Exception(
                "ERROR: Attenuation can be a float value only.")<commit_msg>AnalogBuffer : Updated triggering to work when attenuation changes<commit_after>from BinPy import *


class AnalogBuffer(object):

    """
    Analog Buffer Block
    ===================

    Create an Analog Buffer Bank which buffers the input analog value to the output.
    Enabled input enables the buffer.

    All inputs / outputs / enable are Bus types.

    METHODS
    =======

    set_enable(enable_Bus_1_bit)      : link the enable_Bus_1_bit with the Buffer's enable.

    set_inputs(Bus)                   : link the 'Bus' with the Buffer's inputs.
    set_outputs(Bus)                  : link the 'Bus' with the Buffer's outputs.
    set_inouts(In_Bus, Out_Bus)       : Set the inputs and outputs ( used when the buffer bank width has to be varied )
    set_attenuation(float)            : set the voltage attenuation in decibels.
                                        Voltage attenuation determines the value by which input is attenuated by the buffer block.
    trigger()                         : update the outputs Bus with the value of inputs.

    PROPERTIES
    ==========

    attenuation                       : get the voltage attenuation value ( in db )
    enabled                           : return true if the enable input is high.
    disabled                          : return true if the enable input is low.

    USAGE
    =====

    >>> a = Bus(4)
    >>> a.set_voltage_all(3.5, 4.6, 2.6, 1.1)
    >>> b = Bus(4)
    >>> e = Bus(1)
    >>> e.set_logic_all(1) # Set enable high

    >>> buf1 = AnalogBuffer(a, b, e, 0.8)

    >>> b.get_voltage_all()
    [ 3.1920379377456842,  4.195249861037184,  2.3712281823253654,  1.0032119232915009 ]

    >>> b.get_logic_all()
    [1, 1, 0, 0]

    >>> buf1.set_attenuation(0) # Change the attenuation value

    >>> b.get_voltage_all()
    [3.5, 4.6, 2.6, 1.1]

    >>> b.get_logic_all()
    [1, 1, 1, 0]


    NOTE
    ====

    The input width is taken as the width of the Buffer Bank.
    When inputs are changed accordingly the width varies.
    Consequently  the output width needs to match the input width.

    Hence inputs / outputs need to be updated in parallel.

    """

    def __init__(self, inputs, outputs, enable, attenuation=0.0):
        self.inputs = Bus(inputs.width)
        self.outputs = Bus(inputs.width)
        self._enable = Bus(1)

        self.set_attenuation(attenuation)
        self.set_inouts(inputs, outputs)
        self.set_enable(enable)

        self._history = None
        self._enable_history = None
        self._atten_history = None

    def trigger(self):

        with AutoUpdater._lock:
            inp_voltages = self.inputs.get_voltage_all()
            cur_enable = bool(self._enable[0])

        if (self._history == inp_voltages) and (self._enable_history == cur_enable) and (self._atten_history == self._attenuation):
            return

        self._history = inp_voltages
        self._enable_history = cur_enable
        self._atten_history = self._attenuation

        inp_voltages_atten = [
            float(float(v) * (10 ** (-self._attenuation / 20))) for v in inp_voltages]
        if cur_enable:
            with AutoUpdater._lock:
                self.outputs.set_voltage_all(inp_voltages_atten)

    def set_attenuation(self, attenuation):
        """
        Set the voltage attenuation value in db for the buffer bank.
        Negative value indicates gain.
        """
        if type(attenuation) not in [float, int]:
            raise Exception(
                "ERROR: Attenuation can be a float value only.")
{'fraction_non_alphanumeric': 0.09782765069774134, 'fraction_numerical': 0.03150625809236081, 'mean_word_length': 2.077467906153165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7450529', 'n_tokens_mistral': 2241, 'n_tokens_neox': 1954, 'n_words': 628}