Skip to content
Tristan edited this page Oct 8, 2019 · 7 revisions

pigpiod

Examples

These examples were mostly inspired and modified from https://github.com/nak1114/ruby-extension-pigpio/tree/master/example

blinky

require "pigpio"
include Pigpio::Constant

pi = Pigpio.new

unless pi.connect
  puts "Unable to connect to the pigpiod daemon."
  puts "Maybe you need to run: sudo pigpiod ?"
  exit -1
end

led = pi.gpio(17)
led.mode = PI_OUTPUT
led.pud = PI_PUD_OFF

# make sure we exit gracefully on ctrl-c
trap "SIGINT" do
  puts "Exiting"
  led.write 0
  pi.stop
  exit 130
end

loop do
  led.write 1
  sleep 1
  led.write 0
  sleep 1
end

switch

require "pigpio"
include Pigpio::Constant
pi = Pigpio.new

unless pi.connect
  puts "Unable to connect to the pigpiod daemon."
  puts "Maybe you need to run: sudo pigpiod ?"
  exit -1
end

counter = 0
led = pi.gpio(17)
led.mode = PI_OUTPUT
led.pud = PI_PUD_OFF

button = pi.gpio(16)
button.mode = PI_INPUT
button.pud = PI_PUD_UP
button.glitch_filter(30)

cb = button.callback(EITHER_EDGE) { |tick, level|
  puts level
  led.write level
  counter += 1
}

# make sure we exit gracefully on ctrl-c
trap "SIGINT" do
  puts "Exiting"
  led.write 0
  cb.cancel
  pi.stop
  exit 130
end

led.write 0
while(counter < 5) do
  puts button.read
  sleep 1
end

cb.cancel
led.write 0
pi.stop
Clone this wiki locally