feat(boards): 更新micropython下machine.Timer图形块

This commit is contained in:
王立帮
2026-01-20 18:01:04 +08:00
parent 899c811dc1
commit 0968a1c678
13 changed files with 257 additions and 76 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -212,14 +212,28 @@
</block>
</statement>
</block>
<block type="system_timer_init">
<value name="SUB">
<block type="set_system_timer">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
<value name="PIN">
<shadow type="timer_id_pin">
</shadow>
</value>
<value name="period">
<shadow type="math_number">
<field name="NUM">1000</field>
</shadow>
</value>
<value name="callback">
<shadow type="factory_block_return">
<field name="VALUE">tim_callback</field>
</shadow>
</value>
</block>
<block type="system_timer">
<block type="system_timer_init">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
@@ -253,6 +267,13 @@
</block>
</statement>
</block>
<block type="system_timer_deinit">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
</block>
<block type="controls_millis"></block>
<block type="system_ticks_diff">
<value name="END">

View File

@@ -272,6 +272,23 @@
</shadow>
</value>
</block>
<block type="system_timer_init">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
<value name="period">
<shadow type="math_number">
<field name="NUM">1000</field>
</shadow>
</value>
<value name="callback">
<shadow type="factory_block_return">
<field name="VALUE">tim_callback</field>
</shadow>
</value>
</block>
<block type="procedures_defnoreturn">
<mutation>
<arg name="tim"></arg>
@@ -289,6 +306,13 @@
</block>
</statement>
</block>
<block type="system_timer_deinit">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
</block>
<block type="system_wdt_init">
<value name="period">
<shadow type="math_number">

View File

@@ -247,6 +247,23 @@
</shadow>
</value>
</block>
<block type="system_timer_init">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
<value name="period">
<shadow type="math_number">
<field name="NUM">1000</field>
</shadow>
</value>
<value name="callback">
<shadow type="factory_block_return">
<field name="VALUE">tim_callback</field>
</shadow>
</value>
</block>
<block type="procedures_defnoreturn">
<mutation>
<arg name="tim"></arg>
@@ -264,6 +281,13 @@
</block>
</statement>
</block>
<block type="system_timer_deinit">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
</block>
<block type="system_wdt_init">
<value name="period">
<shadow type="math_number">

View File

@@ -247,6 +247,23 @@
</shadow>
</value>
</block>
<block type="system_timer_init">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
<value name="period">
<shadow type="math_number">
<field name="NUM">1000</field>
</shadow>
</value>
<value name="callback">
<shadow type="factory_block_return">
<field name="VALUE">tim_callback</field>
</shadow>
</value>
</block>
<block type="procedures_defnoreturn">
<mutation>
<arg name="tim"></arg>
@@ -264,6 +281,13 @@
</block>
</statement>
</block>
<block type="system_timer_deinit">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
</block>
<block type="system_wdt_init">
<value name="period">
<shadow type="math_number">

View File

@@ -272,6 +272,23 @@
</shadow>
</value>
</block>
<block type="system_timer_init">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
<value name="period">
<shadow type="math_number">
<field name="NUM">1000</field>
</shadow>
</value>
<value name="callback">
<shadow type="factory_block_return">
<field name="VALUE">tim_callback</field>
</shadow>
</value>
</block>
<block type="procedures_defnoreturn">
<mutation>
<arg name="tim"></arg>
@@ -289,6 +306,13 @@
</block>
</statement>
</block>
<block type="system_timer_deinit">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
</block>
<block type="system_wdt_init">
<value name="period">
<shadow type="math_number">

View File

@@ -272,6 +272,23 @@
</shadow>
</value>
</block>
<block type="system_timer_init">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
<value name="period">
<shadow type="math_number">
<field name="NUM">1000</field>
</shadow>
</value>
<value name="callback">
<shadow type="factory_block_return">
<field name="VALUE">tim_callback</field>
</shadow>
</value>
</block>
<block type="procedures_defnoreturn">
<mutation>
<arg name="tim"></arg>
@@ -289,6 +306,13 @@
</block>
</statement>
</block>
<block type="system_timer_deinit">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
</block>
<block type="system_wdt_init">
<value name="period">
<shadow type="math_number">

View File

@@ -272,6 +272,23 @@
</shadow>
</value>
</block>
<block type="system_timer_init">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
<value name="period">
<shadow type="math_number">
<field name="NUM">1000</field>
</shadow>
</value>
<value name="callback">
<shadow type="factory_block_return">
<field name="VALUE">tim_callback</field>
</shadow>
</value>
</block>
<block type="procedures_defnoreturn">
<mutation>
<arg name="tim"></arg>
@@ -289,6 +306,13 @@
</block>
</statement>
</block>
<block type="system_timer_deinit">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
</block>
<block type="system_wdt_init">
<value name="period">
<shadow type="math_number">

View File

@@ -222,14 +222,28 @@
</block>
</statement>
</block>
<block type="system_timer_init">
<value name="SUB">
<block type="set_system_timer">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
<value name="PIN">
<shadow type="timer_id_pin">
</shadow>
</value>
<value name="period">
<shadow type="math_number">
<field name="NUM">1000</field>
</shadow>
</value>
<value name="callback">
<shadow type="factory_block_return">
<field name="VALUE">tim_callback</field>
</shadow>
</value>
</block>
<block type="system_timer">
<block type="system_timer_init">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
@@ -263,6 +277,13 @@
</block>
</statement>
</block>
<block type="system_timer_deinit">
<value name="VAR">
<shadow type="variables_get">
<field name="VAR">tim</field>
</shadow>
</value>
</block>
<block type="controls_millis"></block>
<block type="system_ticks_diff">
<value name="END">