script/lsec
author Florian Pose <fp@igh-essen.com>
Mon, 20 Aug 2007 14:04:29 +0000
changeset 703 daaa2e8ae232
parent 643 78929d878b2c
child 766 97e98cbd0cd2
permissions -rwxr-xr-x
Added EoE helpers.
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
#!/usr/bin/perl
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
#  l s e c  -  List EtherCAT
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
#  Userspace tool for listing EtherCAT slaves.
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
#  $Id$
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
#  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
#  This file is part of the IgH EtherCAT Master.
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
#  The IgH EtherCAT Master is free software; you can redistribute it
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
#  and/or modify it under the terms of the GNU General Public License
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
#  as published by the Free Software Foundation; either version 2 of the
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
#  License, or (at your option) any later version.
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
#  The IgH EtherCAT Master is distributed in the hope that it will be
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
#  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
#  GNU General Public License for more details.
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
#  You should have received a copy of the GNU General Public License
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
#  along with the IgH EtherCAT Master; if not, write to the Free Software
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
#  The right to use EtherCAT Technology is granted and comes free of
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
#  charge under condition of compatibility of product made by
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
#  Licensee. People intending to distribute/sell products based on the
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
#  code, have to sign an agreement to guarantee that products using
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
#  software based on IgH EtherCAT master stay compatible with the actual
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
#  EtherCAT specification (which are released themselves as an open
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#  standard) as the (only) precondition to have the right to use EtherCAT
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#  Technology, IP and trade marks.
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
require 'sys/ioctl.ph';
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
use strict;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
use Getopt::Std;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
my %opt;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
my $master_index;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
my $term_width;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
$term_width = &get_terminal_width;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
&get_options;
576
158c5a3d0a2a New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
Florian Pose <fp@igh-essen.com>
parents: 563
diff changeset
    53
&query;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
exit 0;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
576
158c5a3d0a2a New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
Florian Pose <fp@igh-essen.com>
parents: 563
diff changeset
    58
