Created
April 28, 2017 08:33
-
-
Save shinob/f399e2c5e2863ddd4ef15ca35c066f01 to your computer and use it in GitHub Desktop.
Raspberry PIにPyCallをインストールしてRubyからGPIOのPython用ライブラリを使ってみる ref: http://qiita.com/mix_dvd/items/778e6f3f63202c147609
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo apt install -y ruby-dev | |
$ sudo gem install --pre pycall |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python gpio_input.py | |
press ^C to exit program ... | |
1 | |
1 | |
1 | |
0 | |
0 | |
1 | |
1 | |
1 | |
0 | |
1 | |
1 | |
^Cdetect key interrupt | |
Program exit | |
$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby gpio_input.rb | |
press ^C to exit program ... | |
ON(1) | |
OFF | |
ON(2) | |
OFF | |
ON(3) | |
OFF | |
^Cdetect key interrupt | |
Program exit | |
$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import RPi.GPIO as GPIO | |
import time | |
IO_NO = 4 | |
print("press ^C to exit program ...\n") | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(IO_NO, GPIO.IN) | |
try: | |
while True: | |
print(GPIO.input(IO_NO)) | |
time.sleep(1) | |
except KeyboardInterrupt: | |
print("detect key interrupt\n") | |
GPIO.cleanup() | |
print("Program exit\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pycall/import' | |
include PyCall::Import | |
pyimport 'RPi.GPIO', as: 'gpio' | |
io_no = 4 | |
status = 1 | |
buff = 1 | |
cnt = 0 | |
puts "press ^C to exit program ...\n" | |
gpio.setmode.(gpio.BCM) | |
gpio.setup.(io_no, gpio.IN) | |
# 「Ctrl」+「C」で停止させる | |
Signal.trap(:INT) do | |
puts "detect key interrupt\n" | |
gpio.cleanup.() | |
puts "Program exit\n" | |
exit 0 | |
end | |
loop do | |
status = gpio.input.(io_no) | |
if buff != status | |
if status == 1 | |
puts "OFF" | |
else | |
cnt += 1 | |
puts "ON(#{cnt})" | |
end | |
end | |
buff = status | |
sleep 0.5 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment