"""本主题专属离线算法，仅读取同目录合成JSON，不联网、不控制设备。"""
import csv, json, math, re, struct
from pathlib import Path
from fractions import Fraction
BASE=Path(__file__).resolve().parent
data=json.loads((BASE/'input.json').read_text(encoding='utf-8'))['data']
rows=[]
for x in data['positions']:
 assert 0<=x<=1
 cc=math.floor(x*127+0.5);pb=math.floor(x*16383+0.5)
 lo,hi=pb&127,(pb>>7)&127
 assert lo+(hi<<7)==pb and lo<128 and hi<128
 rows.append({'position':x,'cc7':cc,'pitch14':pb,'pitch_data_hex':bytes([lo,hi]).hex(),'cc_error':cc/127-x,'pitch_error':pb/16383-x})
assert rows[2]['pitch14']==8192 and rows[2]['pitch_data_hex']=='0040'
result={'mapping':rows,'pitch_center':8192,'physical_parameter_unit':'未约定'}
(BASE/'result.json').write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
print(json.dumps(result,ensure_ascii=False))
