From 2ae93518ad760587af32dc26d2da0bb1fe7d9089 Mon Sep 17 00:00:00 2001 From: cdhigh Date: Tue, 5 Apr 2022 09:48:49 -0300 Subject: [PATCH] v1.2.3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加自动将上位机时间同步到下位机的功能(需要下位机V6.33及以上) --- changelog.md | 6 ++++ lib/common/globals.dart | 3 ++ lib/i18n/main_drawer.i18n.dart | 4 +++ lib/i18n/settings.i18n.dart | 8 +++++ lib/m328v6_load.dart | 54 ++++++++++++++++++++++++++-------- lib/main_page.dart | 15 ++++++---- lib/settings/settings.dart | 20 +++++++++++-- lib/widgets/main_drawer.dart | 7 +++++ pubspec.yaml | 2 +- versions/version.json | 11 +++++-- 10 files changed, 106 insertions(+), 24 deletions(-) diff --git a/changelog.md b/changelog.md index 6aaf162..712ccdd 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,9 @@ +# 1.2.3 +1. 增加自动将上位机时间同步到下位机的功能(需要下位机V6.33及以上) + +# 1.2.2 +1. 加快首次连接下位机后显示下位机信息的速度 + # 1.2.1 1. 双击曲线区域可以查看每次放电的统计信息 2. 配合下位机V6.31,将功率单位修改为10毫瓦(显示小数点后两位) diff --git a/lib/common/globals.dart b/lib/common/globals.dart index c01a118..3573a7e 100644 --- a/lib/common/globals.dart +++ b/lib/common/globals.dart @@ -43,6 +43,7 @@ class Global { static KeepScreenOption keepScreenOn = KeepScreenOption.always; static DateTime lastPaused = DateTime.now(); //上次切换到后台的时间 static DateTime lastHeartBeat = DateTime.now(); //上次接收到下位机的时间 + static bool autoSynchronizeTime = true; //连接后自动自动同步下位机时间 static String lastSerialPort = ""; static int lastBaudRate = 19200; static bool autoReconnect = true; //是否异常中断后自动重连 @@ -68,6 +69,7 @@ class Global { curvaEndColor = Color(prefs.getInt('curvaEndColor') ?? 0xff02d39a); final onValue = min((prefs.getInt('keepScreenOn') ?? KeepScreenOption.always.index), KeepScreenOption.always.index); keepScreenOn = KeepScreenOption.values[onValue]; + autoSynchronizeTime = prefs.getBool('autoSynchronizeTime') ?? true; lastSerialPort = prefs.getString('lastSerialPort') ?? ""; lastBaudRate = prefs.getInt('lastBaudRate') ?? 19200; autoReconnect = prefs.getBool('autoReconnect') ?? true; @@ -97,6 +99,7 @@ class Global { prefs.setInt('curvaStartColor', curvaStartColor.value); prefs.setInt('curvaEndColor', curvaEndColor.value); prefs.setInt("keepScreenOn", keepScreenOn.index); + prefs.setBool('autoSynchronizeTime', autoSynchronizeTime); prefs.setString("lastSerialPort", lastSerialPort); prefs.setInt('lastBaudRate', lastBaudRate); prefs.setBool('autoReconnect', autoReconnect); diff --git a/lib/i18n/main_drawer.i18n.dart b/lib/i18n/main_drawer.i18n.dart index fd05539..2131ca8 100644 --- a/lib/i18n/main_drawer.i18n.dart +++ b/lib/i18n/main_drawer.i18n.dart @@ -67,6 +67,10 @@ extension Localization on String { "en_us": "Clear Time", "zh_cn": "运行时间归零", } + + { + "en_us": "Synchronize Time", + "zh_cn": "同步下位机时间", + } + { "en_us": "Mode", "zh_cn": "切换工作模式", diff --git a/lib/i18n/settings.i18n.dart b/lib/i18n/settings.i18n.dart index 9d3b660..4639743 100644 --- a/lib/i18n/settings.i18n.dart +++ b/lib/i18n/settings.i18n.dart @@ -183,6 +183,14 @@ extension Localization on String { "en_us": "Auto reconnect if connection interruption", "zh_cn": "连接异常断开后自动尝试重新连接", } + + { + "en_us": "Auto synchronize time", + "zh_cn": "自动同步时间", + } + + { + "en_us": "Auto synchronize time to load", + "zh_cn": "连接后自动同步下位机时间", + } + { "en_us": "Number of points for smooth curve", "zh_cn": "用于平滑曲线的点数", diff --git a/lib/m328v6_load.dart b/lib/m328v6_load.dart index 4ed7379..770f055 100644 --- a/lib/m328v6_load.dart +++ b/lib/m328v6_load.dart @@ -9,9 +9,10 @@ import 'common/widget_utils.dart'; import 'common/event_bus.dart'; import 'common/globals.dart'; -///扩展整形,将整形的低16位分解为5个ASCII码 +///扩展整形功能 extension AsListLow16Bits on int { - List asUint8List() { + ///将整形的低16位分解为5个ASCII码 + List asUint8List5() { final retList = List.filled(5, 0); int value = this; int idx = 0; @@ -25,6 +26,22 @@ extension AsListLow16Bits on int { } return retList; } + + ///将整形分解为6个ASCII码 + List asUint8List6() { + final retList = List.filled(6, 0); + int value = this; + int idx = 0; + if (value > 999999) { + value = 999999; + } + for (int i = 100000; i >= 1; i = i ~/ 10) { + retList[idx] = (value ~/ i) + 0x30; + idx++; + value %= i; + } + return retList; + } } ///表示一个下位机 @@ -50,7 +67,7 @@ class M328v6Load { void setV(double volt) { final cmd = BytesBuilder(); cmd.add("^V".codeUnits); - cmd.add((volt * 1000).toInt().asUint8List()); + cmd.add((volt * 1000).toInt().asUint8List5()); cmd.addByte(r"$".codeUnitAt(0)); sendCmd(cmd.toBytes()); } @@ -60,7 +77,7 @@ class M328v6Load { void setI(double i) { final cmd = BytesBuilder(); cmd.add("^I".codeUnits); - cmd.add((i * 1000).toInt().asUint8List()); + cmd.add((i * 1000).toInt().asUint8List5()); cmd.addByte(r"$".codeUnitAt(0)); sendCmd(cmd.toBytes()); } @@ -107,12 +124,23 @@ class M328v6Load { sendCmd(cmd); } - ///运行时间清零:^ZT$ + ///运行时间清零:^T000000$ void clearTime() { - final cmd = Uint8List.fromList(r"^ZT$".codeUnits); + final cmd = Uint8List.fromList(r"^T000000$".codeUnits); sendCmd(cmd); } + ///将上位机的时间同步到下位机: ^T123456$ + void synchronizeTime() { + final now = DateTime.now(); + final int nowSeconds = now.hour * 3600 + now.minute * 60 + now.second; + final cmd = BytesBuilder(); + cmd.add("^T".codeUnits); + cmd.add(nowSeconds.asUint8List6()); + cmd.addByte(r"$".codeUnitAt(0)); + sendCmd(cmd.toBytes()); + } + ///切换为恒流模式:^MC00000$ void switchToCC() { final cmd = Uint8List.fromList(r"^MC00000$".codeUnits); @@ -124,7 +152,7 @@ class M328v6Load { void switchToCR(double resistor) { final cmd = BytesBuilder(); cmd.add("^MR".codeUnits); - cmd.add((resistor * 100).toInt().asUint8List()); //单位切换为下位机使用的10毫欧 + cmd.add((resistor * 100).toInt().asUint8List5()); //单位切换为下位机使用的10毫欧 cmd.addByte(r"$".codeUnitAt(0)); sendCmd(cmd.toBytes()); } @@ -134,7 +162,7 @@ class M328v6Load { void switchToCP(double power) { final cmd = BytesBuilder(); cmd.add("^MP".codeUnits); - cmd.add((power * 100).toInt().asUint8List()); //单位切换为下位机使用的10毫瓦 + cmd.add((power * 100).toInt().asUint8List5()); //单位切换为下位机使用的10毫瓦 cmd.addByte(r"$".codeUnitAt(0)); sendCmd(cmd.toBytes()); } @@ -144,7 +172,7 @@ class M328v6Load { void setDelayOn(int seconds) { final cmd = BytesBuilder(); cmd.add("^DSO".codeUnits); - cmd.add(seconds.asUint8List()); + cmd.add(seconds.asUint8List5()); cmd.addByte(r"$".codeUnitAt(0)); sendCmd(cmd.toBytes()); } @@ -160,7 +188,7 @@ class M328v6Load { void setDelayOff(int seconds) { final cmd = BytesBuilder(); cmd.add("^DSF".codeUnits); - cmd.add(seconds.asUint8List()); + cmd.add(seconds.asUint8List5()); cmd.addByte(r"$".codeUnitAt(0)); sendCmd(cmd.toBytes()); } @@ -176,9 +204,9 @@ class M328v6Load { void setPeriodOnOff(int onSeconds, int offSeconds) { final cmd = BytesBuilder(); cmd.add("^DSP".codeUnits); - cmd.add(onSeconds.asUint8List()); + cmd.add(onSeconds.asUint8List5()); cmd.add(r"$^DSQ".codeUnits); - cmd.add(offSeconds.asUint8List()); + cmd.add(offSeconds.asUint8List5()); cmd.addByte(r"$".codeUnitAt(0)); sendCmd(cmd.toBytes()); } @@ -200,7 +228,7 @@ class M328v6Load { void setBuzzerTime(int onTime) { final cmd = BytesBuilder(); cmd.add("^B".codeUnits); - cmd.add(onTime.asUint8List()); + cmd.add(onTime.asUint8List5()); cmd.addByte(r"$".codeUnitAt(0)); sendCmd(cmd.toBytes()); } diff --git a/lib/main_page.dart b/lib/main_page.dart index ed9d255..a292075 100644 --- a/lib/main_page.dart +++ b/lib/main_page.dart @@ -80,12 +80,14 @@ class _MainPageState extends ConsumerState with AutomaticKeepAliveClie _timerForReconnect.pause(); } - //每隔一段时间重发一次请求额外数据的命令,避免下位机中间复位了 + ///如果一段时间后没有收到下位机上报的额外数据包,则重发一次请求额外数据的命令,避免下位机中途复位了 void qeuryVersionPeriodic() { final load = ref.read(Global.connectionProvider).load; load.requestExtraData(); - Future.delayed(const Duration(milliseconds: 100), load.queryVersion); - //_timerForExtraData..reset()..start(); + Future.delayed(const Duration(milliseconds: 150), load.queryVersion); + if (Global.autoSynchronizeTime) { + Future.delayed(const Duration(milliseconds: 300), load.synchronizeTime); + } } //如果异常中断并且启用了“自动重连”选项,则每隔5s自动尝试重连一次 @@ -170,8 +172,11 @@ class _MainPageState extends ConsumerState with AutomaticKeepAliveClie connProvider.serial.registerListenFunction(newSrlDataReceived); //连接后马上查询下位机版本号,请求上报额外数据 - Future.delayed(const Duration(milliseconds: 250), connProvider.load.queryVersion); - Future.delayed(const Duration(milliseconds: 500), connProvider.load.requestExtraData); + Future.delayed(const Duration(milliseconds: 150), connProvider.load.queryVersion); + Future.delayed(const Duration(milliseconds: 300), connProvider.load.requestExtraData); + if (Global.autoSynchronizeTime) { + Future.delayed(const Duration(milliseconds: 500), connProvider.load.synchronizeTime); + } _timerForExtraData..reset()..start(); } else { //断开连接 final rdProvider = ref.read(Global.runningDataProvider); diff --git a/lib/settings/settings.dart b/lib/settings/settings.dart index 581ffcd..7629e3c 100644 --- a/lib/settings/settings.dart +++ b/lib/settings/settings.dart @@ -11,6 +11,7 @@ import '../common/globals.dart'; import '../common/my_widget_chain.dart'; import '../common/widget_utils.dart'; import '../common/event_bus.dart'; +import '../models/connection_provider.dart'; import '../i18n/settings.i18n.dart'; import '../widgets/colored_indicator.dart'; import '../widgets/modal_dialogs.dart'; @@ -55,8 +56,11 @@ class _SettingsPageState extends ConsumerState { //数据组 buildSettingGroupTile('Data'.i18n), SettingTile(title: 'Auto reconnect'.i18n, subTitle: 'Auto reconnect if connection interruption'.i18n, - switchValue: Global.autoReconnect, switchCallback: (_)=>onTapAutoReconnect()) - .intoGestureDetector(onTap: onTapAutoReconnect), + switchValue: Global.autoReconnect, switchCallback: onTapAutoReconnect) + .intoGestureDetector(onTap: onTapAutoReconnect), + SettingTile(title: 'Auto synchronize time'.i18n, subTitle: 'Auto synchronize time to load'.i18n, + switchValue: Global.autoSynchronizeTime, switchCallback: onTapAutoSynchronizeTime) + .intoGestureDetector(onTap: onTapAutoSynchronizeTime), SettingTile(title: 'Number of points for smooth curve'.i18n, subTitle: Text(Global.curvaFilterDotNum.toString())) .intoInkWell(onTap: onTapDotSmoothNum), SettingTile(title: 'Threshold for smooth curve'.i18n, subTitle: Text(Global.curvaFilterThreshold.toStringAsFixed(3) + " V")) @@ -305,11 +309,21 @@ class _SettingsPageState extends ConsumerState { } ///点击了‘自动重连’功能 - void onTapAutoReconnect() { + void onTapAutoReconnect([_]) { setState(() => Global.autoReconnect = !Global.autoReconnect); Global.saveProfile(); } + ///点击了“自动同步时间”功能 + void onTapAutoSynchronizeTime([_]) { + setState(() => Global.autoSynchronizeTime = !Global.autoSynchronizeTime); + Global.saveProfile(); + if (Global.autoSynchronizeTime) { + final load = ref.read(Global.connectionProvider).load; + load.synchronizeTime(); + } + } + ///点击了“用于平滑曲线的点数” void onTapDotSmoothNum() async { //简单的闭包函数,根据当前平滑点数创建不同的对话框行 diff --git a/lib/widgets/main_drawer.dart b/lib/widgets/main_drawer.dart index e5209f9..ab6e00f 100644 --- a/lib/widgets/main_drawer.dart +++ b/lib/widgets/main_drawer.dart @@ -187,6 +187,13 @@ class MainDrawerDeviceOperations extends ConsumerWidget { load.clearTime(); }, ), + ListTile(title: Text("Synchronize Time".i18n), + enabled: loadMenuEnabled, + onTap: () { + Navigator.of(context).pop(); //关闭drawer + load.synchronizeTime(); + }, + ), ListTile(title: Text("Turn off Buzzer".i18n), enabled: loadMenuEnabled, onTap: () { diff --git a/pubspec.yaml b/pubspec.yaml index 9c0ced6..020cd09 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 1.2.2 +version: 1.2.3 environment: sdk: ">=2.16.1 <3.0.0" diff --git a/versions/version.json b/versions/version.json index 4745937..fc52d40 100644 --- a/versions/version.json +++ b/versions/version.json @@ -1,12 +1,19 @@ { - "lastest": "1.2.2", + "lastest": "1.2.3", "history": [ + { + "version": "1.2.3", + "build": "2022-04-03", + "android": "https://github.com/cdhigh/m328v6host/releases/download/v1.2.3/m328v6_V1.2.3.apk", + "windows": "https://github.com/cdhigh/m328v6host/releases/download/v1.2.3/m328v6_win_V1.2.3.zip", + "whatsnew": "增加自动将上位机时间同步到下位机的功能(需要下位机V6.33及以上)" + }, { "version": "1.2.2", "build": "2022-04-02", "android": "https://github.com/cdhigh/m328v6host/releases/download/v1.2.2/m328v6_V1.2.2.apk", "windows": "https://github.com/cdhigh/m328v6host/releases/download/v1.2.2/m328v6_win_V1.2.2.zip", - "whatsnew": "1.连接电子负载后更快的显示下位机信息" + "whatsnew": "连接电子负载后更快的显示下位机信息" }, { "version": "1.2.1",