#!/usr/bin/perl

use strict;
use warnings;
use Fcntl;
use Time::HiRes qw( sleep );

write_byte(1292, 127);
sleep 0.1;
write_byte(1292, 250);

sub write_byte {
  my $port = shift;
  my $data = shift;

  my $show = chr($data);

  sysopen (PORT,"/dev/port",O_RDWR) || die "error on /dev/port: $!";
  binmode (PORT);
  sysseek (PORT,$port,0)|| die "seek_err\n";
  syswrite(PORT,$show)  || print "write_err while writing $data \n";
  close (PORT);
}

sub read_byte {
  my $port = shift;
  my $data = shift;

  my $buf = "";

  sysopen (PORT,"/dev/port",O_RDWR) || die "error on /dev/port: $!";
  binmode (PORT);
  sysseek (PORT,$port,0);
  sysread(PORT,$buf, 1);
  close (PORT);

  return $buf;
}

sub dec2bin {
  my $str = unpack("B32", pack("N", shift));
  $str =~ s/^0+(?=\d)//;
  return $str;
}

