|
| 1 | +/* |
| 2 | + * TERMS OF USE: MIT License |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL |
| 17 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | + * DEALINGS IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +import Blockly from 'blockly/core'; |
| 24 | +import {colorPalette} from '../propc'; |
| 25 | + |
| 26 | +/** |
| 27 | + * New Cog block definition |
| 28 | + * @type {{ |
| 29 | + * init: Blockly.Blocks.cog_new.init, |
| 30 | + * helpUrl: string, |
| 31 | + * onchange: Blockly.Blocks.cog_new.onchange |
| 32 | + * }} |
| 33 | + */ |
| 34 | +Blockly['Blocks'].cog_new = { |
| 35 | + helpUrl: Blockly.MSG_CONTROL_HELPURL, |
| 36 | + /** |
| 37 | + * Initialize a new cog and assign a function to it |
| 38 | + */ |
| 39 | + init: function() { |
| 40 | + this.setTooltip(Blockly.MSG_COG_NEW_TOOLTIP); |
| 41 | + this.setColour(colorPalette.getColor('programming')); |
| 42 | + this.appendDummyInput() |
| 43 | + .appendField('new processor'); |
| 44 | + this.appendStatementInput('METHOD') |
| 45 | + .setCheck('Function') |
| 46 | + .appendField('function'); |
| 47 | + |
| 48 | + this.setInputsInline(true); |
| 49 | + this.setPreviousStatement(true, 'Block'); |
| 50 | + this.setNextStatement(true, null); |
| 51 | + }, |
| 52 | + |
| 53 | + /** |
| 54 | + * Handle specific change events |
| 55 | + * @param {Blockly.Events} event |
| 56 | + */ |
| 57 | + onchange: function(event) { |
| 58 | + if (event && |
| 59 | + (event.type === Blockly.Events.CHANGE || |
| 60 | + event.type === Blockly.Events.MOVE)) { |
| 61 | + let repeatWarningText = null; |
| 62 | + // Warn if the root block is a repeat block. The code should never loop to |
| 63 | + // assign multiple cogs |
| 64 | + const myRootBlock = this.getRootBlock(); |
| 65 | + if (myRootBlock && myRootBlock.type.indexOf('repeat') > -1 ) { |
| 66 | + repeatWarningText = 'Warning: This block can only start up to 7' + |
| 67 | + ' additional cores - using this block in a repeat loop may cause' + |
| 68 | + ' unexpected errors!'; |
| 69 | + } |
| 70 | + this.setWarningText(repeatWarningText); |
| 71 | + } |
| 72 | + }, |
| 73 | +}; |
| 74 | + |
| 75 | +/** |
| 76 | + * C source code emitter for the New Cog block |
| 77 | + * @return {string} |
| 78 | + */ |
| 79 | +Blockly.propc.cog_new = function() { |
| 80 | + const method = Blockly.propc.statementToCode(this, 'METHOD'); |
| 81 | + const methodName = method |
| 82 | + .replace(' ', '') |
| 83 | + .replace('\n', '') |
| 84 | + .replace('()', '') |
| 85 | + .replace(';', ''); |
| 86 | + let code = ''; |
| 87 | + |
| 88 | + if (method.length > 2) { |
| 89 | + Blockly.propc.cog_methods_[methodName] = method; |
| 90 | + |
| 91 | + code = 'cog_run(' + methodName + ', 128);\n'; |
| 92 | + } |
| 93 | + return code; |
| 94 | +}; |
0 commit comments