Advertisement
Guest User

Untitled

a guest
May 3rd, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.38 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #gpio.pm
  3. #Copyright (c) 2012 Ben S. Eishen
  4. #This library is free software; you can redistribute it and/or
  5. #modify it under the terms of the GNU Lesser General Public
  6. #License as published by the Free Software Foundation; either
  7. #version 2.1 of the License, or (at your option) any later version.
  8.  
  9. #This library is distributed in the hope that it will be useful,
  10. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. #Lesser General Public License for more details.
  13.  
  14. package gpio;
  15.  
  16. require Exporter;
  17. @ISA = qw(Exporter);
  18. @EXPORT_OK = qw(export, set_dir, set_value, get_value);
  19.  
  20. use constant SYSFS_GPIO_DIR => '/sys/class/gpio';
  21.  
  22. #gpio_export(24);
  23. #gpio_set_dir(24,1);
  24. #gpio_set_value(24,0);
  25. #print gpio_get_value(24);
  26.  
  27. sub export{
  28.     my ($gpio) = @_;
  29.     open(FILE, ">@{[SYSFS_GPIO_DIR]}/export") || die "Could not open @{[SYSFS_GPIO_DIR]}/export for GPIO pin ${gpio}";
  30.     print FILE $gpio;
  31.     close(FILE);
  32. }
  33.  
  34. sub unexport{
  35.     my $gpio = @_;
  36.     open(FILE, ">@{[SYSFS_GPIO_DIR]}/unexport") || die "Could not open @{[SYSFS_GPIO_DIR]}/unexport for GPIO pin ${gpio}";
  37.     print FILE $gpio;
  38.     close(FILE);
  39. }
  40.  
  41. sub set_dir{
  42.     my ($gpio,$out_flag) = @_;
  43.     open(FILE, ">@{[SYSFS_GPIO_DIR]}/gpio${gpio}/direction") || die "Could not open file @{[SYSFS_GPIO_DIR]}/gpio${gpio}/direction for GPIO pin ${gpio}";
  44.     if($out_flag){
  45.         print FILE "out";
  46.     }
  47.     else{
  48.         print FILE "in";
  49.     }
  50.     close(FILE);
  51. }
  52.  
  53. sub set_value{
  54.     my ($gpio, $value) = @_;
  55.     open(FILE, ">@{[SYSFS_GPIO_DIR]}/gpio${gpio}/value") || die "Could not open file for setting the value, GPIO pin ${gpio}";
  56.     if($value){
  57.         print FILE 1;
  58.     }
  59.     else{
  60.         print FILE 0;
  61.     }
  62.     close(FILE);
  63. }
  64.  
  65. sub get_value{
  66.     my ($gpio) = @_;
  67.     my $value;
  68.     open(FILE, "<@{[SYSFS_GPIO_DIR]}/gpio${gpio}/value") || die "Could not open file for fetching the value, GPIO pin ${gpio}";
  69.     read(FILE, $value, 1);
  70.     close(FILE);
  71.     return $value;
  72. }
  73.  
  74. sub set_edge{
  75.     my ($gpio,$edge) = @_;
  76.     open(FILE, ">@{[SYSFS_GPIO_DIR]}/gpio${gpio}/edge") || die "Could not open file for setting the edge, GPIO pin ${gpio}";
  77.     print FILE $edge;
  78.     close(FILE);
  79. }
  80.  
  81. sub fd_open{
  82.     my ($gpio) = @_;
  83.     open(FILE, "<@{[SYSFS_GPIO_DIR]}/gpio${gpio}/value") || die "Could not open file, GPIO PIN ${gpio}";
  84.     return FILE;
  85. }
  86.  
  87. sub fd_close{
  88.     my($fd) = @_;
  89.     close($fd) || die "Could not close file!";
  90. }
  91.  
  92. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement