Skip to content

Instantly share code, notes, and snippets.

@shinob
Created April 28, 2017 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinob/f399e2c5e2863ddd4ef15ca35c066f01 to your computer and use it in GitHub Desktop.
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
$ sudo apt install -y ruby-dev
$ sudo gem install --pre pycall
$ 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
$
$ ruby gpio_input.rb
press ^C to exit program ...
ON(1)
OFF
ON(2)
OFF
ON(3)
OFF
^Cdetect key interrupt
Program exit
$
#!/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")
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