no warnings "portable";
use FileHandle;
use Data::Dumper;
+use Fcntl ':flock';
use lib '.';
use MDC;
###############################################################################
sub store_voltage {
my ($addr,$channel,$setting) = @_;
+ my @outfile;
+ my $filename = "../settings_power/settings_voltage.db";
+ my $found = 0;
+ return -1 if $setting < 0 || $setting > 7;
+
+ open FILE, $filename or die $!."\nsettings_voltage.db not found.";
+ while (my $a = <FILE>) {
+ if(my @values = $a =~ /^\s*0x(\w\w\w\w)\s+(\d)\s+(\d)(.*)$/) {
+ my $board = $values[0];
+ if (hex($board) == $addr) {
+ $values[1+$channel] = $setting;
+ $a = " 0x$values[0] $values[1] $values[2]$values[3]\n";
+ $found = 1;
+ }
+ }
+ push(@outfile,$a);
+ }
+ close FILE;
+
+ open(FILE,'>',$filename) or die $!;
+ flock(FILE, LOCK_EX);
+ print FILE join('',@outfile);
+ close FILE;
+ return $found;
}
###############################################################################
--- /dev/null
+#!/usr/bin/perl -w
+use warnings;
+no warnings "portable";
+use Getopt::Long;
+use Data::Dumper;
+use Time::HiRes qw(usleep);
+use lib '.';
+use MDC;
+use MDCPower;
+
+my $help;
+my $board;
+my $channel;
+my $setting;
+my $write;
+
+Getopt::Long::Configure(qw(gnu_getopt));
+GetOptions(
+ 'help|h' => \$help,
+ 'board|b=s' => \$board,
+ 'channel|c=i' => \$channel,
+ 'value|v=i' => \$setting,
+ 'write|w' => \$write,
+ ) ;
+
+
+if($help) {
+print <<HELP;
+mdc_voltage.pl (-board|-b) 0x8nnn [(-channel|c) (0|1)] [(-value|-v) (0-7)] [(-write|-w)]
+
+Reads the current voltage setting from configuration file.
+Sets the current voltage (optionally changes configuration file if -w is set).
+HELP
+}
+
+
+$board = hex($board);
+if($board < 0x8e00 || $board > 0x8fff) {
+ die "Wrong board address (8e00 - 8fff)\n";
+ }
+
+if(defined $channel && ($channel < 0 || $channel > 1)) {
+ die "Wrong channel number (0-1)\n";
+ }
+
+if(defined $setting && ($setting < 0 || $setting > 7)) {
+ die "Wrong setting number (0-7)\n";
+ }
+
+if(!defined $setting || !defined $channel) {
+ my ($v1,$v2) = MDCPower::get_voltage($board);
+ die "Settings not found\n" if $v1 == -1 && $v2 == -1;
+
+ print ("Board\tV0\tV1\n");
+ printf("%04x\t%u\t%u\n\n",$board,$v1,$v2)
+ }
+else {
+ print("Change Settings\n");
+ my $ret = MDCPower::set_voltage($board,$channel,$setting,0);
+ die "Invalid Settings\n" if $ret == -1;
+ die "Power Output not found\n" if $ret == -2;
+
+ if($write) {
+ print("Store Settings\n");
+ my $found = MDCPower::store_voltage($board,$channel,$setting);
+ print "ERROR: Board not found in database.\n" unless $found;
+ }
+ }
+