Class: YaGPIO

Inherits:
Object
  • Object
show all
Defined in:
lib/ya_gpio.rb,
lib/ya_gpio/version.rb

Overview

YaGPIO is a simple module to control GPIO port on a Raspberry Pi. It’s based on the Sysfs interface.

Examples:

Open port 22 as output and set it to high

pin = YaGPIO.new(22, YaGPIO::OUTPUT)
pin.high

Open port 23 as input and read its state

pin = YaGPIO.new(23, YaGPIO::INPUT)
pp pin.low?

Constant Summary collapse

INPUT =

Direction input

'in'
OUTPUT =

Direction output with default low

'out'
OUTPUT_HIGH =

Direction output with default high

'high'
EDGE_RISING =

Interruption on rising edge

'rising'
EDGE_FALLING =

Interruption on falling edge

'falling'
EDGE_BOTH =

Interruption on rising and falling edge

'both'
EDGE_NONE =

Disable interruption

'none'
VERSION =
"0.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pin, direction) ⇒ YaGPIO

Create and configure a new GPIO pin. The pin will be exported via the sysfs interface. The pin will be unexported and released for other use upon garbage collection.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ya_gpio.rb', line 64

def initialize(pin, direction)
	raise 'direction must be one of INPUT, OUTPUT, OUTPUT_HIGH' unless [INPUT, OUTPUT, OUTPUT_HIGH].include?(direction)

	@pin = pin
	@callback = nil
	@direction = direction

	export
	open

	ObjectSpace.define_finalizer(self, self.class.finalize(@pin))
end

Instance Attribute Details

#fileObject (readonly)

File descriptor to the sysfs entry of the pin. It is not recommended to access the file directly. Use high() lov() helpers instead.



35
36
37
# File 'lib/ya_gpio.rb', line 35

def file
  @file
end

Class Method Details

.resumeObject

Stop the wait loop, must be run from a callback triggered by YaGPIO::wait()



177
178
179
# File 'lib/ya_gpio.rb', line 177

def self.resume
	@@wait = false
end

.wait(gpios) ⇒ Object

Wait for an interruption to be trigerred and run the associated callback. This method will block until the program exits or YaGPIO::resume() is called from a callback.

Note that software debounce has not been implemented. You can use a 1µF capacitor in your setup to fix bounce issues.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ya_gpio.rb', line 161

def self.wait(gpios)
	# Initial read to clear interrupt triggered during setup 
	gpios.map{|g| g.high?}

	@@wait = true
	while @@wait do
		rs, ws, es = IO.select(nil, nil, gpios.map{|g| g.file})

		es.each do |f|
			gpio = gpios.select{|g| g.file == f}.first
			gpio.trigger(gpio.high?)
		end
	end
end

Instance Method Details

#active_low=(active) ⇒ Object

Invert all values and settings. After set to true, HIGH means LOW and LOW means HIGH. Be prepared to be confused if you’re using this feature.



106
107
108
# File 'lib/ya_gpio.rb', line 106

def active_low=(active)
	File.write("/sys/class/gpio/gpio#{@pin}/active_low", active ? '1' : '0')
end

#active_low?Boolean

Return true is active_low feature is enabled



113
114
115
# File 'lib/ya_gpio.rb', line 113

def active_low?
	File.read("/sys/class/gpio/gpio#{@pin}/active_low") != '0'
end

#clear_interruptObject

Disable a previously set interruption



129
130
131
132
# File 'lib/ya_gpio.rb', line 129

def clear_interrupt()
	set_edge(EDGE_NONE)	
	@callback = nil
end

#highObject

Set the pin to high



93
94
95
# File 'lib/ya_gpio.rb', line 93

def high
	write(1)
end

#high?Boolean

Return true if the pin is high



81
82
83
# File 'lib/ya_gpio.rb', line 81

def high?
	read() != 0
end

#lowObject

Set the pin to low



98
99
100
# File 'lib/ya_gpio.rb', line 98

def low
	write(0)
end

#low?Boolean

Return true if the pin is low



88
89
90
# File 'lib/ya_gpio.rb', line 88

def low?
	read() == 0
end

#set_interrupt(edge, &block) ⇒ Object

Define a callback to execute when an interruption will be triggered.



121
122
123
124
125
126
# File 'lib/ya_gpio.rb', line 121

def set_interrupt(edge, &block)
	raise 'interrupt can only be set on input pin' unless @direction == INPUT

	set_edge(edge)	
	@callback = block
end

#trigger(active) ⇒ Object

Execute the interruption’s callback.



137
138
139
140
141
142
143
# File 'lib/ya_gpio.rb', line 137

def trigger(active)
	if @callback.nil?
	    puts "No Callback defined for #{@pin}"
	else
	    @callback.call(active)
	end
end

#unexportObject Also known as: close

Release the pin. The object cannot be used after this method is called as the pin will not be configured anymore.



147
148
149
150
# File 'lib/ya_gpio.rb', line 147

def unexport()
	@file.close
	File.write('/sys/class/gpio/unexport', @pin.to_s)
end