import fonts.fontAwesome;
import win.dlg.message;
import win.ui;
/*DSG{{*/
mainForm = win.form(text="K86上位机";right=885;bottom=591;border="dialog frame";max=false)
mainForm.add(
btnClear={cls="button";text="清空";left=741;top=537;right=854;bottom=579;font=LOGFONT(h=-19);z=6};
btnFresh={cls="button";text="刷新";left=41;top=47;right=138;bottom=101;font=LOGFONT(h=-20);z=3};
btnOpen={cls="button";text="打开";left=346;top=47;right=442;bottom=101;font=LOGFONT(h=-20);z=4};
combobox={cls="combobox";left=154;top=61;right=332;bottom=87;edge=1;font=LOGFONT(h=-20);items={};mode="dropdown";z=1};
editRx={cls="edit";left=526;top=25;right=854;bottom=523;bgcolor=16777215;edge=1;font=LOGFONT(h=-19);multiline=1;readonly=1;vscroll=1;z=5};
lbComPort={cls="static";text="串口信息";left=40;top=4;right=413;bottom=40;transparent=1;z=2};
plKey1={cls="plus";text="滑轨前";left=79;top=177;right=179;bottom=246;bgcolor=-5723992;font=LOGFONT(h=-16);z=8};
plKey2={cls="plus";text="滑轨后";left=79;top=252;right=179;bottom=321;bgcolor=-5723992;font=LOGFONT(h=-16);z=9};
plKey3={cls="plus";text="靠背前";left=268;top=177;right=368;bottom=246;bgcolor=-5723992;font=LOGFONT(h=-16);z=10};
plKey4={cls="plus";text="靠背后";left=268;top=252;right=368;bottom=321;bgcolor=-5723992;font=LOGFONT(h=-16);z=11};
plKey5={cls="plus";text="腿托升";left=79;top=361;right=179;bottom=430;bgcolor=-5723992;font=LOGFONT(h=-16);z=12};
plKey6={cls="plus";text="腿托降";left=79;top=437;right=179;bottom=506;bgcolor=-5723992;font=LOGFONT(h=-16);z=13};
plKey7={cls="plus";text="零重力开";left=268;top=361;right=368;bottom=430;bgcolor=-5723992;font=LOGFONT(h=-16);z=14};
plKey8={cls="plus";text="零重力关";left=268;top=437;right=368;bottom=506;bgcolor=-5723992;font=LOGFONT(h=-16);z=15};
static={cls="static";text="V1.0 20250215";left=16;top=563;right=134;bottom=582;font=LOGFONT(h=-16);notify=1;transparent=1;z=7}
)
/*}}*/

var keyColor = {0xFFA8A8A8,0xFF00FF00};

import sys.comPort;
import console;
import sio;
import win.timer;


var sioPort;

logPrint = function(str){
	mainForm.editRx.print(str);
}


//串口列表
FreshHw = function(){
	comPorts = sys.comPort.list();
	mainForm.combobox.items = comPorts;
	mainForm.combobox.selIndex = sys.comPort.find("CP210x")[["index"]] or 1; 
	if(#comPorts == 0){
		mainForm.lbComPort.text = "没有找到硬件"
	}
}
FreshHw();


//获取串口信息
mainForm.combobox.oncommand = function(id,event){
	var item = comPorts[mainForm.combobox.selIndex]
	if(item){
		mainForm.lbComPort.text = item.friendlyName;
	}
}

mainForm.btnFresh.oncommand = function(id,event){
	FreshHw();
	mainForm.combobox.oncommand();
}


var data = {};
var KeyDelay = {};
for(i=1;8;1){
	KeyDelay[i] = 0;
}


display = function(){
	for(i=1;8;1){
		if(KeyDelay[i] > 0){
			KeyLedCtrl(i,1);
			KeyDelay[i]--;
		}
		else {
			KeyLedCtrl(i,0);
		}
		
	
	}
	
}

KeyLedCtrl = function(keyid,state){
	if(keyid > 0 && keyid < 9){
		mainForm["plKey"++keyid].background = keyColor[state + 1];
	}
	
}


DataPrecess = function(str){
	//logPrint(string.left(str,35));
	if(#str != 59){
		return ; 
	}
	if(string.cmpMatch(string.left(str,14),"01 01 02 00 00" )){
		datastr = string.slice(str,36,59);
		logPrint(datastr);
		data = string.split(datastr,' ');

		keydata = tonumber(data[2],16);
		//logPrint(data[2]);
		
		for(i=0;7;1){
			if(keydata & (1<<i)){
				KeyDelay[i+1] = 5;
			}
		
		}

	}
	else {
		logPrint(string.left(str,14));
	}
	
	
	
	if(data[13] == "03"){
		//mainForm.editRx.print("从机未响应");
	}

	
}
Sch_count = 0;
LIN_Sch_Ctrl = function(){
	select(Sch_count) {
		case 0 {
			TxMsg("02 02 00 "++ TX_str ++" 33 44 55 66 77 88 D7 02")
			Sch_count = 1;
		}
		case 1 {
			TxMsg("02 00 06 11 22 33 44 55 66 77 88 D7 02")
			Sch_count = 2;
		}
		case 2 {
			TxMsg("02 00 08 11 22 33 44 55 66 77 88 D7 02")
			Sch_count = 0;
		}
		else {
			Sch_count = 0;
		}
	}
}

var datastr = "";

var timer = win.timer(mainForm,100);
timer.onTimer = function(hwnd,msg,id,tick){
	//LIN_Sch_Ctrl();
	display();
	//mainForm.editRx.print("test");

}

CanDevInit = function(){
	TxMsg("06 01");
	sleep(50);
	
	TxMsg("03 01 F4 01 00 00 12 00 00 05 00");
	sleep(50);
	
	TxMsg("03 05");
	sleep(50);
	
	
}


mainForm.btnOpen.oncommand = function(id,event){
	if(sioPort){
		sioPort.close();
	}
	
	var err;
	sioPort,err = sio.port(mainForm.combobox.selText);
	if(!sioPort){
		return mainForm.msgErr(err,300);
	}
	sioPort.ioctl(115200,8,1,"none");
	//串口接收到数据触发回调
	sioPort.termCntIrq(1,function(port){
		var ret = sioPort.readHex()
		var num = tonumber(ret,16,2);
		datastr = "";
		if(ret != null){
			datastr += ret;
		}

		DataPrecess(string.trim(datastr));

	} )	
	
	mainForm.msgOk("已打开端口",200);
	timer.enable();
}

mainForm.onDestroy = function(){
	if(sioPort){
		sioPort.close();
	}   
}
TxMsg = function(str){
	if(sioPort == null){
		//mainForm.msgErr("端口未打开",300);
		return;
	}
	//mainForm.editRx.print(tostring(time.now()) + " Tx: " + string.upper(str));
	sioPort.writeHex(str);
	return;
}


TX_str = "00 FC"
msg_tx = function(){

}


mainForm.btnClear.oncommand = function(id,event){
	mainForm.editRx.text = ""
}


mainForm.combobox.oncommand();
display();
mainForm.show();
//mainForm.plKey3.background = 0xFF00FF00;
return win.loopMessage();