can_bootloader/main.aardio

284 lines
7.3 KiB
Plaintext
Raw Normal View History

2024-08-31 14:18:38 +08:00
import fonts.fontAwesome;
2022-05-14 15:57:52 +08:00
import win.ui;
/*DSG{{*/
2023-03-20 19:42:48 +08:00
mainForm = win.form(text="CAN_Bootloader";right=699;bottom=465;border="dialog frame";max=false)
2022-05-14 15:57:52 +08:00
mainForm.add(
2024-10-09 09:09:01 +08:00
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};
2024-08-31 14:18:38 +08:00
edit={cls="edit";left=361;top=19;right=692;bottom=401;autovscroll=false;edge=1;multiline=1;vscroll=1;z=1};
2024-10-09 09:09:01 +08:00
plus={cls="plus";text="等待刷写
Waiting for flashing";left=33;top=375;right=316;bottom=460;bgcolor=12639424;z=10};
2024-08-31 14:18:38 +08:00
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};
2024-10-09 09:09:01 +08:00
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}
2022-05-14 15:57:52 +08:00
)
/*}}*/
import win;
import usb2canfd;
2022-05-18 10:43:03 +08:00
import win.timer;
2022-05-18 14:06:44 +08:00
var bootstate = false;//boot状态标志
2022-05-14 15:57:52 +08:00
2022-05-18 14:06:44 +08:00
Display = function(str){//显示日志
2023-03-20 19:42:48 +08:00
var nowtime = time();
nowtime.format="%H:%M:%S";
mainForm.edit.print(tostring(nowtime) + " " + str);
2022-05-14 15:57:52 +08:00
}
2024-08-31 14:18:38 +08:00
2022-05-18 14:06:44 +08:00
CANHw = usb2canfd.USB2CANHW();//尝试加载CAN dll
2022-05-14 15:57:52 +08:00
var ret = CANHw.LoadDll();
if(ret == 0){
Display("DLL加载成功");
2024-10-09 09:09:01 +08:00
Display("DLL loaded successfully
");
2022-05-14 15:57:52 +08:00
}
2022-09-30 16:32:52 +08:00
else {
Display("DLL加载失败");
2024-10-09 09:09:01 +08:00
Display("DLL loading failed
");
2022-09-30 16:32:52 +08:00
}
2022-05-14 15:57:52 +08:00
//CAN通讯线程
import thread.command;
var listener = thread.command();
2022-05-18 10:43:03 +08:00
var flag_connected=0;
2022-05-14 15:57:52 +08:00
listener.$Display = function(str){
Display(str);
}
2022-05-17 16:34:44 +08:00
listener.$ShowPath = function(str){
2024-08-31 14:18:38 +08:00
//mainForm.edFile.text = str;
2023-03-20 19:42:48 +08:00
2022-05-17 16:34:44 +08:00
}
2022-10-04 16:01:27 +08:00
listener.$ShowFlashDrvPath = function(str){
2024-08-31 14:18:38 +08:00
//mainForm.edFile2.text = str;
2022-10-04 16:01:27 +08:00
}
2022-05-18 10:43:03 +08:00
listener.$SetProgress = function(pos){
if(pos >= 0 && pos <= 100){
mainForm.progress.pos = pos;
}
}
listener.$Connected = function(){
2024-08-31 14:18:38 +08:00
Display("设备连接成功");
2024-10-09 09:09:01 +08:00
Display("Device connection successful
");
2024-08-31 14:18:38 +08:00
thrdTable.设备连接状态 = 1;
2022-05-18 10:43:03 +08:00
}
listener.$SendEnd = function(isSuccess){
2022-10-05 09:17:27 +08:00
//timer_bootcount.disable();
var usetime = time.now().diffsecond(starttime);
2022-05-18 14:06:44 +08:00
bootstate = false;
2022-05-18 10:43:03 +08:00
if(isSuccess == true){
2022-10-05 09:17:27 +08:00
Display("刷写成功,用时 " + usetime + " S")
2024-10-09 09:09:01 +08:00
Display("Brushed successfully, took "+usetime+" S
");
mainForm.plus.text = "刷写完成\Brushed successfully";
2024-08-31 14:18:38 +08:00
//mainForm.plus.bgcolor = 0x32CD32;
mainForm.plus.background = 0xFF32CD32;
2022-05-18 10:43:03 +08:00
}
2024-08-31 14:18:38 +08:00
else {
Display("刷写失败");
2024-10-09 09:09:01 +08:00
Display("Flashing failed
");
mainForm.plus.text = "刷写失败 Flashing failed";
2024-08-31 14:18:38 +08:00
//mainForm.plus.bgcolor = 0x2200E3;
mainForm.plus.background = 0xFFE30022;
}
2022-05-18 10:43:03 +08:00
}
2024-08-31 14:18:38 +08:00
import thread.table;
thrdTable = thread.table("多线程共享数据");
thrdTable.设备连接状态 = 0;
thrdTable.设备 = {};
thrdTable.test = "123"
thrdTable["文件加载状态"] = false;
thrdTable["电源电压"] = 0;
2022-05-14 15:57:52 +08:00
//线程函数
var CanThread = function(){
//线程函数内部要添加自已的import语句
import win;
import console;
2024-08-31 14:18:38 +08:00
import thread.table;
thrdTable = thread.table("多线程共享数据");
2022-10-05 09:17:27 +08:00
//console.log("线程开始运行")
2024-08-31 14:18:38 +08:00
//console.log(t_thrdTable.test);
2022-05-14 15:57:52 +08:00
loadcodex("\user\CanThread.aardio");//加载线程程序
2024-08-31 14:18:38 +08:00
2022-05-14 15:57:52 +08:00
//在子线程启动消息循环
win.loopMessage(FuncLoopMsg)
}
2024-08-31 14:18:38 +08:00
var DiagBootReq = function(id){
::PostThreadMessage(thrdId,102,id,0)//自定义消息
2024-10-09 09:09:01 +08:00
mainForm.plus.text = "刷写中请勿断开电源或USB连接
During flashing, do not disconnect the power
or USB connection";
2024-08-31 14:18:38 +08:00
//mainForm.plus.bgcolor = 0x00CCFF;
mainForm.plus.background = 0xFF00CCFF;
2022-05-14 17:00:49 +08:00
}
var DiagStopReq = function(){
::PostThreadMessage(thrdId,103,0,0)//自定义消息
2024-08-31 14:18:38 +08:00
//mainForm.plus.text = "刷写停止";
//mainForm.plus.background = 0xFFC0DCC0
2022-05-14 17:00:49 +08:00
}
2022-05-14 15:57:52 +08:00
2024-08-31 14:18:38 +08:00
//import console;
//console.log("1");
mainForm.btnFlashLeft.oncommand = function(id,event){
if(thrdTable.设备连接状态 == 0){
Display("设备未连接");
2024-10-09 09:09:01 +08:00
Display("device not connected
");
2022-05-18 10:43:03 +08:00
return;
}
2022-05-18 14:06:44 +08:00
if(bootstate == true){
return ;
}
2024-08-31 14:18:38 +08:00
if(thrdTable["文件加载状态"] == false){
Display("烧录文件加载错误");
2024-10-09 09:09:01 +08:00
Display("Burning file loading error
");
2024-08-31 14:18:38 +08:00
return;
2023-03-20 19:42:48 +08:00
}
2022-05-18 14:06:44 +08:00
bootstate = true;
2022-05-18 10:43:03 +08:00
mainForm.edit.text = "";
2024-08-31 14:18:38 +08:00
::PostThreadMessage(thrdId,106,0x720,0x620);//自定义消息
2022-05-18 10:43:03 +08:00
Display("开始刷写流程");
2024-10-09 09:09:01 +08:00
Display("Start flashing process
");
2022-10-10 08:19:50 +08:00
starttime = time.now();
2024-08-31 14:18:38 +08:00
DiagBootReq(1);//左侧
2022-05-14 15:57:52 +08:00
}
2022-05-17 16:34:44 +08:00
2022-05-18 14:06:44 +08:00
mainForm.onClose = function(hwnd,message,wParam,lParam){
2024-08-31 14:18:38 +08:00
raw.closehandle(thrdHandle)
2022-10-12 17:01:37 +08:00
}
2023-03-20 19:42:48 +08:00
2022-05-14 15:57:52 +08:00
2024-08-31 14:18:38 +08:00
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'
2024-10-09 09:09:01 +08:00
mainForm.static2.text = "设备未连接,请先连接设备 The device is not connected, please connect the device first";
2024-08-31 14:18:38 +08:00
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
2024-08-31 14:18:38 +08:00
}
}
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';
2024-10-09 09:09:01 +08:00
mainForm.static2.text = "设备已连接,可以开始刷写 The device is connected and can start flashing";
2024-08-31 14:18:38 +08:00
::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 {
}
2023-03-20 19:42:48 +08:00
}
2024-08-31 14:18:38 +08:00
2023-03-20 19:42:48 +08:00
}
2022-05-14 15:57:52 +08:00
2024-08-31 14:18:38 +08:00
mainForm.btnFlashRight.oncommand = function(id,event){
if(thrdTable.设备连接状态 == 0){
Display("设备未连接");
2024-10-09 09:09:01 +08:00
Display("device not connected
");
2024-08-31 14:18:38 +08:00
return;
}
if(bootstate == true){
return ;
}
if(thrdTable["文件加载状态"] == false){
Display("烧录文件加载错误");
2024-10-09 09:09:01 +08:00
Display("Burning file loading error
");
2024-08-31 14:18:38 +08:00
return;
2023-03-20 19:42:48 +08:00
}
2024-08-31 14:18:38 +08:00
bootstate = true;
mainForm.edit.text = "";
::PostThreadMessage(thrdId,106,0x710,0x610);//自定义消息
Display("开始刷写流程");
2024-10-09 09:09:01 +08:00
Display("Start flashing process
");
2024-08-31 14:18:38 +08:00
starttime = time.now();
DiagBootReq(2);//左侧
2023-03-20 19:42:48 +08:00
}
2022-05-14 15:57:52 +08:00
2024-08-31 14:18:38 +08:00
mainForm.btnStopBoot.oncommand = function(id,event){
DiagStopReq();
}
2024-08-31 14:18:38 +08:00
autoconnect.enable();
2024-08-31 14:18:38 +08:00
//import console;
//console.log("io.exis = ",io.exist("\res\P417_SWTL_20240709.S19"));
2022-05-14 15:57:52 +08:00
thrdHandle,thrdId = thread.create( CanThread );
2022-05-14 15:57:52 +08:00
mainForm.show();
return win.loopMessage();