Update(boards): 将arduino下函数和变量相关图形块定义移动到arduino目录下

This commit is contained in:
王立帮
2025-03-11 02:02:06 +08:00
parent 93c02a23b2
commit 2e18757834
10 changed files with 88 additions and 39 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,97 @@
import * as Blockly from 'blockly/core';
import { Variables } from 'blockly/core';
const VARIABLES_HUE = 330;
// ************************************************************************
// THIS SECTION IS INSERTED INTO BLOCKLY BY BLOCKLYDUINO.
export const variables_declare = {
// Variable setter.
init: function () {
this.setColour(VARIABLES_HUE);
this.appendValueInput('VALUE', null)
.appendField(Blockly.Msg.MIXLY_DECLARE)
.appendField(new Blockly.FieldDropdown([[Blockly.Msg.MIXLY_GLOBAL_VARIABLE, "global_variate"], [Blockly.Msg.MIXLY_LOCAL_VARIABLE, "local_variate"]]), "variables_type")
.appendField(new Blockly.FieldTextInput('item'), 'VAR')
.appendField(Blockly.Msg.MIXLY_AS)
.appendField(new Blockly.FieldDropdown(Variables.DATA_TYPE), "TYPE")
.appendField(Blockly.Msg.MIXLY_VALUE);
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_VARIABLES_DECLARE);
},
getVars: function () {
return [this.getFieldValue('VAR')];
},
renameVar: function (oldName, newName) {
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
this.setFieldValue(newName, 'VAR');
}
}
};
// ************************************************************************
export const variables_get = {
init: function () {
this.setColour(VARIABLES_HUE);
this.appendDummyInput()
.appendField(new Blockly.FieldTextInput('item'), 'VAR')
this.setOutput(true);
this.setTooltip(Blockly.Msg.VARIABLES_GET_TOOLTIP);
},
getVars: function () {
return [this.getFieldValue('VAR')];
},
renameVar: function (oldName, newName) {
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
this.setFieldValue(newName, 'VAR');
}
}/*,
onchange: function() {
var varName = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'),Blockly.Variables.NAME_TYPE);
if(Blockly.Arduino.definitions_['var_declare'+varName]){
this.setWarningText(null);
}else{
this.setWarningText(Blockly.Msg.MIXLY_WARNING_NOT_DECLARE);
}
}*/
};
export const variables_set = {
init: function () {
this.setColour(VARIABLES_HUE);
this.appendValueInput('VALUE')
.appendField(new Blockly.FieldTextInput('item'), 'VAR')
.appendField(Blockly.Msg.MIXLY_VALUE2);
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setTooltip(Blockly.Msg.VARIABLES_SET_TOOLTIP);
},
getVars: function () {
return [this.getFieldValue('VAR')];
},
renameVar: function (oldName, newName) {
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
this.setFieldValue(newName, 'VAR');
}
}
};
/**
* Block for basic data type change.
* @this Blockly.Block
*/
export const variables_change = {
init: function () {
this.setColour(VARIABLES_HUE);
this.appendValueInput('MYVALUE')
.appendField(new Blockly.FieldDropdown(Variables.DATA_TYPE), 'OP');
// Assign 'this' to a variable for use in the tooltip closure below.
this.setOutput(true);
this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_VARIABLES_CHANGE);
}
};

View File

