Download as a PDF
Transcript
Now we will connect to some peripherals. Let‟s start by attaching an LED to channel 4 on our dyio. We can now connect to it on our console by entering the following: led = Led.new(dyio.channel(4)) “led” is the identifier, and unlike before, our new Led requires a parameter; in this case, that parameter is the DyIO channel it is on. We use the DyIO identifier from before (“dyio”) and calling channel(4). Now we can interact with the LED. To turn it on, call its „on‟ function by entering: led.on You can also turn it off by calling its „off‟ function. Other functions include „switch‟ which turns the LED on if it is not on and turns it off if it is on. Creating code for a Peripheral This is probably the most difficult usage of the RDSL as it requires knowledge on the NRSDK and the appropriate calls to what you want to do. We will go over the steps in creating a SimpleButton peripheral, although one is already included in Fez v0.1. The one we are about to make is stripped down and does not handle asynchronicity (just to keep things simple). Create the Ruby Class Create a new Ruby file. For this example, let‟s name it simple_button.rb. Open config.ini and add the following line load “directory\simple_button.rb” where „directory‟ is the directory in which you created simple_button.rb. You can use relative addressing which is relative to the repl.rb file in the Fez folder. The file will contain the following: java_importcom.neuronrobotics.sdk.dyio.peripherals.DigitalInputChannel class SimpleButton #set button to a dyio channel def initialize(channel) @chan = channel @type = DigitalInputChannel.new(@chan) end #returns button state, true is high (5v), false is low (0v) def state return @type.isHigh 46