Class: OrigenLink::Server::Pin
- Inherits:
-
Object
- Object
- OrigenLink::Server::Pin
- Defined in:
- lib/origen_link/server/pin.rb
Constant Summary collapse
- @@pin_setup =
{ in: 'in', out: 'out' }
Instance Attribute Summary collapse
-
#gpio_valid ⇒ Object
readonly
Returns the value of attribute gpio_valid.
Instance Method Summary collapse
- #destroy ⇒ Object
-
#in ⇒ Object
in: description - Reads and returns state of the pin.
-
#initialize(ionumber, direction = :in) ⇒ Pin
constructor
initialize: description - This method will execute system command “sudo echo ionumber > /sys/class/gpio/export” to create the IO file interface.
-
#out(value) ⇒ Object
out: description - Sets the output state of the pin.
- #to_s ⇒ Object
-
#update_direction(direction) ⇒ Object
update_direction: description - Sets the pin direction.
Constructor Details
#initialize(ionumber, direction = :in) ⇒ Pin
initialize:
description - This method will execute system command
"sudo echo ionumber > /sys/class/gpio/export"
to create the IO file interface. It will
set the direction, initial pin state and initialize
instance variables
ionumber - required, value indicating the pin number (BCM IO number,
not the header pin number)
direction - optional, specifies the pin direction. A pin is
initialized as an input if a direction isn't specified.
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 |
# File 'lib/origen_link/server/pin.rb', line 27 def initialize(ionumber, direction = :in) @ionumber = Integer(ionumber) @pin_dir_name = "#{Server.gpio_dir}/gpio#{@ionumber}/direction" @pin_val_name = "#{Server.gpio_dir}/gpio#{@ionumber}/value" if !File.exist?(@pin_dir_name) system("echo #{@ionumber} > #{Server.gpio_dir}/export") sleep 0.05 if $CHILD_STATUS == 0 @gpio_valid = true else @gpio_valid = false end else @gpio_valid = true end if @gpio_valid if File.writable?(@pin_dir_name) @pin_dir_obj = File.open(@pin_dir_name, 'w') update_direction(direction) else @gpio_valid = false puts "#{@pin_dir_name} is not writable. Fix permissions or run as super user." end @pin_val_obj = File.open(@pin_val_name, 'r+') if @gpio_valid end end |
Instance Attribute Details
#gpio_valid ⇒ Object (readonly)
Returns the value of attribute gpio_valid.
14 15 16 |
# File 'lib/origen_link/server/pin.rb', line 14 def gpio_valid @gpio_valid end |
Instance Method Details
#destroy ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/origen_link/server/pin.rb', line 54 def destroy if @gpio_valid @pin_dir_obj.close @pin_val_obj.close # system("echo #{@ionumber} > /sys/class/gpio/unexport") # puts "pin #{@ionumber} is no longer exported" end end |
#in ⇒ Object
in:
description - Reads and returns state of the pin. If the pin
is setup as an output, the direction will first
be changed to input.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/origen_link/server/pin.rb', line 83 def in if @gpio_valid if @direction == :out update_direction(:in) end # below is original read - slow to reopen every time # File.open(@pin_val_name, 'r') do |file| # file.read#.chomp # end # end original read @pin_val_obj.pos = 0 @pin_val_obj.getc end end |
#out(value) ⇒ Object
out:
description - Sets the output state of the pin. If the pin
is setup as an input, the direction will first
be changed to output.
68 69 70 71 72 73 74 75 76 |
# File 'lib/origen_link/server/pin.rb', line 68 def out(value) if @gpio_valid if @direction == :in update_direction(:out) end @pin_val_obj.write(value) @pin_val_obj.flush end end |
#to_s ⇒ Object
116 117 118 |
# File 'lib/origen_link/server/pin.rb', line 116 def to_s 'OrigenLinkPin' + @ionumber.to_s end |
#update_direction(direction) ⇒ Object
update_direction:
description - Sets the pin direction
direction - specifies the pin direction. A pin is
initialized as an input if a direction isn't specified.
Valid direction values:
:in - input
:out - output
107 108 109 110 111 112 113 114 |
# File 'lib/origen_link/server/pin.rb', line 107 def update_direction(direction) if @gpio_valid @pin_dir_obj.pos = 0 @pin_dir_obj.write(@@pin_setup[direction]) @pin_dir_obj.flush @direction = direction end end |