Device-ParallelPort-JayCar

 view release on metacpan or  search on metacpan

JayCar.pm  view on Meta::CPAN

		$this->{PARPORT} = $parport;
	} elsif (defined($parport)) {
		$this->{PARPORT} = Device::ParallelPort->new($parport)
			or croak("Unable to create ParPort Device");
	} else {
		croak "Invalid parport provided";
	}

	# Store array of boards activated (ie: only update those boards)
	$this->{BOARDS} = ref($boards) eq "ARRAY" ? $boards : [$boards];

	$this->{RELAYS} = [];
	for (my $i = 0; $i < $this->RELAYS(); $i++) {
		$this->{RELAYS}[$i] = 0;
	}
}

sub _parport {
	my ($this) = @_;
	return $this->{PARPORT};
}

# Need: Relay Number (0-7)
# Return: True/False
# How: Must remember, can not get it from the board.
sub get {
	my ($this, $id) = @_;
	$this->_checkid($id);
	return $this->{RELAYS}[$id];
}

sub _checkid {
	my ($this, $id) = @_;
	if ($id < 0 || $id >= $this->RELAYS) {
		croak "Invalid relay id specified - $id";
	}
}

# Need: Ralay Number (0-7) (optionally delay update)
# Return: NA
# How: Update memory map bit, set whole byte, flash with Board ID
sub on {
	my ($this, $id, $delay) = @_;
	$this->_checkid($id);
	$this->{RELAY}[$id] = 1;
	$this->update unless ($delay);
}

# See relay_on
sub off {
	my ($this, $id, $delay) = @_;
	$this->_checkid($id);
	$this->{RELAY}[$id] = 0;
	$this->update unless ($delay);
}

# Update the device.
# Need: NA
# Return: NA
# How: Use parport to update byte and then flash it.
sub update {
	my ($this) = @_;

	foreach my $board (@{$this->{BOARDS}}) {
		$this->_parport->set_byte(2, chr($cardmap{$board}));		# Prepare
		$this->_parport->set_byte(0, chr($this->_byte_calc($board)));	# Set data
		$this->_parport->set_byte(2, chr($cardmap{$board} + 1));	# Flash bit 1 (strobe)
		$this->_parport->set_byte(2, chr($cardmap{$board}));		# Undo flash/strobe
	}
}

# Add bits together and return as integer.
# Need: NA (uses stored data)
# Return: Integer representing byte
# How: Add bits together as a byte (only those turned on)
sub _byte_calc {
        my ($this, $board) = @_;
        my $ret = 0;
        for (my $i = 0; $i < 8; $i++) {
		if ($this->{RELAY}[$i + ($board * 8)]) {
			$ret = $ret + (1 << $i);
		}
        }
        return $ret;
}

1;



( run in 1.883 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )