#!/bin/env perl

# Load CA module
use strict;
use lib '/APSshare/epics/base-3.14.12.4/lib/perl';
use CA;

# Get my user-name
my $user = $ENV{USER};

my $debug = 1;
my $exit;

# This adds a Control-C handler for orderly shutdown
$SIG{INT} = \&interrupt;

# Connect a PV
my $chan = CA->new("$user:PV", \&connection);
CA->flush_io;

# Process CA events until told to stop
CA->pend_event(0.1) until $exit;

exit 0;

# ------------- Callback subroutines -------------

# Connect/disconnect handler
sub connection {
    my ($chan, $up) = @_;
    my $name = $chan->name;
    printf "Callback connection('%s', %d)\n", $name, $up if $debug;

    # Add code here ...
    # ...   $chan->create_subscription('v', \&value);
}

# New value handler
sub value {
    my ($chan, $status, $data) = @_;
    my $name = $chan->name;
    print "value($name, $status, $data)\n" if $debug;

    if ($status) {
        printf "%-30s %s\n", $chan->name, $status;
    } else {
        # Add code here...
    }
}

# Control-C handler
sub interrupt {
    print " -- Callback interrupt --\n" if $debug;
    $exit = 1;
}
