Module: RPi::GPIO

Defined in:
lib/rpi_gpio.rb,
ext/rpi_gpio/rpi_gpio.c

Defined Under Namespace

Classes: Callback, GPIO, PWM

Class Method Summary collapse

Class Method Details

.add_callback(gpio, &block) ⇒ Object



281
282
283
# File 'lib/rpi_gpio.rb', line 281

def self.add_callback(gpio, &block)
  @@callbacks << Callback.new(gpio, &block)
end

.add_edge_detect(gpio, edge, bounce_time = nil) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/rpi_gpio.rb', line 193

def self.add_edge_detect(gpio, edge, bounce_time = nil)
  current_edge = get_event_edge(gpio)
  if current_edge.nil?
    g = new_gpio(gpio)
    set_edge(gpio, edge)
    g.edge = edge
    g.bounce_time = bounce_time
  elsif current_edge == edge
    g = get_gpio(gpio)
    if (bounce_time && g.bounce_time != bounce_time) || g.thread_added
      raise RuntimeError, "conflicting edge detection already enabled for GPIO #{gpio}"
    end
  else
    raise RuntimeError, "conflicting edge detection already enabled for GPIO #{gpio}"
  end

  if @@epoll.nil?
    @@epoll = Epoll.create
  end

  begin
    @@epoll.add(g.value_file, Epoll::PRI)
  rescue
    remove_edge_detect(gpio)
    raise
  end

  g.thread_added = true
  if @@epoll_thread.nil? || !@@epoll_thread.alive?
    @@epoll_thread = Thread.new { poll_thread }
  end
end

.callback_exists(gpio) ⇒ Object



289
290
291
# File 'lib/rpi_gpio.rb', line 289

def self.callback_exists(gpio)
  @@gpios.find { |g| g.gpio == gpio } != nil
end

.channel_from_gpioObject

.clean_upObject

.delete_gpio(gpio) ⇒ Object



174
175
176
# File 'lib/rpi_gpio.rb', line 174

def self.delete_gpio(gpio)
  @@gpios.delete_if { |g| g.gpio == gpio }
end

.ensure_gpio_inputObject

.event_cleanup(gpio) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
# File 'lib/rpi_gpio.rb', line 265

def self.event_cleanup(gpio)
  @@gpios.map { |g| g.gpio }.each do |gpio_|
    if gpio.nil? || gpio_ == gpio
      remove_edge_detect(gpio_)
    end
  end

  if @@gpios.empty? && @@epoll_thread
    @@epoll_thread.terminate
  end
end

.event_cleanup_allObject



277
278
279
# File 'lib/rpi_gpio.rb', line 277

def self.event_cleanup_all
  event_cleanup(nil)
end

.export(gpio) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/rpi_gpio.rb', line 105

def self.export(gpio)
  unless File.exist?("/sys/class/gpio/gpio#{gpio}")
    File.open("/sys/class/gpio/export", 'w') do |file|
      file.write(gpio.to_s)
    end
  end
end

.get_event_edge(gpio) ⇒ Object

gpio_event_added in python library



186
187
188
189
190
191
# File 'lib/rpi_gpio.rb', line 186

def self.get_event_edge(gpio) # gpio_event_added in python library
  g = @@gpios.find { |g| g.gpio == gpio }
  if g
    return g.edge
  end
end

.get_gpio(gpio) ⇒ Object



178
179
180
# File 'lib/rpi_gpio.rb', line 178

def self.get_gpio(gpio)
  @@gpios.find { |g| g.gpio == gpio }
end

.get_gpio_by_value_file(value_file) ⇒ Object



182
183
184
# File 'lib/rpi_gpio.rb', line 182

def self.get_gpio_by_value_file(value_file)
  @@gpios.find { |g| g.value_file == value_file }
end

.get_gpio_numberObject

.high?Boolean

Returns:

  • (Boolean)

.low?Boolean

Returns:

  • (Boolean)

.new_gpio(gpio) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rpi_gpio.rb', line 153

def self.new_gpio(gpio)
  g = GPIO.new
  g.gpio = gpio
  export(gpio)
  g.exported = true
  set_direction(gpio, :in)
  begin
    g.value_file = open_value_file(gpio)
  rescue
    unexport(gpio)
    raise
  end
  g.initial_thread = true
  g.initial_wait = true
  g.bounce_time = nil
  g.last_call = 0
  g.thread_added = false
  @@gpios << g
  g
end

.open_value_file(gpio) ⇒ Object



149
150
151
# File 'lib/rpi_gpio.rb', line 149

def self.open_value_file(gpio)
  File.open("/sys/class/gpio/gpio#{gpio}/value", 'r')
end

.poll_threadObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/rpi_gpio.rb', line 238

def self.poll_thread
  loop do
    begin
      events = @@epoll.wait
    rescue IOError # empty interest list
      break
    end
    events.each do |event|
      if event.events & Epoll::PRI != 0
        event.data.seek(0, IO::SEEK_SET)
        g = get_gpio_by_value_file(event.data)
        value = event.data.read.chomp.to_i
        if g.initial_thread # ignore first epoll trigger
          g.initial_thread = false
        else
          now = Time.now.to_f
          if g.bounce_time.nil? || g.last_call == 0 || g.last_call > now ||
             (now - g.last_call) * 1000 > g.bounce_time then
            g.last_call = now
            run_callbacks(g.gpio, value)
          end
        end
      end
    end
  end
end

.remove_callbacks(gpio) ⇒ Object



285
286
287
# File 'lib/rpi_gpio.rb', line 285

def self.remove_callbacks(gpio)
  @@callbacks.delete_if { |g| g.gpio == gpio }
end

