File: Device.pm

package info (click to toggle)
libhipi-perl 0.94-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,048 kB
  • sloc: perl: 471,918; ansic: 22; makefile: 10
file content (84 lines) | stat: -rw-r--r-- 2,224 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#########################################################################################
# Package        HiPi::Device
# Description  : Base class for /dev devices
# Copyright    : Copyright (c) 2013-2017 Mark Dootson
# License      : This is free software; you can redistribute it and/or modify it under
#                the same terms as the Perl 5 programming language system itself.
#########################################################################################

package HiPi::Device;

#########################################################################################

use strict;
use warnings;
use parent qw( HiPi::Class );
use HiPi qw( :rpi );
use Time::HiRes qw( usleep );
use Carp;

__PACKAGE__->create_accessors( qw( devicename ) );

our $VERSION ='0.81';

sub new {
    my ($class, %params) = @_;
    my $self = $class->SUPER::new(%params);
    return $self;
}

sub delay {
    my($class, $millis) = @_;
    usleep( int($millis * 1000));
}

sub delayMicroseconds {
    my($class, $micros) = @_;
    usleep( int($micros) );
}

*HiPi::Device::sleep_milliseconds = \&delay;
*HiPi::Device::sleep_microseconds = \&delayMicroseconds;

sub modules_are_loaded {
    my $class = shift;
    my $modulesloaded = 0;
    my $moduleoptions  = $class->get_required_module_options();
    my @lsmod= qx(lsmod);
    if( $?) {
        carp q(unable to determine if modules are loaded for HiPi::Device);
    } else {
        my %modules = map { (split(/\s+/, $_))[0..1] } @lsmod;
        for my $optionlist ( @$moduleoptions ) {
            my $thislistgood = 1;
            for my $module ( @$optionlist ) {
                unless( exists($modules{$module}) ) {
                    $thislistgood = 0;
                    last;
                }
            }
            if( $thislistgood) {
                # we found an option where required 
                # modules are loaded so we are good
                $modulesloaded = 1;
            }
        }
    }
    return $modulesloaded;
}

sub get_required_module_options {
    return [ [ qw( override in derived class with module list ) ] ];
}

sub close { 1; }

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY;
    $self->close;
}

1;

__END__