BV1AL 之無所不記

2012-12-31

網樂通遙控器好用

朋友送的網樂通有兩支遙控器,這下遙控器多了,就拿來遙控電腦吧!

原本給arduino 用的 IRremote library 可以在這裡下載
https://github.com/shirriff/Arduino-IRremote
而msp430 用的energia 也已經內含 IRremote
http://energia.nu/download/

網樂通新、舊這兩支遙控器都是使用NEC protocol, IRremote 裡已有支援,
可以直接解碼出來,兩支有一些鍵同碼,一些鍵不同碼。

參考這個例子
https://github.com/shirriff/Arduino-IRremote/blob/master/examples/IRrecvDump/IRrecvDump.ino
把它寫入msp430 G2553|G2452 (或Arduino) 就可以解網樂通遙控器的碼。

那支長得像Wii 的我稱它做舊式,另一支跟一般電視遙控器相似的我稱它為新式。
舊式多了「扳機」鍵,新式多了方型的「紅、藍、綠、橘」及「加、減、向上、向下」

 舊式  新式
紅鍵 CE941AE5 CE9454AB
藍鍵 CE9402FD CE94D42B
綠鍵 CE9400FF CE9434CB
橘鍵 CE94C03F CE94B44B
上   CE94906F CE9404FB
下   CE94807F CE94847B
左   CE9450AF CE9444BB
右   CE9410EF CE94C43B
扳機 CE9432CD
紅(方)        CE945AA5
藍(方)        CE94BA45
綠(方)        CE94DA25
橘(方)        CE943AC5
加            CE945CA3
減            CE94DC23
向上          CE9452AD
向下          CE94D22D
 
電腦這一端可以用 py-serial module 來建立呼叫動作,搭配 xdotool 就可以控制電腦了。

import os
from serial import Serial
ser = Serial("/dev/ttyACM0", 9600)

while 1:
    L = ser.readline()
    if L[4:8]=='906F' or L[4:8]=='04FB' or L[4:8]=='5CA3':
       os.system('xdotool key Up')
    # 選一個鍵來啟用mouse cursor, 例如 Mute
    elif ..................
    elif L[4:8]=='40BF':
       # 再利用 xdotool getmouselocation 來讀取 mouse cursor 的x, y 值
       X = os.popen('xdotool getmouselocation').read().strip().split()
       mx=int(X[0][2:])
       my=int(X[1][2:])
       N = ser.readline()
       V=15
       if N[4:8]=='FFFF': # 當按住鍵不方時,它會丟出 FFFFFFFF, 就可以利用它來加速
   N='xxxx'+LAST
   V=W*1.5
       if N[4:8]=='906F' or N[4:8]=='04FB':
   os.system('xdotool mousemove %s %s'%(mx, my -V))
   W=V
   LAST=N[4:8]
       elif ...............
       # 當按下紅鍵或藍鍵就中止 mouse cursor
       elif N[4:8]=='54AB' or N[4:8]=='1AE5':
    os.system('xdotool click 1')
   break
    elif L[4:8]=='06F9': # 利用 Search 鍵來輸出數字
       while 1:
   M = ser.readline()
   if M[4:8]=='708F':
      os.system('xdotool key 1')
   elif ............
    # 利用「紅、藍、綠、桶」四個方鍵來搭配輸出英文字母
    elif L[4:8]=='5AA5':
       C = ser.readline()
       if C[4:8]=='F00F': # 數字鍵 2
   os.system('xdotool key a')
       elif C[4:8]=='30CF': # 數字鍵 3
   os.system('xdotool key d')
       elif ...........
    elif L[4:8]=='DA25':
       C = ser.readline()
       if C[4:8]=='F00F':
   os.system('xdotool key b')
       elif C[4:8]=='30CF':
   os.system('xdotool key e')
       elif ........... 
 其他像是可以用 os.system('xdotool key ctrl+Right') 或
os.system('xdotool key ctrl+Left') 來做出按鍵

這裡可以 chmod 666 /dev/ttyACM0 讓一般user 的權限就能遙控

這樣就可以當成鍵盤使用了,不過感覺舊式遙控器比較好按,但新式遙控器鍵比較多。