add_blocks_for_timer_and_fix_bugs

This commit is contained in:
Eason010212
2025-08-27 17:25:00 +08:00
parent edd97c3d95
commit 880f786a52
5 changed files with 95 additions and 10 deletions

View File

@@ -574,6 +574,35 @@ Blockly.Blocks['lineChart_recieved'] = {
} }
}; };
Blockly.Blocks['timer_triggered'] = {
init: function() {
this.setColour(lineChart_HUE);
this.appendDummyInput().appendField(Blockly.MIXLY_MICROBIT_JS_CURRENT);
this.appendValueInput("name").setCheck("String");
this.appendDummyInput().appendField(Blockly.TIMER_TRIGGERED);
this.appendDummyInput().appendField(Blockly.Msg.PROCEDURES_CALL_BEFORE_PARAMS+"value");
this.setInputsInline(true);
this.appendStatementInput('DO0').appendField(Blockly.Msg.CONTROLS_REPEAT_INPUT_DO);
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setTooltip("");
},
getVars:function(){
return ["time","value"];
}
};
Blockly.Blocks['get_trigger_times'] = {
init: function() {
this.setColour(lineChart_HUE);
this.appendDummyInput().appendField(Blockly.MIXLY_MICROBIT_PY_STORAGE_GET);
this.appendValueInput("name").setCheck("String");
this.appendDummyInput().appendField(Blockly.GET_TRIGGER_TIMES);
this.setOutput(true, null);
this.setTooltip("");
}
};
Blockly.Blocks['lineChart_send'] = { Blockly.Blocks['lineChart_send'] = {
init: function() { init: function() {
this.appendDummyInput().appendField(Blockly.TO); this.appendDummyInput().appendField(Blockly.TO);
@@ -2086,6 +2115,19 @@ Blockly.JavaScript.lineChart_recieved=function(block) {
return code; return code;
}; };
Blockly.JavaScript.timer_triggered=function(block) {
var name = Blockly.JavaScript.valueToCode(this, 'name', Blockly.JavaScript.ORDER_ATOMIC);
var code="MixIO.getInstance("+name+",MixIO.typeTags.TIMER)\n"+".bind(MixIO.eventTags.TIMER_TRIGGERED, function(event,value){\n"
+Blockly.JavaScript.statementToCode(block, "DO0" )+"\n"+"})\n"
return code;
};
Blockly.JavaScript.get_trigger_times=function(block) {
var name = Blockly.JavaScript.valueToCode(this, 'name', Blockly.JavaScript.ORDER_ATOMIC);
var code="MixIO.getInstance("+name+",MixIO.typeTags.TIMER).getTriggerTimes()"
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
Blockly.JavaScript.lineChart_send=function(block) { Blockly.JavaScript.lineChart_send=function(block) {
var name = Blockly.JavaScript.valueToCode(this, 'name', Blockly.JavaScript.ORDER_ATOMIC); var name = Blockly.JavaScript.valueToCode(this, 'name', Blockly.JavaScript.ORDER_ATOMIC);
var message = Blockly.JavaScript.valueToCode(this, 'message', Blockly.JavaScript.ORDER_ATOMIC); var message = Blockly.JavaScript.valueToCode(this, 'message', Blockly.JavaScript.ORDER_ATOMIC);
@@ -3067,6 +3109,9 @@ Blockly.LINECHART_N_MESSAGE="折线图表的至多前";
Blockly.LINECHART_MESSAGE="条消息"; Blockly.LINECHART_MESSAGE="条消息";
Blockly.LINECHART_LATEST_MESSAGE="折线图表的最新一条消息"; Blockly.LINECHART_LATEST_MESSAGE="折线图表的最新一条消息";
Blockly.TIMER_TRIGGERED="定时触发器触发时";
Blockly.GET_TRIGGER_TIMES="定时触发器的触发次数"
Blockly.BARCHART_RECIEVED="柱状图收到消息时"; Blockly.BARCHART_RECIEVED="柱状图收到消息时";
Blockly.BARCHART_SEND_MESSAGE="柱状图发送消息"; Blockly.BARCHART_SEND_MESSAGE="柱状图发送消息";
Blockly.CLEAR_BARCHART="柱状图表清空"; Blockly.CLEAR_BARCHART="柱状图表清空";

View File

@@ -654,8 +654,28 @@
</block> </block>
</category> </category>
<category name="定时触发器" colour="#4e73df" css-icon="customIcon fa fa-clock-o"> <category name="定时触发器" colour="#4e73df" css-icon="customIcon fa fa-clock-o">
<block type="timer_triggered">
<value name="name">
<shadow type="text">
<field name="TEXT">name</field>
</shadow>
</value>
<value name="message">
<shadow type="text">
<field name="TEXT">message</field>
</shadow>
</value>
</block>
<block type="get_trigger_times">
<value name="name">
<shadow type="text">
<field name="TEXT">name</field>
</shadow>
</value>
</block>
</category> </category>
<category name="条件触发器" colour="#4e73df" css-icon="customIcon fa fa-code-fork"> <category name="条件触发器" colour="#4e73df" css-icon="customIcon fa fa-code-fork">
</category> </category>
<category name="蓝牙转发器" colour="#4e73df" css-icon="customIcon fa fa-bluetooth-b"> <category name="蓝牙转发器" colour="#4e73df" css-icon="customIcon fa fa-bluetooth-b">
</category> </category>

View File

@@ -79,13 +79,14 @@ var MixIO = {
DATA_TABLE: 10, DATA_TABLE: 10,
DASHBOARD: 11, DASHBOARD: 11,
DATA_MAP: 12, DATA_MAP: 12,
WEATHER: 13 WEATHER: 13,
TIMER: 14
}, },
oldTags: ["input_button", "input_slider", "input_keyboard", "input_controller", "input_rgb", "output_bulb", "output_text", oldTags: ["input_button", "input_slider", "input_keyboard", "input_controller", "input_rgb", "output_bulb", "output_text",
"output_chart", "output_bar", "table", "output_dashboard", "output_map", "input_weather" "output_chart", "output_bar", "table", "output_dashboard", "output_map", "input_weather", "timer"
], ],
zhcnTags: ["按键/开关", "滑杆", "文本输入", "摇杆手柄", "RGB色盘", "指示灯", "文本显示屏", "折线图表", "柱状图表", "数据表格", "仪表盘", "数据地图", "实时气象仪"], zhcnTags: ["按键/开关", "滑杆", "文本输入", "摇杆手柄", "RGB色盘", "指示灯", "文本显示屏", "折线图表", "柱状图表", "数据表格", "仪表盘", "数据地图", "实时气象仪", "定时触发器"],
/*合法的MixIO事件种类*/ /*合法的MixIO事件种类*/
eventTags: { eventTags: {
@@ -121,7 +122,9 @@ var MixIO = {
DATA_MAP_CHANGED: "1211", //数据地图收到消息 DATA_MAP_CHANGED: "1211", //数据地图收到消息
WEATHER_SYNCED: "1311", //气象仪更新数据 WEATHER_SYNCED: "1311", //气象仪更新数据
WEATHER_SENT: "1312" //气象仪发送数据 WEATHER_SENT: "1312", //气象仪发送数据
TIMER_TRIGGERED: "1411"
}, },
@@ -159,8 +162,9 @@ var MixIO = {
DATA_MAP_CLEAR: "1222", //清空数据地图数据 DATA_MAP_CLEAR: "1222", //清空数据地图数据
WEATHER_SYNC: "1321", //更新气象仪数据 WEATHER_SYNC: "1321", //更新气象仪数据
WEATHER_SEND: "1322" //发送气象仪数据 WEATHER_SEND: "1322", //发送气象仪数据
TRIGGER_TIMER: "1411"
}, },
publish: function(topic, message) { publish: function(topic, message) {
// if message undefined // if message undefined
@@ -328,6 +332,15 @@ var MixIO = {
instance.getData = function(type) { instance.getData = function(type) {
return sepMsgs[type] return sepMsgs[type]
} }
} else if (type === MixIO.typeTags.TIMER){
instance.getTriggerTimes = function(){
try{
return parseInt(instance.attr('user-times'))
}
catch{
return 0
}
}
} }
return instance return instance
}, },

View File

@@ -1,5 +1,6 @@
var globalVer = "MixIO ver 1.11.24" var globalVer = "MixIO ver 1.11.24"
var isChanged = false; var isChanged = false;
var initCodeMirror = false;
var globalBLE = {} var globalBLE = {}
function copy(obj) { function copy(obj) {
return JSON.parse(JSON.stringify(obj)) return JSON.parse(JSON.stringify(obj))
@@ -1917,7 +1918,7 @@ function view_project(projectName, projectType) {
window.addEventListener("resize", function() { window.addEventListener("resize", function() {
chart.resize(); chart.resize();
}); });
if (projectType == LOGIC_MODE && isCode) { if (projectType == LOGIC_MODE && isCode && !initCodeMirror) {
init_codemirror() init_codemirror()
} }
}) })

View File

@@ -2403,6 +2403,7 @@ function add_magic(user_title, user_topic, user_content, user_style, title_style
async function add_timer(user_title, user_topic, user_content, user_style, title_style, isObserve) { async function add_timer(user_title, user_topic, user_content, user_style, title_style, isObserve) {
if(MixIO.editor == undefined) if(MixIO.editor == undefined)
{ {
initCodeMirror = true
await init_codemirror() await init_codemirror()
} }
var isAlive = true var isAlive = true
@@ -2430,19 +2431,23 @@ async function add_timer(user_title, user_topic, user_content, user_style, title
var itemdiv = add_block(1, 1, contents, attrs) var itemdiv = add_block(1, 1, contents, attrs)
MixIO.triggers[title.text()] = function() { MixIO.triggers[title.text()] = function() {
var localTime = 0 var localTime = 0
title.parent().parent().attr('user-times', localTime)
MixIO.setInterval(function() { MixIO.setInterval(function() {
if (triggerTimes == 0 || localTime < triggerTimes) { if (triggerTimes == 0 || localTime < triggerTimes) {
itemdiv.addClass("triggered") itemdiv.addClass("triggered")
setTimeout(function() { setTimeout(function() {
itemdiv.removeClass("triggered") itemdiv.removeClass("triggered")
}, 150) }, 150)
// Aug 2025
var sendMessage = triggerMessage
if(triggerMessage=="$CURR_TIME$") if(triggerMessage=="$CURR_TIME$")
MixIO.publish(triggerTopic, (new Date().getHours() > 10 ? new Date().getHours()+"" : ("0" + new Date().getHours())) +":"+ (new Date().getMinutes() > 10 ? new Date().getMinutes()+"" : ("0" + new Date().getMinutes())) +":"+ (new Date().getSeconds() > 10 ? new Date().getSeconds()+"" : ("0" + new Date().getSeconds()))) sendMessage = (new Date().getHours() > 10 ? new Date().getHours()+"" : ("0" + new Date().getHours())) +":"+ (new Date().getMinutes() > 10 ? new Date().getMinutes()+"" : ("0" + new Date().getMinutes())) +":"+ (new Date().getSeconds() > 10 ? new Date().getSeconds()+"" : ("0" + new Date().getSeconds()))
else if(triggerMessage=="$RAN_NUM$") else if(triggerMessage=="$RAN_NUM$")
MixIO.publish(triggerTopic, Math.round(Math.random()*99+1)) sendMessage = Math.round(Math.random()*99+1)
else MixIO.publish(triggerTopic, sendMessage)
MixIO.publish(triggerTopic, triggerMessage)
localTime = localTime + 1 localTime = localTime + 1
title.parent().parent().attr('user-times', localTime)
itemdiv.trigger(MixIO.eventTags.TIMER_TRIGGERED, sendMessage)
} }
}, triggerInterval) }, triggerInterval)
} }
@@ -2623,6 +2628,7 @@ async function add_timer(user_title, user_topic, user_content, user_style, title
async function add_trigger(user_title, user_topic, user_content, user_style, title_style, isObserve) { async function add_trigger(user_title, user_topic, user_content, user_style, title_style, isObserve) {
if(MixIO.editor == undefined) if(MixIO.editor == undefined)
{ {
initCodeMirror = true
await init_codemirror() await init_codemirror()
} }
var isAlive = true var isAlive = true