From: Cliff Rosson Date: 2013-03-03T10:31:25+09:00 Subject: Extending Ruby. Little help or guidance if you are willing! --047d7b5d8d5fae1c0204d6fb3259 Content-Type: text/plain; charset=ISO-8859-1 Hi Folk, I've been playing around with 7 segment displays on my RPI. I noticed that multiplexing in ruby doesn't perform that well. I get flickring on the displays and the ruby process eats up 10-20% of the RPI CPU. Ruby -rprofile myprogram.rb shows that my display_segments method is the culprit. I'm told that re-writing this in C could speed things up a bit so I started playing around with ruby inline. Admittedly I seem to get lost in the Ruby C API. I'm pretty much an amateur but I thought I would post here for some guidance from any willing kind hearted soul. The culprit method is def display_segments(num, display, io, delay = 0.005) power = display.power seg_pins = display.segments case num when 0 pins = [:a, :b, :c, :d, :e, :f] when 1 pins = [:b, :c] when 2 pins = [:a, :b, :g, :e, :d] when 3 end seg_pins.each do |seg, pin| pins.include?(seg) ? io.write(pin, LOW) : io.write(pin, HIGH) end io.write(power, HIGH) sleep delay io.write(power, LOW) end I think the logic in my C method would be the same as my ruby one. The part where I run into trouble is converting values. For example I can't figure out how to decide which pins to light by hash key. In fact I can't even figure out how to get the hash keys... Code on Github -- vizualize.me/cliffrosson --047d7b5d8d5fae1c0204d6fb3259 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Hi Folk,

I'= ;ve been playing around with 7 segment displays on my RPI. I noticed that m= ultiplexing in ruby doesn't perform that well. I get flickring on the d= isplays and the ruby process eats up 10-20% of the RPI CPU.=A0

Ruby -rprofile myprogr= am.rb shows that my display_segments method is the culprit. I'm told th= at re-writing this in C could speed things up a bit so I started playing ar= ound with ruby inline.=A0Admittedly=A0I seem to get lost in the Ruby C API.=

I'm pretty much an= =A0amateur=A0but I thought I would post here for some guidance from any wil= ling kind hearted soul.=A0

The culprit method is= =A0

def display_segments(num, display, io= , delay =3D 0.005)
=A0 po= wer =3D display.power
=A0= seg_pins =3D display.segments
=A0 case num
=A0 when 0
=A0 =A0 pins =3D [:a, :b, :c, :d, :e, :f]
=A0 when 1
=A0 =A0 pins =3D [:b, :c]
=A0 when 2
=A0 =A0 pins =3D [:a, :b, :g, :e, :d]
=
=A0 when 3
=A0 end
=A0 seg_pins.each do |seg, pin|
=A0 =A0 pins.include?(seg) ? io.write(pin, LO= W) : io.write(pin, HIGH)
=A0 end
=A0 io.write(power, HIGH)
=A0 sleep delay
=A0 io.write(power, LOW)
end

I think the logic in my C method woul= d be the same as my ruby one. The part where I run into trouble is converti= ng values. For example I can't figure out how to decide which pins to l= ight by hash key. In fact I can't even figure out how to get the hash k= eys...


--
--047d7b5d8d5fae1c0204d6fb3259--