add map block

This commit is contained in:
Mario 2020-07-31 16:18:45 +02:00
parent ef80f15782
commit 6678afbdff
7 changed files with 88 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import './sensebox-web';
import './sensebox-display';
import './io';
import './math';
import './map';
import './procedures';
import './time';

View File

@ -1,5 +1,6 @@
import Blockly from 'blockly';
import { getColour } from '../helpers/colour';
Blockly.defineBlocksWithJsonArray([
{
@ -44,6 +45,7 @@ Blockly.defineBlocksWithJsonArray([
inputsInline: false,
previousStatement: null,
nextStatement: null,
colour: getColour().loops,
helpUrl: '%{BKY_CONTROLS_FOR_HELPURL}',
extensions: ['contextMenu_newGetVariableBlock', 'controls_for_tooltip'],
},

View File

@ -0,0 +1,50 @@
/**
* @license Licensed under the Apache License, Version 2.0 (the "License"):
* http://www.apache.org/licenses/LICENSE-2.0
*/
/**
* @fileoverview Block for the Arduino map functionality.
* The Arduino built in functions syntax can be found at:
* http://arduino.cc/en/Reference/HomePage
*
* TODO: This block can be improved to set the new range properly.
*/
import * as Blockly from 'blockly/core';
import { getColour } from '../helpers/colour';
import * as Types from '../helpers/types'
Blockly.Blocks['base_map'] = {
/**
* Block for creating a the map function.
* @this Blockly.Block
*/
init: function () {
this.setHelpUrl('http://arduino.cc/en/Reference/map');
this.setColour(getColour().math);
this.appendValueInput('NUM')
.appendField(Blockly.Msg.ARD_MAP)
.setCheck(Types.NUMBER.compatibleTypes);
this.appendValueInput('FMIN')
.appendField(Blockly.Msg.ARD_MAP_FROMMIN)
.setCheck(Types.NUMBER.compatibleTypes);
this.appendValueInput('FMAX')
.appendField(Blockly.Msg.ARD_MAP_FROMMAX)
.setCheck(Types.NUMBER.compatibleTypes);
this.appendValueInput('DMIN')
.appendField(Blockly.Msg.ARD_MAP_TOMIN)
.setCheck(Types.NUMBER.compatibleTypes);
this.appendValueInput('DMAX')
.appendField(Blockly.Msg.ARD_MAP_TOMAX)
.setCheck(Types.NUMBER.compatibleTypes);
this.setOutput(true);
this.setInputsInline(false);
this.setTooltip(Blockly.Msg.ARD_MAP_TIP);
},
/** @return {string} The type of return value for the block, an integer. */
getBlockType: function () {
return Types.NUMBER.typeId;
}
};

View File

@ -4,8 +4,10 @@ import './sensebox-sensors';
import './sensebox-telegram';
import './sensebox-osem';
import './sensebox-web';
import './sensebox-display';
import './logic';
import './math';
import './map';
import './io';
import './procedures';
import './time';

View File

@ -0,0 +1,24 @@
import * as Blockly from 'blockly/core';
/**
* Code generator for the map block.
* Arduino code: loop { map(x, 0, 1024, 0, y) }
* @param {!Blockly.Block} block Block to generate the code from.
* @return {array} Completed code with order of operation.
*/
Blockly.Arduino['base_map'] = function (block) {
var valueNum = Blockly.Arduino.valueToCode(
block, 'NUM', Blockly.Arduino.ORDER_NONE) || '0';
var fromMin = Blockly.Arduino.valueToCode(
block, 'FMIN', Blockly.Arduino.ORDER_ATOMIC) || '0';
var fromMax = Blockly.Arduino.valueToCode(
block, 'FMAX', Blockly.Arduino.ORDER_ATOMIC) || '0';
var valueDmin = Blockly.Arduino.valueToCode(
block, 'DMIN', Blockly.Arduino.ORDER_ATOMIC) || '0';
var valueDmax = Blockly.Arduino.valueToCode(
block, 'DMAX', Blockly.Arduino.ORDER_ATOMIC) || '0';
var code = 'map(' + valueNum + ',' + fromMin + ',' + fromMax + ',' + valueDmin + ',' + valueDmax + ')';
return [code, Blockly.Arduino.ORDER_NONE];
};

View File

@ -61,30 +61,32 @@ Blockly.Arduino.sensebox_display_plotDisplay = function () {
};
Blockly.Arduino.sensebox_display_fillCircle = function () {
let code = '';
var x = Blockly.Arduino.valueToCode(this, 'X', Blockly.Arduino.ORDER_ATOMIC) || '0'
var y = Blockly.Arduino.valueToCode(this, 'Y', Blockly.Arduino.ORDER_ATOMIC) || '0'
var radius = Blockly.Arduino.valueToCode(this, 'Radius', Blockly.Arduino.ORDER_ATOMIC) || '0'
var fill = this.getFieldValue('FILL');
if (fill == 'TRUE') {
var code = 'display.fillCircle(' + x + ',' + y + ',' + radius + ',1);\n';
if (fill === 'TRUE') {
code = 'display.fillCircle(' + x + ',' + y + ',' + radius + ',1);\n';
}
else {
var code = 'display.drawCircle(' + x + ',' + y + ',' + radius + ',1);\n';
code = 'display.drawCircle(' + x + ',' + y + ',' + radius + ',1);\n';
}
return code;
}
Blockly.Arduino.sensebox_display_drawRectangle = function () {
let code = '';
var x = Blockly.Arduino.valueToCode(this, 'X', Blockly.Arduino.ORDER_ATOMIC) || '0'
var y = Blockly.Arduino.valueToCode(this, 'Y', Blockly.Arduino.ORDER_ATOMIC) || '0'
var width = Blockly.Arduino.valueToCode(this, 'width', Blockly.Arduino.ORDER_ATOMIC) || '0'
var height = Blockly.Arduino.valueToCode(this, 'height', Blockly.Arduino.ORDER_ATOMIC) || '0'
var fill = this.getFieldValue('FILL');
if (fill == 'TRUE') {
var code = 'display.fillRect(' + x + ',' + y + ',' + width + ',' + height + ',1);\n';
if (fill === 'TRUE') {
code = 'display.fillRect(' + x + ',' + y + ',' + width + ',' + height + ',1);\n';
}
else {
var code = 'display.drawRect(' + x + ',' + y + ',' + width + ',' + height + ',1);\n';
code = 'display.drawRect(' + x + ',' + y + ',' + width + ',' + height + ',1);\n';
}
return code;
}

View File

@ -240,7 +240,7 @@ class Toolbox extends React.Component {
</Value>
</Block>
<Block type="math_random_float"></Block>
{/* <Block type="base_map"></Block> */}
<Block type="base_map"></Block>
</Category>
<sep></sep>
<Category name="Input/Output" colour={getColour().io}>