can_bootloader/main.aardio
2024-10-09 09:09:01 +08:00

284 lines
7.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fonts.fontAwesome;
import win.ui;
/*DSG{{*/
mainForm = win.form(text="CAN_Bootloader";right=699;bottom=465;border="dialog frame";max=false)
mainForm.add(
btnFlashLeft={cls="button";text="左侧刷写
Left side flashing";left=30;top=129;right=168;bottom=243;z=2};
btnFlashRight={cls="button";text="右侧刷写
Right side flashing";left=181;top=131;right=319;bottom=245;z=7};
btnStopBoot={cls="button";text="停止
Stop";left=29;top=272;right=319;bottom=346;z=3};
edit={cls="edit";left=361;top=19;right=692;bottom=401;autovscroll=false;edge=1;multiline=1;vscroll=1;z=1};
plus={cls="plus";text="等待刷写
Waiting for flashing";left=33;top=375;right=316;bottom=460;bgcolor=12639424;z=10};
progress={cls="progress";left=361;top=435;right=689;bottom=463;edge=1;max=100;min=0;z=4};
static={cls="static";text='\uF127';left=43;top=30;right=114;bottom=98;align="center";center=1;color=255;font=LOGFONT(h=-56;name='FontAwesome');transparent=1;z=8};
static2={cls="static";text="设备未连接,请先连接设备";left=126;top=42;right=326;bottom=91;transparent=1;z=9};
static3={cls="static";text="刷写进度 Brush writing progress";left=361;top=410;right=688;bottom=430;transparent=1;z=5};
static5={cls="static";text="V0.2_20240921";left=545;top=1;right=689;bottom=17;align="right";center=1;notify=1;transparent=1;z=6}
)
/*}}*/
import win;
import usb2canfd;
import win.timer;
var bootstate = false;//boot状态标志
Display = function(str){//显示日志
var nowtime = time();
nowtime.format="%H:%M:%S";
mainForm.edit.print(tostring(nowtime) + " " + str);
}
CANHw = usb2canfd.USB2CANHW();//尝试加载CAN dll
var ret = CANHw.LoadDll();
if(ret == 0){
Display("DLL加载成功");
Display("DLL loaded successfully
");
}
else {
Display("DLL加载失败");
Display("DLL loading failed
");
}
//CAN通讯线程
import thread.command;
var listener = thread.command();
var flag_connected=0;
listener.$Display = function(str){
Display(str);
}
listener.$ShowPath = function(str){
//mainForm.edFile.text = str;
}
listener.$ShowFlashDrvPath = function(str){
//mainForm.edFile2.text = str;
}
listener.$SetProgress = function(pos){
if(pos >= 0 && pos <= 100){
mainForm.progress.pos = pos;
}
}
listener.$Connected = function(){
Display("设备连接成功");
Display("Device connection successful
");
thrdTable.设备连接状态 = 1;
}
listener.$SendEnd = function(isSuccess){
//timer_bootcount.disable();
var usetime = time.now().diffsecond(starttime);
bootstate = false;
if(isSuccess == true){
Display("刷写成功,用时 " + usetime + " S")
Display("Brushed successfully, took "+usetime+" S
");
mainForm.plus.text = "刷写完成\Brushed successfully";
//mainForm.plus.bgcolor = 0x32CD32;
mainForm.plus.background = 0xFF32CD32;
}
else {
Display("刷写失败");
Display("Flashing failed
");
mainForm.plus.text = "刷写失败 Flashing failed";
//mainForm.plus.bgcolor = 0x2200E3;
mainForm.plus.background = 0xFFE30022;
}
}
import thread.table;
thrdTable = thread.table("多线程共享数据");
thrdTable.设备连接状态 = 0;
thrdTable.设备 = {};
thrdTable.test = "123"
thrdTable["文件加载状态"] = false;
thrdTable["电源电压"] = 0;
//线程函数
var CanThread = function(){
//线程函数内部要添加自已的import语句
import win;
import console;
import thread.table;
thrdTable = thread.table("多线程共享数据");
//console.log("线程开始运行")
//console.log(t_thrdTable.test);
loadcodex("\user\CanThread.aardio");//加载线程程序
//在子线程启动消息循环
win.loopMessage(FuncLoopMsg)
}
var DiagBootReq = function(id){
::PostThreadMessage(thrdId,102,id,0)//自定义消息
mainForm.plus.text = "刷写中请勿断开电源或USB连接
During flashing, do not disconnect the power
or USB connection";
//mainForm.plus.bgcolor = 0x00CCFF;
mainForm.plus.background = 0xFF00CCFF;
}
var DiagStopReq = function(){
::PostThreadMessage(thrdId,103,0,0)//自定义消息
//mainForm.plus.text = "刷写停止";
//mainForm.plus.background = 0xFFC0DCC0
}
//import console;
//console.log("1");
mainForm.btnFlashLeft.oncommand = function(id,event){
if(thrdTable.设备连接状态 == 0){
Display("设备未连接");
Display("device not connected
");
return;
}
if(bootstate == true){
return ;
}
if(thrdTable["文件加载状态"] == false){
Display("烧录文件加载错误");
Display("Burning file loading error
");
return;
}
bootstate = true;
mainForm.edit.text = "";
::PostThreadMessage(thrdId,106,0x720,0x620);//自定义消息
Display("开始刷写流程");
Display("Start flashing process
");
starttime = time.now();
DiagBootReq(1);//左侧
}
mainForm.onClose = function(hwnd,message,wParam,lParam){
raw.closehandle(thrdHandle)
}
autoconnect = win.timer(mainForm,100);
var autoconnect_state = 0;
var power_err_count = 0;
autoconnect.onTimer = function(){
select(autoconnect_state) {
case 0 {
if(thrdTable.设备连接状态 == 0){
//断开
mainForm.static.text='\uF127'
mainForm.static2.text = "设备未连接,请先连接设备 The device is not connected, please connect the device first";
mainForm.static.color=0xff0;
::PostThreadMessage(thrdId,100,0,0);
//Display("刷新");
sleep(20);
if(#thrdTable.设备 > 0){
Display(thrdTable.设备[1].name);
::PostThreadMessage(thrdId,101,1,0x80);//连接 0x80 -> CANFD
}
}
else {
autoconnect_state = 3;
power_err_count = 0;
}
}
case 1 {
::PostThreadMessage(thrdId,104,0,0);
autoconnect_state = 2;
}
case 2 {
if(thrdTable["电源电压"] < 100){
::PostThreadMessage(thrdId,105,1,0);
power_err_count++;
if(power_err_count < 10){
autoconnect_state = 1;
}
else {
mainForm.static2.text = "电源异常,请检查线路"
autoconnect_state = 4;
}
}
else {
import console;
console.log(thrdTable["电源电压"])
autoconnect_state = 3;
}
}
case 3 {
if(thrdTable["文件加载状态"] == true){
mainForm.static.color=0xff00;
mainForm.static.text='\uF0C1';
mainForm.static2.text = "设备已连接,可以开始刷写 The device is connected and can start flashing";
::PostThreadMessage(thrdId,115,1,0);//连接
autoconnect_state = 4;
}
else {
mainForm.static2.text = "文件已损坏,请重新获取烧录上位机";
autoconnect_state = 4;
mainForm.btnFlashLeft.disabled = true;
mainForm.btnFlashRight.disabled = true;
}
}
case 4 {
//...
}
else {
}
}
}
mainForm.btnFlashRight.oncommand = function(id,event){
if(thrdTable.设备连接状态 == 0){
Display("设备未连接");
Display("device not connected
");
return;
}
if(bootstate == true){
return ;
}
if(thrdTable["文件加载状态"] == false){
Display("烧录文件加载错误");
Display("Burning file loading error
");
return;
}
bootstate = true;
mainForm.edit.text = "";
::PostThreadMessage(thrdId,106,0x710,0x610);//自定义消息
Display("开始刷写流程");
Display("Start flashing process
");
starttime = time.now();
DiagBootReq(2);//左侧
}
mainForm.btnStopBoot.oncommand = function(id,event){
DiagStopReq();
}
autoconnect.enable();
//import console;
//console.log("io.exis = ",io.exist("\res\P417_SWTL_20240709.S19"));
thrdHandle,thrdId = thread.create( CanThread );
mainForm.show();
return win.loopMessage();