@@ -1,7 +1,11 @@
import * as ArduinoEthernetBlocks from './blocks/ethernet';
import * as ArduinoProceduresBlocks from './blocks/procedures';
import * as ArduinoTextBlocks from './blocks/text';
import * as ArduinoVariablesBlocks from './blocks/variables';
import * as ArduinoProceduresGenerators from './generators/procedures';
import * as ArduinoEthernetGenerators from './generators/ethernet';
import * as ArduinoTextGenerators from './generators/text';
import * as ArduinoVariablesGenerators from './generators/variables';
import Names from './others/names';
import Procedures from './others/procedures';
import Variables from './others/variables';
@@ -9,9 +13,13 @@ import { ArduinoGenerator, Arduino } from './arduino_generator';
export {
ArduinoEthernetBlocks,
ArduinoProceduresBlocks,
ArduinoTextBlocks,
ArduinoVariablesBlocks,
ArduinoEthernetGenerators,
ArduinoProceduresGenerators,
ArduinoTextGenerators,
ArduinoVariablesGenerators,
Names,
Procedures,
Variables,

View File

@@ -0,0 +1,114 @@
import { Variables, Procedures } from 'blockly/core';
export const procedures_defreturn = function (_, generator) {
// Define a procedure with a return value.
var funcName = generator.variableDB_.getName(this.getFieldValue('NAME'),
Procedures.NAME_TYPE);
var branch = (this.getInput('STACK') && generator.statementToCode(this, 'STACK'));
if (generator.INFINITE_LOOP_TRAP) {
branch = generator.INFINITE_LOOP_TRAP.replace(/%1/g,
'\'' + this.id + '\'') + branch;
}
var returnValue = generator.valueToCode(this, 'RETURN',
generator.ORDER_NONE) || '';
var type = this.getFieldValue('TYPE');
if (type === 'CUSTOM') {
if (this.getField('RETRUN_TYPE')) {
type = this.getFieldValue('RETRUN_TYPE');
} else {
type = 'void';
}
}
if (returnValue) {
returnValue = ' return ' + returnValue + ';\n';
}
var returnType = type ? type : 'void';
var args = [];
for (var x = 0; x < this.arguments_.length; x++) {
args[x] = this.argumentstype_[x] + ' ' + generator.variableDB_.getName(this.arguments_[x],
Variables.NAME_TYPE);
}
var code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' +
branch + returnValue + '}\n';
code = generator.scrub_(this, code);
generator.definitions_[funcName] = code;
return null;
}
export const procedures_defnoreturn = function (_, generator) {
// Define a procedure with a return value.
var funcName = generator.variableDB_.getName(this.getFieldValue('NAME'),
Procedures.NAME_TYPE);
var branch = (this.getInput('STACK') && generator.statementToCode(this, 'STACK'));
if (generator.INFINITE_LOOP_TRAP) {
branch = generator.INFINITE_LOOP_TRAP.replace(/%1/g,
'\'' + this.id + '\'') + branch;
}
var returnType = 'void';
var args = [];
for (var x = 0; x < this.arguments_.length; x++) {
args[x] = this.argumentstype_[x] + ' ' + generator.variableDB_.getName(this.arguments_[x],
Variables.NAME_TYPE);
}
var code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' +
branch + '}\n';
code = generator.scrub_(this, code);
generator.definitions_[funcName] = code;
return null;
}
export const procedures_callreturn = function (_, generator) {
// Call a procedure with a return value.
var funcName = generator.variableDB_.getName(this.getFieldValue('NAME'),
Procedures.NAME_TYPE);
var args = [];
for (var x = 0; x < this.arguments_.length; x++) {
args[x] = generator.valueToCode(this, 'ARG' + x,
generator.ORDER_NONE) || 'null';
}
var code = funcName + '(' + args.join(', ') + ')';
return [code, generator.ORDER_UNARY_POSTFIX];
}
export const procedures_callnoreturn = function (_, generator) {
// Call a procedure with no return value.
var funcName = generator.variableDB_.getName(this.getFieldValue('NAME'),
Procedures.NAME_TYPE);
var args = [];
for (var x = 0; x < this.arguments_.length; x++) {
args[x] = generator.valueToCode(this, 'ARG' + x,
generator.ORDER_NONE) || 'null';
}
var code = funcName + '(' + args.join(', ') + ');\n';
return code;
}
export const procedures_ifreturn = function (_, generator) {
// Conditionally return value from a procedure.
var condition = generator.valueToCode(this, 'CONDITION',
generator.ORDER_NONE) || 'false';
var code = 'if (' + condition + ') {\n';
if (this.hasReturnValue_) {
var value = generator.valueToCode(this, 'VALUE',
generator.ORDER_NONE) || '';
code += ' return ' + value + ';\n';
} else {
code += ' return;\n';
}
code += '}\n';
return code;
}
export const procedures_return = function (_, generator) {
// Conditionally return value from a procedure.
var code = ""
if (this.hasReturnValue_) {
var value = generator.valueToCode(this, 'VALUE',
generator.ORDER_NONE) || 'None';
code += 'return ' + value + ';\n';
} else {
code += 'return;\n';
}
code += '\n';
return code;
}

View File

@@ -0,0 +1,61 @@
import { Variables } from 'blockly/core';
export const variables_get = function (_, generator) {
// Variable getter.
var code = generator.variableDB_.getName(this.getFieldValue('VAR'),
Variables.NAME_TYPE);
return [code, generator.ORDER_ATOMIC];
}
export const variables_declare = function (_, generator) {
var dropdown_type = this.getFieldValue('TYPE');
var dropdown_variables_type = this.getFieldValue('variables_type');
var argument0;
var code = '';
//TODO: settype to variable
if (dropdown_variables_type == 'global_variate') {
if (dropdown_type == 'String') {
argument0 = generator.valueToCode(this, 'VALUE', generator.ORDER_ASSIGNMENT) || '""';
} else {
argument0 = generator.valueToCode(this, 'VALUE', generator.ORDER_ASSIGNMENT) || '0';
}
var varName = generator.variableDB_.getName(this.getFieldValue('VAR'),
Variables.NAME_TYPE);
if (dropdown_type == 'String' || dropdown_type == 'char*')
generator.definitions_['var_declare' + varName] = dropdown_type + ' ' + varName + ';';
else
generator.definitions_['var_declare' + varName] = 'volatile ' + dropdown_type + ' ' + varName + ';';
generator.setups_['setup_var' + varName] = varName + ' = ' + argument0 + ';';
}
else {
if (dropdown_type == 'String') {
argument0 = generator.valueToCode(this, 'VALUE', generator.ORDER_ASSIGNMENT) || '""';
} else {
argument0 = generator.valueToCode(this, 'VALUE', generator.ORDER_ASSIGNMENT) || '0';
}
var varName = generator.variableDB_.getName(this.getFieldValue('VAR'),
Variables.NAME_TYPE);
code = dropdown_type + ' ' + varName + ' = ' + argument0 + ';\n';
}
//generator.variableTypes_[varName] = dropdown_type;//处理变量类型
return code;
}
export const variables_set = function (_, generator) {
// Variable setter.
var argument0 = generator.valueToCode(this, 'VALUE',
generator.ORDER_ASSIGNMENT) || '0';
var varName = generator.variableDB_.getName(this.getFieldValue('VAR'),
Variables.NAME_TYPE);
return varName + ' = ' + argument0 + ';\n';
}
export const variables_change = function (_, generator) {
// Variable setter.
var operator = this.getFieldValue('OP');
var varName = generator.valueToCode(this, 'MYVALUE', generator.ORDER_ASSIGNMENT);
//修复强制类型转换不正确的bug
var code = '((' + operator + ')(' + varName + '))';
return [code, generator.ORDER_ATOMIC];
}

View File

@@ -25,9 +25,15 @@
* @namespace
*/
import * as Blockly from 'blockly/core';
import Variables from './variables';
const Procedures = {};
Procedures.DATA_TYPE = [
...Variables.DATA_TYPE,
[Blockly.Msg.MIXLY_OTHER, 'CUSTOM']
];
/**
* Constant to separate procedure names from variables and generated functions
* when running generators.