#!/usr/bin/perl


# charger_lib.pm revision 0.02b

# useful subs:
#	 new($debug (opt.)) - create a new object, optionally with debuglevel $debug)

#	write_eeprom($address,$data) - write eeprom $address with data $data
#	read_eeprom($address)	     - read eeprom $address, returns data
#	write_with_verify($address,$data) - write eeprom and verify write ok


#	turn_off_led() 			- guess
#	turn_on_led()			- again, take a wild guess
#	stop_charger()			- no matter what it's doing

#        solve  - figure out charger calibration automatically, givin
#		  two arrays
#		  output is in y = m(x-b) + c form

#	write_eeprom() - write 

# I know there's a newer version of this around somewhere
# mostly I keep it around for the sample of ordinal RS232 stuff


package charger_lib;

use Device::SerialPort;


sub new {
	my %self = {};
	my $type = shift;
	my $debug = shift;
	my $baud = shift || 9600;
	my $ob;

	$ob = Device::SerialPort->new("/dev/ttyS1");
	$ob->baudrate($baud);
	$ob->parity("none");
	$ob->databits(8);
	$ob->stopbits(1);
	$ob->handshake("none");
	$ob->rts_active(No);
	$ob->dtr_active(No);
	$ob->read_const_time(500);
	$ob->read_char_time(800);
	$self->{debug} = $debug;
	$self->{delay} = 0.02;
	$ob->debug($self->{debug}) if($self->{debug} & 0x20);
	$ob->write_settings;

	$self->{ob} = $ob;
	$self->{unit} = 1;

	init_netdata($self);

	bless($self, $type);
	return($self);
}

sub set_delay {
	my $self = shift;
	my $delay = shift;
	$self->{delay} = $delay;
}

sub open_bus {
	my $self = shift;

	my $unit = $self->{unit};
	$self->{unit} = 0;
	$self->do_write_cmd(0x0);
	$self->{unit} = $unit;
}


