Class: Artoo::Adaptors::IO::DigitalPin
- Inherits:
-
Object
- Object
- Artoo::Adaptors::IO::DigitalPin
- Defined in:
- lib/artoo/adaptors/io/digital_pin.rb
Constant Summary collapse
- GPIO_PATH =
"/sys/class/gpio"
- GPIO_DIRECTION_READ =
"in"
- GPIO_DIRECTION_WRITE =
"out"
- HIGH =
1
- LOW =
0
Instance Attribute Summary collapse
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#pin_file ⇒ Object
readonly
Returns the value of attribute pin_file.
-
#pin_num ⇒ Object
readonly
Returns the value of attribute pin_num.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#close ⇒ Object
Unexports the pin in GPIO to leave it free.
-
#digital_read ⇒ Object
Reads digitally from the specified pin on initialize.
-
#digital_write(value) ⇒ Object
Writes to the specified pin Digitally accepts values :high or :low, 1 or 0, "1" or "0".
-
#initialize(pin_num, mode) ⇒ DigitalPin
constructor
A new instance of DigitalPin.
-
#off! ⇒ Object
Sets digital write for the pin to LOW.
- #off? ⇒ Boolean
-
#on! ⇒ Object
Sets digital write for the pin to HIGH.
- #on? ⇒ Boolean
-
#set_mode(mode) ⇒ Object
Sets the pin in GPIO for read or write.
- #set_pin(settings) ⇒ Object
Constructor Details
#initialize(pin_num, mode) ⇒ DigitalPin
Returns a new instance of DigitalPin.
13 14 15 16 17 18 19 20 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 13 def initialize(pin_num, mode) @pin_num = pin_num File.open("#{ GPIO_PATH }/export", "w") { |f| f.write("#{ pin_num }") } File.close # Sets the pin for read or write set_mode(mode) end |
Instance Attribute Details
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
5 6 7 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 5 def mode @mode end |
#pin_file ⇒ Object (readonly)
Returns the value of attribute pin_file.
5 6 7 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 5 def pin_file @pin_file end |
#pin_num ⇒ Object (readonly)
Returns the value of attribute pin_num.
5 6 7 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 5 def pin_num @pin_num end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
5 6 7 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 5 def status @status end |
Instance Method Details
#close ⇒ Object
Unexports the pin in GPIO to leave it free
86 87 88 89 90 91 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 86 def close off! if @mode == 'w' File.open("#{ GPIO_PATH }/unexport", "w") { |f| f.write("#{pin_num}") } File.close @pin_file.close end |
#digital_read ⇒ Object
Reads digitally from the specified pin on initialize
42 43 44 45 46 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 42 def digital_read set_mode('r') unless @mode == 'r' @pin_file.read end |
#digital_write(value) ⇒ Object
Writes to the specified pin Digitally accepts values :high or :low, 1 or 0, "1" or "0"
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 24 def digital_write(value) set_mode('w') unless @mode == 'w' if value.is_a? Symbol value = (value == :high) ? 1 : 0 end value = value.to_i raise StandardError unless ([HIGH, LOW].include? value) @status = (value == 1) ? 'high' : 'low' @pin_file.write(value) @pin_file.flush end |
#off! ⇒ Object
Sets digital write for the pin to LOW
81 82 83 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 81 def off! digital_write(:off) end |
#off? ⇒ Boolean
71 72 73 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 71 def off? !self.on? end |
#on! ⇒ Object
Sets digital write for the pin to HIGH
76 77 78 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 76 def on! digital_write(:high) end |
#on? ⇒ Boolean
67 68 69 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 67 def on? (@status == 'high') ? true : false end |
#set_mode(mode) ⇒ Object
Sets the pin in GPIO for read or write.
49 50 51 52 53 54 55 56 57 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 49 def set_mode(mode) @mode = mode if mode == 'w' set_pin(mode: 'w', direction: GPIO_DIRECTION_WRITE) elsif mode =='r' set_pin(mode: 'r', direction: GPIO_DIRECTION_READ) end end |
#set_pin(settings) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/artoo/adaptors/io/digital_pin.rb', line 59 def set_pin(settings) mode = settings[:mode] direction = settings[:direction] File.open("#{ GPIO_PATH }/gpio#{ pin_num }/direction", "w") { |f| f.write(direction) } File.close @pin_file = File.open("#{ GPIO_PATH }/gpio#{ pin_num }/value", mode) end |