/********************************************************************
* Description:  messagev2.icomp
*               Message HAL component.
*
* Author: Les Newell <les at sheetcam dot com>
* Copyright (c) 2011 All rights reserved.
* License: GPL Version 2 or later
* 
*   Adapted and altered for instantiated component
*   by ArcEye <arceye@mgwareDOTcoDOTuk> 2015
*
*   newinst message low_warn -- "Oil Low" "Coolant Low" "Lunch Is Ready"
*   setp low_warn.message-num 0 (default message anyway)
*   setp low_warn.message-num 1
*   setp low_warn.message-num 2
*    
*************************************************************************/
 
component messagev2 "Display a message";
 
description "Allows HAL pins to trigger a message.";

pin_ptr in bit trigger = false   "signal that triggers the message";
pin_ptr in bit force = false     "FALSE->TRUE transition forces message redisplay";
pin_ptr io bit edge = true "Selects desired edge: TRUE = falling, FALSE = rising";
pin_ptr in s32 message_num = 0   "Required message number";

variable hal_bit_t prev_trigger = false;
variable hal_bit_t prev_force = true;
variable hal_bit_t prev_edge = true;

option extra_inst_setup yes;
 
function _ nofp "Display a message when triggered";

license "GPL v2";
;;
 
 
FUNCTION(_)
{
hal_bit_t show = false;
    
    if(prev_edge != gb(edge)) // edge type has changed 
        {
        prev_edge = gb(edge);
        prev_trigger = !gb(edge);
        }
    if(gb(force) != prev_force) // force type has changed 
        {
        prev_force = gb(force);
        if(gb(force) && (gb(trigger) == gb(edge)))
            show = true;
        }
       
    if(gb(trigger) != prev_trigger) // trigger has changed 
        {
        prev_trigger = gb(trigger);
        if(gb(trigger) == gb(edge))
            show = true;
        }
    // once in the component function, print our local copy of the messages
    if(show && (gs(message_num) < local_argc))
        rtapi_print_msg(RTAPI_MSG_ERR, "%s\n", local_argv(gs(message_num)));
    
    return 0;
}
 
EXTRA_INST_SETUP()
{
hal_s32_t i = 0;

    // demonstrate that both argv and local_argv hold same
    
    for(i = 0; i < local_argc; i++)
        rtapi_print_msg(RTAPI_MSG_ERR, "%s", argv[i]);
       
    for(i = 0; i < local_argc; i++)
        rtapi_print_msg(RTAPI_MSG_ERR, "%s", local_argv(i));

    return 0;

}