sub init_netdata {
	my $self = shift;
	my $i;

	
	$self->{netdata} = {};
	
	$self->{netdata}->{cmd}{"1"} = "Ping";
	$self->{netdata}->{cmd}{"2"} = "Ping Reply";
	$self->{netdata}->{cmd}{"3"} = "Prep Write";
	$self->{netdata}->{cmd}{"4"} = "Prep Write OK";
	$self->{netdata}->{cmd}{"5"} = "Unknown Command";
	$self->{netdata}->{cmd}{"6"} = "Write";
	$self->{netdata}->{cmd}{"7"} = "Write OK";
	$self->{netdata}->{cmd}{"8"} = "Write Error";
	$self->{netdata}->{cmd}{"9"} = "Read";
	$self->{netdata}->{cmd}{"10"} = "Read OK";
	$self->{netdata}->{cmd}{"102"} = "System Failure";
	$self->{netdata}->{cmd}{"11"} = "Read raw sensor data";
	$self->{netdata}->{cmd}{"12"} = "Setup charger";
	$self->{netdata}->{cmd}{"13"} = "Engage charger";
	$self->{netdata}->{cmd}{"14"} = "Engage charger error";
	$self->{netdata}->{cmd}{"15"} = "Engage charger OK";
	$self->{netdata}->{cmd}{"16"} = "Command charger parameters";
	$self->{netdata}->{cmd}{"17"} = "Cmd setup phase (choose phase to engage)";
	$self->{netdata}->{cmd}{"18"} = "Cmd engage phase";


	# I skipped 13. For some reason it struck me as a funny thing to do -
	# I mean, would 13 in hex be lucky or unlucky?
	# Funny thought, eh?

	$self->{netdata}->{cmd}{"20"} = "Cmd engage phase OK";
	$self->{netdata}->{cmd}{"21"} = "Cmd manual charge control";
	$self->{netdata}->{cmd}{"22"} = "Cmd manual charge control OK";
	$self->{netdata}->{cmd}{"23"} = "Kill charger";
	$self->{netdata}->{cmd}{"24"} = "Turn On LED";
	$self->{netdata}->{cmd}{"25"} = "Turn Off LED";
	$self->{netdata}->{cmd}{"26"} = "Kill Charger Ok";
	$self->{netdata}->{cmd}{"27"} = "Reply with sensor data";
	$self->{netdata}->{cmd}{"28"} = "Massacre charger";
	$self->{netdata}->{cmd}{"29"} = "Reboot charger";
	$self->{netdata}->{cmd}{"30"} = "Read cooked values";
	$self->{netdata}->{cmd}{"31"} = "Read cooked values reply";

	$self->{netdata}->{cmd}{"223"} = "Read charger parameters reply";
	$self->{netdata}->{cmd}{"240"} = "Cascading read cooked";
	$self->{netdata}->{cmd}{"241"} = "Write block";
	$self->{netdata}->{cmd}{"242"} = "Write block error";
	$self->{netdata}->{cmd}{"243"} = "Write block okay";
	$self->{netdata}->{cmd}{"244"} = "Read block";
	$self->{netdata}->{cmd}{"245"} = "Read block error";
	$self->{netdata}->{cmd}{"246"} = "Read block ok";
	$self->{netdata}->{cmd}{"247"} = "Execute loader";
	$self->{netdata}->{cmd}{"248"} = "Execute loader error";
	
	$self->{netdata}->{d0}{"3"} = "Write hi addr +0x80 for ee";
	$self->{netdata}->{d1}{"3"} = "Write lo addr";
	$self->{netdata}->{d2}{"3"} = "Write Data";

	$self->{netdata}->{d0}{"6"} = "Write hi addr +0x80 for ee";
	$self->{netdata}->{d1}{"6"} = "Write lo addr";
	$self->{netdata}->{d2}{"6"} = "Write Data";

	$self->{netdata}->{d0}{"7"} = "Write hi addr +0x80 for ee";
	$self->{netdata}->{d1}{"7"} = "Write lo addr";
	$self->{netdata}->{d2}{"7"} = "Write Data";

	$self->{netdata}->{d0}{"9"} = "Read hi addr +0x80 for ee";
	$self->{netdata}->{d1}{"9"} = "Read lo addr";

	$self->{netdata}->{d0}{"10"} = "Returned Read Data";

	$self->{netdata}->{d0}{"12"} = "limit bvolts (0 = none)";
	$self->{netdata}->{d1}{"12"} = "limit bamps (0 = none)";
	$self->{netdata}->{d2}{"12"} = "charge_time_lo";
	$self->{netdata}->{d3}{"12"} = "charge_time_hi";
	$self->{netdata}->{d4}{"12"} = "pwm_out";
	$self->{netdata}->{d5}{"12"} = "charge_phase";

	$self->{netdata}->{d0}{"13"} = "limit bvolts (0 = none)";
	$self->{netdata}->{d1}{"13"} = "limit bamps (0 = none)";
	$self->{netdata}->{d2}{"13"} = "charge_time_lo";
	$self->{netdata}->{d3}{"13"} = "charge_time_hi";
	$self->{netdata}->{d4}{"13"} = "pwm_out";
	$self->{netdata}->{d5}{"13"} = "charge_phase";

	$self->{netdata}->{d0}{"15"} = "limit bvolts (0 = none)";
	$self->{netdata}->{d1}{"15"} = "limit bamps (0 = none)";
	$self->{netdata}->{d2}{"15"} = "charge_time_lo";
	$self->{netdata}->{d3}{"15"} = "charge_time_hi";
	$self->{netdata}->{d4}{"15"} = "pwm_out";
	$self->{netdata}->{d5}{"15"} = "charge_phase";

	$self->{netdata}->{d0}{"21"} = "PWM * 2 (255 = 60%)";
	$self->{netdata}->{d1}{"21"} = "Freq Hi Byte";
	$self->{netdata}->{d2}{"21"} = "Freq Lo Byte";

	$self->{netdata}->{d0}{"27"} = "RAW Analog input 0 / BatMod Temp";
	$self->{netdata}->{d1}{"27"} = "RAW Analog input 1 / Battery Temp";
	$self->{netdata}->{d2}{"27"} = "RAW Analog input 2 / Battery Volts";
	$self->{netdata}->{d3}{"27"} = "RAW Analog input 3 / Battery Amps";
	$self->{netdata}->{d4}{"27"} = "VFIN hi order byte";
	$self->{netdata}->{d5}{"27"} = "VFIN low order byte";

	$self->{netdata}->{d0}{"31"} = "current_bvolts";
	$self->{netdata}->{d1}{"31"} = "current_bamps";
	$self->{netdata}->{d2}{"31"} = "current_bwatts";
	$self->{netdata}->{d3}{"31"} = "current_btemp";
	$self->{netdata}->{d4}{"31"} = "current_bhv";
	$self->{netdata}->{d5}{"31"} = "current_errno";

	$self->{netdata}->{d0}{"223"} = "hold bvolts (0 = none)";
	$self->{netdata}->{d1}{"223"} = "hold bamps (0 = none)";
	$self->{netdata}->{d2}{"223"} = "charge_time_lo";
	$self->{netdata}->{d3}{"223"} = "charge_time_hi";
	$self->{netdata}->{d4}{"223"} = "pwm_out";
	$self->{netdata}->{d5}{"223"} = "charge_phase";

	
	$self->{netdata}->{addr}{"240"} = "Net addr plus1";
	$self->{netdata}->{d0}{"240"} = "bvolts";
	$self->{netdata}->{d1}{"240"} = "bamps";
	$self->{netdata}->{d2}{"240"} = "btemp";
	$self->{netdata}->{d3}{"240"} = "charge phase";
	$self->{netdata}->{d4}{"240"} = "pwm out (255 = 60%)";
	$self->{netdata}->{d5}{"240"} = "errno";

	$self->{netdata}->{d0}{"241"} = "address 0xFF00";
	$self->{netdata}->{d1}{"241"} = "address 0x00FF";
	$self->{netdata}->{d2}{"241"} = "data byte 0 (addr)";
	$self->{netdata}->{d3}{"241"} = "data byte 1 (addr+1)";
	$self->{netdata}->{d4}{"241"} = "data byte 2 (addr+2)";
	$self->{netdata}->{d5}{"241"} = "data byte 3 (addr+3)";

	$self->{netdata}->{d0}{"244"} = "address 0xFF00";
	$self->{netdata}->{d1}{"244"} = "address 0x00FF";

	$self->{netdata}->{d0}{"245"} = "address 0xFF00";
	$self->{netdata}->{d1}{"245"} = "address 0x00FF";
	$self->{netdata}->{d2}{"245"} = "data byte 0 (addr)";
	$self->{netdata}->{d3}{"245"} = "data byte 1 (addr+1)";
	$self->{netdata}->{d4}{"245"} = "data byte 2 (addr+2)";
	$self->{netdata}->{d5}{"245"} = "data byte 3 (addr+3)";

	$self->{netdata}->{d0}{"246"} = "address 0xFF00";
	$self->{netdata}->{d1}{"246"} = "address 0x00FF";
	$self->{netdata}->{d2}{"246"} = "revision";

	$self->{netdata}->{d0}{"247"} = "address 0xFF00";
	$self->{netdata}->{d1}{"247"} = "address 0x00FF";
	$self->{netdata}->{d2}{"247"} = "revision sent";
	$self->{netdata}->{d2}{"247"} = "revision current";
	$self->{netdata}->{d2}{"247"} = "software lock (121 to exec)";
	
	# provide lookup table for volts

	for($i=0;$i<256;$i++) {
		$self->{netdata}{d0}{"31"}{$i} = voltb_to_volt($self,$i);
	}
}


