星期一, 5月 08, 2006

remote control device 的用法

device node是/dev/ir。
先open :
fno = open("/dev/ir",O_RONLY);
讀取ir的資料:
read(fno,&ldata,sizeof(unsigned long))
ir的key 一次會有4個byte.
  • byte 0 : 公司id
  • byte 1 : custom id
  • byte 2 : code
  • byte 3: ~code 就是byte 2: code反向。
一次讀入4個byte後,如果有需要,檢查一下公司id,確認是同一家的ir。如果有需要,再檢查一下custom code,確認是不是你這家龜毛公司特製的。

然後check一下byte2+byte 3是不是0xFF,
如果是,就可以拿byte 2來動作了。

寫這個code要注意big-endian, little-endia的問題。不然,就要用char 來操作。

因為dev/ir是char device,所以read的方法可以apply一般的file read option。
也可以用select來作timeout read:
fd_set rfds;
struct timeval tv;

FD_ZERO(&rfds); // 初始化flag
FD_SET(fno,&rfds); // 設定待會要動作的file id.

tv.tv_sec=5; // time out 5 sec
tv.tv_usec=0;

ret = select(fno+1, &rfds, NULL, NULL, &tv); // 進行select - wait 動作,max fno 要是wait 的file id +1
if(ret) {
// 收到資料
}else{
// timeout
}

沒有留言:

網誌存檔