├── .gitignore ├── irdata └── .gitkeep ├── LICENSE ├── README.md ├── adrsirlib.py └── ircontrol /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ -------------------------------------------------------------------------------- /irdata/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 tokieng 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # adrsirlib 2 | ビットトレードワン製 ADRSIR 用をPythonから簡単に使うためのライブラリです。   3 | 4 | ## ADRSIRとは 5 |  Raspberry Piに取り付けて、Raspberry Piを学習リモコンにするという、ビットトレードワンの製品です。 6 |  http://bit-trade-one.co.jp/product/module/adrsir/ 7 | 8 | ## ファイルについて 9 | * adrsirlib.py : ライブラリ本体 10 | * ircontrol   : ライブラリの使用例。コマンドライン上で簡単な操作を行えます。 11 | 12 | ## ライブラリの説明 13 | 2つの関数があるだけです。 14 | * get(no) 15 |  指定した番号(0~19)のボタンでADRSIRが記憶している、赤外線データを取り込みます。 16 |  結果は16進数の文字列に変換して返します。 17 | * send(ir_str_data) 18 |  get()で取得した赤外線データの文字列を、ADRSIRに赤外線送信させます。 19 | 20 | ## ライブラリの簡単な使い方 21 | ```python 22 | import adrsirlib as ir 23 | str = ir.get(0) # ボタン0のデータを取り込む 24 | ir.send(str)     # そのデータを赤外線送信する 25 | ``` 26 | 27 | ## ircontrolの使い方 28 | `./ircontrol <コマンド> [<オプション>...]` 29 | * storeコマンド 30 | ADRSIRから赤外線データを取り込み、ファイルに保存します。 31 | オプションに、ボタン番号とファイル名を、0:filename のようにコロンで区切って指定します。複数指定できます。   32 | 例: `./ircontrol store 0:power 1:volume_up 2:volume_down` 33 | * sendコマンド 34 | 保存した赤外線データを、ADRSIRに送信させます。   35 | オプションには、保存したファイル名を指定します。1つだけ指定します。 36 | 例: `./ircontrol send power` 37 | * listコマンド 38 | 保存した赤外線データの一覧を表示します。 39 | 例: `./ircontrol list` 40 | 41 | ## もしかしたら 42 | Raspberry Pi 1では、adrsirlib.pyを書き換えないと動かないかも? 43 | ``` 44 | bus = smbus.SMBus(1) 45 | ↓ 46 | bus = smbus.SMBus(0) 47 | ``` 48 | 49 | ## 参考文献 50 | ビットトレードワンによる以下のブログ記事を参考にしました。 51 | http://bit-trade-one.co.jp/blog/2017121302/ 52 | 53 | ## ライセンス 54 | 切ったり貼ったり、お好きに使ってください。 55 | いちおうMITライセンスってしておきますが、著作権表記等も一切不要です。 56 | -------------------------------------------------------------------------------- /adrsirlib.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | # ADRSIR用ライブラリ 4 | # Copyright 2018 tokieng 5 | # MIT License 6 | # 7 | # 準備: sudo apt-get install python3-smbus 8 | 9 | ## 参考文献 10 | # http://bit-trade-one.co.jp/blog/2017121302/ 11 | # ビット・トレード・ワン社提供のラズベリー・パイ専用 学習リモコン基板(型番:ADRSIR)用のツール 12 | # 著作権者:(C) 2015 ビット・トレード・ワン社 13 | # ライセンス: ADL(Assembly Desk License) 14 | 15 | 16 | import smbus 17 | 18 | # ADRSIRのI2Cアドレス 19 | ADDR = 0x52 20 | 21 | # コマンド群 22 | R1_memo_no_write = 0x15 23 | R2_data_num_read = 0x25 24 | R3_data_read = 0x35 25 | W2_data_num_write = 0x29 26 | W3_data_write = 0x39 27 | T1_trans_start = 0x59 28 | 29 | bus = smbus.SMBus(1) 30 | 31 | def get(no): 32 | ### ●赤外線データ長読み出し手順 33 | senddata = [no] 34 | bus.write_i2c_block_data(ADDR, R1_memo_no_write, senddata) 35 | data = bus.read_i2c_block_data(ADDR, R2_data_num_read, 3) # data = [0, データ長H, データ長L] 36 | read_length = (data[1] << 8) + data[2] # データのバイト数は、 read_length * 4 になる 37 | 38 | if read_length == 0xffff: 39 | return [] 40 | 41 | ### ●赤外線データ読み出し手順 42 | # 1バイト目は常に0が得られるっぽいから、読んで捨てる 43 | data = bus.read_i2c_block_data(ADDR, R3_data_read, 1) 44 | 45 | # read_length分繰り返す。 46 | ir_data = [] 47 | for i in range(read_length): 48 | data = bus.read_i2c_block_data(ADDR, R3_data_read, 4) # 4バイトずつ読むらしい 49 | ir_data.extend(data) 50 | 51 | ir_str_data = '' 52 | for i in ir_data: 53 | ir_str_data += "{:02x}".format(i) 54 | return ir_str_data 55 | 56 | 57 | def send(ir_str_data): 58 | 59 | ir_data = [int(ir_str_data[i:i+2],16) for i in range(0,len(ir_str_data),2)] 60 | 61 | # ●赤外線データ長書き込み手順 62 | data_length = len(ir_data) 63 | senddata = [data_length >> 8, data_length & 0xff] 64 | bus.write_i2c_block_data(ADDR, W2_data_num_write, senddata) 65 | 66 | # read_length分繰り返す。 67 | for i in range(0, data_length, 4): 68 | bus.write_i2c_block_data(ADDR, W3_data_write, ir_data[i:i+4]) # 4バイトずつ 69 | 70 | bus.write_i2c_block_data(ADDR, T1_trans_start, [0] ) #= 71 | 72 | ##### 73 | 74 | if __name__ == '__main__': 75 | str = get(0) 76 | print(str) 77 | send(str) 78 | -------------------------------------------------------------------------------- /ircontrol: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | #coding: utf-8 3 | 4 | import adrsirlib as ir 5 | import os 6 | import sys 7 | 8 | DATA_DIR = os.path.join(os.path.dirname(__file__), "irdata") 9 | 10 | def read_to_file(no, filename): 11 | str = ir.get(no) 12 | path = os.path.join(DATA_DIR, filename) 13 | with open(path, 'wt') as fout: 14 | fout.write(str) 15 | 16 | def send_from_file(filename): 17 | path = os.path.join(DATA_DIR, filename) 18 | if not os.path.isfile(path): 19 | print("Not exist.") 20 | return 21 | 22 | print(path) 23 | with open(path, 'rt') as fin: 24 | str = fin.read() 25 | ir.send(str) 26 | 27 | def display_list(): 28 | print("保存されているIRデータ一覧:") 29 | 30 | files = os.listdir(DATA_DIR) 31 | 32 | if len(files) == 0: 33 | print ("ありません") 34 | else: 35 | for file in files: 36 | if file[0] !=".": 37 | print (" * {}".format(file)) 38 | 39 | ##### 40 | 41 | def usage(): 42 | print("\n使い方: {0} [options...]\n".format(__file__)) 43 | print(" コマンド") 44 | print(" store: ADRSIRからIRデータを読み取って、ファイルに保存する") 45 | print(" <ボタン番号>:<ファイル名> [<ボタン番号>:<ファイル名>...]") 46 | print(" ※ボタン番号 = 0~19") 47 | print(" ex) {0} store 0:power 1:volume_up 2:volume_down".format(__file__)) 48 | print(" send : ファイルに保存しているデータを、ADRSIRに送ってIR送信する") 49 | print(" ex) {0} send volume_up".format(__file__)) 50 | print(" list : ファイルに保存しているデータの一覧を表示する") 51 | print(" ex) {0} list".format(__file__)) 52 | 53 | def store(list): 54 | for one in list: 55 | ones = one.split(':') 56 | if len(ones) != 2: 57 | print("Skip: {}".format(one)) 58 | continue 59 | no = ones[0] 60 | file = ones[1] 61 | if (not no.isdigit()) or int(no)<0 or 19