add new blocks
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import * as Blockly from 'blockly/core';
|
||||
|
||||
|
||||
/**
|
||||
* Function for turning the tone library on on a given pin (X).
|
||||
* Arduino code: setup { pinMode(X, OUTPUT) }
|
||||
* loop { tone(X, frequency) }
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
|
||||
Blockly.Arduino['io_tone'] = function (block) {
|
||||
var pin = block.getFieldValue('TONEPIN');
|
||||
var freq = Blockly.Arduino.valueToCode(block, 'FREQUENCY', Blockly.Arduino.ORDER_ATOMIC);
|
||||
Blockly.Arduino.setupCode_['io_tone' + pin] = 'pinMode(' + pin + ', OUTPUT);\n';
|
||||
var code = 'tone(' + pin + ',' + freq + ');\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Arduino['io_notone'] = function (block) {
|
||||
var pin = block.getFieldValue("TONEPIN");
|
||||
Blockly.Arduino.setupCode_['io_tone' + pin] = 'pinMode(' + pin + ', OUTPUT);\n';
|
||||
var code = 'noTone(' + pin + ');\n';
|
||||
return code;
|
||||
};
|
||||
@@ -7,11 +7,15 @@ import './sensebox-web';
|
||||
import './sensebox-display';
|
||||
import './sensebox-lora';
|
||||
import './sensebox-led';
|
||||
import './sensebox-sd';
|
||||
import './logic';
|
||||
import './text';
|
||||
import './math';
|
||||
import './map';
|
||||
import './io';
|
||||
import './audio';
|
||||
import './procedures';
|
||||
import './time';
|
||||
import './variables';
|
||||
|
||||
|
||||
|
||||
@@ -86,7 +86,37 @@ Blockly.Arduino['controls_if'] = function (Block) {
|
||||
return code + '\n';
|
||||
};
|
||||
|
||||
Blockly.Arduino['controls_ifelse'] = Blockly.Arduino['control_if'];
|
||||
Blockly.Arduino['controls_ifelse'] = function (Block) {
|
||||
// If/elseif/else condition.
|
||||
let n = 0;
|
||||
let code = '',
|
||||
branchCode,
|
||||
conditionCode;
|
||||
do {
|
||||
conditionCode =
|
||||
Blockly.Arduino.valueToCode(
|
||||
Block,
|
||||
'IF' + n,
|
||||
Blockly.Arduino.ORDER_NONE
|
||||
) || 'false';
|
||||
branchCode = Blockly.Arduino.statementToCode(Block, 'DO' + n);
|
||||
code +=
|
||||
(n > 0 ? ' else ' : '') +
|
||||
'if (' +
|
||||
conditionCode +
|
||||
') {\n' +
|
||||
branchCode +
|
||||
'}';
|
||||
|
||||
++n;
|
||||
} while (Block.getInput('IF' + n));
|
||||
|
||||
if (Block.getInput('ELSE')) {
|
||||
branchCode = Blockly.Arduino.statementToCode(Block, 'ELSE');
|
||||
code += ' else {\n' + branchCode + '}';
|
||||
}
|
||||
return code + '\n';
|
||||
}
|
||||
|
||||
Blockly.Arduino['logic_negate'] = function (Block) {
|
||||
// Negation.
|
||||
@@ -95,3 +125,30 @@ Blockly.Arduino['logic_negate'] = function (Block) {
|
||||
const code = '!' + argument0;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Blockly.Arduino['switch_case'] = function (block) {
|
||||
var n = 0;
|
||||
var argument = Blockly.Arduino.valueToCode(this, 'CONDITION',
|
||||
Blockly.Arduino.ORDER_NONE) || '';
|
||||
var branch = Blockly.Arduino.statementToCode(block, 'CASECONDITON0' + n);
|
||||
var cases = '';
|
||||
var default_code = '';
|
||||
var DO = Blockly.Arduino.statementToCode(block, ('CASE' + n));
|
||||
for (n = 0; n <= block.caseCount_; n++) {
|
||||
var DO = Blockly.Arduino.statementToCode(block, ('CASE' + n));
|
||||
var branch = Blockly.Arduino.valueToCode(block, ('CASECONDITION' + n), Blockly.Arduino.ORDER_NONE) || '0';
|
||||
cases += 'case ' + branch + ':\n';
|
||||
cases += DO + '\nbreak;\n';
|
||||
}
|
||||
if (block.defaultCount_) {
|
||||
var branch = Blockly.Arduino.statementToCode(block, 'ONDEFAULT');
|
||||
default_code = 'default: \n' + branch + '\n break;\n';
|
||||
}
|
||||
var code = 'switch (' + argument + ') {\n' + cases + default_code + '}';
|
||||
return code + '\n';
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import Blockly from 'blockly';
|
||||
|
||||
|
||||
/* SD-Card Blocks using the Standard SD Library*/
|
||||
/**
|
||||
* Code generator for variable (X) getter.
|
||||
* Arduino code: loop { X }
|
||||
* @param {Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
|
||||
Blockly.Arduino.sensebox_sd_create_file = function (block) {
|
||||
var filename = this.getFieldValue('Filename');
|
||||
var res = filename.slice(0, 4);
|
||||
Blockly.Arduino.libraries_['library_spi'] = '#include <SPI.h>';
|
||||
Blockly.Arduino.libraries_['library_sd'] = '#include <SD.h>';
|
||||
Blockly.Arduino.definitions_['define_' + filename] = 'File dataFile' + res + ';';
|
||||
Blockly.Arduino.setupCode_['sensebox_sd'] = 'SD.begin(28);';
|
||||
Blockly.Arduino.setupCode_['sensebox_sd' + filename] = 'dataFile' + res + ' = SD.open("' + filename + '", FILE_WRITE);\ndataFile' + res + '.close();\n';
|
||||
var code = '';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Arduino.sensebox_sd_open_file = function (block) {
|
||||
var filename = this.getFieldValue('Filename');
|
||||
var res = filename.slice(0, 4);
|
||||
var branch = Blockly.Arduino.statementToCode(block, 'SD');
|
||||
var code = 'dataFile' + res + ' = SD.open("' + filename + '", FILE_WRITE);\n'
|
||||
code += branch;
|
||||
code += 'dataFile' + res + '.close();\n'
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Arduino.sensebox_sd_write_file = function (block) {
|
||||
var res = filename.slice(0, 4);
|
||||
if (this.parentBlock_ != null) {
|
||||
var filename = this.getSurroundParent().getFieldValue('Filename');
|
||||
}
|
||||
var text = Blockly.Arduino.valueToCode(this, 'DATA', Blockly.Arduino.ORDER_ATOMIC) || '"Keine Eingabe"';
|
||||
var linebreak = this.getFieldValue('linebreak');
|
||||
if (linebreak == "TRUE") {
|
||||
linebreak = "ln";
|
||||
} else {
|
||||
linebreak = "";
|
||||
}
|
||||
if (text == "gps.getLongitude()" || text == "gps.getLatitude()") {
|
||||
var code = 'dataFile' + res + '.print' + linebreak + '(' + text + ',5);\n'
|
||||
}
|
||||
else {
|
||||
var code = 'dataFile' + res + '.print' + linebreak + '(' + text + ');\n'
|
||||
}
|
||||
return code;
|
||||
};
|
||||
@@ -0,0 +1,85 @@
|
||||
import * as Blockly from 'blockly/core';
|
||||
|
||||
|
||||
/**
|
||||
* Code generator for a literal String (X).
|
||||
* Arduino code: loop { "X" }
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
Blockly.Arduino['text'] = function (block) {
|
||||
var code = Blockly.Arduino.quote_(block.getFieldValue('TEXT'));
|
||||
return [code, Blockly.Arduino.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Code generator for a String concatenation (X...Y). This string can be made
|
||||
* up of any number of elements of any type.
|
||||
* This block uses a mutator.
|
||||
* String construction info: http://arduino.cc/en/Reference/StringConstructor
|
||||
* Arduino code: loop { "String(X)" + ... + "String(Y)" }
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
Blockly.Arduino['text_join'] = function (block) {
|
||||
var code;
|
||||
if (block.itemCount_ === 0) {
|
||||
return ['""', Blockly.Arduino.ORDER_ATOMIC];
|
||||
} else if (block.itemCount_ === 1) {
|
||||
var argument0 = Blockly.Arduino.valueToCode(block, 'ADD0',
|
||||
Blockly.Arduino.ORDER_UNARY_POSTFIX) || '""';
|
||||
code = 'String(' + argument0 + ')';
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
} else {
|
||||
var argument;
|
||||
code = [];
|
||||
for (var n = 0; n < block.itemCount_; n++) {
|
||||
argument = Blockly.Arduino.valueToCode(
|
||||
block, 'ADD' + n, Blockly.Arduino.ORDER_NONE);
|
||||
if (argument === '') {
|
||||
code[n] = '""';
|
||||
} else {
|
||||
code[n] = 'String(' + argument + ')';
|
||||
}
|
||||
}
|
||||
code = code.join(' + ');
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Code generator for appending text (Y) to a variable in place (X).
|
||||
* String constructor info: http://arduino.cc/en/Reference/StringConstructor
|
||||
* Arduino code: loop { X += String(Y) }
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {string} Completed code.
|
||||
*/
|
||||
Blockly.Arduino['text_append'] = function (block) {
|
||||
// Append to a variable in place.
|
||||
var varName = Blockly.Arduino.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.Arduino.valueToCode(block, 'TEXT',
|
||||
Blockly.Arduino.ORDER_UNARY_POSTFIX);
|
||||
if (argument0 === '') {
|
||||
argument0 = '""';
|
||||
} else {
|
||||
argument0 = 'String(' + argument0 + ')';
|
||||
}
|
||||
return varName + ' += ' + argument0 + ';\n';
|
||||
};
|
||||
|
||||
/**
|
||||
* Code generator to get the length of a string (X).
|
||||
* String length info: http://arduino.cc/en/Reference/StringLength
|
||||
* Arduino code: loop { String(X).length() }
|
||||
* @param {!Blockly.Block} block Block to generate the code from.
|
||||
* @return {array} Completed code with order of operation.
|
||||
*/
|
||||
Blockly.Arduino['text_length'] = function (block) {
|
||||
var argument0 = Blockly.Arduino.valueToCode(block, 'VALUE',
|
||||
Blockly.Arduino.ORDER_UNARY_POSTFIX) || '""';
|
||||
var code = 'String(' + argument0 + ').length()';
|
||||
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import Blockly from 'blockly';
|
||||
|
||||
const setVariableFunction = function (defaultValue) {
|
||||
return function (block) {
|
||||
const variableName = Blockly['Arduino'].variableDB_.getName(
|
||||
block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE
|
||||
);
|
||||
const variableValue = Blockly['Arduino'].valueToCode(
|
||||
block,
|
||||
'VALUE',
|
||||
Blockly['Arduino'].ORDER_ATOMIC
|
||||
);
|
||||
|
||||
return variableName + ' = ' + (variableValue || defaultValue) + ';\n';
|
||||
};
|
||||
};
|
||||
|
||||
const getVariableFunction = function (block) {
|
||||
const variableName = Blockly['Arduino'].variableDB_.getName(
|
||||
block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE
|
||||
);
|
||||
|
||||
return [variableName, Blockly['Arduino'].ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly['Arduino']['variables_set_number'] = setVariableFunction(10);
|
||||
Blockly['Arduino']['variables_set_boolean'] = setVariableFunction('true');
|
||||
Blockly['Arduino']['variables_set_string'] = setVariableFunction('" "');
|
||||
Blockly['Arduino']['variables_set_colour'] = setVariableFunction(
|
||||
`{ 22, 0, 22}`
|
||||
);
|
||||
|
||||
Blockly['Arduino']['variables_get_number'] = getVariableFunction;
|
||||
Blockly['Arduino']['variables_get_boolean'] = getVariableFunction;
|
||||
Blockly['Arduino']['variables_get_string'] = getVariableFunction;
|
||||
Blockly['Arduino']['variables_get_colour'] = getVariableFunction;
|
||||
Reference in New Issue
Block a user