sub volt_to_voltb {
	my $self = shift;
	my $volt = shift;

	return 0 if($volt == 0);


	return(voltk_to_voltb($self,($volt * 1000)));
}

sub tempb_to_temp {
	my $self = shift;
	my $temp = shift;

	# 0 is -40
	# 40 is 0
	# 252 is 212

	return($temp - 40);
}

sub temp_to_tempb {
	my $self = shift;
	my $temp = shift;

	return(0) if($temp == 0);

	return($temp + 40);
}


sub hvb_to_hv {
	my $self = shift;
	my $hv = shift;
	
	return($hv * 2);
}

sub hv_to_hvb {
	my $self = shift;
	my $hv = shift;
	
	return(int($hv / 2));
}

sub voltk_to_voltb {
	my $self = shift;
	my $voltk = shift;

	# 0 = 5.12V
	# 255 = 17.87V

	return(0) if($voltk < 512);
	return(255) if($voltk > 17870);
	
	return(int(($voltk - 5120)/50));
}

sub voltb_to_voltk {
	my $self = shift;
	my $voltb = shift;

	return(5120 + (50*$voltb));
}

sub voltb_to_volt {
	my $self = shift;
	my $voltb = shift;
	return((voltb_to_voltk($self,$voltb) / 1000));
}

sub voltk_to_volt {
	my $self = shift;
	my $voltk = shift;

	return(($voltk / 1000));
}

