【发布时间】:2014-10-27 08:44:05
【问题描述】:
我试图了解位操作,但经过几个小时的分析 - 仍然没有。
这里是代码:https://github.com/merbanan/rtl_433/blob/master/src/rtl_433.c
但最有趣的部分在这里:
static int prologue_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
int rid;
int16_t temp2;
/* FIXME validate the received message better */
if (((bb[1][0]&0xF0) == 0x90 && (bb[2][0]&0xF0) == 0x90 && (bb[3][0]&0xF0) == 0x90 && (bb[4][0]&0xF0) == 0x90 &&
(bb[5][0]&0xF0) == 0x90 && (bb[6][0]&0xF0) == 0x90) ||
((bb[1][0]&0xF0) == 0x50 && (bb[2][0]&0xF0) == 0x50 && (bb[3][0]&0xF0) == 0x50 && (bb[4][0]&0xF0) == 0x50)) {
/* Prologue sensor */
temp2 = (int16_t)((uint16_t)(bb[1][2] << 8) | (bb[1][3]&0xF0));
temp2 = temp2 >> 4;
fprintf(stderr, "Sensor temperature event:\n");
fprintf(stderr, "protocol = Prologue\n");
fprintf(stderr, "button = %d\n",bb[1][1]&0x04?1:0);
fprintf(stderr, "battery = %s\n",bb[1][1]&0x08?"Ok":"Low");
fprintf(stderr, "temp = %s%d.%d\n",temp2<0?"-":"",abs((int16_t)temp2/10),abs((int16_t)temp2%10));
fprintf(stderr, "humidity = %d\n", ((bb[1][3]&0x0F)<<4)|(bb[1][4]>>4));
fprintf(stderr, "channel = %d\n",(bb[1][1]&0x03)+1);
fprintf(stderr, "id = %d\n",(bb[1][0]&0xF0)>>4);
rid = ((bb[1][0]&0x0F)<<4)|(bb[1][1]&0xF0)>>4;
fprintf(stderr, "rid = %d\n", rid);
fprintf(stderr, "hrid = %02x\n", rid);
fprintf(stderr, "%02x %02x %02x %02x %02x\n",bb[1][0],bb[1][1],bb[1][2],bb[1][3],bb[1][4]);
if (debug_output)
debug_callback(bb);
return 1;
}
return 0;
我不明白什么是 bb[BITBUF_ROWS][BITBUF_COLS]。
算法得到 9 个半字节,这些半字节被解码为一些变量,例如。温度、湿度等
例子取自http://goughlui.com/2013/12/20/rtl-sdr-433-92mhz-askook-decoding-of-various-devices-with-rtl_433/
输入:10010110 01000100 00010000 00010010 10111000
结果:
button: 1
battery: Low
temp: 25.7
humidity: 43
channel:1
id: 9
rid :100
hrid: 64
因为我不知道输入是负数还是 LSB 反转,所以我准备了所有情况的表格:
bin dec neg neg rev rev neg neg
bin dec dec rev rev dec
1001 9 0110 6 1001 9 0110 6
0110 6 1001 9 0110 6 1001 9
0100 4 1011 11 0010 2 1101 13
0100 4 1011 11 0010 2 1101 13
0001 1 1110 14 1000 8 0111 7
0000 0 1111 15 0000 0 1111 15
0001 1 1110 14 1000 8 0111 7
0010 2 1101 13 0100 4 1011 11
1011 11 0100 4 1101 13 0010 2
1000 8 0111 7 0001 1 1110 14
但我绝对不明白温度是 25.7,而根本没有 5。湿度 id 43 但没有 3 值。
我做错了什么?
【问题讨论】:
-
bb[BITBUF_ROWS][BITBUF_COLS]表示该函数接收一个二维数组作为参数。它有BITBUF_ROWS行,每行有BITBUF_COLS列。 -
是的,但是数组的项目是位还是字节?
-
元素为
uint8_t,无符号8位字节。 -
但是BITBUF_ROWS的最高索引是6,而BITBUF_COLS的最高索引是3,所以7x4=28字节远比输入10字节多
-
许多高位半字节似乎被保留(
0x50或0x90)——见第一行if。用肉眼数数,我认为 8 个半字节包含所有这些数据。
标签: c bit-manipulation