I've built to build a timer that shows the content in a 16x2 LCD.
So far so good. Now, what I want to achieve is that when I push a button, pause the timer.
I'm using ruby, with the library rpi_gpio and
This is my code:
require 'rpi_gpio'
require 'charlcd'
RPi::GPIO.set_numbering :bcm
RPi::GPIO.setup 18, :as => :input, :pull => :up
lcd = CharLcd.new
lcd.begin(16, 2)
lcd.clear
total_seconds = 0
start_time = Time.now
t = Thread.new do
loop do
sleep 1
total_seconds = Time.now - start_time
hours = (total_seconds/ 3600).to_i
minutes = ((total_seconds % 3600) / 60).to_i
seconds = ((total_seconds % 3600) % 60).to_i
lcd.clear
tim = hours.to_s.rjust(2, '0') + ':' + minutes.to_s.rjust(2, '0') + ':' + seconds.to_s.rjust(2, '0')
lcd.message(tim)
end
end
while RPi::GPIO.high? 18 do
end
t.kill
lcd.clean_pins()
The problem comes when, as far as I know, the only way to control when button is pushed is with a loop, and I think two loops (the one of the timer and the one that controls the button) make slow the printing of the message in the LCD display.
I'm record this video to show you guys the behavior:
I'd be grateful if somebody could help me out with this, or give me another solution.
Thanks in advanced.
while
. On a Pi 2, the timer thread should still run okay, but this will completely screw up a single core machine. There's no way someone intended the library to be used that way. There should be means of waiting on an blocking call for an event involving the pin. This may be identical to using "poll" or "select" on a (set of) file/socket handles, if you've ever done any basic networking. The event here might be called an interrupt (although it is sort of a misnomer). – goldilocks Dec 31 '15 at 12:50watch_for()
("watch the pin asynchronously"). BTW, I am pretty sure you should be able to use both these libraries at the same time as long they are using separate pins. So you can do PWM and buttons. – goldilocks Dec 31 '15 at 13:17While I was investigating for Ruby gems for this, I found the one you mention. The thing is that, in background, this gem also use a
– JV Lobo Jan 10 '16 at 15:05while
so I suppose that the problem stills happen -> https://github.com/sausheong/ruby-gpio/blob/master/lib/ruby-gpio.rb#L61/sys/class/gpio
as explained here ("write these strings to select the signal edge(s) that will make poll(2) on the "value" file return").poll()
andselect()
are standard C functions; glancing around an equivalent in ruby would beIO.select()
– goldilocks Jan 10 '16 at 15:13