sub ampb_to_amp {
	my $self = shift;
	my $voltb = shift;
	
	my $ret = ($voltb / 10);

	return($ret);
}

sub amp_to_ampb {
	my $self = shift;
	my $amp = shift;

	return(int($amp * 10));
}

sub charge {
	my $self = shift;
	my $amps = shift;
	my $volts = shift;
	my $chgtime = shift;
	my $flags = shift || 0;

	
	$ampsb = $self->amp_to_ampb($amps);
	$voltsb = $self->volt_to_voltb($volts);
	$chgtime_hi = ($chtime & 0xFF00) / 256;
	$chgtime_lo = $chgtime & 0x00FF;

	$self->do_write_cmd(0x0C,$voltsb,$ampsb,$chgtime_lo,$chgtime_hi,$flags);

	return -1 if($self->do_read(8) < 8);

	$self->do_write_cmd(0x0D,$voltsb,$ampsb,$chgtime_lo,$chgtime_hi,$flags);
	return -1 if($self->do_read(8) < 8);
	return -2 if($self->{read_cmd} != 0x0F);
	return 0;
}

sub show_inputs {
	my $self = shift;

	$self->do_write_cmd(0x0B);

	return -1 if $self->do_read < 8;
	
	# this is all it really takes - make sure you
	# have read debugging engaged
}

sub show_charge_state {
	my $self = shift;
	$self->do_write_cmd(0x10);
	return -1 if $self->do_read < 8;
}

sub show_cooked {
	my $self = shift;
	
	$self->do_write_cmd(0x1E);
	
	return -1 if $self->do_read < 8;

}


sub force_on_charger {
	my $self  = shift;
	my $pwm = shift;
	my $freq = shift;

	$self->do_write_cmd(0x15, $pwm, int($freq / 256), int($freq & 255));

	return -1 if $self->do_read() < 8;
	
	return -2 if($self->{read_cmd} != 0x16);
	
	return 0;
}

	
sub turn_on_led {

	my $self = shift;

	$self->do_write_cmd(0x18,42);
	
	return -1 if $self->do_read() < 8;
	return -2 if ($self->{read_cmd} != 0x0A);
	return 0;
}

sub turn_off_led {

	my $self = shift;

	$self->do_write_cmd(0x19,42);
	
	return -1 if $self->do_read() < 8;
	return -2 if ($self->{read_cmd} != 0x0A);
	return 0;
}

sub stop_charger {
	my $self = shift;
	
	$self->do_write_cmd(0x17);

	return -1 if $self->do_read() < 8;
	return -2 if ($self->{read_cmd} != 0x1A);
	return 0;
}
	

sub write_with_verify {
	my $self = shift;
	my $addr = shift;
	my $data = shift;

	print "write_with_verify()\n" if $self->{debug} & 4;

	$self->write_eeprom($addr,$data);
	return -1 if ($self->read_eeprom($addr) != $data);
	return 0;
}

		
sub set_unit {
	my $self = shift;
	$self->{unit} = shift || 1;
}

sub ping {
	my $self = shift;
	my $unit = $self->{unit};
	my $cmd = shift || 1;
	my $string, $len, $data, $foo;

	print "ping\n" if ($self->{debug} & 4);

	$self->do_write_cmd($cmd);

	return(0) if($self->do_read() < 8);

	if(($foo = $self->{read_cmd}) != 2) {
		print "Error: ping read unexpected result $foo\n" if($self{debug} & 4);
		return -2;
	}

	return $self->{read_addr};
}

sub read_ram {
	my $self = shift;
	my $addr = shift;

	my $addr_lo, $addr_hi;
	my $string, $len, $data, $foo;

	$addr_hi = ($addr & 0xFF00) / 256;
	$addr_lo = $addr & 0x00FF;

	print "read addr: $addr_hi $addr_lo\n" if($self->{debug} & 4);

	$self->do_write_cmd(0x09,$addr_hi,$addr_lo);
	return -1 if ($self->do_read() < 8);
	return($self->{read_d0});
}

sub write_eeprom_int {
	my $self = shift;
	my $addr = shift;
	my $data = shift;
	my $negative = 0;


	my ($data_lo, $data_hi);
	my $ret;

	if($data < 0) {
		$data = $data * -1;
		$negative = 1;
	}
	
	$data_lo = int($data / 256);
	$data_hi = $data % 256;

	if($negative) {
		$data_hi = 0xFF - $data_hi;
		$data_lo = 0xFF - $data_lo;
	}

 	$ret =	$self->write_eeprom($addr+1, $data_lo);
	$ret =+ $self->write_eeprom($addr,$data_hi);

	return($ret);
}

