]> jspc29.x-matter.uni-frankfurt.de Git - hades_mdc_settings.git/commitdiff
add function to change voltage settings
authorJan Michel <mail@janmichel.eu>
Fri, 6 Jan 2023 16:11:50 +0000 (17:11 +0100)
committerJan Michel <mail@janmichel.eu>
Fri, 6 Jan 2023 16:11:50 +0000 (17:11 +0100)
add command line tool to change voltage settings

scripts/MDCPower.pm
scripts/mdc_voltage.pl [new file with mode: 0755]

index f2d761a88d97aa7a4b087a7f036f12f4fc645a5d..4bf4478ff4bcd1db20cc3624020dfbfa073d126b 100644 (file)
@@ -4,6 +4,7 @@ use warnings;
 no warnings "portable";
 use FileHandle;
 use Data::Dumper;
+use Fcntl ':flock';
 use lib '.';
 use MDC;
 
@@ -36,6 +37,30 @@ sub set_voltage {
 ###############################################################################
 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;
   }  
   
 ###############################################################################  
diff --git a/scripts/mdc_voltage.pl b/scripts/mdc_voltage.pl
new file mode 100755 (executable)
index 0000000..e6f1b20
--- /dev/null
@@ -0,0 +1,69 @@
+#!/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;
+    }
+  }
+