Firmata解析

一、简介

Firmata是一个PC与MCU通讯的一个常用协议。其遵旨是能与任何主机PC软件包兼容。到目前为止,已经得到不少语言的支持,这样可方便地将对协议的支持加入软件系统中。Firmata起初是针对于PC与Arduino通讯的固件(Firmware),其目标是让开发者可以通过PC软件完全地控件Arduino。

image

 

二、Firmata协议详解

 
I/O支持
  • 超过16路模拟通道
  • 超过128(16 * 8-bit ports)路数字通道

消息类型
/* This protocol uses the MIDI message format, but does not use the whole
 * protocol.  Most of the command mappings here will not be directly usable in
 * terms of MIDI controllers and synths.  It should co-exist with MIDI without
 * trouble and can be parsed by standard MIDI interpreters.  Just some of the
 * message data is used differently.
 *
 * MIDI format: http://www.harmony-central.com/MIDI/Doc/table1.html
 * 
 *                              MIDI       
 * type                command  channel    first byte            second byte 
 *----------------------------------------------------------------------------
 * analog I/O message    0xE0   pin #      LSB(bits 0-6)         MSB(bits 7-13)
 * digital I/O message   0x90   port       LSB(bits 0-6)         MSB(bits 7-13)
 * report analog pin     0xC0   pin #      disable/enable(0/1)   - n/a -
 * report digital port   0xD0   port       disable/enable(0/1)   - n/a -
 *
 * sysex start           0xF0   
 * set pin mode(I/O)     0xF4              pin # (0-127)         pin state(0=in)
 * sysex end             0xF7   
 * protocol version      0xF9              major version         minor version
 * system reset          0xFF
 *
 */
/* SysEx-based commands (0x00-0x7F) are used for an extended command set.
 *
 * type                command  first byte       second byte      ...
 *-----------------------------------------------------------------------------
 * string                0x71   char *string ...
 * firmware name/version 0x79   major version   minor version     char *name...
 */ 

该协议使用了MIDI消息格式,但并非MIDI消息格式的完整实现,Firmata中大多数命令不能直接按MIDI规定的的术语来使用。
但可与MIDI兼容并可被任何标准MIDI解释器兼容。仅有部分消息数据的使用是不同的。

MIDI消息格式

类型

命令

MIDI通道

第一字节

第二字节

模拟IO消息 0xE0 引脚# 低字节(0-6) 高字节(7-13)
数字IO消息 0x90 端口 低字节(0-6) 高字节(7-13)
报告模拟引脚 0xC0 引脚# 禁止/使能(0/1)  
报告数字端口 0xD0 端口 禁止/使能(0/1)  
         
sysex命令起始 0xF0      
引脚模式设置 0xF4   引脚# 引脚状态(0=输入)
sysex命令停止 0xF7      
协议版本 0xF9   主版本 次版本
系统重启 0xFF      
         
基于sysex的命令        
字符串 0x71 char *string ...    
固件版本 0x79 主版本 次版本 char *name...
 
数据消息详解
/* two byte digital data format    两字节数字数据格式
 * 0  digital data, 0x90-0x9F, (MIDI NoteOn, but different data format)    数字数据,0x90-0x9F
 * 1  digital pins 0-6 bitmask    数字引脚0-6位标志掩码
 * 2  digital pins 7-13 bitmask    数字引脚7-13位标志掩码 
 */
/* analog 14-bit data format    14位模拟数据格式
 * 0  analog pin, 0xE0-0xEF, (MIDI Pitch Wheel)    模拟引脚,0xE0-0xEF
 * 1  analog least significant 7 bits    模拟最低有效位7位
 * 2  analog most significant 7 bits     模拟最高有效位7位
 */
/* version report format
 * -------------------------------------------------
 * 0  version report header (0xF9) (MIDI Undefined)
 * 1  major version (0-127)
 * 2  minor version (0-127)
 */

 
控制消息详解
/* set pin mode设置引脚模式
 * 1  set digital pin mode (0xF4) (MIDI Undefined)
 * 2  pin number (0-127)
 * 3  state (INPUT/OUTPUT/ANALOG/PWM/SERVO, 0/1/2/3/4)
 */
/* toggle analogIn reporting by pin
 * 0  toggle digitalIn reporting (0xC0-0xCF) (MIDI Program Change)
 * 1  disable(0)/enable(non-zero) 
 */
/* toggle digital port reporting by port
 * 0  toggle digital port reporting (0xD0-0xDF) (MIDI Aftertouch)
 * 1  disable(0)/enable(non-zero) 
 */
/* request version report
 * 0  request version report (0xF9) (MIDI Undefined)
 */

 
Sysex消息格式

The idea for SysEx is to have a second command space using the first byte after the SysEx Start byte. The key difference is that the data can be of any size, rather than just one or two bytes for standard MIDI messages.

SysEX的思想是在继SysEx起始字节之后,具备包含第二消息的能力。与非SysEX的命令相比,其最大的优点在于数据的大小可以任意定义,而不是像标准MIDI格式那样的仅仅一个或二个字节

/* Generic Sysex Message
 * 0     START_SYSEX (0xF0)
 * 1     sysex command (0x00-0x7F)
 * x     between 0 and MAX_DATA_BYTES 7-bit bytes of arbitrary data
 * last  END_SYSEX (0xF7)
 */

Query Firmware Name and Version

The firmware name to be reported should be exactly the same as the name of the Arduino file, minus the .pde. So for Standard_Firmata.pde, the firmware name is: Standard_Firmata.

/* Query Firmware Name and Version
 * 0  START_SYSEX (0xF0)
 * 1  queryFirmware (0x79)
 * 2  END_SYSEX (0xF7)
 */
/* Receive Firmware Name and Versio
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值