jwhitehorn / pi_piper

Event driven Raspberry Pi GPIO programming in Ruby
BSD 2-Clause "Simplified" License
694 stars 71 forks source link

Can I watch my pin change with streaming? #41

Open rubyon opened 9 years ago

rubyon commented 9 years ago

watch :pin => 21 do

end

It seems to be "event type"

example)

1 0 1 0

but I want to use "streaming type"

like this...

1 1 1 1 1 0 0 1 1 1 1 1

can you help me?

elmatou commented 9 years ago

Hi rubyon, as it seems to be a bit resource heavy, you can achieve this by looping with a file.read. You should take inspiration in Platform#pin_wait_trigger

      def pin_wait_trigger(pin, trigger)
        fd = File.open(pin_value_path(pin), "r")
        pin_set_edge(pin, :both)

        value = pin_read(pin)
        loop do
          fd.read
          IO.select(nil, nil, [fd], nil)
          last_value = value
          value = pin_read(pin)
          if last_value != value
            next if trigger == :rising and value == 0
            next if trigger == :falling and value == 1
            break
          end
        end
      end

but you will get a new bit (or byte) sychroneously with the internal clock (a few MHz)