Linux   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby – 选择GPIO监控大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在尝试监视GPIO引脚,根据Linux文档,我应该能够通过使SELEct监视/ sys / class / gpio / gpio ## / value文件来完成此操作:'value' ... reads as either 0 (low) or 1 (high). If the GPIO is configured as an output

我正在尝试监视GPIO引脚,根据Linux文档,我应该能够通过使SELEct监视/ sys / class / gpio / gpio ## / value文件来完成此操作:

"value" ... reads as either 0 (low) or 1 (high).  If the GPIO
    is configured as an output,this value may be written;
    any nonzero value is treated as high.

    If the pin can be configured as interrupt-generating interrupt
    and if it has been configured to generatE interrupts (see the
    description of "edge"),you can poll(2) on that file and
    poll(2) will return whenever thE interrupt was triggered. If
    you use poll(2),set the events POLLPRI and POLLERR. If you
    use SELEct(2),set the file descriptor in exceptfds. After
    poll(2) returns,either lseek(2) to the beginning of the sysfs
    file and read the new value or close the file and re-open it

我试图在Ruby中执行此操作,并且根据IO.Select documentation调用SELEct(2).

所以,凭借这些知识,我将以下测试程序汇集在一起​​:

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块并显示引脚的当前值,然后每5秒打印一次超时.

我读错了文档吗?不应该选择能够监控吗?

最佳答案
在选择将在GPIO引脚上正确触发之前,您需要设置引脚的边沿触发.从the GPIO docs开始:

"edge" ... reads as either "none","rising","falling",or
    "both". Write these Strings to SELEct the signal edge(s)
    that will make poll(2) on the "value" file return.

    This file exists only if the pin can be configured as an
    interrupt generating input pin.

在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,5)
  if es
    r = es[0]
    puts r.read(1)
  else
    puts "timeout"
  end
end

大佬总结

以上是大佬教程为你收集整理的ruby – 选择GPIO监控全部内容,希望文章能够帮你解决ruby – 选择GPIO监控所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。