/******************************************************************************
 *
 * Copyright (C) 2015 Alexander Rössler
 *
 *
 * This module resets a IO signal
 *
 ******************************************************************************
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * THE AUTHORS OF THIS PROGRAM ACCEPT ABSOLUTELY NO LIABILITY FOR
 * ANY HARM OR LOSS RESULTING FROM ITS USE.  IT IS _EXTREMELY_ UNWISE
 * TO RELY ON SOFTWARE ALONE FOR SAFETY.  Any machinery capable of
 * harming persons must have provisions for completely removing power
 * from all motors, etc, before persons enter any danger area.  All
 * machinery must be designed to comply with local and national safety
 * codes, and the authors of this software can not, and do not, take
 * any responsibility for such compliance.
 *
 * This code was written as part of the LinuxCNC project.  For more
 * information, go to www.linuxcnc.org.
 *
 ******************************************************************************/

component reset "Resets a IO signal";
pin in  bit     trigger             "Trigger input";
pin io  u32     out_u32      = 0    "Unsigned 32 bit integer output value";
pin in  u32     reset_u32    = 0    "Unsigned 32 bit integer reset value";
pin io  s32     out_s32      = 0    "Signed 32 bit integer output value";
pin in  s32     reset_s32    = 0    "Signed 32 bit integer reset value";
pin io  float   out_float    = 0.0  "Float output value";
pin in  float   reset_float  = 0.0  "Float reset value";
pin io  bit     out_bit      = false  "Bit integer output value";
pin in  bit     reset_bit    = false  "Bit reset value";
pin in  bit     retriggerable = true  "Allow additional edges to reset";
pin in  bit     rising        = true  "Trigger on rising edge";
pin in  bit     falling       = false "Trigger on falling edge";
function _  fp "Update the output value";
description """
Component to reset IO signals.
""";
license "GPL";
variable hal_bit_t last_trigger = false;
;;
FUNCTION(_) {
    if (((rising && (trigger)) || (falling && (!trigger)))
       && (trigger != last_trigger))
    {
        out_u32 = reset_u32;
        out_s32 = reset_s32;
        out_float = reset_float;
        out_bit = reset_bit;
    }
    last_trigger = trigger;

    return 0;
}
