add map block
This commit is contained in:
		
							parent
							
								
									ef80f15782
								
							
						
					
					
						commit
						6678afbdff
					
				| @ -8,6 +8,7 @@ import './sensebox-web'; | ||||
| import './sensebox-display'; | ||||
| import './io'; | ||||
| import './math'; | ||||
| import './map'; | ||||
| import './procedures'; | ||||
| import './time'; | ||||
| 
 | ||||
|  | ||||
| @ -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'], | ||||
|     }, | ||||
|  | ||||
							
								
								
									
										50
									
								
								src/components/Blockly/blocks/map.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								src/components/Blockly/blocks/map.js
									
									
									
									
									
										Normal 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; | ||||
|     } | ||||
| }; | ||||
| @ -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'; | ||||
|  | ||||
							
								
								
									
										24
									
								
								src/components/Blockly/generator/map.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/components/Blockly/generator/map.js
									
									
									
									
									
										Normal 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]; | ||||
| }; | ||||
| @ -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; | ||||
| } | ||||
| @ -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}> | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user