.remove_edge_detect(gpio) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rpi_gpio.rb', line 226

def self.remove_edge_detect(gpio)
  g = get_gpio(gpio)
  if g and @@epoll
    @@epoll.del(g.value_file)
    set_edge(gpio, :none)
    g.edge = :none
    g.value_file.close
    unexport(gpio)
    delete_gpio(gpio)
  end
end

.resetObject

.run_callbacks(gpio, value) ⇒ Object



293
294
295
296
297
298
299
# File 'lib/rpi_gpio.rb', line 293

def self.run_callbacks(gpio, value)
  @@callbacks.each do |callback|
    if callback.gpio == gpio
      callback.block.call(channel_from_gpio(gpio), value)
    end
  end
end

.set_direction(gpio, direction) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rpi_gpio.rb', line 119

def self.set_direction(gpio, direction)
  validate_direction(direction)
  tries = 0
  begin
    File.open("/sys/class/gpio/gpio#{gpio}/direction", 'w') do |file|
      file.write(direction.to_s)
    end
  rescue
    raise if tries > 5
    sleep 0.1
    tries += 1
    retry
  end
end

.set_edge(gpio, edge) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rpi_gpio.rb', line 134

def self.set_edge(gpio, edge)
  validate_edge(edge)
  tries = 0
  begin
    File.open("/sys/class/gpio/gpio#{gpio}/edge", 'w') do |file|
      file.write(edge.to_s)
    end
  rescue
    raise if tries > 5
    sleep 0.1
    tries += 1
    retry
  end
end

.set_highObject

.set_lowObject

.set_numberingObject

.set_warningsObject

.stop_watching(channel) ⇒ Object



17
18
19
20
21
22
# File 'lib/rpi_gpio.rb', line 17

def self.stop_watching(channel)
  gpio = get_gpio_number(channel)
  ensure_gpio_input(gpio)
  remove_edge_detect(gpio)
  remove_callbacks(gpio)
end

.unexport(gpio) ⇒ Object



113
114
115
116
117
# File 'lib/rpi_gpio.rb', line 113

def self.unexport(gpio)
  File.open("/sys/class/gpio/unexport", 'w') do |file|
    file.write(gpio.to_s)
  end
end

.validate_direction(direction) ⇒ Object



301
302
303
304
305
306
# File 'lib/rpi_gpio.rb', line 301

def self.validate_direction(direction)
  direction = direction.to_s
  if direction != 'in' && direction != 'out'
    raise ArgumentError, "`direction` must be 'in' or 'out'; given '#{direction}'"
  end
end

.validate_edge(edge) ⇒ Object



308
309
310
311
312
313
# File 'lib/rpi_gpio.rb', line 308

def self.validate_edge(edge)
  edge = edge.to_s
  if edge != 'rising' && edge != 'falling' && edge != 'both' && edge != 'none'
    raise ArgumentError, "`edge` must be 'rising', 'falling', 'both', or 'none'; given '#{edge}'"
  end
end

.wait_for_edge(channel, edge, bounce_time: nil, timeout: -1)) ⇒ Object



24
25
26
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rpi_gpio.rb', line 24

def self.wait_for_edge(channel, edge, bounce_time: nil, timeout: -1)
  gpio = get_gpio_number(channel)
  if callback_exists(gpio)
    raise RuntimeError, "conflicting edge detection already enabled for GPIO #{gpio}"
  end

  ensure_gpio_input(gpio)
  validate_edge(edge)
  was_gpio_new = false
  current_edge = get_event_edge(gpio)
  if current_edge == edge
    g = get_gpio(gpio)
    if g.bounce_time && g.bounce_time != bounce_time
      raise RuntimeError, "conflicting edge detection already enabled for GPIO #{gpio}"
    end
  elsif current_edge.nil?
    was_gpio_new = true
    g = new_gpio(gpio)
    set_edge(gpio, edge)
    g.edge = edge
    g.bounce_time = bounce_time
  else
    g = get_gpio(gpio)
    set_edge(gpio, edge)
    g.edge = edge
    g.bounce_time = bounce_time
    g.initial_wait = 1
  end

  if @@epoll_blocking.nil?
    @@epoll_blocking = Epoll.create
  end
  @@epoll_blocking.add(g.value_file, Epoll::PRI)

  initial_edge = true
  the_value = nil
  timed_out = false
  begin
    while the_value.nil? && !timed_out do
      events = @@epoll_blocking.wait(timeout)
      if events.empty?
        timed_out = true
      end
      events.each do |event|
        if event.events & Epoll::PRI != 0
          event.data.seek(0, IO::SEEK_SET)
          value = event.data.read.chomp.to_i
          if event.data == g.value_file
            if initial_edge # ignore first epoll trigger
              initial_edge = false
            else
              now = Time.now.to_f
              if g.bounce_time.nil? || g.last_call == 0 || g.last_call > now ||
                 (now - g.last_call) * 1000 > g.bounce_time then
                g.last_call = now
                the_value = value
              end
            end
          end
        end
      end
    end

    if the_value
      return the_value
    end
  ensure
    @@epoll_blocking.del(g.value_file)
    if was_gpio_new
      delete_gpio(gpio)
    end
  end
end

.watch(channel, on:, bounce_time: nil, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/rpi_gpio.rb', line 6

def self.watch(channel, on:, bounce_time: nil, &block) 
  gpio = get_gpio_number(channel)
  ensure_gpio_input(gpio)
  validate_edge(on)
  if bounce_time && bounce_time <= 0
    raise ArgumentError, "`bounce_time` must be greater than 0; given #{bounce_time}"
  end
  add_edge_detect(gpio, on, bounce_time)
  add_callback(gpio, &block)
end