Hardware Reference
In-Depth Information
process that data. Data is received directly on the serial port. To see if data is
waiting, use
available()
:
result = Firmata.available();
This function does not take any parameters and returns
true
if one or more
bytes are waiting to be processed. To process data, use
processInput():
Firmata.processInput();
Typically, you would use both functions together:
while(Firmata.available())
{
Firmata.processInput();
}
The Firmata library hides all the complicated parts of receiving data, including
the data storage and processing. The library automatically decodes messages and
enables you to perform actions on the data received using a system of callbacks.
Callbacks
Firmata works by using a system of
callbacks
, routines that are called when a
specii c action is performed, or in this case, when a specii c message is received.
Callbacks are highly customizable, and you can write a callback to perform almost
any action you want simply by creating a function. Callbacks are put in place
using an attach function; in the case of the Firmata library, it is called
attach()
:
Firmata.attach(messagetype, function);
Table 16-1 lists the
messagetype
parameter, which is one of the constants. The
function
parameter is the callback function that you have written.
Table 16-1:
Callback Constants
CONSTANT
USE
ANALOG_MESSAGE
Analog value of a single pin
DIGITAL_MESSAGE
Digital value of a digital port
REPORT_ANALOG
Enables or disables the reporting of an analog pin
REPORT_DIGITAL
Enables or disables the reporting of a digital port
SET_PIN_MODE
Change the mode of the selected pin (input, output, and so on)
FIRMATA_STRING
Used for receiving text messages
SYSEX_START
Used for sending generic messages
SYSTEM_RESET
Used to reset fi rmware to default state
Search WWH ::
Custom Search