时间:2021-07-01 10:21:17 帮助过:9人阅读
参数fname为mix文件在系统中的位置,buf为读取文件数据存放的缓冲区,maxlen为buf的最大长度。
- int load_mixer_file(const char *fname, char *buf, unsigned maxlen)
来交给具体的设备处理。
- int ret = ioctl(dev, MIXERIOCLOADBUF, (unsigned long)buf);
输出值 , MIX文件中 O: 后面的第4个整数/10000.0f
- /** simple channel scaler */
- struct mixer_scaler_s {
- floatnegative_scale;//负向缩放, MIX文件中 O: 后面的第1个整数/10000.0f
- floatpositive_scale;//正向缩放, MIX文件中 O: 后面的第2个整数/10000.0f
- floatoffset; //偏移 , MIX文件中 O: 后面的第3个整数/10000.0f
- floatmin_output;//最小
- int MixerGroup::load_from_buf(const char *buf, unsigned &buflen)
- {
- int ret = -1;
- const char *end = buf + buflen;
- /*
- * Loop until either we have emptied the buffer, or we have failed to
- * allocate something when we expected to.
- */
- while (buflen > 0) {
- Mixer *m = nullptr;
- const char *p = end - buflen;
- unsigned resid = buflen;
- /*
- * Use the next character as a hint to decide which mixer class to construct.
- */
- switch (*p) {//首先看该行的第一个字母,来确定数据的类别.
- case 'Z':
- m = NullMixer::from_text(p, resid);
- break;
- case 'M':
- m = SimpleMixer::from_text(_control_cb, _cb_handle, p, resid);
- break;
- case 'R':
- m = MultirotorMixer::from_text(_control_cb, _cb_handle, p, resid);
- break;
- default:
- /* it's probably junk or whitespace, skip a byte and retry */
- buflen--;
- continue;
- }
- /*
- * If we constructed something, add it to the group.
- */
- if (m != nullptr) {
- add_mixer(m);
- /* we constructed something */
- ret = 0;
- /* only adjust buflen if parsing was successful */
- buflen = resid;
- debug("SUCCESS - buflen: %d", buflen);
- } else {
- /*
- * There is data in the buffer that we expected to parse, but it didn't,
- * so give up for now.
- */
- break;
- }
- }
- /* nothing more in the buffer for us now */
- return ret;
- }
输出域,并将期填充到mixinfo的output_scaler字段中.
- SimpleMixer *
- SimpleMixer::from_text(Mixer::ControlCallback control_cb, uintptr_t cb_handle, const char *buf, unsigned &buflen)
- {
- SimpleMixer *sm = nullptr;
- mixer_simple_s *mixinfo = nullptr;
- unsigned inputs;
- int used;
- const char *end = buf + buflen;
- /* get the base info for the mixer */
- if (sscanf(buf, "M: %u%n", &inputs, &used) != 1) {
- debug("simple parse failed on '%s'", buf);
- goto out;
- }//复制M:后面第一个数值到无符号整型数据到变量inputs中,并将已经处理的字条数目赋值给used
- buf = skipline(buf, buflen);//让buf指定下一行
- if (buf == nullptr) {
- debug("no line ending, line is incomplete");
- goto out;
- }
- mixinfo = (mixer_simple_s *)malloc(MIXER_SIMPLE_SIZE(inputs));
- //M:后面的数字为struct mixer_control_s 结构的数量.MIXER_SIMPLE_SIZE的字义为sizeof(mixer_simple_s) + inputs*sizeof(mixer_control_s),
- //即一个完整的mixer_simple_s的定义,controls[0]一共有inputs个.
- if (mixinfo == nullptr) {
- debug("could not allocate memory for mixer info");
- goto out;
- }
- mixinfo->control_count = inputs;//input 信号的数量
- if (parse_output_scaler(end - buflen, buflen, mixinfo->output_scaler)) {
- debug("simple mixer parser failed parsing out scaler tag, ret: '%s'", buf);
- goto out;
- }//该函数解析
输出格式域定义的首字符'O'.
- int SimpleMixer::parse_output_scaler(const char *buf, unsigned &buflen, mixer_scaler_s &scaler)
- {
- int ret;
- int s[5];
- int n = -1;
- buf = findtag(buf, buflen, 'O');//寻找"O:"这样的控制符,返回指针指向
- if ((buf == nullptr) || (buflen < 12)) {
- debug("output parser failed finding tag, ret: '%s'", buf);
- return -1;
- }//12,表示O:这行的定义至少有12个字符(O:和五个1位长的整数),例如最短的定义为: O: 0 0 0 0 0
- if ((ret = sscanf(buf, "O: %d %d %d %d %d %n",//O:后面必须有5个整数,且整数间用至少一个空格分开,此处是取出O:后面的5个整数值.
- &s[0], &s[1], &s[2], &s[3], &s[4], &n)) != 5) {
- debug("out scaler parse failed on '%s' (got %d, consumed %d)", buf, ret, n);
- return -1;
- }
- buf = skipline(buf, buflen);
- if (buf == nullptr) {
- debug("no line ending, line is incomplete");
- return -1;
- }
- //从下面的赋值操作可以得出 O:后面5个数值的字义.,分别为 [negative_scale] [positive_scale] [offset] [min_output] [max_output]
- //并且每个这都做了除10000的操作,所以MIX格式定义中说这些值都是被放大10000倍后的数值.
- scaler.negative_scale= s[0] / 10000.0f;
- scaler.positive_scale= s[1] / 10000.0f;
- scaler.offset= s[2] / 10000.0f;
- scaler.min_output= s[3] / 10000.0f;
- scaler.max_output= s[4] / 10000.0f;
- return 0;
- }
- int SimpleMixer::parse_control_scaler(const char *buf, unsigned &buflen, mixer_scaler_s &scaler, uint8_t &control_group,
- uint8_t &control_index)
- {
- unsigned u[2];
- int s[5];
- buf = findtag(buf, buflen, 'S');//找到剩余缓冲区中的第一个'S',并让buf指向该行的行首;
- //
- //16表示该S:行至少有16个字符,即至少有7个整数(因为整数间至少有1个空格分隔)
- if ((buf == nullptr) || (buflen < 16)) {
- debug("control parser failed finding tag, ret: '%s'", buf);
- return -1;
- }
- //读取S:后面的7个整数.
- if (sscanf(buf, "S: %u %u %d %d %d %d %d",
- &u[0], &u[1], &s[0], &s[1], &s[2], &s[3], &s[4]) != 7) {
- debug("control parse failed on '%s'", buf);
- return -1;
- }
- buf = skipline(buf, buflen);
- if (buf == nullptr) {
- debug("no line ending, line is incomplete");
- return -1;
- }
- //从下面的赋值可以看出MIXER文件S:定义的格式,S:后面的整数分别为
- // [control_group] [ontrol_index] [negative_scale] [positive_scale] [offset] [min_output] [max_output]
- // 可以看出,输入信号的定义比输入出信号的定义多了两个整数,用来表示当前输入信号所在的组和组内的序号. 第1和第2个整就是用来
- // 说明组号和组内序号.而后面5个整数的定义和输入信号的定义一样,且也要除以10000.
- control_group= u[0];
- control_index= u[1];
- scaler.negative_scale= s[0] / 10000.0f;
- scaler.positive_scale= s[1] / 10000.0f;
- scaler.offset= s[2] / 10000.0f;
- scaler.min_output= s[3] / 10000.0f;
- scaler.max_output= s[4] / 10000.0f;
- return 0;
- }
http://www.bkjia.com/PHPjc/1117247.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1117247.htmlTechArticlemixer 结构分析[uavcan为例] mixer指令为系统的app命令,位置在Firmware/src/systemcmds/mixer目录下面,其功能是装载mix文件中的有效内容到具体的设备...