sub read_eeprom_int {
	my $self = shift;
	my $addr = shift;
	my ($data_lo, $data_hi);

	$data_lo = $self->read_eeprom($addr+1);
	return -1 if($data_lo < 0);

	$data_hi = $self->read_eeprom($addr);
	return -1 if($data_hi < 0);

	if($data_lo & 0x80) {
		
		$res = (((((0xFF - $data_lo) * 256) + (0xFF - $data_hi)) * -1) );
	} else {
		$res = (($data_lo * 256) + $data_hi);
	}
	return($res);
}

sub write_eeprom {
	my $self = shift;
	my $addr = shift;
	my $data = shift;

	my ($addr_lo, $addr_hi, $new_addr);

	$addr_hi = ($addr & 0xFF00) / 256;
	$addr_lo = $addr & 0x00FF;
	
	$new_addr = (($addr_hi + 0x80) * 256) + $addr_lo;
	
	$ret = $self->write_ram($new_addr, $data);
	select(undef,undef,undef,0.10); # recovery time
	return($ret);
}	

sub read_eeprom {
	my $self = shift;
	my $addr = shift;

	my $addr_lo, $addr_hi, $new_addr;

	$addr_hi = ($addr & 0xFF00) / 256;
	$addr_lo = $addr & 0x00FF;
	
	$new_addr = (($addr_hi + 0x80) * 256) + $addr_lo;
	
	return($self->read_ram($new_addr));
}	

sub write_ram {
	my $self = shift;
	my $addr = shift;
	my $data = shift;

	my $addr_lo, $addr_hi, $string;

	$addr_hi = ($addr & 0xFF00) / 256;
	$addr_lo = $addr & 0x00FF;

	print "write eeprom addr $addr -> $addr_lo / $addr_hi\n" if ($self->{debug} & 4);

	# prepare a prep cmd

	$self->do_write_cmd(0x03,$addr_hi,$addr_lo,$data);

	return -1 if($self->do_read() < 8);

	print_data($self->{data}) if ($self->{debug} & 4);

	return -2 if($self->{read_cmd} != 4);
	
	$self->do_write_cmd(0x06,$addr_hi,$addr_lo,$data);

	print_data($self->{write_data}) if ($self->{debug} & 4);

	return -2 if($self->do_read() < 8);
	
	if(($foo = $self->{read_cmd}) != 0x07) {
		print "ERROR: Expected 0x07 got back $foo\n" if($self->{debug} & 4);
		return -3;
	}

	return(0);
}

