ラズパイ・PICOによるモールス会話器
ラズパイ・PICOによるモールス会話器
プログラムを貼っておきます、main_morse_comm_20240823.py
file=main_morse_comm_****.py
v1.0 2022/8/17 jh1cdv
v1.1 2022/9/1 key reverce
v2.0 2022/12/16
V2.1 2022/12/18 表示を変更
V3.0 2023/5/8 送信_I2C_KBを追加
V3.1 2023/5/22 送信_speed_volを追加
V4.0 2023/10/11 makerfaire用に
V5.0 2024/2/3 yoxo_festibal用に
V5.1 2024/2/24 jh1ymc用 morse練習器に
V5.2 2024/2/24 jh1ymc用 morse会話器に
v6.1 2024/8/17 ハムフェア用 main_morse_traner_20240817.py
v6.2 2024/8/23 ハムフェア用 main_morse_comm_****.py
L0:モールス信号をサンプリングして、時間測定
L1:モールス符号の短点(.:dot)、長点(ー:bar) 認識
L2:モールス符号の文字認識
L3:モールス符号の単語認識
L4:コマンドの認識
L5:モールス符号をローマ字→漢字変換→リモコンコマンド
rx_tx_morse.pyにモールス練習機機能を追加
#
in
out
'''
サンプリング周期(mSec)と通信速度(字/分)の関係
・短点50個で、PARIS符号 5文字に相当→短点10個/文字
・通信速度60字/分→60*10=600個/分→600/60=10個/秒
→短点が100mSecの時、通信速度60字/分
'''
ライブラリの import------------------------------------------------------------------------
import json
#from lcd1602 import LCD1602
from lcd_i2c import LCD
from machine import I2C,Pin,ADC,UART,PWM
from utime import sleep
import select
import _thread
import time
from ssd1306 import SSD1306_I2C
from display import PinotDisplay
from pnfont import Font
辞書定義
受信モールス辞書
rx_codes = {
".-" : "a", "-..." : "b", "-.-." : "c",
"-.." : "d", "." : "e", "..-." : "f",
"--." : "g", "...." : "h", ".." : "i",
".---" : "j", "-.-" : "k", ".-.." : "l",
"--" : "m", "-." : "n", "---" : "o",
".--." : "p", "--.-" : "q", ".-." : "r",
"..." : "s", "-" : "t", "..-" : "u",
"...-" : "v", ".--" : "w", "-..-" : "x",
"-.--" : "y", "--.." : "z",
".----" : "1", "..---" : "2","...--" : "3", "....-" : "4", "....." : "5",
"-...." : "6", "--..." : "7","---.." : "8", "----." : "9", "-----" : "0",
"...---..." : "SOS"
}
print(rx_codes.get(".-"))
受信ローマ字辞書
roomaji_codes = {
"a" : "あ", "i" : "い", "u" : "う","e" : "え", "o" : "お",
"ka" : "か", "ki" : "き", "ku" : "く","ke" : "け", "ko" : "こ",
"sa" : "さ", "si" : "し", "su" : "す","se" : "せ", "so" : "そ",
"ta" : "た", "ti" : "ち", "tu" : "つ","te" : "て", "to" : "と",
"na" : "な", "ni" : "に", "nu" : "ぬ","ne" : "ね", "no" : "の",
"ha" : "は", "hi" : "ひ", "hu" : "ふ","he" : "へ", "ho" : "ほ",
"ma" : "ま", "mi" : "み", "mu" : "む","me" : "め", "mo" : "も",
"ya" : "や", "yi" : "い", "yu" : "ゆ","ye" : "いぇ", "yo" : "よ",
"ra" : "ら", "ri" : "り", "ru" : "る","re" : "れ", "ro" : "ろ",
"wa" : "わ", "wi" : "うぃ", "we" : "うぇ", "wo" : "を", "nn" : "ん"
}
print(roomaji_codes.get("a"))
送信モールス辞書
codes = {
"a" : ".-", "b" : "-...", "c" : "-.-.",
"d" : "-..", "e" : ".", "f" : "..-.",
"g" : "--.", "h" : "....", "i" : "..",
"j" : ".---", "k" : "-.-", "l" : ".-..",
"m" : "--", "n" : "-.", "o" : "---",
"p" : ".--.", "q" : "--.-", "r" : ".-.",
"s" : "...", "t" : "-", "u" : "..-",
"v" : "...-", "w" : ".--", "x" : "-..-",
"y" : "-.--", "z" : "--..",
"1" : ".----", "2" : "..---", "3" : "...--", "4" : "....-", "5" : ".....",
"6" : "-....", "7" : "--...", "8" : "---..", "9" : "----.", "0" : "-----"
}
送信用マクロ文字列
tx_macro = {
"0" : "parisparis",
"1" : "cq cq de jh1cdv",
"2" : "konnnitiha",
"3" : "ko nn ni tri wa",
"4" : "macro4",
"5" : "macro5",
"6" : "macro6",
"7" : "macro7",
"8" : "macro8",
"9" : "macro9"
}
通信速度 WPS:words per minute 60字/分⇒100msec
wps_num = {"0" : "10","1" :"20", "2" : "30","3": "40","4":"45", "5" :"50","6" :"60","7":"70","8":"80","9":"90"}
通信clk WPS:words per minute 60字/分⇒100msec
wps_clk = {"0" : 600, "1" : 300, "2" : 200,"3" : 150,"4" : 133, "5" : 120,"6" : 100,"7" : 85,"8" : 75,"9" : 67}
hw Pin設定--------------------------------------------------------------------------------
#I2C_0
sda0=machine.Pin(12)
scl0=machine.Pin(13)
i2c0=machine.I2C(0,sda=sda0,scl=scl0,freq=400000)
#i2c0 =machine.I2C(0,sda=sda0,scl=scl0,freq=100000)
#I2C_1
sda1=machine.Pin(14)
scl1=machine.Pin(15)
i2c1=machine.I2C(1,sda=sda1,scl=scl1,freq=400000)
#I2Cデバイス試験
devices0 = i2c0.scan()
if len(devices0) == 0:
print("No i2c device !")
else:
print('i2c0 devices found:',len(devices0))
for device0 in devices0:
print("Decimal address: ",device0," | Hexa address: ",hex(device0))
print("--------------------------")
devices1 = i2c1.scan()
if len(devices1) == 0:
print("No i2c1 device !")
else:
print('i2c1 devices found:',len(devices1))
for device1 in devices1:
print("Decimal address: ",device1," | Hexa address: ",hex(device1))
time.sleep_ms(1000)
PCF8574 on 0x50
I2C_ADDR = 0x3F # DEC 39, HEX 0x27
NUM_ROWS = 4
NUM_COLS = 20
define custom I2C interface, default is 'I2C(0)'
check the docs of your device for further details and pin infos
#i2c = I2C(0, scl=Pin(13), sda=Pin(12), freq=800000)
lcd = LCD(addr=I2C_ADDR, cols=NUM_COLS, rows=NUM_ROWS, i2c=i2c0)
time.sleep_ms(1000)
lcd.begin() #address : 0x3cを接続すると、OSError: [Errno 5] EIO
print('time.sleep_ms(1000)start')
time.sleep_ms(1000)
#LCD_para = '{"row0":"Set row 0, column 0", "row1":"Set row 1, column 0","row2":"Set row 2, column 0","row3":"Set row 3, column 0"}'
LCD_para = '{"row0":" ", "row1":" ","row2":" ","row3":" "}' #lcd初期化の為スペース
LCD_json = json.loads(LCD_para)
#lcd .clear()
lcd.set_cursor(col=0, row=0)
lcd.print(LCD_json["row0"])
lcd.set_cursor(col=0, row=1)
lcd.print(LCD_json["row1"])
lcd.set_cursor(col=0, row=2)
lcd.print(LCD_json["row2"])
lcd.set_cursor(col=0, row=3)
lcd.print(LCD_json["row3"])
#lcd .no_cursor()
time.sleep_ms(1000) #address : 0x3c を接続するとOSError: [Errno 5] EIO、
日本語表示 ssd1306 全角10文字3行
ssd = SSD1306_I2C(128, 32, i2c1)
fnt = Font('/fonts/shnmk12u.pfn')
disp = PinotDisplay(panel = ssd, font = fnt)
#SSD_para = '{"row0":"日本語表示", "row1":"モールス会話器","row2":"試験開始567890"}'
SSD_para = '{"row0":"row0", "row1":"row1","row2":"row2"}' #
SSD_json = json.loads(SSD_para)
disp.locate(1, 0)
disp.text(SSD_json["row0"])
disp.locate(1, 11)
disp.text(SSD_json["row1"])
disp.locate(1, 22)
disp.text(SSD_json["row2"])
text_kb の設定
text_kb = i2c0.readfrom(0x5F, 1)
print('text_kb type=',type(text_kb)) #<class 'bytes'>
text_kb_str = str(text_kb,'shift_jis')
print('str_text_kb type=',type(text_kb_str)) #<class 'bytes'>
#print (text_kb)
#print (type(text_kb)) #<class 'bytes'>
print('text_kb_i2c5F=',text_kb_str)
#シリアルの設定
#シリアル0 u = UART(0) # 115200bps
u = UART(0,baudrate=115200,tx=Pin(0),rx=Pin(1)) # 115200bps
#u = UART(0,baudrate=9600,tx=Pin(0),rx=Pin(1)) # 9600bps
#シリアル1 ローマ字で出力すると、音声発生
lcd.set_cursor(col=0, row=2)
lcd.print("onsei_out")
u1 = UART(1,baudrate=9600,tx=Pin(4),rx=Pin(5)) # 9600bps
#text_kb_str = str(text_kb,'shift_jis')
text_kb_str = str("a" ,'shift_jis')
print('text_kb_str',text_kb_str)
u1.write(text_kb_str + "\n\r")
#u .write("morse_traner_start" + "\n\r")
u.write("morse_traner_start" + "\n\r")
LED = Pin(20,Pin.OUT)
LED.value(1)
REL = Pin(16,Pin.OUT)
REL.value(1)
sleep(1)
REL.value(0)
#sleep (1)
time.sleep_ms(1000)
LED.value(0)
macro 入力の設定
macro_ad0 = ADC(0)
print('macro=',macro_ad0.read_u16(),str(int(macro_ad0.read_u16()/6400010)))
print(tx_macro.get(str(int(macro_ad0.read_u16()/6400010))))
#sleep (1)
#light = ADC(1)
#print (light.read_u16())
#sleep (1)
#for i in range(30): #試験用に300程度 、
#print (light.read_u16())
#sleep (0.1)
#sound = ADC(2)
#print (sound.read_u16())
#sleep (1)
speed(入力)の設定です。
speed_ad1 = ADC(1)
#print ('speed=',speed.read_u16())
print('wps_num=',wps_num.get(str(int(speed_ad1.read_u16()/6400010))))
print('wps_clk=',wps_clk.get(str(int(speed_ad1.read_u16()/6400010))))
sleep(1)
key(出力)の設定です。
key_out = machine.Pin(8, machine.Pin.OUT)
key_out.value(0)
sleep(1)
#key_out .value(1)
key(入力)の設定です。
key_in = machine.Pin(9, machine.Pin.IN)
print('key_in=',key_in.value())
sleep(1)
macro_key(入力)の設定です。
macro_key_in = machine.Pin(17, machine.Pin.IN)
print('macro_key_in=',macro_key_in.value())
sleep(1)
buz = PWM(Pin(18))
#buz = PWM(Pin(8))
buz.freq(1046)
buz.duty_u16(1000)
#sleep (1)
buz.duty_u16(0)
JSON変数の定義、初期化
サンプリング回数100(y)/サンプリング周期1mSec(x)/100*60=60字/分
L0_para = '{"L0_sts0":0, "L0_sts1":0,"L0_1ctr":0,"L0_0ctr":0}'
#L0_para = '{"L0_sts0":0, "L0_sts1":0,"L0_1ctr":0,"L0_0ctr":10000}' # L0_0ctr の初期値を 10000mS→10秒とする
#L0_para = '{"L0_sts0":0, "L0_sts1":0,"L0_1ctr":0,"L0_0ctr":1000}' # L0_0ctr の初期値を 1000mS→1秒とする
#L1_para = '{"L1_avr":10, "L1_dot_bar":"."}' #最初にごみの様に入ってしまうが苦肉の策 l1_avr=4 time.sleep_ms(20) #dot 2
#L1_para = '{"L1_avr":10, "L1_dot_bar":"."}' #最初にごみの様に入ってしまうが苦肉の策 l1_avr=8 time.sleep_ms(10) #dot 6 dash 17
L1_para = '{"L1_avr":8, "L1_dot_bar":"."}' #100ms L1_avr":20 はdot=10
#L2_para = '{"L2_char":"a","L2_3dot":6}'
L2_para = '{"L2_char":"a","L2_3dot":12}' #文字の切れ目 l2_3dotは短くて良いSyntaxError: invalid syntax?
#L3_para = '{"L3_word":"a","L3_7dot":14,"L3_words":"s:","L3_100dot":500}'
L3_para = '{"L3_word":"a","L3_7dot":120,"L3_words":"s:","L3_100dot":30}' #L3_7dot ":70→120
L4_para = '{"L4_cmd":"SOS"}'
L5_para = '{"L5_roomaji":"あ"}'
#in_para = '{"key_centor":4000, "light_centor":25000,"sound_centor": 8700}'
in_para = '{"key_centor":2000, "light_centor":30000,"sound_centor": 8700}'
out_para = '{"speed":0.2, "char_min":30}'
L0_json = json.loads(L0_para)
time.sleep_ms(100)
L1_json = json.loads(L1_para)
time.sleep_ms(100)
L2_json = json.loads(L2_para)
time.sleep_ms(100)
L3_json = json.loads(L3_para)
time.sleep_ms(100)
L4_json = json.loads(L4_para)
time.sleep_ms(100)
L5_json = json.loads(L5_para)
time.sleep_ms(100)
in_json = json.loads(in_para)
time.sleep_ms(100)
out_json = json.loads(out_para)
time.sleep_ms(100)
JSON変数の定義、初期化 モールス練習機用
dot_para = '{"dot_min":10000, "dot_ave":10,"dot_max":1,"dot_sum":1,"dot_num":1,"dot_rasio":1}' #0はdevide byzero
dot_json = json.loads(dot_para)
time.sleep_ms(100)
#dash_para = '{"dash_min":10000, "dash_ave":0,"dash_max":0,"dash_sum":0,"dash_num":1,"dash_rasio":0}'
dash_para = '{"das_min":10000, "das_ave":30,"das_max":1,"das_sum":1,"das_num":1,"das_rasio":1}'
#{"dash_min": 13, "dash_rasio": -19, "dash_max": 44, "dash_sum": 266, "dash_ave": 26, "d ""}\n\rd": 10} dash_numが壊れる???
dash_json = json.loads(dash_para)
time.sleep_ms(100)
traner_para = '{"traner_num":10,"traner_ctr":0,"traner_dot_das_rasio":0,"traner_speed":25}' # 試験用3
traner_json = json.loads(traner_para)
time.sleep_ms(100)
LCD_json["row0"] = "morse_dec_2"
print('wps_clk=',wps_clk.get(str(int(speed_ad1.read_u16()/6400010))))
dot_duration = wps_clk.get(str(int(speed_ad1.read_u16()/6400010)))
print('dot_duration =',dot_duration)
#dot_duration_ms = 200
#dash_duration = dot_duration * 3.0
#word_gap = dot_duration * 7.0
dash_duration = dot_duration * 3
word_gap = dot_duration * 7
durations = {"." : dot_duration, "-" : dash_duration}
def lcd_disp():
#lcd .begin()
#lcd .clear()
lcd.set_cursor(col=0, row=0)
lcd.print(LCD_json["row0"])
lcd.set_cursor(col=0, row=1)
lcd.print(LCD_json["row1"])
lcd.set_cursor(col=0, row=2)
lcd.print(LCD_json["row2"])
lcd.set_cursor(col=0, row=3)
lcd.print(LCD_json["row3"])
#lcd .no_cursor()
def lcd_disp2():
#lcd .begin()
#lcd .clear()
lcd.set_cursor(col=0, row=0)
lcd.print(LCD_json["row0"])
lcd.set_cursor(col=0, row=1)
lcd.print(LCD_json["row1"])
#lcd .set_cursor(col=0, row=2)
#lcd .print(LCD_json["row2"])
#lcd .set_cursor(col=0, row=3)
#lcd .print(LCD_json["row3"])
#lcd .no_cursor()
def ssd_disp():
disp.locate(1, 0)
disp.text(SSD_json["row0"])
disp.locate(1, 11)
disp.text(SSD_json["row1"])
disp.locate(1, 22)
disp.text(SSD_json["row2"])
def send_pulse(dot_or_dash):
#
#dot_duration = 0.2
#dot_duration = 0.5123456789
#print ('speed=',speed.read_u16())
#print ('speed=',(speed.read_u16()/65000)*1.0+0.01)
#print(str(speed_ad1.read_u16()))
#print("speed=" + str((speed_ad1.read_u16()/65000)*10.0+0.05))
#dot_duration = (speed_ad1.read_u16()/65000)*10.0+0.05
#dot_duration = (speed.read_u16()/65000)*1.0+0.01
#dot_duration =
#dot_duration = wps_clk.get(str(int(speed_ad1.read_u16()/64000*10)))
dot_duration = int(wps_clk.get(str(int(speed_ad1.read_u16()/64000*10))))
print('dot_duration =',dot_duration)
#dot_duration_ms = 200
dash_duration = dot_duration * 3
word_gap = dot_duration * 7
durations = {"." : dot_duration, "-" : dash_duration}
if dot_or_dash == '.':
delay = dot_duration
else:
delay = dash_duration
#buz.freq(1046)
#buz.duty_u16(1000)
key_out.value(1)
#REL.value(1)
#sleep(delay)
time.sleep_ms(delay)
#buz.duty_u16(0)
key_out.value(0)
#REL.value(0)
#sleep(dot_duration)
time.sleep_ms(dot_duration)
#sleep(0.05)
#sleep(delay)
def send_morse_for(character):
if character == ' ':
#sleep (word_gap)
time.sleep_ms(word_gap)
else:
dots_n_dashes = codes.get(character.lower())
if dots_n_dashes:
print(character + " " + dots_n_dashes)
for pulse in dots_n_dashes:
send_pulse(pulse)
#sleep (dash_duration)
time.sleep_ms(dash_duration)
else:
print("unknown character: " + character)
#def core1():
#text = "a"
text_byte = "null"
text_moji = "null"
def core1_thread():
u.write("morse_traner_start" + "\n\r")
#speed = ADC(1)
#print ('speed=',speed.read_u16())
#out_json ["speed"] = str((speed.read_u16()/65000)100+10)
#SSD_json ["row1"] = ('wps_clk=',str(wps_clk.get(str(int(speed_ad1.read_u16()/6400010)))))
speed = wps_clk.get(str(int(speed_ad1.read_u16()/64000*10)))
#print ('speed=',speed.read_u16())
out_json["speed"] = str(speed)
print(type(out_json["speed"]))
print(out_json["speed"])
print('macro=',macro_ad0.read_u16(),str(int(macro_ad0.read_u16()/64000*10)))
print(tx_macro.get(str(int(macro_ad0.read_u16()/64000*10))))
macro_int0 = int(macro_ad0.read_u16()/64000*10)
print('macro_int0=',macro_int0)
text = "end" #この行がないとNameError: local variable referenced before assignment
while True:
#print('macro_int0=',macro_int0)
macro_int1 = int(macro_ad0.read_u16()/64000*10)
if macro_int0 == macro_int1 :
text_kb_0 = ('NULL')
#print('macro_int0=macro_int1')
else:
print(tx_macro.get(str(int(macro_ad0.read_u16()/64000*10))))
LCD_json["row3"] = str(int(macro_ad0.read_u16()/64000*10)) + ":"+ tx_macro.get(str(int(macro_ad0.read_u16()/64000*10)))
lcd_disp()
macro_int0 = macro_int1
text_kb = i2c0.readfrom(0x5F, 1)
text_kb_str = str(text_kb,'shift_jis')
if text_kb == bytes(b'\x00') :
text_kb_0 = ('NULL')
#print('kb_input_none') #連続出力になってしまう
else:
text = str(text_kb,'shift_jis')
print("kb_input=",text)
if macro_key_in.value() == 0 :
#text = "parisparis"
text = tx_macro.get(str(int(macro_ad0.read_u16()/64000*10)))
#print('speed=',(speed.read_u16()/65000)+0.1)
#
#dot_duration = 0.2
#dot_duration = 0.5123456789
#dot_duration = (speed.read_u16()/65000)+0.1
#dot_duration = (speed.read_u16()/65000)+0.1
dot_duration = wps_clk.get(str(int(speed_ad1.read_u16()/64000*10)))
print('dot_duration =',dot_duration)
#print(dot_duration)
#dot_duration_ms = 200
dash_duration = dot_duration * 3
word_gap = dot_duration * 7
durations = {"." : dot_duration, "-" : dash_duration}
#print(type(text))
#print("4:" + text)
if text == "end":
text__0 = ('NULL')
else:
for character in text:
send_morse_for(character)
print("tx_end: ")
text = "end"
text = str(text,'shift_jis')
print(type(text))
print(text)
ssd_disp() #20240823
sleep(0.1) #
#sleep(1) #I2C_KBの連続打ちができない
_thread.start_new_thread(core1_thread, ())
print("start_main")
LCD_json["row0"] = "start_main"
lcd_disp()
print('wps_num=',wps_num.get(str(int(speed_ad1.read_u16()/6400010))))
print('wps_clk=',wps_clk.get(str(int(speed_ad1.read_u16()/6400010))))
#SSD_json ["row0"] = ('wps_num=',wps_num.get(str(int(speed_ad1.read_u16()/6400010))))
#SSD_json ["row1"] = ('wps_clk=',str(wps_clk.get(str(int(speed_ad1.read_u16()/6400010)))))
#ssd_disp ()
while True:
time.sleep_ms(9)
L0_json["L0_sts1"] = L0_json["L0_sts0"]
L0_json["L0_sts0"] = 0
#if key.read_u16() > in_json["key_centor"]:
#if key.read_u16() < in_json["key_centor"]:
#L0_json ["L0_sts0"] = 1
#if light.read_u16() > in_json["light_centor"]:
#L0_json ["L0_sts0"] = 1
#print ('key_in=',key_in.value())
#key = key_in.value()
#print (key)
if key_in.value() == 0:
L0_json["L0_sts0"] = 1
if L0_json["L0_sts0"] == 1:
#LED .value(1)
L0_json["L0_sts0"] = 1
else:
#LED .value(0)
L0_json["L0_sts0"] = 0
#0 →0
if ((L0_json["L0_sts1"] == 0) and (L0_json["L0_sts0"] == 0)):
L0_json["L0_0ctr"] += 1
L0_json["L0_1ctr"] = 0
#1 →1
if ((L0_json["L0_sts1"] == 1) and (L0_json["L0_sts0"] == 1)):
#L0_json ["L0_1ctr"] = L0_json["L0_1ctr"] + 1
L0_json["L0_1ctr"] += 1
L0_json["L0_0ctr"] = 0
#変化を検出 #1→0 立下り検出
if ((L0_json["L0_sts1"] == 1) and (L0_json["L0_sts0"] == 0)):
if (L0_json["L0_1ctr"] > 0): #1 →0 立下り検出
#if ((L0_json["L0_1ctr"]-L1_json["L1_avr"]) == 0):
# L1_json["L1_avr"] = int(L1_json["L1_avr"])
#else :
# L1_json["L1_avr"] = int(L1_json["L1_avr"] + (L0_json["L0_1ctr"]-L1_json["L1_avr"])/10)
#if (L0_json["L0_1ctr"] > 1): #1 →0 立下り検出
#if (L0_json["L0_1ctr"] < L1_json["L1_avr"]):
# L1_json["L1_avr"] = L1_json["L1_avr"] + 1
#else :
# L1_json["L1_avr"] = L1_json["L1_avr"] - 1 #足せるが 、引けない
if (L0_json["L0_1ctr"] < L1_json["L1_avr"]):
L1_json["L1_dot_bar"] = L1_json["L1_dot_bar"] + "."
#traner
dot_json["dot_sum"] = dot_json["dot_sum"] + L0_json["L0_1ctr"]
dot_json["dot_num"] += 1
if (dot_json["dot_min"] > L0_json["L0_1ctr"]):
dot_json["dot_min"] = L0_json["L0_1ctr"]
if (dot_json["dot_max"] < L0_json["L0_1ctr"]):
dot_json["dot_max"] = L0_json["L0_1ctr"]
else:
L1_json["L1_dot_bar"] = L1_json["L1_dot_bar"] + "-"
#L2_json ["L2_3dot"] = L0_json["L0_1ctr"] #3dot1 =3dot
#L3_json ["L3_7dot"] = L0_json["L0_1ctr"] * 3 #3dot3 =9dot 要調整
#traner
dash_json["das_sum"] = dash_json["das_sum"] + L0_json["L0_1ctr"]
#dash_json ["das_num"] = dash_json["das_num"] + 1
dash_json["das_num"] += 1
if (dash_json["das_min"] > L0_json["L0_1ctr"]):
dash_json["das_min"] = L0_json["L0_1ctr"]
if (dash_json["das_max"] < L0_json["L0_1ctr"]):
dash_json["das_max"] = L0_json["L0_1ctr"]
L0_json["L0_0ctr"] = 0
#0 →1 立上がり検出
if ((L0_json["L0_sts1"] == 0) and (L0_json["L0_sts0"] == 1)): #0 →1 立上がり検出 l0_1ctが0にならない???
L0_json["L0_1ctr"] = 0
#文字の切れ目
if (L0_json["L0_0ctr"] == L2_json["L2_3dot"]) : #文字の切れ目 l2_3dotは短くて良い
if (L1_json["L1_dot_bar"] == ""):
u.write("dot_bar_null" + json.dumps(L1_json) + "\n\r") #文字の切れ目でデータなしの時
else:
if rx_codes.get(L1_json["L1_dot_bar"]):
L2_json["L2_char"] = rx_codes.get(L1_json["L1_dot_bar"])
L3_json["L3_word"] = L3_json["L3_word"] + L2_json["L2_char"]
#d .clear()
#d .setCursor(0,0)
#d .print("RX:" + str(L1_json["L1_dot_bar"]) + ":" + str(L3_json["L3_word"]) + " " + str(out_json["char_min"]))
#d .print("RX:" + str(L1_json["L1_dot_bar"]) + ":" + str(L2_json["L2_char"]) )
LCD_json["row0"] = "RX:" + str(L1_json["L1_dot_bar"]) + ":" + str(L2_json["L2_char"])
SSD_json["row0"] = LCD_json["row0"]
lcd_disp()
print("moji_end_L1_dot_bar:" + str(L1_json["L1_dot_bar"]) + " : " + str(L2_json["L2_char"]) + " : "+ str(L3_json["L3_word"]) + " " + str(out_json["char_min"]))
if (str(L2_json["L2_char"]) == "SOS") :
print("SOS_detect=")
LED = Pin(20,Pin.OUT)
LED.value(1)
buz.duty_u16(1000)
time.sleep_ms(3000)
buz.duty_u16(0)
LED.value(0)
L1_json["L1_dot_bar"] = ""
L2_json["L2_char"] = ""
print("L1_dot_bar_clear:" + str(L1_json["L1_dot_bar"]) + " : " + str(L2_json["L2_char"]) + " : "+ str(L3_json["L3_word"]) )
#print ("L1_dot_bar_clear:" + str(L1_json["L1_dot_bar"]) + " : " + str(L2_json["L2_char"]) + " : "+ str(L3_json["L3_word"]) + " " + str(out_json["char_min"]))
time.sleep_ms(10)
#d.setCursor(0,1)
L3_json["L3_words"] = L3_json["L3_words"] + L3_json["L3_word"]
#d.print(str(L3_json["L3_word"])) #ok
#d.print(":" + str(L3_json["L3_words"])) #ng 386???
LCD_json["row1"] = str(L3_json["L3_word"])
SSD_json["row1"] = LCD_json["row1"]
lcd_disp2()
#d.print("-:"+ str(dash_json["das_min"]) +":"+ str(dash_json["das_ave"])+":"+ str(dash_json["das_ave"]) +":"+ str(dash_json["das_rasio"])+":"+ str(traner_json["traner_speed"]))
time.sleep_ms(1)
#L1_json["L1_dot_bar"] = ""
#L2_json["L2_char"] = ""
#traner
#traner_json["traner_ctr"] += 1
if (traner_json["traner_ctr"] == traner_json["traner_num"]):
dot_json["dot_ave"] = int(dot_json["dot_sum"] / dot_json["dot_num"])
dot_json["dot_rasio"] = int(((dot_json["dot_ave"]*2 - (dot_json["dot_max"]-dot_json["dot_min"]))/ (dot_json["dot_ave"] *2)* 100))
dash_json["das_ave"] = int(dash_json["das_sum"] / dash_json["das_num"])
dash_json["das_rasio"] = int(((dash_json["das_ave"]*2 - (dash_json["das_max"]-dash_json["das_min"]))/ (dash_json["das_ave"]*2) * 100))
traner_json["traner_dot_das_rasio"] = int((6.0 - (3.0 - dash_json["das_ave"] / dot_json["dot_ave"]))/ 6.0 *100)
# サンプリング回数10(y)/サンプリング周期10mSec(x)*60=60字/分
#traner_json["traner_speed"] = int( 10/dot_json["dot_ave"] *60)
# サンプリング回数10(y)/サンプリング周期50mSec(x)*60=12字/分
#traner_json["traner_speed"] = int( 10/dot_json["das_ave"] *4)
#traner_json["traner_speed"] = int( 10/dot_json["dot_ave"] *12) ##time.sleep_ms(20)
traner_json["traner_speed"] = int( 10/dot_json["dot_ave"] *6) ##time.sleep_ms(10)
u.write(json.dumps(dot_json) + "\n\r")
u.write(json.dumps(dash_json) + "\n\r")
u.write(json.dumps(traner_json) + "\n\r")
#d.clear()
#d.setCursor(0,0)
#d.print(".:"+ str(dot_json["dot_min"]) +":"+ str(dot_json["dot_ave"]) +":"+ str(dot_json["dot_max"]) +":"+ str(dot_json["dot_rasio"])+":"+ str(L1_json["L1_avr"]))
#d.print(".:"+ str(dot_json["dot_min"]) +":"+ str(dot_json["dot_ave"]) +":"+ str(dot_json["dot_max"]) +":"+ str(dot_json["dot_rasio"]))
#d.print(".:"+ str(dot_json["dot_min"]) +":"+ str(dot_json["dot_ave"]) +":"+ str(dot_json["dot_max"]) +":"+ str(dot_json["dot_rasio"])+":"+ str(traner_json["traner_dot_das_rasio"]))
#d.setCursor(0,1)
#d.print("-:"+ str(dash_json["das_min"]) +":"+ str(dash_json["das_ave"])+":"+ str(dash_json["das_max"]) +":"+ str(dash_json["das_rasio"])+":"+ str(traner_json["traner_speed"]))
LCD_json["row2"] = ".:"+ str(dot_json["dot_min"]) +":"+ str(dot_json["dot_ave"]) +":"+ str(dot_json["dot_max"]) +":"+ str(dot_json["dot_rasio"])+":"+ str(L1_json["L1_avr"])
LCD_json["row3"] = "-:"+ str(dash_json["das_min"]) +":"+ str(dash_json["das_ave"])+":"+ str(dash_json["das_max"]) +":"+ str(dash_json["das_rasio"])+":"+ str(traner_json["traner_speed"])
lcd_disp()
#変数初期化
u.write(json.dumps(dot_json) + "\n\r")
u.write(json.dumps(dash_json) + "\n\r")
dot_json = json.loads(dot_para)
dash_json = json.loads(dash_para)
traner_json = json.loads(traner_para)
traner_json["traner_ctr"] += 1
#単語の切れ目
if (L0_json["L0_0ctr"] == L3_json["L3_7dot"]) :
u.write(json.dumps(L3_json) + "\n\r")
L4_json["L4_cmd"] = L3_json["L3_word"]
#L3_json ["L3_words"] = L3_json["L3_words"] + " " + L3_json["L3_word"]
print("L3_words_end=" + L3_json["L3_words"])
#print (out_json["char_min"]) #TypeError : can't convert 'int' object to str implicitly
#L3_json ["L3_words"] = " "
#print ("L3_words_clear=" + L3_json["L3_words"])
#d .clear()
#d .setCursor(0,0)
#d .print("RX:" + str(L1_json["L1_dot_bar"]) + ":" + str(L3_json["L3_word"]) )
#d .print("RX:" + str(L1_json["L1_dot_bar"]) + ":" + str(L3_json["L3_word"]) + " " + str(out_json["char_min"]))
time.sleep_ms(10)
#d.setCursor(0,1)
#L3_json["L3_words"] = L3_json["L3_words"] + L3_json["L3_word"]
#d.print(str(L3_json["L3_word"])) #ok
#d.print("RX_W:" + str(L3_json["L3_words"])) #ng 386???
L3_json["L3_word"] = ""
L3_json["L3_words"] = ""
print("L3_words_clear=" + L3_json["L3_words"])
u.write(json.dumps(L4_json) + "\n\r")
if roomaji_codes.get(L4_json["L4_cmd"]):
L5_json["L5_roomaji"] = roomaji_codes.get(L4_json["L4_cmd"])
SSD_json["row2"] = L5_json["L5_roomaji"]
#ssd_disp()
u.write(json.dumps(L5_json) + "\n\r")
# コマンド解析
#if (L4_json["L4_cmd"] == "oto") :
#buz.duty_u16(1000)
#time.sleep_ms(3000)
#buz.duty_u16(0)
#if (L4_json["L4_cmd"] == "sos") :
#print("sos_detect=")
#buz.duty_u16(1000)
#time.sleep_ms(3000)
#buz.duty_u16(0)
#文章の切れ目
if (L0_json["L0_0ctr"] == L3_json["L3_100dot"]) :
u.write(json.dumps(L3_json) + "\n\r")
print("bunsyou_end=" + json.dumps(L3_json) + "\n\r")
L3_json["L3_word"] = ""
L3_json["L3_words"] = ""
L1_json["L1_dot_bar"] = ""
L2_json["L2_char"] = ""
#time.sleep_ms(5)
#time.sleep_ms(8)
time.sleep_ms(10) #dot 0
#time.sleep_ms(20) #dot 2
#time.sleep_ms(50) #20240203
#time.sleep_ms(100)