Ruby 使用select进行GPIO监控
我正在尝试监视GPIO pin,根据Linux文档,我应该能够通过使用select监视/sys/class/GPIO/GPIO/value文件来实现这一点: 我正试图在Ruby中实现这一点,根据它调用select2 因此,有了这些知识,我制定了以下测试计划:Ruby 使用select进行GPIO监控,ruby,linux,gpio,Ruby,Linux,Gpio,我正在尝试监视GPIO pin,根据Linux文档,我应该能够通过使用select监视/sys/class/GPIO/GPIO/value文件来实现这一点: 我正试图在Ruby中实现这一点,根据它调用select2 因此,有了这些知识,我制定了以下测试计划: fd = File.open("/sys/class/gpio/gpio17/value", "r") loop do rs,ws,es = IO.select(nil, nil, [fd], 5) if es r = e
fd = File.open("/sys/class/gpio/gpio17/value", "r")
loop do
rs,ws,es = IO.select(nil, nil, [fd], 5)
if es
r = es[0]
puts r.read(1)
else
puts "timeout"
end
end
但是,它没有检测到任何管脚变化。当我启动这个应用程序时,它会立即进入if块并显示pin的当前值,然后每5秒打印一次超时
我看错文件了吗?select无法监视此情况吗?在select将正确触发GPIO引脚之前,您需要设置引脚的边缘触发器。发件人:
在Ruby中,简单地说:
File.open("/sys/class/gpio/gpio17/edge", "w") { |f| f.write("both") }
上面的完整示例如下所示:
fd = File.open("/sys/class/gpio/gpio17/value", "r")
File.open("/sys/class/gpio/gpio17/edge", "w") { |f| f.write("both") }
loop do
rs,ws,es = IO.select(nil, nil, [fd], 5)
if es
r = es[0]
puts r.read(1)
else
puts "timeout"
end
end
File.open("/sys/class/gpio/gpio17/edge", "w") { |f| f.write("both") }
fd = File.open("/sys/class/gpio/gpio17/value", "r")
File.open("/sys/class/gpio/gpio17/edge", "w") { |f| f.write("both") }
loop do
rs,ws,es = IO.select(nil, nil, [fd], 5)
if es
r = es[0]
puts r.read(1)
else
puts "timeout"
end
end