sub do_read {
	my $self = shift;
	my $count = shift || 8;
	
	my($len,$data) = $self->{ob}->read(8);
	my @array;

	
	$self->{data} = $data;
	$self->{len} = $len;
	$self->print_data($self->{data},"read<") if ($self->{debug} & 1);
	@array = split(//,$data);
	
	$self->{"read_addr"} = ord(@array[0]);
	$self->{"read_cmd"} = ord(@array[1]);
	$self->{"read_d0"} = ord(@array[2]);
	$self->{"read_d1"} = ord(@array[3]);
	$self->{"read_d2"} = ord(@array[4]);
	$self->{"read_d3"} = ord(@array[5]);
	$self->{"read_d4"} = ord(@array[6]);
	$self->{"read_d5"} = ord(@array[7]);
	return($len);
}

	
	

sub print_data {
	my $self = shift;
	my $string = shift;
	my $prefix = shift || "";

	my @letters = split(//,$string);
	my $l = 0;
	my $o;
	my $p;
	my %purposes;
	my $wc;
	my $d0;
	my $buf;
	my $errno;

	$purpose{"0"} = "addr";
	$purpose{"1"} = "cmd";
	$purpose{"2"} = "d0";
	$purpose{"3"} = "d1";
	$purpose{"4"} = "d2";
	$purpose{"5"} = "d3";
	$purpose{"6"} = "d4";
	$purpose{"7"} = "d5";

	$self->{"written_addr"} = ord($letters[0]);
	$self->{"written_cmd"} = ord($letters[1]);
	$self->{"written_d0"} = ord($letters[2]);
	$self->{"written_d1"} = ord($letters[3]);
	$self->{"written_d2"} = ord($letters[4]);
	$self->{"written_d3"} = ord($letters[5]);
	$self->{"written_d4"} = ord($letters[6]);
	$self->{"written_d5"} = ord($letters[7]);

	foreach $letterz (@letters) {
		$o = ord($letterz);
		$p = $purpose{$l};
		$wc = ord($letters[1]); #cmd
		$dz = ord($letters[2]); #d0

		if(defined $self->{netdata}->{$p}{$wc}) {
			$n = $self->{netdata}->{$p}{$self->{"written_cmd"}};
		
			if($n =~ /bamps/) {
				$n .= "\t{";
				$buf = "written_" . $p;
				$n .= $self->ampb_to_amp($self->{$buf});
				$n .= "A }";
			}

			if($n =~ /bvolts/) {
				$n .= "\t{";
				$buf = "written_" . $p;
				$n .= $self->voltb_to_volt($self->{$buf});
				$n .= "V }";
			}
				
			if($n =~ /btemp/) {
				$n .= "\t{";
				$buf = "written_" . $p;
				$n .= $self->tempb_to_temp($self->{$buf});
				$n .= "F }";
			}
			
			if($n =~ /bwatts/) {
				$n .= "\t{";
				$buf = "written_" . $p;
				$n .= $self->{$buf};
				$n .= "W }";
			}

			if($n =~ /hv/) {
				$n .= "\t{";
				$buf = "written_" . $p;
				$n .= $self->hvb_to_hv($self->{$buf});
				$n .= "V }";
			}	
			
			if($n =~ /errno/) {
				$n .= "\t(";
				$buf = "written_" . $p;
				$errno = $self->{$buf};
				$n .= "OVRTMP " if($errno & 1);
				$n .= "UNDTMP " if($errno & 2);
				$n .= "UNCMD!  " if($errno & 4);
				$n .= "OVRVLT " if($errno & 8);
				$n .= "UNDVLT " if($errno & 16);
				$n .= "BOVTMP " if($errno & 32);
				$n .= "BUNTMP " if($errno & 64);
				$n .= "CRASH! " if($errno & 128);
				$n .= ")";
			}
				
			
		} else {
			$n = "";
		}
	
		printf("%s%x:\t%x\t(%d)\t[%s\t] %s\n",$prefix,$l,$o,$o,$p,$n) if($self->{debug} & 2);
		$l++;
	}
}


sub do_write_cmd {
	my $self = shift;

	print "do_write_cmd\n" if ($self->{debug} & 16);

	my $string = chr($self->{unit}) . chr(shift) . chr(shift || 0) . chr(shift || 0)
	   . chr(shift || 0) . chr(shift || 0) . chr(shift || 0) . chr(shift || 0);
	$self->do_write($string);
}

sub do_write {
	my $self = shift;
	my $string = shift;
	my @letters = split(//,$string);
	my $count = 0;
	my $ord;
	my $letter;
	my $purpose;
	my %purposes;

	$self->{write_data} = $string;

	if ($self->{debug} & 2) {
		$self->print_data($self->{write_data},"WRTE:");
	}

	$purpose{"0"} = "addr";
	$purpose{"1"} = "cmd";
	$purpose{"2"} = "d0";
	$purpose{"3"} = "d1";
	$purpose{"4"} = "d2";
	$purpose{"5"} = "d3";
	$purpose{"6"} = "d4";
	$purpose{"7"} = "d5";


	foreach $letter (@letters) {
		$ord = ord($letter);
		$purpose = $purposes{$count};
		print "$count: sending $ord [$purpose]\n" if($self->{debug} & 16);
		$count++;
		$self->{ob}->write($letter);
		select(undef,undef,undef,$self->{delay});
	}
}


sub solve {

	my $self = shift;
	my %params = @_;

	# x_array = \@array,
	# y_array = \@array

	$href = $self->find_best_mcb("m_res" => 100,
                   "c_res" => 1,
                   "b_res" => 10,
                   "m_start" => 1,
                   "m_stop" => 1000,
                   "c_start" => 1,
                   "c_stop" => 2,
                   "b_start" => 1,
                   "b_stop" => 1000,
                   "x_array" => $params{x_array},
                   "y_array" => $params{y_array});

	my $m_start = $href->{best_m} - 100;
	my $m_stop  = $href->{best_m} + 100;

	$href = $self->find_best_mcb("m_res" => 1,
		"c_res" => 1,
		"b_res" => 1,
		"m_start" => $m_start,
		"m_stop" => $m_stop,
		"c_start" => 1,
		"c_stop" => 2,
		"b_start" => 1,
		"b_stop" => 255,
		"x_array" => $params{x_array},
		"y_array" => $params{y_array});

	$m_start = $href->{best_m} - 5;
	$m_stop = $href->{best_m} + 5;

	my $b_start = $href->{best_b} - 5;
	my $b_stop = $href->{best_b} + 5;
	
	
   	$href = $self->find_best_mcb("m_res" => 1,
		"c_res" => 1,
		"b_res" => 1,
		"m_start" => $m_start,
		"m_stop" => $m_stop,
		"c_start" => 1,
		"c_stop" => 1000,
		"b_start" => $b_start,
		"b_stop" => $b_stop,
		"x_array" => $params{x_array},
		"y_array" => $params{y_array});


	return($href);
}

sub find_best_mcb {
	my $self = shift;
	my %params = @_;
	
	#options
	# x_array => \@array,
	# y_array => \@array,
	# m_res => 4,
	# c_res => 4,
	# b_res => 4,
	# m_start => 0,
	# m_stop => 1000,
	# c_start => 0,
	# c_stop => 1000,
	# b_start => 0,
	# b_stop => 1000

	#results are unpredictable if no seed values are givin

	#return value contains these options:

	# best_error
	# best_m
	# best_c
	# best_b

	my $besterror = 100000000; # seed besterror with some improbably large number
	my ($m, $c, $b, $best_m, $best_c, $best_b);
	my ($m_start, $m_stop, $m_res);
	my ($b_start, $b_stop, $b_res);
	my ($c_start, $c_stop, $c_res);
	my ($error, $count, $max_count, $testresult);

	$m_start = $params{"m_start"} || 1;
	$m_stop = $params{"m_stop"} || 1000;
	$m_res = $params{"m_res"} || 1;
	
	$b_start = $params{"b_start"} || 1;
	$b_stop = $params{"b_stop"} || 1000;
	$b_res = $params{"b_res"} || 1;
	
	$c_start = $params{"c_start"} || 1;
	$c_stop = $params{"c_stop"} || 1000;
	$c_res = $params{"c_res"} || 1;

	$max_count = @{$params{"x_array"}};


for($m=$m_start;$m<$m_stop;$m+=$m_res) {
	for($b=$b_start;$b<$b_stop;$b+=$b_res) {
		for($c=$c_start;$c<$c_stop;$c+=$c_res) {

		$error = 0;
			
		for($count = 0;$count<$max_count;$count++) {
			
			$testresult = ((@{$params{"x_array"}}[$count] - $b) * $m) + $c;
			$error = $error + abs(@{$params{"y_array"}}[$count] - $testresult);
		}
		
		if(abs($error) < $besterror) {
			$best_b = $b;
			$best_m = $m;
			$best_c = $c;
			$besterror = $error;
		}
		
		
		}		
	}
}


	$ret = {};
	
	$ret{"best_b"} = $best_b;
	$ret{"best_c"} = $best_c;
	$ret{"best_m"} = $best_m;
	$ret{"x_array"} = $params{"x_array"};
	$ret{"y_array"} = $params{"y_array"};
	$ret{"best_error"} = $besterror;
	$ret{"max_count"} = $max_count;
	return(\%ret);
}

sub print_mcb_error {
	my $self = shift;
	my %params = @_;
	
	my ($count, $max_count, $r, $c, $a, $e);

	my ($best_m, $best_c, $best_b);

	$best_m = $params{'best_m'};
	$best_c = $params{'best_c'};
	$best_b = $params{'best_b'};
	$max_count = $params{'max_count'};

	for($count=0;$count<$max_count;$count++) {
        $r = @{$params{x_array}}[$count];
        $c = ((@{$params{x_array}}[$count] - $best_b) * $best_m) + $best_c;
        $a = @{$params{y_array}}[$count];
        $e = $a - $c;

	open(LOG,">>ampcal.log"); # todo: make this more OO
        print "Sensor value: $r Calculated value: $c Actual: $a Error: $e\n" if($self->{debug} & 4);
        print LOG "sensor value: $r Calculated value: $c Actual: $a Error: $e\n";
	close(LOG);
	}
}

1;