sub query
158c5a3d0a2a New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
Florian Pose <fp@igh-essen.com>
parents: 563
diff changeset
    59
{
158c5a3d0a2a New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
Florian Pose <fp@igh-essen.com>
parents: 563
diff changeset
    60
    my $master_dir;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
    my $dirhandle;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
    my $entry;
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    63
    my $slave_info_file;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
    my @slaves;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
    my $slave;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
    my $abs;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
    my $line;
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    68
    my $ring_col_width;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    69
    my $alias_col_width;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
    my $fmt;
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    71
    my $width;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    72
    my $last_alias;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    73
    my $alias_index;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
576
158c5a3d0a2a New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
Florian Pose <fp@igh-essen.com>
parents: 563
diff changeset
    75
    $master_dir = "/sys/ethercat/master" . $master_index;
158c5a3d0a2a New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
Florian Pose <fp@igh-essen.com>
parents: 563
diff changeset
    76
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
    unless (opendir $dirhandle, $master_dir) {
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    78
        print "Failed to open directory \"$master_dir\".\n";
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    79
        exit 1;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
    }
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
    while ($entry = readdir $dirhandle) {
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
        next unless $entry =~ /^slave(\d+)$/;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    85
        $slave = {};
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    86
        $slave_info_file = "$master_dir/$entry/info"; 
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    87
        open INFO, $slave_info_file or die
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    88
            "ERROR: Failed to open $slave_info_file.";
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    89
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    90
        while ($line = <INFO>) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    91
            if ($line =~ /^Name: (.*)$/) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    92
                $slave->{'name'} = $1;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    93
            }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    94
            elsif ($line =~ /^Ring position: (\d+)$/) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    95
                $slave->{'ring_position'} = $1;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    96
            }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    97
            elsif ($line =~ /Configured station alias: .* \((\d+)\)$/) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    98
                $slave->{'alias'} = $1;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    99
            }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   100
            elsif ($line =~ /^State: (.+) /) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   101
                $slave->{'state'} = $1;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   102
            }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   103
            elsif ($line =~ /^Current consumption: (-?\d+) mA$/) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   104
                $slave->{'current'} = $1;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   105
            }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   106
        }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   107
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   108
        close INFO;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   109
        push @slaves, $slave;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
    }
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
    closedir $dirhandle;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
    @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   115
    # create field addresses and calculate column widths
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   116
    $ring_col_width = 0;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   117
    $alias_col_width = 0;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   118
    $last_alias = "";
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
    for $slave (@slaves) {
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   120
        if ($slave->{'alias'}) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   121
            $last_alias = $slave->{'alias'};
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   122
            $alias_index = 0;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   123
        }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   124
        if ($last_alias) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   125
            $slave->{'field_address'} = "#" . $last_alias . ":" . $alias_index;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   126
            $width = length $slave->{'field_address'};
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   127
            $alias_col_width = $width if ($width > $alias_col_width);
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   128
        }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   129
        $width = length $slave->{'ring_position'};
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   130
        $ring_col_width = $width if ($width > $ring_col_width);
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   131
        $alias_index++;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
    }
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
    if (defined $opt{'c'}) { # display power consumtion
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   135
        $fmt = sprintf " %%%is  %%%is  %%6i  %%6i  %%s\n",
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   136
            $ring_col_width, $alias_col_width;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   137
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   138
        my $current_sum = 0;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   139
        for $slave (@slaves) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   140
            &print_line if $slave->{'alias'} and !defined $opt{n};
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   141
            $current_sum = 0 if $slave->{'current'} < 0;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   142
            $current_sum -= $slave->{'current'};
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   143
            printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'},
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   144
                    $slave->{'current'}, $current_sum, $slave->{'name'});
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   145
        }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   146
    }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   147
    else { # normal display
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   148
        $fmt = sprintf " %%%is  %%%is  %%-6s  %%s\n",
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   149
            $ring_col_width, $alias_col_width;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   150
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   151
        for $slave (@slaves) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   152
            &print_line if $slave->{'alias'} and !defined $opt{n};
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   153
            printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'},
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   154
                    $slave->{'state'}, $slave->{'name'});
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   155
        }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   156
    }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   157
}
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   158
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   159
#------------------------------------------------------------------------------
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   160
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   161
sub get_options
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   162
{
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   163
    my $optret = getopts "m:cnh", \%opt;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   164
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   165
    &print_usage if defined $opt{h} or $#ARGV > -1 or !$optret;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   166
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   167
    if (defined $opt{m}) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   168
        $master_index = $opt{m};
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
    }
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
    else {
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   171
        $master_index = 0;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   172
    }
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   173
}
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   175
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
sub print_usage
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   178
{
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
    my $cmd = `basename $0`;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
    chomp $cmd;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   181
    print "Usage: $cmd [OPTIONS]\n";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
    print "        -m <IDX>    Query master <IDX>.\n";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
    print "        -c          Display current [mA] ";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
    print "(3: consumption, 4: remaining).\n";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
    print "        -n          Display no coupler lines.\n";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
    print "        -h          Show this help.\n";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
    exit 0;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
}
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   191
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
sub get_terminal_width
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
{
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
    my $winsize;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
    die "no TIOCGWINSZ " unless defined &TIOCGWINSZ;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
    open(TTY, "+</dev/tty") or die "No tty: $!";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
    unless (ioctl(TTY, &TIOCGWINSZ, $winsize='')) {
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   198
        die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
    }
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
    (my $row, my $col, my $xpixel, my $ypixel) = unpack('S4', $winsize);
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
    return $col;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
}
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
sub print_line
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
{
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   207
    for (my $i = 0; $i < $term_width; $i++) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   208
        print "-";
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   209
    }
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
    print "\n";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
}
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
#------------------------------------------------------------------------------