Библиотека Firmata реализует протокол Firmata для связи с программным обеспечением на главном компьютере. Это позволяет вам писать собственные прошивки без необходимости создавать собственный протокол и объекты для используемой вами среды программирования.
Методы:
1 2 3 4 5 6 7 8 |
begin(); //start the library begin(long); //start the library and override the default baud rate begin(Stream &s); // start the library using a [Stream](http://www.arduino.cc/en/Reference/Stream) other than Serial (eg Serial1 or EthernetClient) printVersion(); //send the protocol version to the host computer blinkVersion(): //blink the protocol version on the build in LED (typically pin 13) printFirmwareVersion(); //send the firmware name and version to the host computer setFirmwareVersion(byte major, byte minor); //set the firmware name and version, using the sketch's filename, minus the '.ino' setFirmwareNameAndVersion(const char *name, byte major, byte minor); //set both the name and version of the firmware |
Отправка сообщений
1 2 3 4 5 6 |
sendAnalog(byte pin, int value); //send an analog message sendDigitalPort(byte portNumber, int portData); //send an 8-bit port in a single digital message sendString(const char* string); //send a string to the host computer sendString(byte command, byte bytec, byte *bytev); //send a string to the host computer using a custom command type sendSysex(byte command, byte bytec, byte* bytev); //send a command with an arbitrary array of bytes write(byte c); //write a byte to the Stream |
Получение сообщений
1 2 3 4 |
available(); //check to see if there are any incoming messages in the buffer processInput(); //process incoming messages from the buffer, sending the data to any registered callback functions attach(byte command, callbackFunction myFunction); //attach a function to an incoming message type detach(byte command); //detach a function from an incoming message type |
Полезные методы:
1 2 3 |
sendValueAsTwo7bitBytes(int value); //writes value as 2 bytes startSysex(void); //starts a sysex message endSysex(void); //ends a sysex message |
Функции обратного вызова:
Чтобы прикрепить вашу функцию к типу сообщения, ваша функция должна соответствовать стандартной функции обратного вызова. В настоящее время в Firmata существует три типа функций обратного вызова: универсальные (generic), строковые (string) и sysex.
generic
1 |
void callbackFunction(byte pin, int value); |
system_reset
1 |
void systemResetCallbackFunction(void); |
string
1 |
void stringCallbackFunction(char *myString); |
sysex
1 |
void sysexCallbackFunction(byte command, byte byteCount, byte *arrayPointer); |
Типы сообщений
Существуют различные типы сообщений, к которым вы можете прикрепить функции обратного вызова.
1 2 3 4 5 6 7 8 |
ANALOG_MESSAGE //the analog value for a single pin DIGITAL_MESSAGE //8-bits of digital pin data (one port) REPORT_ANALOG //enable/disable the reporting of an analog pin REPORT_DIGITAL //enable/disable the reporting of a digital port SET_PIN_MODE //change the pin mode between INPUT/OUTPUT/PWM/etc. STRING_DATA //C-style strings, uses stringCallbackFunction for the function type SYSEX_START //generic, arbitrary length messages (via MIDI SysEx protocol), uses sysexCallbackFunction for the function type SYSTEM_RESET //message to reset firmware to its default state, uses systemResetCallbackFunction for the function type |
Пример
В этом примере показано, как отправлять и получать аналоговые сообщения с помощью Firmata.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <Firmata.h> byte analogPin; void analogWriteCallback(byte pin, int value) { pinMode(pin, OUTPUT); analogWrite(pin, value); } void setup() { Firmata.setFirmwareVersion(FIRMATA_MAJOR_VERSION, FIRMATA_MINOR_VERSION); Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); Firmata.begin(); } void loop() { while (Firmata.available()) { Firmata.processInput(); } for (analogPin = 0; analogPin < TOTAL_ANALOG_PINS; analogPin++) { Firmata.sendAnalog(analogPin, analogRead(analogPin)); } } |