/ src / hal / components / message.comp
message.comp
  1  /********************************************************************
  2  * Description:  message.comp
  3  *               Message HAL component.
  4  *
  5  * Author: Les Newell <les at sheetcam dot com>
  6  * License: GPL Version 2 or later
  7  *    
  8  * Copyright (c) 2011 All rights reserved.
  9  *
 10  ********************************************************************
 11   *
 12   * This program is free software; you can redistribute it and/or
 13   * modify it under the terms of version 2 or later of the GNU General
 14   * Public License as published by the Free Software Foundation.
 15   * This library is distributed in the hope that it will be useful,
 16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18   * GNU General Public License for more details.
 19   *
 20   * You should have received a copy of the GNU General Public
 21   * License along with this library; if not, write to the Free Software
 22   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 23   *
 24   * THE AUTHORS OF THIS LIBRARY ACCEPT ABSOLUTELY NO LIABILITY FOR
 25   * ANY HARM OR LOSS RESULTING FROM ITS USE.  IT IS _EXTREMELY_ UNWISE
 26   * TO RELY ON SOFTWARE ALONE FOR SAFETY.  Any machinery capable of
 27   * harming persons must have provisions for completely removing power
 28   * from all motors, etc, before persons enter any danger area.  All
 29   * machinery must be designed to comply with local and national safety
 30   * codes, and the authors of this software can not, and do not, take
 31   * any responsibility for such compliance.
 32   *
 33   * This code was written as part of the EMC HAL project.  For more
 34   * information, go to www.linuxcnc.org.
 35   *
 36  *************************************************************************/
 37   
 38  component message "Display a message";
 39   
 40  description """Allows HAL pins to trigger a message. Example hal commands:
 41   loadrt message names=oillow,oilpressure,inverterfail messages="Slideway oil low,No oil
 42  pressure,Spindle inverter fault"
 43   addf oillow servo-thread
 44   addf oilpressure servo-thread
 45   addf inverterfail servo-thread
 46   
 47   setp oillow.edge 0 #this pin should be active low
 48   net no-oil classicladder.0.out-21 oillow.trigger
 49   net no-pressure classicladder.0.out-22 oilpressure.trigger
 50   net no-inverter classicladder.0.out-23 inverterfail.trigger
 51   
 52  When any pin goes active, the corresponding message will be displayed.""";
 53   
 54  pin in bit trigger =FALSE "signal that triggers the message";
 55  pin in bit force =FALSE """A FALSE->TRUE transition forces the message to be
 56  displayed again if the trigger is active""";
 57   
 58  param rw bit edge =TRUE """Selects the desired edge: TRUE means falling, FALSE
 59  means rising""";
 60  
 61  modparam dummy messages """The messages to display. These should be listed,
 62  comma-delimited, inside a single set of quotes. See the "Description" section
 63  for an example.
 64  If there are more messages than "count" or "names" then the excess will be
 65  ignored. If there are fewer messages than "count" or "names" then an error will
 66  be raised and the component will not load.""";
 67   
 68  variable int myidx;
 69  variable hal_bit_t prev_trigger = FALSE;
 70  variable hal_bit_t prev_force = TRUE;
 71  variable hal_bit_t prev_edge = TRUE;
 72   
 73  option extra_setup yes;
 74   
 75  function _ nofp "Display a message";
 76  license "GPL v2";
 77  ;;
 78   
 79  char *messages[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
 80  RTAPI_MP_ARRAY_STRING(messages, 16, "Displayed strings");
 81   
 82  FUNCTION(_){
 83      hal_bit_t show = false;    
 84      if(!!prev_edge != !!edge) /* edge type has changed */
 85      {
 86          prev_edge = edge;
 87          prev_trigger = !edge;
 88      }
 89      if(!!force != !!prev_force) /* force type has changed */
 90      {
 91          prev_force = force;
 92          if(force && (!!trigger == !!edge))
 93          {
 94              show = true;
 95          }
 96      }
 97      if(!!trigger != !!prev_trigger) /* trigger has changed */
 98      {
 99          prev_trigger = trigger;
100          if(!!trigger == !!edge)
101          {
102              show = true;
103          }
104      }
105      if(show && (messages[myidx] != 0))
106      {
107          rtapi_print_msg(RTAPI_MSG_ERR, "%s\n", messages[myidx]);
108      }
109  }
110   
111  EXTRA_SETUP(){
112      myidx = extra_arg;
113      if(myidx<0 || myidx >15)
114      {
115          rtapi_print_msg(RTAPI_MSG_ERR,"Count of names= is outside the allowable range 0..15\n");
116          return -EINVAL;
117      }
118      if(messages[myidx] == 0)
119      {
120          rtapi_print_msg(RTAPI_MSG_ERR,"Message string for index %d missing\n", myidx);
121          return -EINVAL;
122      }
123      return(0);
124  }