/* NTControl_keypad class */

function NTControl_keypad(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_keypad_data");

    pcm_read_ptr(this._symbol + "->rom->control_params");
    pcm_define_symbol(this._symbol + "->rom->control_params", pcm.LastVariable_vValue, "nt_control_keypad*");
    
    debug_print("The Keypad Control has been created.", false);
}

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

NTControl_keypad.prototype.GetKeyAutorepRate = function()
{
   if(!pcm_read_var(this._symbol + "->data.autorepeat_rate", 2))
        return 0;
        
    return pcm.LastVariable_vValue;
}

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

NTControl.prototype.GetGuiTags = function()
{
  var str_grouped_electrodes = "images/icn_yes_hidden.png";

  // get maximum range
    if(pcm_read_var(this._symbol + "->rom->control_params->groups_size", 1) && pcm.LastVariable_vValue)
    {
        
        str_grouped_electrodes = "images/icn_yes.png";
    }

  var str = "";
  str = "<td><span id=" + this._gui_id0 + "st>?</span></td>"+
        "<td><span id=" +  this._gui_id0 + "autorep_rate>?</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 + "GroupE src=\""+str_grouped_electrodes+"\" style=\"height:24px;width:24px;\"></td>";
  return str;  
}

NTControl_keypad.prototype.GetGuiTagsLabels = function()
{
    var str = "";
    str = "<th>state</th><th>autorepeat_rate</th><th>touch</th><th>grouped electrodes</th>"
    return str;
}

NTControl_keypad.prototype.UpdateGui = function()
{
    var kst = this.GetKeyState();
    var kobj = document.getElementById(this._gui_id0+"st");
    
    if(kobj)
        kobj.innerHTML = "0x"+ kst.toString(16);
    kobj = document.getElementById(this._gui_id0+"autorep_rate");
    
    if(kobj)
        kobj.innerHTML = this.GetKeyAutorepRate().toString(10);

    kobj = document.getElementById(this._gui_id0+"Touch");    
    if(kobj)
    {
        kobj.src = (kst != 0) ? "images/icn_yes.png" : "images/icn_yes_hidden.png";         
    }

}

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

NTControl_keypad.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, keypad 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__keypad.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"); 
}

