/* NTElectrode class */
  
function NTElectrode_afid(module, symbol, addr, type)
{
    var ascAddr, ascSymbol;

    // call base when not typed dynamically (see change_class_type()) 
    // otherwise, the base object is already constructed
    if(!this._dynamically_typed)
        NTElectrode(module, symbol, addr, null)
}

// AFID integration_value is common to all AFID electrodes, we define it here to avoid duplicating it in each
NTElectrode_afid.prototype.DefineAfidSlowSignalVariable = function(vname)
{
    debug_assert(this._keydetsymbol, "need typed keydetector");
    return pcm_define_variable2(vname, this._keydetsymbol + "->slow_signal", "UINT16");
}

// AFID integration_value is common to all AFID electrodes, we define it here to avoid duplicating it in each
NTElectrode_afid.prototype.DefineAfidIntegrationValueVariable = function(vname)
{
    debug_assert(this._keydetsymbol, "need typed keydetector");
    return pcm_define_variable2(vname, this._keydetsymbol + "->integration_value", "SINT16");
}

// AFID touch_threshold is common to all AFID electrodes, we define it here to avoid duplicating it in each
NTElectrode_afid.prototype.DefineAfidTouchThreshold = function(vname)
{
    debug_assert(this._keydetsymbol, "need typed keydetector");
    return pcm_define_variable2(vname, this._keydetsymbol + "->touch_threshold", "SINT16");
}

// AFID touch_reset_counter is common to all AFID electrodes, we define it here to avoid duplicating it in each
NTElectrode_afid.prototype.DefineAfidTouchResetCounter = function(vname)
{
    debug_assert(this._keydetsymbol, "need typed keydetector");
    return pcm_define_variable(vname, this._keydetsymbol + "->touch_reset_counter");
}

// AFID release_reset_counter is common to all AFID electrodes, we define it here to avoid duplicating it in each
NTElectrode_afid.prototype.DefineAfidReleaseResetCounter = function(vname)
{
    debug_assert(this._keydetsymbol, "need typed keydetector");
    return pcm_define_variable(vname, this._keydetsymbol + "->release_reset_counter");
}

// ASC threshold is common to all AFID electrodes, we define it here to avoid duplicating it in each 
NTElectrode_afid.prototype.DefineAfidAscThresholdVariable = function(vname)
{
    debug_assert(this._keydetsymbol, "need typed keydetector");
    return pcm_define_variable2(vname, this._keydetsymbol + "->asc.dest_threshold", "SINT16");
}

// ASC noise reset is common to all AFID electrodes, we define it here to avoid duplicating it in each 
NTElectrode_afid.prototype.DefineAfidAscNoiseResetsVariable = function(vname)
{
    debug_assert(this._keydetsymbol, "need typed keydetector");
    return pcm_define_variable2(vname, this._keydetsymbol + "->asc.noise_resets", "SINT16");
}

// ASC maximal reset is common to all AFID electrodes, we define it here to avoid duplicating it in each 
NTElectrode_afid.prototype.DefineAfidAscMaxResetsVariable = function(vname)
{
    debug_assert(this._keydetsymbol, "need typed keydetector");
    return pcm_define_variable2(vname, this._keydetsymbol + "->asc.max_resets", "SINT16");
}

// ASC threshold is common to all AFID electrodes, we define it here to avoid duplicating it in each 
NTElectrode_afid.prototype.DefineAfidThresholdVariable = function(vname)
{
    debug_assert(this._keydetsymbol, "need typed keydetector");
    return pcm_define_variable2(vname, this._keydetsymbol + "->touch_threshold", "UINT32");
}


