Skip to content

Commit 3c489d1

Browse files
committed
Refactor cog blocks into a separate module.
1 parent fdfec1a commit 3c489d1

File tree

3 files changed

+97
-61
lines changed

3 files changed

+97
-61
lines changed

src/modules/blockly/generators/propc/base.js

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ Blockly.Blocks.music_note = {
800800
Blockly.propc.music_note = function() {
801801
const note = (Math.round(
802802
parseFloat(this.getFieldValue('NOTE')) *
803-
parseFloat(this.getFieldValue('OCTAVE'))
803+
parseFloat(this.getFieldValue('OCTAVE')),
804804
)).toString(10);
805805
return [note, Blockly.propc.ORDER_NONE];
806806
};
@@ -1938,65 +1938,6 @@ Blockly.propc.logic_boolean = function() {
19381938
return [code, Blockly.propc.ORDER_ATOMIC];
19391939
};
19401940

1941-
/**
1942-
*
1943-
* @type {{
1944-
* init: Blockly.Blocks.cog_new.init,
1945-
* helpUrl: string,
1946-
* onchange: Blockly.Blocks.cog_new.onchange
1947-
* }}
1948-
*/
1949-
Blockly.Blocks.cog_new = {
1950-
helpUrl: Blockly.MSG_CONTROL_HELPURL,
1951-
init: function() {
1952-
this.setTooltip(Blockly.MSG_COG_NEW_TOOLTIP);
1953-
this.setColour(colorPalette.getColor('programming'));
1954-
this.appendDummyInput()
1955-
.appendField('new processor');
1956-
this.appendStatementInput('METHOD')
1957-
.setCheck('Function')
1958-
.appendField('function');
1959-
1960-
this.setInputsInline(true);
1961-
this.setPreviousStatement(true, 'Block');
1962-
this.setNextStatement(true, null);
1963-
},
1964-
onchange: function(event) {
1965-
if (event &&
1966-
(event.type === Blockly.Events.CHANGE ||
1967-
event.type === Blockly.Events.MOVE)) {
1968-
let repeatWarningText = null;
1969-
const myRootBlock = this.getRootBlock();
1970-
if (myRootBlock && myRootBlock.type.indexOf('repeat') > -1 ) {
1971-
repeatWarningText = 'Warning: This block can only start up to 7' +
1972-
' additional cores - using this block in a repeat loop may cause' +
1973-
' unexpected errors!';
1974-
}
1975-
this.setWarningText(repeatWarningText);
1976-
}
1977-
},
1978-
};
1979-
1980-
/**
1981-
*
1982-
* @return {string}
1983-
*/
1984-
Blockly.propc.cog_new = function() {
1985-
const method = Blockly.propc.statementToCode(this, 'METHOD');
1986-
const methodName = method
1987-
.replace(' ', '')
1988-
.replace('\n', '')
1989-
.replace('()', '')
1990-
.replace(';', '');
1991-
let code = '';
1992-
1993-
if (method.length > 2) {
1994-
Blockly.propc.cog_methods_[methodName] = method;
1995-
1996-
code = 'cog_run(' + methodName + ', 128);\n';
1997-
}
1998-
return code;
1999-
};
20001941

20011942
/**
20021943
*
@@ -3366,7 +3307,7 @@ Blockly.Blocks.custom_code_multiple = {
33663307
// source code field
33673308
.appendField(
33683309
new Blockly.FieldTextInput(''),
3369-
value[1].toUpperCase()
3310+
value[1].toUpperCase(),
33703311

33713312
// new Blockly.FieldAceEditor(
33723313
// `${value[1]} code`,
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
};

src/modules/editor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import './blockly/generators/propc';
3232
import './blockly/generators/propc/base';
3333
import './blockly/generators/propc/communicate';
3434
import './blockly/generators/propc/control';
35+
import './blockly/generators/propc/cogs';
3536
import './blockly/generators/propc/gpio';
3637
import './blockly/generators/propc/oled';
3738
import './blockly/generators/propc/heb';

0 commit comments

Comments
 (0)