Skip to content

Commit

Permalink
implemt get-all-input and get-all-output
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanstowe committed May 16, 2016
1 parent a0b371c commit db5d744
Showing 1 changed file with 105 additions and 24 deletions.
129 changes: 105 additions & 24 deletions lib/Device/Velleman/K8055.pm
Original file line number Diff line number Diff line change
Expand Up @@ -59,41 +59,122 @@ class Device::Velleman::K8055 {
MEM => -12
);

class Device is repr('CStruct') {
}


sub k8055_debug(bool $value) is native(LIB) { * }

sub k8055_open_device(int32 $port, Pointer[Device] $device) is native(LIB) returns int32 { * }
my class Device is repr('CPointer') {


sub k8055_close_device(Device $device) is native(LIB) { * }
sub k8055_close_device(Device $device) is native(LIB) { * }

method close() {
k8055_close_device(self);
}

sub k8055_set_all_digital(Device $device, int32 $bitmask) is native(LIB) returns int32 { * }

method set-all-digital(Int $bitmask where * < 256) returns Bool {
my $rc = k8055_set_all_digital(self, $bitmask);
$rc == SUCCESS;
}

sub k8055_set_all_digital(Device $device, int32 $bitmask) is native(LIB) returns int32 { * }
sub k8055_set_digital(Device $device, int32 $channel, bool $value) is native(LIB) returns int32 { * }

method set-digital(Int $channel where * < 8, Bool $v) returns Bool {
my $rc = k8055_set_digital(self, $channel, $v.value);
$rc == SUCCESS;
}

sub k8055_set_digital(Device $device, int32 $channel, bool $value) is native(LIB) returns int32 { * }
sub k8055_set_all_analog(Device $device, int32 $analog0, int32 $analog1) is native(LIB) returns int32 { * }

subset AnalogValue of Int where * < 256;

method set-all-analog(AnalogValue $analog0, AnalogValue $analog1) returns Bool {
my $rc = k8055_set_all_analog(self, $analog0, $analog1);
$rc == SUCCESS;
}

sub k8055_set_all_analog(Device $device, int32 $analog0, int32 $analog1) is native(LIB) returns int32 { * }
sub k8055_set_analog(Device $device, int32 $channel, int32 $value) is native(LIB) returns int32 { * }

method set-analog(Int $channel where 2 > * > 0, AnalogValue $value) returns Bool {
my $rc = k8055_set_analog(self, $channel, $value);
$rc == SUCCESS;
}

sub k8055_set_analog(Device $device, int32 $channel, int32 $value) is native(LIB) returns int32 { * }
sub k8055_reset_counter(Device $device, int32 $counter) is native(LIB) returns int32 { * }

method reset-counter(Int $counter where 2 > * > 0) returns Bool {
my $rc = k8055_reset_counter(self, $counter);
$rc == SUCCESS;
}

sub k8055_reset_counter(Device $device, int32 $counter) is native(LIB) returns int32 { * }
sub k8055_set_debounce_time(Device $device, int32 $counter, int32 $debounce) is native(LIB) returns int32 { * }

method set-debounce-time(Int $counter where 2 > * > 0, Int $debounce where * < 7450) returns Bool {
my $rc = k8055_set_debounce_time(self, $counter, $debounce);
$rc == SUCCESS;
}

sub k8055_set_debounce_time(Device $device, int32 $counter, int32 $debounce) is native(LIB) returns int32 { * }
sub k8055_get_all_input(Device $device, int32 $digitalBitmask is rw,
int32 $analog0 is rw,
int32 $analog1 is rw,
int32 $counter0 is rw,
int32 $counter1 is rw, bool $quick) is native(LIB) returns int32 { * }

method get-all-input(Bool :$quick = False) {
my int32 $digitalBitmask;
my int32 $analog0;
my int32 $analog1;
my int32 $counter0;
my int32 $counter1;
my $rc = k8055_get_all_input(self, $digitalBitmask, $analog0, $analog1, $counter0, $counter1, $quick.value);

if $rc != SUCCESS {
die "Failed to get inputs ({ Error($rc) })";
}
$digitalBitmask, $analog0, $analog1, $counter0, $counter1;
}

sub k8055_get_all_input(Device $device, Pointer[int32] $digitalBitmask,
Pointer[int32] $analog0,
Pointer[int32] $analog1,
Pointer[int32] $counter0,
Pointer[int32] $counter1, bool $quick) is native(LIB) returns int32 { * }
sub k8055_get_all_output(Device $device, int32 $digitalBitmask is rw,
int32 $analog0 is rw,
int32 $analog1 is rw,
int32 $debounce0 is rw,
int32 $debounce1 is rw) is native(LIB) { * }
method get-all-output() {
my int32 $digitalBitmask;
my int32 $analog0;
my int32 $analog1;
my int32 $debounce0;
my int32 $debounce1;
my $rc = k8055_get_all_output(self, $digitalBitmask, $analog0, $analog1, $debounce0, $debounce1);

if $rc != SUCCESS {
die "Failed to get inputs ({ Error($rc) })";
}
$digitalBitmask, $analog0, $analog1, $debounce0, $debounce1;
}
}

sub k8055_get_all_output(Device $device, Pointer[int32] $digitalBitmask,
Pointer[int32] $analog0,
Pointer[int32] $analog1,
Pointer[int32] $debounce0,
Pointer[int32] $debounce1) is native(LIB) { * }
sub k8055_open_device(int32 $port, Pointer $device is rw) is native(LIB) returns int32 { * }

method !open-device(Int :$port where { 0 <= $_ < 4 } = 0) returns Device {
my $p = Pointer[Device].new;
my $rc = k8055_open_device($port, $p);

if $rc != SUCCESS {
die "Cannot open device";
}
$p.deref;
}

sub k8055_debug(bool $value) is native(LIB) { * }

has Device $!device handles <set-all-digital set-digital set-all-analog set-analog reset-counter set-debounce-time get-all-input get-all-output>;

submethod BUILD(Int :$!address where 0 <= * < 4 = 0, Bool :$debug) {
$!device = self!open-device(:$!address);
if $debug {
k8055_debug(1);
}
}

}

# vim: expandtab shiftwidth=4 ft=perl6

0 comments on commit db5d744

Please sign in to comment.