Class: Omega2Gpio::Gpio
- Inherits:
-
Object
- Object
- Omega2Gpio::Gpio
- Defined in:
- lib/omega2_gpio/gpio.rb
Instance Attribute Summary collapse
-
#gpio_number ⇒ Object
readonly
Returns the value of attribute gpio_number.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #get ⇒ Object
-
#initialize(gpio_number) ⇒ Gpio
constructor
A new instance of Gpio.
- #is_high? ⇒ Boolean
- #is_low? ⇒ Boolean
- #read ⇒ Object
- #set_direction(direction) ⇒ Object
Constructor Details
#initialize(gpio_number) ⇒ Gpio
Returns a new instance of Gpio.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/omega2_gpio/gpio.rb', line 7 def initialize(gpio_number) @gpio_number = gpio_number @value = 0 if Omega2Gpio.configuration.mock and gpio_number.between?(0, Omega2Gpio.configuration.highest_gpio_number) Omega2Gpio.messenger.debug "Init Fake-GPIO#{@gpio_number} and mock all interactions as valid" else set_direction('output') end self end |
Instance Attribute Details
#gpio_number ⇒ Object (readonly)
Returns the value of attribute gpio_number.
4 5 6 |
# File 'lib/omega2_gpio/gpio.rb', line 4 def gpio_number @gpio_number end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
5 6 7 |
# File 'lib/omega2_gpio/gpio.rb', line 5 def value @value end |
Class Method Details
.is_mock? ⇒ Boolean
20 21 22 |
# File 'lib/omega2_gpio/gpio.rb', line 20 def self.is_mock? Omega2Gpio.configuration.mock end |
Instance Method Details
#get ⇒ Object
43 44 45 |
# File 'lib/omega2_gpio/gpio.rb', line 43 def get read end |
#is_high? ⇒ Boolean
47 48 49 |
# File 'lib/omega2_gpio/gpio.rb', line 47 def is_high? self.read == 1 end |
#is_low? ⇒ Boolean
51 52 53 |
# File 'lib/omega2_gpio/gpio.rb', line 51 def is_low? self.read == 0 end |
#read ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/omega2_gpio/gpio.rb', line 28 def read if Omega2Gpio.configuration.mock Omega2Gpio.messenger.debug "Mocked GPIO#{self.gpio_number} value is '#{@value}'" else stdin, stdout, stderr = Open3.popen3("fast-gpio read #{@gpio_number}") if stderr.gets raise_error(stderr.gets) else @value = stdout.gets.to_s.split.last.to_i end end @value end |
#set_direction(direction) ⇒ Object
24 25 26 |
# File 'lib/omega2_gpio/gpio.rb', line 24 def set_direction(direction) execute_fast_gpio_command "fast-gpio set-#{direction} #{self.gpio_number}" end |