init implementation of procedures
This commit is contained in:
@@ -44,7 +44,7 @@ Blockly['Arduino'].addReservedWords(
|
||||
'setup,loop,if,else,for,switch,case,while,' +
|
||||
'do,break,continue,return,goto,define,include,' +
|
||||
'HIGH,LOW,INPUT,OUTPUT,INPUT_PULLUP,true,false,' +
|
||||
'interger, constants,floating,point,void,bookean,char,' +
|
||||
'interger, constants,floating,point,void,boolean,char,' +
|
||||
'unsigned,byte,int,word,long,float,double,string,String,array,' +
|
||||
'static, volatile,const,sizeof,pinMode,digitalWrite,digitalRead,' +
|
||||
'analogReference,analogRead,analogWrite,tone,noTone,shiftOut,shitIn,' +
|
||||
|
||||
@@ -28,3 +28,97 @@ Blockly.Arduino['arduino_functions'] = function (block) {
|
||||
//var loopcode = Blockly.Arduino.scrub_(block, loopBranch); No comment block
|
||||
return loopBranch;
|
||||
};
|
||||
|
||||
Blockly.Arduino['procedures_defreturn'] = function (block: Block | any) {
|
||||
// Define a procedure with a return value.
|
||||
const funcName = Blockly.Arduino.variableDB_.getName(
|
||||
block.getFieldValue('NAME'),
|
||||
Blockly.Procedures.NAME_TYPE
|
||||
);
|
||||
const branch = Blockly.Arduino.statementToCode(block, 'STACK');
|
||||
const returnType = block.getFieldValue('RETURN TYPE') || 'void';
|
||||
|
||||
let returnValue =
|
||||
Blockly.Arduino.valueToCode(block, 'RETURN', Blockly.Arduino.ORDER_NONE) ||
|
||||
'';
|
||||
if (returnValue) {
|
||||
returnValue = Blockly.Arduino.INDENT + 'return ' + returnValue + ';\n';
|
||||
}
|
||||
const args = [];
|
||||
for (let i = 0; i < block.argumentVarModels_.length; i++) {
|
||||
args[i] =
|
||||
translateType(block.argumentVarModels_[i].type) +
|
||||
' ' +
|
||||
block.argumentVarModels_[i].name;
|
||||
}
|
||||
let code =
|
||||
translateType(returnType) +
|
||||
' ' +
|
||||
funcName +
|
||||
'(' +
|
||||
args.join(', ') +
|
||||
') {\n' +
|
||||
branch +
|
||||
returnValue +
|
||||
'}';
|
||||
code = Blockly.Arduino.scrub_(block, code);
|
||||
// Add % so as not to collide with helper functions in definitions list.
|
||||
Blockly.Arduino.functionNames_['%' + funcName] = code;
|
||||
return null;
|
||||
};
|
||||
|
||||
function translateType(type) {
|
||||
switch (type) {
|
||||
case 'Number':
|
||||
return 'double';
|
||||
case 'String':
|
||||
return 'String';
|
||||
case 'Boolean':
|
||||
return 'boolean';
|
||||
case 'void':
|
||||
return 'void';
|
||||
default:
|
||||
throw new Error('Invalid Parameter Type');
|
||||
}
|
||||
}
|
||||
|
||||
Blockly.Arduino['procedures_defnoreturn'] =
|
||||
Blockly.Arduino['procedures_defreturn'];
|
||||
|
||||
Blockly.Arduino['procedures_callreturn'] = function (block) {
|
||||
// Call a procedure with a return value.
|
||||
const funcName = Blockly.Arduino.variableDB_.getName(
|
||||
block.getFieldValue('NAME'),
|
||||
Blockly.Procedures.NAME_TYPE
|
||||
);
|
||||
const args = [];
|
||||
for (let i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] =
|
||||
Blockly.Arduino.valueToCode(
|
||||
block,
|
||||
'ARG' + i,
|
||||
Blockly.Arduino.ORDER_COMMA
|
||||
) || 'null';
|
||||
}
|
||||
const code = funcName + '(' + args.join(', ') + ')';
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Arduino['procedures_callnoreturn'] = function (block) {
|
||||
// Call a procedure with no return value.
|
||||
const funcName = Blockly.Arduino.variableDB_.getName(
|
||||
block.getFieldValue('NAME'),
|
||||
Blockly.Procedures.NAME_TYPE
|
||||
);
|
||||
const args = [];
|
||||
for (let i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] =
|
||||
Blockly.Arduino.valueToCode(
|
||||
block,
|
||||
'ARG' + i,
|
||||
Blockly.Arduino.ORDER_COMMA
|
||||
) || 'null';
|
||||
}
|
||||
|
||||
return funcName + '(' + args.join(', ') + ');\n';
|
||||
};
|
||||
@@ -263,32 +263,22 @@ Blockly.Arduino.sensebox_scd30 = function () {
|
||||
Blockly.Arduino.libraries_['scd30_library'] = '#include "SparkFun_SCD30_Arduino_Library.h"'
|
||||
Blockly.Arduino.libraries_['library_senseBoxMCU'] = '#include "SenseBoxMCU.h"';
|
||||
Blockly.Arduino.definitions_['SCD30'] = 'SCD30 airSensor;';
|
||||
Blockly.Arduino.variables_['scd30_temp'] = 'float scd30_temp;';
|
||||
Blockly.Arduino.variables_['scd30_humi'] = 'float scd30_humi;';
|
||||
Blockly.Arduino.variables_['scd30_co2'] = 'float scd30_co2;';
|
||||
Blockly.Arduino.setupCode_['init_scd30'] = ` Wire.begin();
|
||||
if (airSensor.begin() == false)
|
||||
{
|
||||
Serial.println("Air sensor not detected. Please check wiring. Freezing...");
|
||||
while (1)
|
||||
;
|
||||
}`;
|
||||
Blockly.Arduino.loopCodeOnce_['scd30_getData'] = `if (airSensor.dataAvailable())
|
||||
{
|
||||
scd30_co2 = airSensor.getCO2();
|
||||
scd30_temp = airSensor.getTemperature();
|
||||
scd30_humi = airSensor.getHumidity();
|
||||
}`
|
||||
var code = '';
|
||||
switch (dropdown) {
|
||||
case 'temperature':
|
||||
code = 'scd30_temp';
|
||||
code = 'aireSensor.getTemperature()';
|
||||
break;
|
||||
case 'humidity':
|
||||
code = 'scd30_humi';
|
||||
code = 'airSensor.getHumiditiy()';
|
||||
break;
|
||||
case 'CO2':
|
||||
code = 'scd30_co2';
|
||||
code = 'aireSensor.getCO2()';
|
||||
break;
|
||||
default:
|
||||
code = ''
|
||||
|
||||
Reference in New Issue
Block a user