#! /usr/bin/perl

# Name:			CEAmp.pm
# Author:		James M. Lynes, Jr.
# Created:		May 1, 2015
# Modified By:		James M. Lynes, Jr.
# Last Modified:	May 1, 2015
# Change Log:		5/1/2015 - Program Created
# Description:		Common Emitter Amplifier Designer
#				Support Module for wxPerl transistor amplifier designer - CEAmp1.pl
#

package CEAmp;
use strict;
use warnings;
use Data::Dumper;

my %defaults = (
		Av	=> 5,			# Desired Amplifier Voltage Gain
		Vcc	=> 12,			# Collector Voltage
		Vbe	=> .7,			# Base->Emitter Voltage
		Vceq	=> 5,			# Collector->Emitter Voltage at Quiessence
		VR1	=> 0,			# Voltage across VR1
		VR2	=> 0,			# Voltage across Vr2
		R1	=> 0,			# Bias Resistor R1
		R2	=> 0,			# Bias Resistor R2
		Rc	=> 0,			# Collector Resistor
		Re	=> 0,			# Emitter Resistor
		Rt	=> 0,			# Total of Bias Resistors
		Ib	=> 0,			# Base Current
		Ic	=> 0,			# Collector Current
		Ie	=> 0,			# Emitter Current
		Ir	=> 0,			# Bias Current
		Icq	=> .004,		# Desired Collector Current at Quiessence
		Beta	=> 150,			# Transistor DC Current Gain
		Cin	=> 10E-6,		# Input Coupling Capacitor - currently not implemented
		Cout	=> 10E-6,		# Output Coupling Capacitor - currently not implemented
		Transistor	=> "2N3904",	# Transistor Type
		);

sub design {
    my ($class, $self, $amp) = @_;

    $amp->Rt(($amp->Vcc - $amp->Vceq) / $amp->Icq);
    $amp->Re(($amp->Rt / ($amp->Av + 1)));
    $amp->Rc($amp->Rt - $amp->Re);

    $amp->Ib($amp->Icq / $amp->Beta);
    $amp->Ir($amp->Ib * 10);

    $amp->VR2($amp->Vbe + ($amp->Icq * $amp->Re));
    $amp->R2($amp->VR2 / $amp->Ir);
    $amp->VR1($amp->Vcc - $amp->VR2);
    $amp->R1($amp->VR1 / $amp->Ir);
}

package CEAmp::Data;
use strict;
use warnings;
use Class::Accessor::Fast;
use base qw(Class::Accessor::Fast);

__PACKAGE__->mk_accessors(qw(Av Vcc Vbe VR1 VR2 Vceq R1 R2 Rc Re Rt
                             Ib Ic Ie Ir Icq Beta Cin Cout Transistor));

sub new {shift->SUPER::new(@_, \%defaults);}
1;
