Skip to content

Commit

Permalink
v1.2.3
Browse files Browse the repository at this point in the history
增加自动将上位机时间同步到下位机的功能(需要下位机V6.33及以上)
  • Loading branch information
cdhigh committed Apr 5, 2022
1 parent 8d16cb2 commit 2ae9351
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 24 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.2.3
1. 增加自动将上位机时间同步到下位机的功能(需要下位机V6.33及以上)

# 1.2.2
1. 加快首次连接下位机后显示下位机信息的速度

# 1.2.1
1. 双击曲线区域可以查看每次放电的统计信息
2. 配合下位机V6.31,将功率单位修改为10毫瓦(显示小数点后两位)
Expand Down
3 changes: 3 additions & 0 deletions lib/common/globals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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; //是否异常中断后自动重连
Expand All @@ -68,6 +69,7 @@ class Global {
curvaEndColor = Color(prefs.getInt('curvaEndColor') ?? 0xff02d39a);
final onValue = min<int>((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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions lib/i18n/main_drawer.i18n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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": "切换工作模式",
Expand Down
8 changes: 8 additions & 0 deletions lib/i18n/settings.i18n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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": "用于平滑曲线的点数",
Expand Down
54 changes: 41 additions & 13 deletions lib/m328v6_load.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> asUint8List() {
///将整形的低16位分解为5个ASCII码
List<int> asUint8List5() {
final retList = List<int>.filled(5, 0);
int value = this;
int idx = 0;
Expand All @@ -25,6 +26,22 @@ extension AsListLow16Bits on int {
}
return retList;
}

///将整形分解为6个ASCII码
List<int> asUint8List6() {
final retList = List<int>.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;
}
}

///表示一个下位机
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand Down Expand Up @@ -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);
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand Down
15 changes: 10 additions & 5 deletions lib/main_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ class _MainPageState extends ConsumerState<MainPage> with AutomaticKeepAliveClie
_timerForReconnect.pause();
}

//每隔一段时间重发一次请求额外数据的命令,避免下位机中间复位了
///如果一段时间后没有收到下位机上报的额外数据包,则重发一次请求额外数据的命令,避免下位机中途复位了
void qeuryVersionPeriodic() {
final load = ref.read<ConnectionProvider>(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自动尝试重连一次
Expand Down Expand Up @@ -170,8 +172,11 @@ class _MainPageState extends ConsumerState<MainPage> 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<RunningDataProvider>(Global.runningDataProvider);
Expand Down
20 changes: 17 additions & 3 deletions lib/settings/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -55,8 +56,11 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
//数据组
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"))
Expand Down Expand Up @@ -305,11 +309,21 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
}

///点击了‘自动重连’功能
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<ConnectionProvider>(Global.connectionProvider).load;
load.synchronizeTime();
}
}

///点击了“用于平滑曲线的点数”
void onTapDotSmoothNum() async {
//简单的闭包函数,根据当前平滑点数创建不同的对话框行
Expand Down
7 changes: 7 additions & 0 deletions lib/widgets/main_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: () {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 9 additions & 2 deletions versions/version.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 2ae9351

Please sign in to comment.