Hardware Reference
In-Depth Information
A callback requires a certain number of parameters to be dei ned, which is
extremely specii c as to the datatypes to use. The system restart callback does
not require any parameters:
void systemResetCallback(void);
To receive strings, the stringCallback function requires one parameter:
void stringCallback(char *datastring);
SysEx messages require more information and have three parameters:
void sysexCallback(byte pin, byte count, byte *array);
Finally, all other callbacks use a generic format:
void genericCallback(byte pin, int value);
Callbacks must have different names. If you use both digital and analog
pins, you will have two functions: one for handling digital data and the other
for analog input. For example, code will allow you to receive both digital and
analog instructions:
void analogWriteCallback(byte pin, int value)
{
// Code goes here
}
void digitalWriteCallback(byte pin, int value)
{
// Code goes here
}
Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
A note on handling digital data: Analog data is sent one pin at a time, but this
is not the case with digital pins. As seen previously, digital pin data is sent in
groups of 8. This is known as a port . Port 1 will send the data of pins 1 to 8, and
port 2 will send the data of pins 9 to 16, and so on. It is up to you to control if
the pins should be written. To write all pins from a specii ed port, use this code:
void digitalWriteCallback(byte port, int value)
{
byte i;
byte pinValue;
if (port < TOTAL_PORTS)
{
for(i=0; i<8; i++)
{
Search WWH ::




Custom Search