Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- #gpio.pm
- #Copyright (c) 2012 Ben S. Eishen
- #This library is free software; you can redistribute it and/or
- #modify it under the terms of the GNU Lesser General Public
- #License as published by the Free Software Foundation; either
- #version 2.1 of the License, or (at your option) any later version.
- #This library is distributed in the hope that it will be useful,
- #but WITHOUT ANY WARRANTY; without even the implied warranty of
- #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- #Lesser General Public License for more details.
- package gpio;
- require Exporter;
- @ISA = qw(Exporter);
- @EXPORT_OK = qw(export, set_dir, set_value, get_value);
- use constant SYSFS_GPIO_DIR => '/sys/class/gpio';
- #gpio_export(24);
- #gpio_set_dir(24,1);
- #gpio_set_value(24,0);
- #print gpio_get_value(24);
- sub export{
- my ($gpio) = @_;
- open(FILE, ">@{[SYSFS_GPIO_DIR]}/export") || die "Could not open @{[SYSFS_GPIO_DIR]}/export for GPIO pin ${gpio}";
- print FILE $gpio;
- close(FILE);
- }
- sub unexport{
- my $gpio = @_;
- open(FILE, ">@{[SYSFS_GPIO_DIR]}/unexport") || die "Could not open @{[SYSFS_GPIO_DIR]}/unexport for GPIO pin ${gpio}";
- print FILE $gpio;
- close(FILE);
- }
- sub set_dir{
- my ($gpio,$out_flag) = @_;
- open(FILE, ">@{[SYSFS_GPIO_DIR]}/gpio${gpio}/direction") || die "Could not open file @{[SYSFS_GPIO_DIR]}/gpio${gpio}/direction for GPIO pin ${gpio}";
- if($out_flag){
- print FILE "out";
- }
- else{
- print FILE "in";
- }
- close(FILE);
- }
- sub set_value{
- my ($gpio, $value) = @_;
- open(FILE, ">@{[SYSFS_GPIO_DIR]}/gpio${gpio}/value") || die "Could not open file for setting the value, GPIO pin ${gpio}";
- if($value){
- print FILE 1;
- }
- else{
- print FILE 0;
- }
- close(FILE);
- }
- sub get_value{
- my ($gpio) = @_;
- my $value;
- open(FILE, "<@{[SYSFS_GPIO_DIR]}/gpio${gpio}/value") || die "Could not open file for fetching the value, GPIO pin ${gpio}";
- read(FILE, $value, 1);
- close(FILE);
- return $value;
- }
- sub set_edge{
- my ($gpio,$edge) = @_;
- open(FILE, ">@{[SYSFS_GPIO_DIR]}/gpio${gpio}/edge") || die "Could not open file for setting the edge, GPIO pin ${gpio}";
- print FILE $edge;
- close(FILE);
- }
- sub fd_open{
- my ($gpio) = @_;
- open(FILE, "<@{[SYSFS_GPIO_DIR]}/gpio${gpio}/value") || die "Could not open file, GPIO PIN ${gpio}";
- return FILE;
- }
- sub fd_close{
- my($fd) = @_;
- close($fd) || die "Could not close file!";
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement