/* NTControl_proxi class */

function NTControl_proxi(symbol, addr)
{
    // call base when not typed dynamically (see change_class_type()) 
    // otherwise, the base object is already constructed
    if(!this._dynamically_typed)
        NTControl(symbol, addr)

    this._gui_id0 = get_unique_id(); // proxi image

    // define RAM type
    pcm_define_symbol(this._symbol, this._addr, "nt_control_data*");
    pcm_read_ptr(this._symbol + "->data");
    pcm_define_symbol(this._symbol + "->data", pcm.LastVariable_vValue, "nt_control_proxi_data");
    
    pcm_read_ptr(this._symbol + "->rom->control_params");
    pcm_define_symbol(this._symbol + "->rom->control_params", pcm.LastVariable_vValue, "nt_control_proxi*");
        
    pcm_define_variable_bit(this._name + "->flags_direction", this._symbol + "->flags", 16);
    pcm_define_variable_bit(this._name + "->flags_movement", this._symbol + "->flags", 17);
    pcm_define_variable_bit(this._name + "->flags_touch", this._symbol + "->flags", 18);

    // get maximum range
    if(pcm_read_var(this._symbol + "->rom->control_params->range", 4) && pcm.LastVariable_vValue)
    {
        this._range = pcm.LastVariable_vValue;
    }
}

NTControl_proxi.prototype.GetRange = function()
{
    return this._range;
}

NTControl_proxi.prototype.GetProximity = function()
{
     if(!pcm_read_var(this._symbol + "->data.proximity", 4))
        return 0;
        
    return pcm.LastVariable_vValue; 
}


NTControl_proxi.prototype.GetIndex = function()
{
   if(!pcm_read_var(this._symbol + "->data.index", 1))
        return 0;
        
    return pcm.LastVariable_vValue;
}


NTControl_proxi.prototype.GetGuiTag = function()
{
    var str = "";
    str = "<center><img src=\"images/proxi.png\" width=120px height=120px></center>";
    return str;
}

NTControl_proxi.prototype.GetGuiTagsLabels = function()
{
    var str = "";
    str = "<th>proximity</th><th>index</th><th>touch</th><th>movement</th><th>direction</th>"
    return str;
}

NTControl_proxi.prototype.GetGuiTags = function()
{
  var str = "";
  str = "<td><span id=" + this._gui_id0 + "pos>?</span></td>"+
        //"<td><span id=" +  this._gui_id0 + "range>?</span></td>"+
        "<td><span id=" +  this._gui_id0 + "ind>?</span></td>"+
        "<td><img id=" + this._gui_id0 + "Touch src=\"images/icn_yes_hidden.png\" style=\"height:24px;width:24px;\"></td>"+
        "<td><img id=" + this._gui_id0 + "Movement src=\"images/icn_arrow_move_hidden.png\" style=\"height:24px;width:24px;\">"+
        "</td><td><img id=" + this._gui_id0 + "Direction src=\"images/icn_dir_right.png\" style=\"height:24px;width:24px;\"></td>";
  return str;
}

NTControl_proxi.prototype.UpdateGui = function()
{
    var pos = this.GetProximity();
    var rng = this.GetRange();
    var flags = this.GetFlags();
    var ind = this.GetIndex();

    var kobj = document.getElementById(this._gui_id0+"pos");
    if(kobj)
        kobj.innerHTML = pos.toString(10);
/*
    kobj = document.getElementById(this._gui_id0+"range");    
    if(kobj)
        kobj.innerHTML = rng.toString(10);
*/

    kobj = document.getElementById(this._gui_id0+"ind");    
    if(kobj)
        kobj.innerHTML = ind.toString(10);


    kobj = document.getElementById(this._gui_id0+"Touch");    
    if(kobj)
    {
        kobj.src = (flags & (1<<18)) ? "images/icn_yes.png" : "images/icn_yes_hidden.png"; 
    }

    kobj = document.getElementById(this._gui_id0+"Movement");    
    if(kobj)
    {
        kobj.src = (flags & (1<<17)) ? "images/icn_arrow_move.png" : "images/icn_arrow_move_hidden.png"; 
    }

    kobj = document.getElementById(this._gui_id0+"Direction");    
    if(kobj)
    {
        kobj.src = (flags & (1<<16)) ? "images/icn_dir_right.png" : "images/icn_dir_left.png"; 
    }
            
}

NTControl_proxi.prototype.DefineProximityVariable = function(vname)
{
    return pcm_define_variable(vname, this._symbol + "->data.proximity");
}

NTControl_proxi.prototype.OnLinkClicked = function()
{
    var pname = this._name + "_proximity";
    var ok = this.DefineProximityVariable(pname);

    // scope variables
    var vars = [ 
        {"variable":pname, "visible":true, "y_block":0 } 
    ]; 

    var fname = this._name + "->flags_direction";
    var fdef = { "variable":fname, "visible":true, "y_block":2 };
    vars.push(fdef);

    fname = this._name + "->flags_movement";
    fdef = { "variable":fname, "visible":true, "y_block":2 };
    vars.push(fdef);

    fname = this._name + "->flags_touch";
    fdef = { "variable":fname, "visible":true, "y_block":2 };
    vars.push(fdef);

    // up to 4 electrode signals
    var el_count = this._electrodes.length;
    if(el_count > 4)
        el_count = 4;

    for(var e=0; e<el_count; e++)
    {
        var ename = this._name + "_electrode" + e + "_signal";
        ok = ok && this._electrodes[e].DefineSignalVariable(ename);
        
        var edef = { "variable":ename, "visible":true, "y_block":1 }; 
        vars.push(edef);
    }

    // scope Y-blocks
    var yblocks = [
        //{ "laxis_label":"proximity", "join_class":0, "laxis_min":0, "laxis_max":this.GetRange(), "laxis_min_auto":false, "laxis_max_auto":false },
        { "laxis_label":"proximity", "join_class":0, "laxis_min":0, "laxis_max":this.GetIndex(), "laxis_min_auto":false, "laxis_max_auto":false },
        { "laxis_label":"flags", "join_class":1, "laxis_min":0, "laxis_max":2, "laxis_min_auto":false, "laxis_max_auto":false },
        { "laxis_label":"signals", "join_class":2, "laxis_min_auto":true, "laxis_max_auto":true },
        
    ];

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

    var item = this._name + " Details";
    ok = ok && pcm.DefineScope(item, JSON.stringify(def));
    
    if(ok)
        pcm.SelectItem(item, "scope");
    else
        alert("Could not create graph with selected signals"); 
}