NTElectrode_afid.prototype.OnLinkClicked = function()
{
    var vname_rawsignal  = this._name + "_raw_signal";
    var vname_fast_signal  = this._name + "_fast_signal";
    var vname_slow_signal  = this._name + "_slow_signal";
    var vname_baseline = this._name + "_baseline";
    var vname_afid_trc = this._name + "_touch_rcount";
    var vname_afid_rrc = this._name + "_release_rcount";
    var vname_afid_i = this._name + "_afid_integration";
    var vname_reset_threshold = this._name + "_reset_threshold";
    
    var ok =
        this.DefineRawSignalVariable(vname_rawsignal) &&
        this.DefineSignalVariable(vname_fast_signal) && 
        this.DefineAfidSlowSignalVariable(vname_slow_signal) && 
        this.DefineBaselineVariable(vname_baseline) &&
        this.DefineAfidTouchResetCounter(vname_afid_trc) &&
        this.DefineAfidReleaseResetCounter(vname_afid_rrc) &&
        this.DefineAfidIntegrationValueVariable(vname_afid_i) &&
        this.DefineAfidThresholdVariable(vname_reset_threshold);

    // scope variables
    var vars = [ 
        {"variable":vname_rawsignal, "visible":true, "y_block":0},
        {"variable":vname_fast_signal, "visible":true, "y_block":0 }, 
        {"variable":vname_slow_signal, "visible":true, "y_block":0 }, 
        {"variable":vname_baseline, "visible":true, "y_block":0 },
        {"variable":vname_afid_i, "visible":true, "y_block":1 },
        {"variable":vname_afid_trc, "visible":true, "y_block":2 },
        {"variable":vname_afid_rrc, "visible":true, "y_block":2 },
        {"variable":vname_reset_threshold, "visible":true, "y_block":3 }
    ]; 

    // scope traces color
    var trace_colors = [ 
        0xcfacdb,
        0x00ff00, 
        0x000000, 
        0x8B008B, 
        0xFF8C00, 
        0xff0000,
    ];

    // scope Y-blocks
    var yblocks = [
        { "laxis_label":"Input signals", "join_class":0, "laxis_min_auto":true, "laxis_max_auto":true },
        { "laxis_label":"AFID Integ.", "join_class":1, "laxis_min_auto":true, "laxis_max_auto":true },
        { "laxis_label":"AFID Resets", "join_class":2, "laxis_min_auto":true, "laxis_max_auto":true },
        { "laxis_label":"AFID Dynamic Threshold", "join_class":3, "laxis_min_auto":true, "laxis_max_auto":true }
    ];

    // scope definition
    var def = {};
    def["var_info"] = vars;
    def["yblock_info"] = yblocks;
    def["var_color"] = trace_colors;
    def["scope_period"] = 0.025;
    def["auto_delete"] = true;
    def["href"] = DOC_PATH+"group__electrodes.html#details";

    this.CreateScope(def);
}
NTElectrode_afid.prototype.GetKeydetGui = function()
{
    var str = "";
    str += "keydetectortype: "+this._keydettype;
    str += "<table id=\""+this._id+"keydet\">";
    str += "<tr><th>filt_st</th><th>integr_val</th><th>touch_trshold</th><th>touch_res_cnt</th><th>rel_res_cnt</th><th>ASC_max_resets</th>";
    str +="</tr>"
    str += "<tr><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td>"
    str +="</tr>"

    str += "</table>"
    return str;
}

NTElectrode_afid.prototype.UpdateGuiKeydetRaw = function(shieldding)
{
    var tbl = document.getElementById(this._id+shieldding+"keydet");
    var tmp;
    if(tbl)
    {
      var row = tbl.rows[1];
      row.cells[0].innerHTML = this.GetVar(shieldding + "keydetector_data->filter_state");
      row.cells[1].innerHTML = ToSign32(this.GetVar(shieldding + "keydetector_data->integration_value"));
      tmp = this.GetVar(shieldding + "keydetector_data->touch_threshold");
      row.cells[2].innerHTML = ToSign16(tmp);
      row.cells[3].innerHTML = this.GetVar(shieldding + "keydetector_data->touch_reset_counter");
      row.cells[4].innerHTML = this.GetVar(shieldding + "keydetector_data->release_reset_counter");
      tmp = this.GetVar(shieldding + "keydetector_data->asc.max_resets");
      row.cells[5].innerHTML = tmp;
    }
}
