Class: RPiPinIn
Instance Method Summary collapse
-
#initialize(id, simulator = nil, pull: nil) ⇒ RPiPinIn
constructor
A new instance of RPiPinIn.
-
#on_change(value) ⇒ Object
override this method if you wish.
-
#on_high ⇒ Object
override this method if you wish.
- #to_s ⇒ Object
- #watch ⇒ Object
- #watch_high ⇒ Object
Constructor Details
#initialize(id, simulator = nil, pull: nil) ⇒ RPiPinIn
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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 |
Instance Method Details
#on_change(value) ⇒ Object
override this method if you wish
33 34 35 |
# File 'lib/rpi_pinin.rb', line 33 def on_change(value) end |
#on_high ⇒ Object
override this method if you wish
39 40 41 |
# File 'lib/rpi_pinin.rb', line 39 def on_high() end |
#to_s ⇒ Object
81 82 83 |
# File 'lib/rpi_pinin.rb', line 81 def to_s() @id end |
#watch ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# 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 ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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 |