#!/usr/bin/env python
import mido
MyMessage = '' # グローバル


def midiToNumber(mido_message):
    number_list = ('1','2','3','4','5','6','7','8','9','0','*','#')
    if mido_message.type != 'note_on':
        return None
    n = mido_message.note % 12
    return number_list[n]


def numberToChar(couple_of_chars):
    convert_list = {
        '11':'あ','12':'い','13':'う','14':'え','15':'お','16':'A','17':'B','18':'C','19':'D','10':'1',
        '21':'か','22':'き','23':'く','24':'け','25':'こ','26':'E','27':'F','28':'G','29':'H','20':'2',
        '31':'さ','32':'し','33':'す','34':'せ','35':'そ','36':'I','37':'J','38':'K','39':'L','30':'3',
        '41':'た','42':'ち','43':'つ','44':'て','45':'と','46':'M','47':'N','48':'O','49':'P','40':'4',
        '51':'な','52':'に','53':'ぬ','54':'ね','55':'の','56':'Q','57':'R','58':'S','59':'T','50':'5',
        '61':'は','62':'ひ','63':'ふ','64':'へ','65':'ほ','66':'U','67':'V','68':'W','69':'X','60':'6',
        '71':'ま','72':'み','73':'む','74':'め','75':'も','76':'Y','77':'Z','78':'?','79':'!','70':'7',
        '81':'や','82':'(','83':'ゆ','84':')','85':'よ','86':'#','87':'*','88':'@','89':'❤︎','80':'8',
        '91':'ら','92':'り','93':'る','94':'れ','95':'ろ','96':'↓','97':'↑','98':':','99':';','90':'9',
        '01':'わ','02':'を','03':'ん','04':'ﾞ','05':'ﾟ','06':'/','07':'ｰ','08':'&','09':' ','00':'0'
        }
    return convert_list.get(str(couple_of_chars))


if __name__ == "__main__":
    # 文字バッファ(数字2文字ぶん)
    char_buf = ''
    # MIDI入力一覧を取得する
    inputs = mido.get_input_names()
    # ラズパイの場合、USB-MIDI(入力)を一つ繋げたとき
    # 2番目にそのデバイス名がくるためindex=1
    with mido.open_input(inputs[1]) as inport:
        for msg in inport:
            n = midiToNumber(msg) # ノート番号から2タッチ入力数字へ変換
            if n is not None: # nがNoneでない(=ノートオン命令のときのみ)
                if len(char_buf) == 0:
                    char_buf = n
                else:
                    char_buf += n
                    new_char = numberToChar(char_buf) # 2数字を文字に変換
                    char_buf = ''
                    if new_char is not None:
                        MyMessage += new_char # 変換可能な2数字であれば送信文に追加
                    else:
                        print(MyMessage) # 変換不可(##など)であれば送信してクリア
                        MyMessage = ''



