class RPiPinIn
file: rpi_pinin.rb
Public Class Methods
new(id, simulator=nil, pull: nil)
click to toggle source
# File lib/rpi_pinin.rb, line 11 def initialize(id, simulator=nil, pull: nil) @id, @simulator = id, simulator if not @simulator then unexport() File.write '/sys/class/gpio/export', id File.write "/sys/class/gpio/gpio#{id}/direction", 'in' case pull when :up File.write "/sys/class/gpio/gpio#{id}/direction", 'high' end at_exit { unexport() } end end
Public Instance Methods
on_change(value)
click to toggle source
override this method if you wish
# File lib/rpi_pinin.rb, line 33 def on_change(value) end
on_high()
click to toggle source
override this method if you wish
# File lib/rpi_pinin.rb, line 39 def on_high() end
to_s()
click to toggle source
# File lib/rpi_pinin.rb, line 81 def to_s() @id end
watch() { |value| ... }
click to toggle source
# File lib/rpi_pinin.rb, line 58 def watch() old_value = read_value() loop do value = read_value() if value != old_value then yield(value) if block_given? on_change(value) end old_value = value sleep 0.1 end end
watch_high() { || ... }
click to toggle source
# File lib/rpi_pinin.rb, line 43 def watch_high() watch() do |value| if value == 1 then yield if block_given? on_high() end end end
Private Instance Methods
read_value()
click to toggle source
# File lib/rpi_pinin.rb, line 87 def read_value() return FileX.read(@simulator) if @simulator File.read("/sys/class/gpio/gpio#{@id}/value").chomp == '0' ? 1 : 0 end
unexport()
click to toggle source
to avoid “Device or resource busy @ fptr_finalize - /sys/class/gpio/export” we unexport the pins we used
# File lib/rpi_pinin.rb, line 97 def unexport() return unless File.exists? '/sys/class/gpio/gpio' + @id.to_s File.write "/sys/class/gpio/unexport", @id end