/* NTControl_matrix class */

function NTControl_matrix(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(); // span
    // 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_matrix_data");

    pcm_read_ptr(this._symbol + "->rom->control_params");
    pcm_define_symbol(this._symbol + "->rom->control_params", pcm.LastVariable_vValue, "nt_control_matrix*");
    
    debug_print("The matrix Control has been created.", false);
}
/**********************************************************************************************************************/
NTControl_matrix.prototype.GetSizeHor= function()
{                                                 /**/
   if(!pcm_read_var(this._symbol + "->rom->control_params->touchpad_size_horizontal", 1))
        return 0;
        
    return pcm.LastVariable_vValue;
}
NTControl_matrix.prototype.GetSizeVer= function()
{                                                 /**/
   if(!pcm_read_var(this._symbol + "->rom->control_params->touchpad_size_vertical", 1))
        return 0;
        
    return pcm.LastVariable_vValue;
}

NTControl_matrix.prototype.GetPosX= function()
{
   if(!pcm_read_var(this._symbol + "->data.actual_position[0].x", 2))
        return 0;
        
    return pcm.LastVariable_vValue;
}

NTControl_matrix.prototype.GetPosY= function()
{
   if(!pcm_read_var(this._symbol + "->data.actual_position[0].y", 2))
        return 0;
        
    return pcm.LastVariable_vValue;
}

NTControl_matrix.prototype.GetPosX_2= function()
{
   if(!pcm_read_var(this._symbol + "->data.actual_position[1].x", 2))
        return 0;
        
    return pcm.LastVariable_vValue;
}

NTControl_matrix.prototype.GetPosY_2= function()
{
   if(!pcm_read_var(this._symbol + "->data.actual_position[1].y", 2))
        return 0;
        
    return pcm.LastVariable_vValue;
}

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


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

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

NTControl.prototype.GetGuiTags = function()
{
  var str = "";
  str = "<td><img id=" + this._gui_id0 + "Touch src=\"images/icn_yes_hidden.png\" style=\"height:24px;width:24px;\"></td>"+
        "<td><span id=" +  this._gui_id0 + "PosX>?</span></td>"+
        "<td><span id=" +  this._gui_id0 + "PosY>?</span></td>"+
        "<td><span id=" +  this._gui_id0 + "PosX_2>?</span></td>"+
        "<td><span id=" +  this._gui_id0 + "PosY_2>?</span></td>"+
        "<td><span id=" +  this._gui_id0 + "Gestcure>?</span></td>";//+
    //    "<td><span id=" +  this._gui_id0 + "Vizu>XXX<canvas id=\"myCanvas\" width=\"50\" height=\"50\" style=\"border:5px solid #d3d3d3;\"></span></td>";
   /*
var c = document.getElementById("myCanvas");//.style.background = "url('matrix.png')";
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(30, 30);
ctx.stroke();
*/
  return str;  
}

NTControl_matrix.prototype.GetGuiTagsLabels = function()
{
    var str = "";
    str = "<th>touch</th><th>Position X</th><th>Positon Y</th><th>Position X_2</th><th>Positon Y_2</th><th>Recognized gescure</th>"//<th>Vizual gescure</th>"
    return str;
}

NTControl_matrix.prototype.UpdateGui = function()
{
    var gest = this.GetRecognized_gesture();
    var posX = this.GetPosX();
    var posY = this.GetPosY();
    var posX_2 = this.GetPosX_2();
    var posY_2 = this.GetPosY_2();
    var hor = this.GetSizeHor();
    var ver = this.GetSizeVer();
    var Elstate =  this.GetElState();
    var first_touch = 0;
      


    kobj = document.getElementById(this._gui_id0+"Touch");    
    if(kobj)
    {
     kobj.src = Elstate ? "images/icn_yes.png" : "images/icn_yes_hidden.png"; 
            
    }
    kobj = document.getElementById(this._gui_id0+"PosX");    
    if(kobj)
    {
        if (Elstate)
        {
         kobj.innerHTML = posX.toString(10);
        }   else
             kobj.innerHTML = '?';
    }
 
        
    kobj = document.getElementById(this._gui_id0+"PosY");    
    if(kobj)
    {
    if (Elstate)
        {
         kobj.innerHTML = posY.toString(10);
        }   else
             kobj.innerHTML = '?';
    
    }
    
    kobj = document.getElementById(this._gui_id0+"PosX_2");    
    if(kobj)
    {
        if (Elstate)
        {
         kobj.innerHTML = posX_2.toString(10);
        }   else
             kobj.innerHTML = '?';
    }
 
        
    kobj = document.getElementById(this._gui_id0+"PosY_2");    
    if(kobj)
    {
    if (Elstate)
        {
         kobj.innerHTML = posY_2.toString(10);
        }   else
             kobj.innerHTML = '?';
    
    }

        
    kobj = document.getElementById(this._gui_id0+"Gestcure");    
    if(kobj)
    {
     if (!Elstate)
        {
         kobj.innerHTML = gest.toString(10);
        }   else
             kobj.innerHTML = '?';
    }
        

}

NTControl_matrix.prototype.DefineKeyStateVariable = function(vname)
{
    return pcm_define_variable(vname, this._symbol + "->data.last_state");
}

NTControl_matrix.prototype.OnLinkClicked = function()
{
    var pname = this._name + "_keys_state";
    var ok = this.DefineKeyStateVariable(pname);

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

    // up to 7 electrodes, matrix may have more
    var ec = this._electrodes.length < 7 ? this._electrodes.length : 7;  
    
    for(var e=0; e<this._electrodes.length; 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":"key state", "join_class":0, "laxis_min":0, "laxis_min_auto":false, "laxis_max_auto":true },
        { "laxis_label":"signal", "join_class":1, "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__matrix.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"); 
}

