From 0968a1c67809d68df70e06f2ecbd565f8c8826d8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=8E=8B=E7=AB=8B=E5=B8=AE?= <3294713004@qq.com>
Date: Tue, 20 Jan 2026 18:01:04 +0800
Subject: [PATCH] =?UTF-8?q?feat(boards):=20=E6=9B=B4=E6=96=B0micropython?=
=?UTF-8?q?=E4=B8=8Bmachine.Timer=E5=9B=BE=E5=BD=A2=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../default_src/micropython/blocks/system.js | 95 +++++++++----------
.../micropython/generators/system.js | 37 ++++----
.../micropython_educore/template.xml | 27 +++++-
.../micropython_esp32/template.xml | 24 +++++
.../micropython_esp32c2/template.xml | 24 +++++
.../micropython_esp32c3/template.xml | 24 +++++
.../micropython_esp32c5/template.xml | 24 +++++
.../micropython_esp32s2/template.xml | 24 +++++
.../micropython_esp32s3/template.xml | 24 +++++
.../micropython_robot/template.xml | 27 +++++-
common/msg/blockly/en.js | 1 +
common/msg/blockly/zh-hans.js | 1 +
common/msg/blockly/zh-hant.js | 1 +
13 files changed, 257 insertions(+), 76 deletions(-)
diff --git a/boards/default_src/micropython/blocks/system.js b/boards/default_src/micropython/blocks/system.js
index 5dc6b62f..f1febe27 100644
--- a/boards/default_src/micropython/blocks/system.js
+++ b/boards/default_src/micropython/blocks/system.js
@@ -133,36 +133,6 @@ export const timer = {
}
};
-/**
- * @deprecated To be removed in the future
- */
-export const system_timer = {
- init: function () {
- this.setColour(SYSTEM_HUE);
- this.appendValueInput('VAR')
- .appendField("Timer")
- .setCheck("var");
- // .appendField("I2C " + Blockly.Msg.MIXLY_SETUP)
- this.appendValueInput("period")
- .setAlign(Blockly.inputs.Align.RIGHT)
- .appendField(Blockly.Msg.MIXLY_MICROBIT_JS_PERIOD_MIL)
- .setCheck(Number);
- this.appendDummyInput("")
- .appendField(Blockly.Msg.MIXLY_mSecond)
- .appendField(Blockly.Msg.MIXLY_MODE)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.MIXLY_PYTHON_ONE_SHOT, "ONE_SHOT"],
- [Blockly.Msg.MIXLY_PYTHON_PERIODIC, "PERIODIC"]
- ]), "mode");
- this.appendValueInput('callback')
- .appendField(Blockly.Msg.MIXLY_DO)
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip(Blockly.Msg.MIXLY_ESP32_SYSTEM_TIMER_TOOLTIP);
- this.setWarningText(Blockly.Msg.MIXLY_DEPRECATED_WARNING_TEXT);
- }
-};
-
export const system_ticks_diff = {
init: function () {
this.setColour(SYSTEM_HUE);
@@ -180,24 +150,6 @@ export const system_ticks_diff = {
}
};
-/**
- * @deprecated To be removed in the future
- */
-export const system_timer_init = {
- init: function () {
- this.setColour(SYSTEM_HUE);
- this.appendDummyInput("")
- .appendField("Timer")
- this.appendValueInput('SUB')
- .appendField(Blockly.Msg.MIXLY_SETUP)
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setTooltip(Blockly.Msg.MIXLY_ESP32_SYSTEM_TIMER_INIT_TOOLTIP);
- this.setWarningText(Blockly.Msg.MIXLY_DEPRECATED_WARNING_TEXT);
- }
-};
-
export const set_system_timer = {
init: function () {
this.setColour(SYSTEM_HUE);
@@ -227,6 +179,50 @@ export const set_system_timer = {
}
};
+export const system_timer_init = {
+ init: function () {
+ this.setColour(SYSTEM_HUE);
+ this.appendValueInput('VAR')
+ .appendField("Timer")
+ .setCheck("var");
+ this.appendDummyInput()
+ .setAlign(Blockly.inputs.Align.RIGHT)
+ .appendField(Blockly.Msg.MIXLY_RESTART);
+ this.appendValueInput("period")
+ .setAlign(Blockly.inputs.Align.RIGHT)
+ .appendField(Blockly.Msg.MIXLY_MICROBIT_JS_PERIOD_MIL)
+ .setCheck(Number);
+ this.appendDummyInput("")
+ .appendField(Blockly.Msg.MIXLY_mSecond)
+ .appendField(Blockly.Msg.MIXLY_MODE)
+ .appendField(new Blockly.FieldDropdown([
+ [Blockly.Msg.MIXLY_PYTHON_ONE_SHOT, "ONE_SHOT"],
+ [Blockly.Msg.MIXLY_PYTHON_PERIODIC, "PERIODIC"]
+ ]), "mode");
+ this.appendValueInput('callback')
+ .setAlign(Blockly.inputs.Align.RIGHT)
+ .appendField(Blockly.Msg.MIXLY_DO)
+ this.setPreviousStatement(true);
+ this.setNextStatement(true);
+ this.setTooltip(Blockly.Msg.MIXLY_ESP32_SYSTEM_TIMER_TOOLTIP);
+ }
+};
+
+export const system_timer_deinit = {
+ init: function () {
+ this.setColour(SYSTEM_HUE);
+ this.appendValueInput('VAR')
+ .appendField("Timer")
+ .setCheck("var");
+ this.appendDummyInput()
+ .setAlign(Blockly.inputs.Align.RIGHT)
+ .appendField(Blockly.Msg.MIXLY_STOP);
+ this.setPreviousStatement(true);
+ this.setNextStatement(true);
+ this.setTooltip('');
+ }
+};
+
export const system_wdt_init = {
init: function () {
this.setColour(SYSTEM_HUE);
@@ -287,6 +283,7 @@ export const system_bitbot_shutdown = {
};
export const Timer_init = system_timer_init;
-export const timer2 = system_timer;
+export const system_timer = system_timer_init;
+export const timer2 = system_timer_init;
export const time_ticks_diff = system_ticks_diff;
export const base_delay = controls_delay;
\ No newline at end of file
diff --git a/boards/default_src/micropython/generators/system.js b/boards/default_src/micropython/generators/system.js
index 6028fb20..f4a1377e 100644
--- a/boards/default_src/micropython/generators/system.js
+++ b/boards/default_src/micropython/generators/system.js
@@ -143,7 +143,7 @@ export const set_system_timer = function (_, generator) {
var period = generator.valueToCode(this, "period", generator.ORDER_NONE) || "0";
var mode = this.getFieldValue('mode');
var callback = generator.valueToCode(this, "callback", generator.ORDER_NONE) || "None";
- var code = v + " = machine.Timer("+ i +",period = " + period + ", mode=machine.Timer." + mode + ", callback=" + callback + ")\n";
+ var code = v + " = machine.Timer("+ i +", period=" + period + ", mode=machine.Timer." + mode + ", callback=" + callback + ")\n";
return code;
}
@@ -154,30 +154,24 @@ export const system_ticks_diff = function (_, generator) {
var code = "time.ticks_diff(" + end + ", " + start + ")";
return [code, generator.ORDER_ATOMIC];
}
-/**
- * @deprecated To be removed in the future
- */
+
export const system_timer_init = function (_, generator) {
- var v = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
generator.definitions_['import_machine'] = 'import machine';
- var code = v + ' = machine.Timer(0)\n';
- return code;
-}
-/**
- * @deprecated To be removed in the future
- */
-export const system_timer = function (_, generator) {
- generator.definitions_['import_machine'] = 'import machine';
- var v = generator.valueToCode(this, "VAR", generator.ORDER_NONE) || "None";
- var period = generator.valueToCode(this, "period", generator.ORDER_NONE) || "0";
+ var v = generator.valueToCode(this, 'VAR', generator.ORDER_NONE) || 'None';
+ var period = generator.valueToCode(this, 'period', generator.ORDER_NONE) || '0';
var mode = this.getFieldValue('mode');
- //var branch = generator.statementToCode(this, 'callback') || generator.PASS;
- var callback = generator.valueToCode(this, "callback", generator.ORDER_NONE) || "None";
- //var code = v + ".init(period = " + period + ", mode = machine.Timer." + mode + ", callback = " + v + "_callback_func)\n";
- //generator.setups_['timer_callback_func'] = 'def ' + v + '_callback_func(t):\n' + branch + '\n';
- var code = v + ".init(period = " + period + ", mode=machine.Timer." + mode + ", callback=" + callback + ")\n";
+ var callback = generator.valueToCode(this, 'callback', generator.ORDER_NONE) || 'None';
+ var code = `${v}.init(period=${period}, mode=machine.Timer.${mode}, callback=${callback})\n`;
return code;
}
+
+export const system_timer_deinit = function (_, generator) {
+ generator.definitions_['import_machine'] = 'import machine';
+ var v = generator.valueToCode(this, 'VAR', generator.ORDER_NONE) || 'None';
+ var code = `${v}.deinit()\n`;
+ return code;
+}
+
export const system_wdt_init = function (_, generator) {
generator.definitions_['import_machine'] = 'import machine';
var period = generator.valueToCode(this, "period", generator.ORDER_NONE) || "0";
@@ -204,6 +198,7 @@ export const system_bitbot_shutdown = function (_, generator) {
}
export const Timer_init = system_timer_init;
-export const timer2 = system_timer;
+export const system_timer = system_timer_init;
+export const timer2 = system_timer_init;
export const time_ticks_diff = system_ticks_diff;
export const base_delay = controls_delay;
\ No newline at end of file
diff --git a/boards/default_src/micropython_educore/template.xml b/boards/default_src/micropython_educore/template.xml
index 9fca83f4..59d2ebb2 100644
--- a/boards/default_src/micropython_educore/template.xml
+++ b/boards/default_src/micropython_educore/template.xml
@@ -212,14 +212,28 @@
-
-
+
+
tim
+
+
+
+
+
+
+ 1000
+
+
+
+
+ tim_callback
+
+
-
+
tim
@@ -253,6 +267,13 @@
+
+
+
+ tim
+
+
+
diff --git a/boards/default_src/micropython_esp32/template.xml b/boards/default_src/micropython_esp32/template.xml
index dec01714..c14956d3 100644
--- a/boards/default_src/micropython_esp32/template.xml
+++ b/boards/default_src/micropython_esp32/template.xml
@@ -272,6 +272,23 @@
+
+
+
+ tim
+
+
+
+
+ 1000
+
+
+
+
+ tim_callback
+
+
+
@@ -289,6 +306,13 @@
+
+
+
+ tim
+
+
+
diff --git a/boards/default_src/micropython_esp32c2/template.xml b/boards/default_src/micropython_esp32c2/template.xml
index 08b42855..f564f41d 100644
--- a/boards/default_src/micropython_esp32c2/template.xml
+++ b/boards/default_src/micropython_esp32c2/template.xml
@@ -247,6 +247,23 @@
+
+
+
+ tim
+
+
+
+
+ 1000
+
+
+
+
+ tim_callback
+
+
+
@@ -264,6 +281,13 @@
+
+
+
+ tim
+
+
+
diff --git a/boards/default_src/micropython_esp32c3/template.xml b/boards/default_src/micropython_esp32c3/template.xml
index a46e562f..97be0ea7 100644
--- a/boards/default_src/micropython_esp32c3/template.xml
+++ b/boards/default_src/micropython_esp32c3/template.xml
@@ -247,6 +247,23 @@
+
+
+
+ tim
+
+
+
+
+ 1000
+
+
+
+
+ tim_callback
+
+
+
@@ -264,6 +281,13 @@
+
+
+
+ tim
+
+
+
diff --git a/boards/default_src/micropython_esp32c5/template.xml b/boards/default_src/micropython_esp32c5/template.xml
index 7853e302..bbbdc1cc 100644
--- a/boards/default_src/micropython_esp32c5/template.xml
+++ b/boards/default_src/micropython_esp32c5/template.xml
@@ -272,6 +272,23 @@
+
+
+
+ tim
+
+
+
+
+ 1000
+
+
+
+
+ tim_callback
+
+
+
@@ -289,6 +306,13 @@
+
+
+
+ tim
+
+
+
diff --git a/boards/default_src/micropython_esp32s2/template.xml b/boards/default_src/micropython_esp32s2/template.xml
index cd9d047c..5884dc14 100644
--- a/boards/default_src/micropython_esp32s2/template.xml
+++ b/boards/default_src/micropython_esp32s2/template.xml
@@ -272,6 +272,23 @@
+
+
+
+ tim
+
+
+
+
+ 1000
+
+
+
+
+ tim_callback
+
+
+
@@ -289,6 +306,13 @@
+
+
+
+ tim
+
+
+
diff --git a/boards/default_src/micropython_esp32s3/template.xml b/boards/default_src/micropython_esp32s3/template.xml
index ca1d1668..993536fe 100644
--- a/boards/default_src/micropython_esp32s3/template.xml
+++ b/boards/default_src/micropython_esp32s3/template.xml
@@ -272,6 +272,23 @@
+
+
+
+ tim
+
+
+
+
+ 1000
+
+
+
+
+ tim_callback
+
+
+
@@ -289,6 +306,13 @@
+
+
+
+ tim
+
+
+
diff --git a/boards/default_src/micropython_robot/template.xml b/boards/default_src/micropython_robot/template.xml
index e44d741d..392d9360 100644
--- a/boards/default_src/micropython_robot/template.xml
+++ b/boards/default_src/micropython_robot/template.xml
@@ -222,14 +222,28 @@
-
-
+
+
tim
+
+
+
+
+
+
+ 1000
+
+
+
+
+ tim_callback
+
+
-
+
tim
@@ -263,6 +277,13 @@
+
+
+
+ tim
+
+
+
diff --git a/common/msg/blockly/en.js b/common/msg/blockly/en.js
index 89cea43b..3c3e897d 100644
--- a/common/msg/blockly/en.js
+++ b/common/msg/blockly/en.js
@@ -4115,5 +4115,6 @@ En.MIXLY_TURTLE_TRACER_DELAY_MS = 'Delay (milliseconds)';
En.MIXLY_TURTLE_TITLE_SET = 'Set window title to';
En.MIXLY_TURTLE_SETUP_SET = 'Set window size';
En.MIXLY_TURTLE_UPDATE = 'Refresh the canvas';
+En.MIXLY_RESTART = 'Restart';
})();
diff --git a/common/msg/blockly/zh-hans.js b/common/msg/blockly/zh-hans.js
index 0e094ec9..1deab9dd 100644
--- a/common/msg/blockly/zh-hans.js
+++ b/common/msg/blockly/zh-hans.js
@@ -4298,5 +4298,6 @@ ZhHans.MIXLY_TURTLE_TRACER_DELAY_MS = '延迟 (毫秒)';
ZhHans.MIXLY_TURTLE_TITLE_SET = '设置窗口标题为';
ZhHans.MIXLY_TURTLE_SETUP_SET = '设置窗口大小';
ZhHans.MIXLY_TURTLE_UPDATE = '刷新画布';
+ZhHans.MIXLY_RESTART = '重新启动';
})();
\ No newline at end of file
diff --git a/common/msg/blockly/zh-hant.js b/common/msg/blockly/zh-hant.js
index 3d8426b1..3865e573 100644
--- a/common/msg/blockly/zh-hant.js
+++ b/common/msg/blockly/zh-hant.js
@@ -4268,5 +4268,6 @@ ZhHant.MIXLY_TURTLE_TRACER_DELAY_MS = '延遲 (毫秒)';
ZhHant.MIXLY_TURTLE_TITLE_SET = '設定視窗標題為';
ZhHant.MIXLY_TURTLE_SETUP_SET = '設定視窗大小';
ZhHant.MIXLY_TURTLE_UPDATE = '刷新畫布';
+ZhHant.MIXLY_RESTART = '重新啟動';
})();