austinbv / dino

Dino is a ruby gem that helps you bootstrap prototyping with an Arduino
MIT License
388 stars 84 forks source link

Add component piezo and board API for tone, noTone #74

Closed MinasMazar closed 6 years ago

MinasMazar commented 10 years ago

This respond to http://github.com/austinbv/dino/issues/70 This modify board.rb, Dino.h and Dino.cpp in order to respond tone/noTone Arduino commands and add a piezo.rb component.

MinasMazar commented 10 years ago

Value can accepts only 4 digits. Next improvement is to pass freq and duration parameters of Dino::tone both on auxMsg.

vickash commented 10 years ago

Don't know how I missed this. Good work! I notice you use the duration parameter of the native tone() function. What happens if you try doing the timing in Ruby?

MinasMazar commented 10 years ago

I think this can be easily done.. calling the tone() method (without duration parameter), start a timeout (sleep) and then send a noTone(). But is there a particular reason for adopting this implementation?

vickash commented 10 years ago

I haven't tested this, but using tone() with the duration parameter seems to block execution for that duration (i.e. stops more commands from being processed by the board), while using it without the duration starts the tone and continues execution.

If, for example, you have Ruby code that blinks an LED with a specific timing on the same board, the timing can be thrown off by this. If a tone (with duration sent) is playing while a command to toggle the LED state comes in, that command has to wait until the tone finishes playing, instead of happening when it should.

If you move all the timing logic into the Ruby code, you free up the board to toggle the LED as soon as the command comes in. I'm using an LED as an example here, but it could be any other operation that needs to happen while the tone plays.

MinasMazar commented 10 years ago

Just tested and I do not see the behaviour you describe... I execute that in the IRB:

2.0.0-p451 :042 > piezo.tone 222, 8000 => nil 2.0.0-p451 :043 > led.blink

and it blinks while piezo sounds.. however if you think move timing logic in Ruby is better, i'll do it ;)

ps. what about the Piezo#tone method called without duration parameter? Current implementation take a duration default parameter of 0.5 sec, but it can be implemented a register that remember last duration..

vickash commented 6 years ago

I ended up patching this in manually to 0.12 since your fork seems to be missing.